Menu Close

Subscribe and Publish data on Ubidots using NodeMCU ESP8266 and Si7021 over MQTT

This tutorial of Robo India is a basic tutorial to subscribe and publish data on Ubidots Application using NodeMCU ESP8266 to Development Platform over MQTT.

1. Introduction:

This tutorial explains how to POST and GET data to/from Ubidots using the NodeMCU ESP8266  and Si7021 module. 

You need to have basic understanding of ESP8266 and Si7021, below mentioned tutorials of Robo India may help you out in this.

1. This tutorial explains configuration and setup of ESP8266

2. Si7021 tutorial

2. Required Hardware

Following Hardware will be required to perform this tutorial.

S.No.ItemQuantity
1 NodeMCU 1
2 Si7021  1
3 Breadboard  1
4 LED 1
5 1k Resistor 1
6 Jumper 8

3. Building Circuit –

Make the following circuit with the help of above mentioned components.

Connections:

NodeMCUSi7021
GPIO5CL
GPIO4DA
VCC3V3
GNDGND

Connection of NodeMCU with LED

GNDBreadboard(-)
3V3 Breadboard(+)
GPIO141k
1kLED(+)
LED(-) Breadboard(-)

4. Software Setup :

Ubidots:

Create an account on Ubidots Education and then Signin.

1. Creating Devices:
A Ubidots device is a virtual representation of a data-source or simply, an asset taking sensor data and transmitting said data through a connection protocol to Ubidots cloud.

->To create a device, click the “+” icon in the top right corner of the screen.
Device name : ESP8266

2. Creating Variables:
Once a device is created and receiving data from your hardware , the data will be presented in its raw or calculated form as a variable.

Types of Variables:
– Default – raw data coming from devices (people counted).
– Synthetic – correspond to statistical or arithmetical operations of default variables in a determined time-frame (e.g. average daily traffic this month).

->To create a variable click the “+” icon found in device screen.
->Click on Add Variable>Default>Variable Name(temerature, humidity, LED)

3. Dashboards and Visualizations
Dashboards are the human-machine interfaces where data is easily visualized. 

->By clicking the “+” icon you can add widgets to Dashboard.
->Click on chart> Line chart >Add Variable>Select Device( ESP8266 )> Select Variable(temperature(100/0), humidity(100/0), LED(On/Off)).

->For temperature/humidity( chart ) :

->For LED :

5. Programming – 1 :

Install Libraries:

Download ubidotsMQTTESP library

Dowload PubSubClient library

Include libraries ESP8266WiFi, Adafruit_Si7021 from Arduino.

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include "Adafruit_Si7021.h"
#include <PubSubClient.h>

Adafruit_Si7021 sensor = Adafruit_Si7021();
#include <ESP8266WiFi.h>
/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "....." // Your Ubidots TOKEN
#define WIFINAME "....." //Your SSID
#define WIFIPASS "....." // Your Wifi Pass

Ubidots client(TOKEN);
int LED = 14;
float h=0.0;
float t=0.0;


/****************************************
 * Auxiliar Functions
 ****************************************/

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]);
  }
  if ((char)payload[0]=='1'){
    digitalWrite(14, HIGH);
  }
  else{
    digitalWrite(14, LOW);
  }
  Serial.println();
}

/****************************************
 * Main Functions
 ****************************************/

}*/              
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  if(!sensor.begin())
  {Serial.println("did not find sensor Si7021");
  while(true);
    }
  pinMode(14, OUTPUT);
  client.ubidotsSubscribe("esp8266-1","led");
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
  h = sensor.readTemperature();
  t = sensor.readTemperature();
 
  client.add("humidity", h);
  client.add("temperature", t); //Insert your variable Labels and the value to be sent
  client.ubidotsPublish("esp8266-1");
  client.loop();
  delay(6000);
  }

->Add Token from Ubidots.
-> API Credentials>Token

->Add variable labels from API label in Variables.

 6. Screenshot of Serial output:

Here is the screenshot of serial outputs. 

Leave a Reply