Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Manufacturing Technology >> Herstellungsprozess

Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

Komponenten und Materialien

Arduino MKR1000 × 2
DHT11 Temperatur- & Luftfeuchtigkeitssensor (4 Pins) × 1
LED (generisch) × 2

Apps und Online-Dienste

Samsung ARTIK Cloud für IoT
Arduino IDE

Über dieses Projekt

Dieses Projekt nutzt zwei MKR1000 mit DHT11 als Sensor und LEDs als metaphorische Aktoren. Die echten Aktoren könnten Klimaanlage und Deckenventilator sein. Der DHT11 sendet Temperatur- und Luftfeuchtigkeitsdaten an die ARTIK Cloud. Regeln wurden festgelegt, um Befehle an die rote und gelbe LED zu senden.

Arduino IDE einrichten

Über den Bibliotheksverwalter der Arduino IDE folgende Bibliotheken installieren:

Hinweis: Siehe meinen früheren Beitrag für Details zur Arbeit mit ARTIK Cloud.

DHT11 Temperatursensor einrichten

Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

ARTIK Cloud MQTT-Parameter einrichten:

char mqttCloudServer[] = "api.artik.cloud";
int mqttCloudPort = 8883;
char mqttCloudClientName[] = "ARTIK-Arduino";
char mqttCloudUsername[] = "[device-id]";
char mqttCloudPassword[] = "[device-token]";
char mqttCloudDataOut[] = "/v1.1/messages/[device-id]";
WiFiSSLClient ipCloudStack;
MQTTClient mqttCloudClient;

Nachrichten an ARTIK Cloud senden:

void sendToArtikCloud(float temperature, float humidity) {
  loadBuffer(temperature, humidity); // Aktuelle Werte in Buffer laden
  mqttCloudClient.publish(mqttCloudDataOut, buf);
}

void loadBuffer(float temperature, float humidity) {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& dataPair = jsonBuffer.createObject();
  dataPair["temperature"] = temperature;
  dataPair["humidity"] = humidity;
  dataPair.printTo(buf, sizeof(buf));
}
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

LED-Temperatur- und Luftfeuchtigkeits-Aktoren einrichten:

Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

ARTIK Cloud MQTT-Parameter für Aktor:


// ARTIK Cloud MQTT Parameter
char mqttCloudServer[] = "api.artik.cloud";
int mqttCloudPort = 1883;
char mqttCloudClientName[] = "ARTIK-Arduino";
char mqttCloudUsername[] = "[device-id]";
char mqttCloudPassword[] = "[device-token]";
char mqttCloudActionsIn[] = "/v1.1/actions/[device-id]";
WiFiClient ipCloudStack;
MQTTClient mqttCloudClient;

MQTT-Aktionen empfangen:

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("topic=");
  Serial.println(topic);
  Serial.print("payload=");
  Serial.println(payload);
  Serial.print("bytes=");
  Serial.println(bytes);
  Serial.print("length=");
  Serial.println(length);
  parseBuffer(payload);
}

Buffer parsen und Aktionen verarbeiten:

void parseBuffer(String payload) {
  StaticJsonBuffer<200> jsonBuffer;
  String json = payload;
  JsonObject& root = jsonBuffer.parseObject(json);
  const char* nameparam = root["actions"][0]["name"];
  const int actionLEDRed = root["actions"][0]["parameters"]["led_red"];
  const int actionLEDYellow = root["actions"][0]["parameters"]["led_yellow"];

  Serial.print("name=");
  Serial.println(nameparam);
  Serial.print("led_red=");
  Serial.println(actionLEDRed);
  Serial.print("led_yellow=");
  Serial.println(actionLEDYellow);
  Serial.println();

  if (actionLEDRed == 1) {
    if (savedRedValue != actionLEDRed) {
      digitalWrite(LED_RED_PIN, HIGH);
      savedRedValue = actionLEDRed;
    }
    savedRedTime = millis();
  } else {
    if (savedRedValue != actionLEDRed) {
      if (millis() - savedRedTime > RED_DELAY) {
        digitalWrite(LED_RED_PIN, LOW);
        savedRedValue = actionLEDRed;
      }
    }
  }

  if (actionLEDYellow == 1) {
    if (savedYellowValue != actionLEDYellow) {
      digitalWrite(LED_YELLOW_PIN, HIGH);
      savedYellowValue = actionLEDYellow;
    }
    savedYellowTime = millis();
  } else {
    if (savedYellowValue != actionLEDYellow) {
      if (millis() - savedYellowTime > YELLOW_DELAY) {
        digitalWrite(LED_YELLOW_PIN, LOW);
        savedYellowValue = actionLEDYellow;
      }
    }
  }
}
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

