Industriële fabricage
Industrieel internet der dingen | Industriële materialen | Onderhoud en reparatie van apparatuur | Industriële programmering |
home  MfgRobots >> Industriële fabricage >  >> Manufacturing Technology >> Productieproces

Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

Onderdelen en benodigdheden

Arduino MKR1000 2
DHT11 Temperatuur- & vochtigheidssensor (4 pinnen) 1
LED (generiek) 2

Apps en online diensten

Samsung ARTIK Cloud voor IoT
Arduino IDE

Over dit project

Dit project gebruikt twee MKR1000's met DHT11 als sensor en LED's als metaforische actuatoren. De echte actuatoren zouden de airconditioner en het plafondventilator kunnen zijn. De DHT11-temperatuur- en vochtigheidsensor stuurt data naar ARTIK Cloud. Regels zijn ingesteld om commando's naar de rode en gele LED's te sturen.

Arduino IDE instellen

Installeer via de Library Manager van de Arduino IDE de volgende bibliotheken:

  1. Installeer WiFi101 (zie deze handleiding)
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud
  1. Installeer MQTT door Joel Gaehwiler
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud
  1. Installeer ArduinoJson
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

Opmerking: Raadpleeg mijn eerdere bericht voor details over werken met ARTIK Cloud.

DHT11-temperatuur- en vochtigheidssensor instellen

  1. Maak een apparaattype aan (DHT11 Sensor)
  2. Voeg een apparaat toe (DHT11 Sensor A1)
  3. Verbind de fysieke apparaten
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

ARTIK Cloud MQTT-parameters instellen:

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;

Berichten naar ARTIK Cloud sturen:

void sendToArtikCloud(float temperature, float humidity) {
  loadBuffer(temperature, humidity); // huidige waarden 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 op Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

LED-temperatuur- en vochtigheidsactuatoren instellen:

  1. Maak een apparaattype aan (DHT11 Actor)
  2. Voeg een apparaat toe (DHT11 Actor A1)
  3. Verbind de fysieke apparaten
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

ARTIK Cloud MQTT-parameters voor actuator:


// ARTIK Cloud MQTT params
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-acties ontvangen:

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);
}

Acties van ARTIK Cloud parsen en verwerken:

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 op Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

Regels aanmaken

Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

Er zijn 4 regels:

  • ALS DHT11 Sensor A1 temperatuur > 32 EN vochtigheid > 34 DAN stuur naar DHT11 Actor A1 actie setValue met led_red = 1, led_yellow = 1
  • ALS DHT11 Sensor A1 temperatuur > 32 EN vochtigheid < 35 DAN stuur naar DHT11 Actor A1 actie setValue met led_red = 1, led_yellow = 0
  • ALS DHT11 Sensor A1 temperatuur < 33 EN vochtigheid > 34 DAN stuur naar DHT11 Actor A1 actie setValue met led_red = 0, led_yellow = 1
  • ALS DHT11 Sensor A1 vochtigheid < 35 EN temperatuur < 33 DAN stuur naar DHT11 Actor A1 actie setValue met led_red = 0, led_yellow = 0
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud

Demovideo

Let op: je moet wachten tot de temperatuur opwarmt (LED aan) en afkoelt (LED uit). De rode LED gaat aan op 37 seconden en uit op 1:13 minuten. De demovideo toont alleen temperatuurveranderingen. Ik gebruikte een haardroger om de temperatuur rond de DHT11-sensor te veranderen.

Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op Samsung ARTIK Cloud
Arduino MKR1000 + DHT11 Sensor: MQTT IoT-demo op 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 params
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();
  
  // WiFi instellen
  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 params
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 instellen
  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);
}

Productieproces

  1. DHT11 Temperatuur- & Vochtigheidsensor Project met LED-indicatoren en Piezo-luidspreker
  2. Bluetooth-gecontroleerde Arduino Spybot bouwen
  3. FlickMote: Gebarengecontroleerde IR-afstandsbediening voor Slimme Huizen
  4. DIY Arduino TV-B-Gone: Universele TV-uit-schakelaar
  5. Bouw een Aangepaste LED Hoofdklok met Alarm – Zichtbaar vanaf 12 Meter
  6. Find Me: Slimme Item Locator met Arduino en Bluetooth
  7. Geoptimaliseerde Voedingsoplossingen voor Arduino Projecten
  8. Arduino Boter-Kaas-Eieren met MAX7219 LED Matrix en Kartonnen Behuizing
  9. Bouw een Slimme Arduino Vierpotige Robot: Stap-voor-Stap Handleiding
  10. Temperatuur en Vochtigheid met DHT11 op Blynk - Stap-voor-Stap Arduino Tutorial
  11. Arduino MKR1000 Pinout: IoT Bord Handleiding