Summary of ARTIK + MKR1000 + DHT11 + MQTT
This article describes an IoT project using two Genuino MKR1000 boards, a DHT11 sensor, and LEDs to monitor temperature and humidity via Samsung ARTIK Cloud. The system sends sensor data to the cloud and triggers LED actions based on set rules using the MQTT protocol.
Parts used in the IoT Temperature and Humidity Monitor:
- Arduino MKR1000
- DHT11 Temperature & Humidity Sensor (4 pins)
- LED (generic)
- Samsung ARTIK Cloud for IoT
- Arduino IDE
- WiFi101 library
- MQTT library by Joel Gaehwiler
- ArduinoJson library
An IOT app which make uses of ARTIK Cloud, Genuino MKR1000, DHT11 temperature and humidity sensor and MQTT protocol.

Things used in this project
Story
This project makes use of two MKR1000s with DHT11 as sensor and LEDs as metaphor actuators. The real actuators could be the air-conditioner and ceiling fan. The DHT11 temperature and humidity device sends data to ARTIK cloud. Rules have been set to send command to the Red and Yellow LEDs.
Setup Arduino IDE
With Arduino IDE’s Library Manager, install the following libraries.
Install WiFi101 (see this guide)

Install MQTT by Joel Gaehwiler

Install ArduinoJson

Note: Refer to my earlier post to find out details about working with ARTIK cloud.
Setup the DHT11 temperature and humidity sensor
Create a device type (DHT11 Sensor)
Connect a device (DHT11 Sensor A1)
Connect the physical devices

Setup the ARTIK cloud MQTT parameters
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;
Sending messages to ARTIK cloud.
void sendToArtikCloud(float temperature, float humidity) {
loadBuffer(temperature, humidity); // load current values into the buffer
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));
}



Setup the LED temperature and humidity actuators
Create a device type (DHT11 Actor)
Connect a device (DHT11 Actor A1)
Connect the physical devices

Setup ARTIK cloud MQTT parameters
// 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;
Receiving MQTT actions.
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);
}
Parse and process the actions from 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"];
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;
}
}
}
}


Create Rules



There are 4 rules:
- IF DHT11 Sensor A1 temperature is more than 32 and humidity is more than 34 THEN send to DHT11 Actor A1 the action setValue with led_red = 1, led_yellow = 1
- IF DHT11 Sensor A1 temperature is more than 32 and humidity is less than 35 THEN send to DHT11 Actor A1 the action setValue with led_red = 1, led_yellow = 0
- IF DHT11 Sensor A1 temperature is less than 33 and humidity is more than 34 THEN send to DHT11 Actor A1 the action setValue with led_red = 0, led_yellow = 1
- IF DHT11 Sensor A1 humidity is less than 35 and temperature is less than 33 THEN send to DHT11 Actor A1 the action setValue with led_red = 0, led_yellow = 0

Demo Video
Please note that you will need to wait for the temperature to heat up (LED on) and cool off (LED off). The red LED is turned on at 37 seconds and turned off at 1:13 seconds. The demo video shows only temperature changes. I used a hair dryer to change the temperature around the DHT11 sensor.


Code
- How many Arduino MKR1000 boards are used in this project?
The project uses two MKR1000 boards. - What sensors are connected to the system?
A DHT11 temperature and humidity sensor is used. - Which cloud service does this IoT app utilize?
The app makes use of Samsung ARTIK Cloud for IoT. - What protocol is used for communication between devices and the cloud?
The MQTT protocol is used. - How can I control the LEDs based on environmental conditions?
Rules are set in ARTIK Cloud to send commands to the Red and Yellow LEDs based on temperature and humidity thresholds. - What libraries must be installed in Arduino IDE for this project?
You need to install WiFi101, MQTT by Joel Gaehwiler, and ArduinoJson. - What happens when the temperature exceeds 32 and humidity exceeds 34?
The system sends an action to set both led_red and led_yellow to 1. - Can real actuators like air conditioners replace the LEDs?
Yes, the real actuators could be the air-conditioner and ceiling fan instead of LEDs.





