In this tutorial, let’s discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be over MQTT protocol.
Introduction to Ubidots
Ubidots value-add in development time and cost saved is only increased when combined with its usability. Thus with a core architecture focused on data efficiency and an engaging UX (user experience), users can connect, build, and deploy cloud IoT applications with ease, leavings it to handle the cloud and end-user UX infrastructure.
Introduction to MQTT
The MQTT known as the Message Queuing Telemetry Transport) is a light weight publish/subscribe protocol designed for M2M (Machine to Machine) telemetry in low bandwidth environments.
Hardware requirements
- NODEMCU
- DHT11 Sensor
- Led
- Bread Board
Software Requirements
Circuit Configuration
NODEMCU | |
D4 | S (DHT11) |
D2 | led (Anode) |
Gnd | Led(Cathode) |
3V3 | vcc(DHT11) |
Programming NodeMCU in Arduino IDE
In order to code NodeMCU in Arduino IDE follow these steps .
Configuration of Ubidots
1.Firstly, Create an account here
2. So now choose Ubidots stem for free account
3. Following, select API Credentials and copy the Token
4. Then select Devices and add a new device
5. In addition to it click on your device and then add variables required as per project requirement. Finally saving it.
6. Similarly copy API label in above step if you are receiving data from ubidots cloud
7. Now, go back and select data then choose dashboard
8. Likewise, add a new dashboard and complete the required general information
9. Finally, now add widgets and select the required variable, range, and style, etc
10. Include Ubidots library from here. Along with it include pubsubclient library as well.
11. Lastly run the code, by pasting all necessary tokens, API labels, and variable names. Notice the parameters in the dashboard.
Code
The code below integrates NodeMcu with ubidots over MQTT protocol thus it’s necessary to include all necessary API token, Device names, and Variable names.
#include "UbidotsESPMQTT.h"
#include <dht.h>
#define TOKEN "........." // Your Ubidots TOKEN
#define WIFINAME ".........." //Your SSID
#define WIFIPASS "........."// Your Wifi Pass
#define DHT11_PIN 2//D4
#define LED_PIN 0//D3
dht DHT;
Ubidots client(TOKEN);
#define DEVICE_LABEL "iotedu1" // Put here your Ubidots device label
#define VARIABLE_LABEL "led1" // Put here your Ubidots variable label
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0]=='1')
{
digitalWrite(LED_PIN, HIGH);
}
else if ((char)payload[0]=='0')
{
digitalWrite(LED_PIN, LOW);
}
}
void setup()
{
client.setDebug(true); // Pass a true or false bool value to activate debug messages
Serial.begin(115200);
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
pinMode(LED_PIN,OUTPUT);
client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL);//subscribe to the topic
}
void loop()
{
if(!client.connected())
{
client.reconnect();
}
client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL);
int chk = DHT.read11(DHT11_PIN);
client.add("temperature", DHT.temperature);
client.add("Humidity", DHT.humidity);
client.ubidotsPublish("IOTEDU1");
client.loop();
delay(100);
}
Result
Thus, we can observe that the data sent from Arduino noted in the serial monitor and the dashboard parameters appears to be the same.
Summary
Thus, we learned how to use NodeMCU with ubidots illustrating with an example of monitoring temperature and Humidity. In addition to it, we have controlled the led bulb from Ubidots dashboard using MQTT protocol.