Regeln erstellen

Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

Es gibt 4 Regeln:

Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

Demo-Video

Bitte beachten Sie, dass Sie warten müssen, bis sich die Temperatur aufheizt (LED an) und abkühlt (LED aus). Die rote LED geht bei 37 Sekunden an und bei 1:13 Minuten aus. Das Demo-Video zeigt nur Temperaturänderungen. Ich habe einen Föhn verwendet, um die Temperatur am DHT11-Sensor zu ändern.

Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT Demo auf Samsung ARTIK Cloud

Code

artik_dht11_sensor.ino (C/C++)

#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <WiFi101.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const char* _SSID = "[Wi-Fi SSID]";
const char* _PASSWORD = "[Wi-Fi Password]";

// ARTIK Cloud MQTT Parameter
char mqttCloudServer[] = "api.artik.cloud";
int mqttCloudPort = 8883;
char mqttCloudClientName[] = "ARTIK-Arduino";
char mqttCloudUsername[] = "[device-id]";
char mqttCloudPassword[] = "[device-token]";
char mqttCloudDataOut[] = "/v1.1/messages/[device-id]";
WiFiSSLClient ipCloudStack;
MQTTClient mqttCloudClient;
char buf[128];
float temperature, humidity;
int n = 0;

void getNextSample(float* Temperature, float* Humidity) {
  delay(2000);
  *Humidity = dht.readHumidity();
  *Temperature = dht.readTemperature();
  Serial.print("Temperature=");
  Serial.println(*Temperature);
  Serial.print("Humidity=");
  Serial.println(*Humidity);
}

void setup() {
  Serial.begin(57600);
  dht.begin();

  // WLAN verbinden
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  mqttCloudClient.begin(mqttCloudServer, mqttCloudPort, ipCloudStack);
  Serial.println("start ARTIK Cloud connect");
  Serial.println();
  while (!mqttCloudClient.connect(mqttCloudClientName, mqttCloudUsername, mqttCloudPassword)) {
    Serial.print("*");
    delay(500);
  }
}

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {}

void sendToArtikCloud(float temperature, float humidity) {
  loadBuffer(temperature, humidity);
  mqttCloudClient.publish(mqttCloudDataOut, buf);
}

void loadBuffer(float temperature, float humidity) {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& dataPair = jsonBuffer.createObject();
  dataPair["temperature"] = temperature;
  dataPair["humidity"] = humidity;
  dataPair.printTo(buf, sizeof(buf));
}

void loop() {
  if (++n > 10) {
    Serial.println("Stopped.");
    exit(0);
  }
  mqttCloudClient.loop();
  delay(1000);
  getNextSample(&temperature, &humidity);
  Serial.println("Publishing...");
  Serial.println();
  sendToArtikCloud(temperature, humidity);
  delay(15000);
}

artik_led_actor.ino (C/C++)

#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <WiFi101.h>

#define LED_RED_PIN 11
#define LED_YELLOW_PIN 13
#define RED_DELAY 5000
#define YELLOW_DELAY 10000

const char* _SSID = "[Wi-Fi SSID]";
const char* _PASSWORD = "[Wi-Fi Password]";

