Manufactura industrial
Internet industrial de las cosas | Materiales industriales | Mantenimiento y reparación de equipos | Programación industrial |
home  MfgRobots >> Manufactura industrial >  >> Manufacturing Technology >> Proceso de manufactura

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

Componentes y Suministros

Componente Cantidad
Arduino MKR1000 2
Sensor Temperatura y Humedad DHT11 (4 pines) 1
LED (genérico) 2

Aplicaciones y Servicios Online

Descripción del Proyecto

Este proyecto utiliza dos MKR1000 con DHT11 como sensor y LEDs como actuadores metafóricos. Los actuadores reales podrían ser el aire acondicionado y el ventilador de techo. El dispositivo DHT11 envía datos de temperatura y humedad a ARTIK Cloud. Se han configurado reglas para enviar comandos a los LEDs Rojo y Amarillo.

Configurar Arduino IDE

Con el Gestor de Librerías de Arduino IDE, instale las siguientes librerías:

  1. WiFi101 (ver guía)
  2. MQTT de Joel Gaehwiler
  3. ArduinoJson

Nota: Consulte mi publicación anterior para detalles sobre el trabajo con ARTIK Cloud.

Configurar Sensor DHT11 de Temperatura y Humedad

  1. Crear un tipo de dispositivo (Sensor DHT11)
  2. Conectar un dispositivo (Sensor DHT11 A1)
  3. Conectar los dispositivos físicos

Configurar parámetros MQTT de ARTIK Cloud:

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;

Enviar mensajes a ARTIK Cloud:

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

Configurar Actuadores LED de Temperatura y Humedad

  1. Crear tipo de dispositivo (Actor DHT11)
  2. Conectar dispositivo (Actor DHT11 A1)
  3. Conectar dispositivos físicos

Configurar parámetros MQTT ARTIK Cloud:

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;

Recibir acciones MQTT:

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

Procesar acciones desde ARTIK Cloud:

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"];

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

Crear Reglas

Se crean 4 reglas en ARTIK Cloud:

Video Demo

El LED rojo se enciende a los 37 segundos y se apaga a 1:13. El video muestra solo cambios de temperatura. Se usó un secador de pelo para cambiar la temperatura alrededor del sensor DHT11.

Código

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]";

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.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");
  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...");
  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]";

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");
  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);
  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"];

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

Proceso de manufactura

  1. Proyecto Sensor DHT11 Temperatura y Humedad con Indicadores LED y Altavoz Piezo
  2. Construye un Spybot controlado por Bluetooth con Arduino
  3. FlickMote: Mando IR por gestos para Smart Home
  4. TV‑B‑Gone DIY con Arduino: Mando universal para apagar cualquier TV
  5. Construye un Reloj Maestro LED Personalizado con Alarma – Visible desde 12 Metros
  6. Find Me: Localizador Inteligente de Objetos con Arduino y Bluetooth
  7. Soluciones de Alimentación Optimizadas para Proyectos Arduino
  8. Tres en raya con Arduino, matriz LED MAX7219 y carcasa de cartón
  9. Construye un Robot Cuadrúpedo Inteligente con Arduino: Guía Paso a Paso
  10. Leer Temperatura y Humedad con DHT11 en Blynk – Tutorial Paso a Paso con Arduino
  11. Arduino MKR1000: Pinout, Especificaciones y Guía para Proyectos IoT