아두이노 MKR1000 + DHT11 센서: 삼성 ARTIK 클라우드 MQTT IoT 데모
부품 및 소모품
| 부품 |
수량 |
| 아두이노 MKR1000 |
2 |
| DHT11 온습도 센서 (4핀) |
1 |
| LED (일반) |
2 |
앱 및 온라인 서비스
| 서비스 |
| 삼성 ARTIK 클라우드 (IoT) |
| 아두이노 IDE |
프로젝트 소개
이 프로젝트는 DHT11 센서를 장착한 두 대의 MKR1000과 액추에이터 역할을 하는 LED를 사용합니다. 실제 액추에이터는 에어컨과 천장 선풍기가 될 수 있습니다. DHT11 온습도 장치가 ARTIK 클라우드로 데이터를 전송하면, 설정된 규칙에 따라 빨간색과 노란색 LED에 명령이 전송됩니다.
아두이노 IDE 설정
라이브러리 관리자에서 다음 라이브러리 설치:
- WiFi101 (가이드 참조)
- MQTT (Joel Gaehwiler)
- ArduinoJson
참고: ARTIK 클라우드 작업에 대한 자세한 내용은 이전 포스트 참조
DHT11 온습도 센서 설정
- 디바이스 타입 생성: DHT11 Sensor
- 디바이스 연결: DHT11 Sensor A1
- 물리적 디바이스 연결

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



LED 온습도 액추에이터 설정
- 디바이스 타입 생성: DHT11 Actor
- 디바이스 연결: DHT11 Actor A1
- 물리적 디바이스 연결

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


규칙 생성



총 4개 규칙:
- IF DHT11 Sensor A1 온도 > 32 AND 습도 > 34 THEN DHT11 Actor A1에 led_red=1, led_yellow=1 전송
- IF DHT11 Sensor A1 온도 > 32 AND 습도 < 35 THEN DHT11 Actor A1에 led_red=1, led_yellow=0 전송
- IF DHT11 Sensor A1 온도 < 33 AND 습도 > 34 THEN DHT11 Actor A1에 led_red=0, led_yellow=1 전송
- IF DHT11 Sensor A1 습도 < 35 AND 온도 < 33 THEN DHT11 Actor A1에 led_red=0, led_yellow=0 전송

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


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