// ARTIK Cloud MQTT Parameter
char mqttCloudServer[] = "api.artik.cloud";
int mqttCloudPort = 1883;
char mqttCloudClientName[] = "ARTIK-Arduino";
char mqttCloudUsername[] = "[device-id]";
char mqttCloudPassword[] = "[device-token]";
char mqttCloudActionsIn[] = "/v1.1/actions/[device-id]";
WiFiClient ipCloudStack;
MQTTClient mqttCloudClient;
char buf[128];
int savedRedValue, savedYellowValue;
unsigned long savedRedTime, savedYellowTime;

void setup() {
  Serial.begin(57600);
  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_YELLOW_PIN, OUTPUT);
  savedRedValue = savedYellowValue = 0;

  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  mqttCloudClient.begin(mqttCloudServer, mqttCloudPort, ipCloudStack);
  Serial.println("start ARTIK Cloud connect");
  Serial.println();
  while (!mqttCloudClient.connect(mqttCloudClientName, mqttCloudUsername, mqttCloudPassword)) {
    Serial.print("*");
    delay(500);
  }
  mqttCloudClient.subscribe(mqttCloudActionsIn);
}

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("topic=");
  Serial.println(topic);
  Serial.print("payload=");
  Serial.println(payload);
  Serial.print("bytes=");
  Serial.println(bytes);
  Serial.print("length=");
  Serial.println(length);
  parseBuffer(payload);
}

void parseBuffer(String payload) {
  StaticJsonBuffer<200> jsonBuffer;
  String json = payload;
  JsonObject& root = jsonBuffer.parseObject(json);
  const char* nameparam = root["actions"][0]["name"];
  const int actionLEDRed = root["actions"][0]["parameters"]["led_red"];
  const int actionLEDYellow = root["actions"][0]["parameters"]["led_yellow"];

  Serial.print("name=");
  Serial.println(nameparam);
  Serial.print("led_red=");
  Serial.println(actionLEDRed);
  Serial.print("led_yellow=");
  Serial.println(actionLEDYellow);
  Serial.println();

  if (actionLEDRed == 1) {
    if (savedRedValue != actionLEDRed) {
      digitalWrite(LED_RED_PIN, HIGH);
      savedRedValue = actionLEDRed;
    }
    savedRedTime = millis();
  } else {
    if (savedRedValue != actionLEDRed) {
      if (millis() - savedRedTime > RED_DELAY) {
        digitalWrite(LED_RED_PIN, LOW);
        savedRedValue = actionLEDRed;
      }
    }
  }

  if (actionLEDYellow == 1) {
    if (savedYellowValue != actionLEDYellow) {
      digitalWrite(LED_YELLOW_PIN, HIGH);
      savedYellowValue = actionLEDYellow;
    }
    savedYellowTime = millis();
  } else {
    if (savedYellowValue != actionLEDYellow) {
      if (millis() - savedYellowTime > YELLOW_DELAY) {
        digitalWrite(LED_YELLOW_PIN, LOW);
        savedYellowValue = actionLEDYellow;
      }
    }
  }
}

void loop() {
  mqttCloudClient.loop();
  delay(500);
}

Herstellungsprozess

  1. DHT11 Temperatur- & Feuchtesensor mit LED-Anzeige & Piezo-Summer
  2. Bluetooth-gesteuerter Arduino-Spybot bauen – Überwachungsroboter mit IP-Kamera
  3. FlickMote: Gestensteuerung per Ultraschall & IR für Smart Home (Arduino 101)
  4. DIY Arduino TV-B-Gone: Universelle TV-Aus-Fernbedienung
  5. Einen eigenen LED-Master-Clock mit Alarm bauen – aus 12 Metern Entfernung ablesbar
  6. Find Me: Smarter Gegenstands-Finder mit Arduino und Bluetooth
  7. Optimierte Stromversorgungslösungen für Arduino-Projekte
  8. Arduino Tic Tac Toe mit MAX7219 LED-Matrix und Karton-Gehäuse
  9. Intelligenten Arduino-Vierbeiner-Roboter bauen: Schritt-für-Schritt-Anleitung
  10. Arduino MKR1000 Pinout: Das ideale Board für IoT-Entwicklung