산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Manufacturing Technology >> 제조공정

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

부품 및 소모품

부품 수량
아두이노 MKR1000 2
DHT11 온습도 센서 (4핀) 1
LED (일반) 2

앱 및 온라인 서비스

서비스
삼성 ARTIK 클라우드 (IoT)
아두이노 IDE

프로젝트 소개

이 프로젝트는 DHT11 센서를 장착한 두 대의 MKR1000과 액추에이터 역할을 하는 LED를 사용합니다. 실제 액추에이터는 에어컨과 천장 선풍기가 될 수 있습니다. DHT11 온습도 장치가 ARTIK 클라우드로 데이터를 전송하면, 설정된 규칙에 따라 빨간색과 노란색 LED에 명령이 전송됩니다.

아두이노 IDE 설정

라이브러리 관리자에서 다음 라이브러리 설치:

  1. WiFi101 (가이드 참조)
  2. MQTT (Joel Gaehwiler)
  3. ArduinoJson

참고: ARTIK 클라우드 작업에 대한 자세한 내용은 이전 포스트 참조

DHT11 온습도 센서 설정

  1. 디바이스 타입 생성: DHT11 Sensor
  2. 디바이스 연결: DHT11 Sensor A1
  3. 물리적 디바이스 연결

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

ARTIK 클라우드 MQTT 파라미터 설정 (센서 측)

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;

ARTIK 클라우드로 메시지 전송

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

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

LED 온습도 액추에이터 설정

  1. 디바이스 타입 생성: DHT11 Actor
  2. 디바이스 연결: DHT11 Actor A1
  3. 물리적 디바이스 연결

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

ARTIK 클라우드 MQTT 파라미터 설정 (액추에이터 측)

// 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 액션 수신

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

ARTIK 클라우드 액션 파싱 및 처리

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

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

규칙 생성

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

총 4개 규칙:

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

데모 영상

온도 상승(LED 켜짐)과 하강(LED 꺼짐)을 기다려야 합니다. 빨간 LED는 37초에 켜지고 1분 13초에 꺼집니다. 데모 영상은 온도 변화만 보여주며, 헤어드라이어로 DHT11 센서 주변 온도를 변경했습니다.

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모

코드

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

제조공정

  1. DHT11 온습도 센서 + LED 표시등 + 피에조 부저 프로젝트
  2. 블루투스 제어 아두이노 스파이봇(Spybot) 만들기
  3. 플릭모트(FlickMote): 스마트 홈을 위한 제스처 제어 IR 리모컨
  4. DIY 아두이노 TV B-Gone: 모든 TV를 끄는 만능 리모컨
  5. 아두이노로 만드는 대형 LED 마스터 시계 (알람 기능 포함, 12m 거리에서도 식별 가능)
  6. 아두이노와 블루투스로 만드는 스마트 물건 찾기 'Find Me'
  7. 아두이노 프로젝트를 위한 최적화된 전원 솔루션
  8. 아두이노 틱택토 게임 - MAX7219 LED 매트릭스와 골판지 케이스
  9. 스마트 아두이노 4족 보행 로봇 제작: 단계별 완벽 가이드
  10. 아두이노 MKR1000 핀아웃 완전 정복: IoT 개발의 필수 보드