top of page
Writer's pictureRamesh G

IoT Based DHT 11 Weather Monitor using MQTT

Updated: Jan 30, 2021



Here to learn how to build an IoT system using ESP8266, myDevices Cayenne, and MQTT. In more detail, this IoT tutorial discovers how to use an ESP8266 to send data to Cayenne using the MQTT protocol. Moreover, this ESP8266 MQTT project investigates how to use MQTT to control remote peripheral devices using a web interface. This is a complete step-by-step tutorial on building an IoT system using Cayenne and ESP.

On the other hand, Cayenne is an IoT cloud platform that provides several cloud services, such as:

  • Data visualization

  • IoT cloud

  • Alerts

  • Scheduling Events

We will focus our attention on data visualization and on the IoT cloud services.


Cayenne

Cayenne IoT Platform accelerates the development of IoT-based solutions, including quick design, prototyping and other commercialized projects. It is a drag-and-drop IoT project builder that can help developers build complete, ready-to-use IoT solutions with little to no coding.

Cayenne IoT Platform contains a vast catalogue of certified IoT-ready devices and connectivity options. This allows users to easily add any device to the library utilizing MQTT API. All devices in Cayenne are interoperable and benefit from features such as rules engine, asset tracking, remote monitoring and control, and tools to visualize real-time and historical data.

MQTT is a lightweight messaging protocol for sensors and mobile devices.


NodeMCU

NodeMCU ESP8266-12E MCU is a development board with one analogue and many general-purpose input output (GPIO) pins. It has 4MB flash memory, and can operate at a default clock frequency of 80MHz. In this project, digital pin D1 of NodeMCU is used to read Data of Dht11 temperature sensor.


DHT 11/22/AM2302


Connecting DHT11/DHT22/AM2302 sensor to ESP8266 NodeMCU is fairly simple.

Connect VCC pin on the sensor to the 3.3V pin on the NodeMCU and ground to ground. Also connect Data pin on the sensor to D1 pin of the ESP8266 NodeMCU.


Installing DHT Sensor Library



Circuit Diagram

NodeMCU programming with Arduino IDE

You need Arduino IDE software to program NodeMCU.

Steps for adding NodeMCU ESP8266 board and library to Arduino IDE are given below.


  • Open Arduino IDE. Go to File menu and select Preferences.

  • Copy this package and paste in Additional Boards Manager URL.

  • Go to Tools and select Boards Manager.

  • Type esp8266 in search box and press Enter.

  • Install the latest version.

  • From Tools menu, select NodeMCU 1.0 (ESP-12E module) board.

  • Select Include Library from Sketch menu.

  • Download the library from this link.

  • Click on Add ZIP Library and add Cayenne-MQTT-ESP-master zip file, or directly copy the folder (Cayenne-MQTT-ESP-master) and paste it in Libraries folder of Arduino IDE.


Arduino Code


#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space

#define DHTPIN 5 // D1


// Uncomment whatever type you're using! In this project we used DHT11

#define DHTTYPE DHT11 // DHT 11

//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321

//#define DHTTYPE DHT21 // DHT 21, AM2301


#include <CayenneMQTTESP8266.h>

#include <DHT.h>


//Make a wifi name and password as access point

char ssid[] = "SSID"; // your SSID

char wifiPassword[] = "PASSWORD"; // YOUR WIFI PASSWORD


// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.

char username[] = "2031dd30-5414-11eb-b767-3f1a8f1211ba"; // YOURS

char password[] = "f2ce829d98df3a768328ac6936eae9fd47d28289"; // YOURS

char clientID[] = "138092f0-54b1-11eb-a2e4-b32ea624e442";// YOURS


DHT dht(DHTPIN, DHTTYPE);


//Variables for DHT11 values

float h;

float t;

bool humidity = false;

bool Temperature = false;



void setup()

{

Serial.begin(9600);

Serial.print("Setup");

Cayenne.begin(username, password, clientID, ssid, wifiPassword);


humidity = false;

Temperature = false;

}


void loop()

{

//Run Cayenne Functions

Cayenne.loop();

}


CAYENNE_OUT(V0){

//Serial.println("humidity");


//Check if read failed and try until success

do {

//Read humidity (percent)

h = dht.readHumidity();


delay(1000);

} while (isnan(h));


Serial.print("humidity: ");

Serial.println(h);


//Set Humidity to true so we know when to sleep

humidity = true;


//Write to Cayenne Dashboard`

Cayenne.virtualWrite(V0, h);

}


CAYENNE_OUT(V1){

//Serial.println("temperature");

//Check if read failed and try until success

do {

//Read temperature as Celsius

t = dht.readTemperature();


delay(1000);

} while (isnan(t));


Serial.print("temperature: ");

Serial.println(t);


//Set Temperature to true so we know when to sleep

//Temperature = true;


//Write to Cayenne Dashboard

Cayenne.virtualWrite(V1, t);

}



Hardware interfacing with Cayenne IoT platform

To interface the circuit with the IoT platform, open this link on any browser.


Click on Sign Up. Fill all the fields and click on Get Started

Click on Add new and then Device/Widget in Settings, Add New Device here and select Generic ESP8266 for in this project.


Configure device Generic ESP8266, MQTT username, password and client ID from Create App



Paste these respective details under username, password and client ID in Arduino source code , along with your Wi-Fi name and password.



After successfully compiling and uploading the code to NodeMCU, You will see ESP8266 connected to Wi-Fi. After the connection is established, the previous page is automatically updated on Cayenne. A new dashboard opens in the browser. Cayenne generates an ID and a device icon for your device.

Click on Custom Widgets and then value, and populate all fields . The channel number should be 1. (Make sure the channel number is same as in code.) Now, click on Add Widget.

When a connection is made, sensor data gets uploaded to Cayenne. Temperature and Humidity data on Cayenne.




You can get a graphical representation of temperature and Humidity data by clicking on the Graph icon.



1,050 views0 comments

Comments


bottom of page