ESP8266IOT Projects

Ubidots IOT Platform Control and Monitoring using Nodemcu esp8266 wifi Module

Ubidots IoT Platform, Description:

 

Ubidots IoT Platform- In this tutorial, you will learn how to monitor and control both at the same time using the Nodemcu esp8266 wifi module and the Ubidots IoT Platform. In this project, a variable resistor or potentiometer will be used as the sensor for the demonstration purposes. When the variable resistor value exceeds a predefined value the indicator lamp is turned on, which is a feedback system and is accomplished using the Ubidots events.

In this Tutorial, you will also learn how to control the electrical loads; these are basically 220Vac indicator lamps, which of course you can replace with any electrical loads. For higher loads, you can use large electromagnetic relays.


Amazon Links:

Nodemcu ESP8266 WiFi Module:

LM7805 Voltage Regulator:

470uf capacitor:

330-ohm resistor:

DC Female Power Jack:

Female Headers:

Male Headers:

12V SPDT Relay:

One-Channel Relay Module:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


Nodemcu Power Supply and Relay Interfacing Circuit Diagram:

ubidots

This is the 5v regulated power supply based on the lm7805 voltage regulator. This 5v power supply will be used to power up the Nodemcu esp8266 wifi module. Two 470uf capacitors are connected at the input and output sides of the 7805 voltage regulator. A 330-ohm resistor is connected in series with the led. This is a current limiting resistor.  J1 is the dc female power jack this is where we can connect a 12v adaptor or battery. A wire from the output of the voltage regulator is connected with the Vin pin of the Nodemcu module and makes sure the power supply ground is connected with the Nodemcu ground pin.


The middle leg of the Variable resistor or Potentiometer is connected with the Analog pin A0 of the Nodemcu module. while the leftmost and rightmost legs of the variable resistor are connected with the 3.3v and ground. It really doesn’t matter which pin you connect with the 3.3v and ground as the resistor has no polarity.

For controlling these relays we will need relay drivers, the relay drivers simply consist of the 2n2222 NPN transistors, 10k resistors, and diodes. As you can see a 10k resistor is connected with the base of the 2n2222 NPN transistor as it’s a bjt “bipolar junction transistor” a current-controlled device, that’s why we need a current limiting resistor. The emitter of the 2n2222 NPN transistor is connected with the ground while the collector is connected with one side of the relay coil, while the other side of the relay coil is connected with 12 volts. This relay can be energized and de-energized using this transistor.  As you can see this relay consists of 5 pins, two coil pins, common, normally closed and normally open. These three pins have no physical connection with the coil pins….


So this is a 3 channel relay module, you can also use a readymade relay module. these are 10k resistors connected with the base of 2n2222 NPN transistors, these are the 1n4007 diodes connected across the relay coil pins, these are 12v SPDT type relays and these are the terminal blocks. I have soldered some jumper wires so that this relay module can be easily interfaced with the Nodemcu module other circuits. This relay module consists of 3 relays but I will be using only 2 relays. These relay will be connected with D0 and D1 pins of the Nodemcu module as per the circuit diagram.

Nodemcu ESP8266 Programming:

Before you start the programming, first of all, make sure that you download the Ubidots espmqtt library from the GitHub. after you download the library then simply open the latest version of the Arduino ide and add this library.

download: 

After you add the Ubidots espmqtt library then also download the pubsubclient library and add it the same way as I did for the ubidosts espmqtt library.

Download: 

#include "UbidotsESPMQTT.h"

/****************************************
   Define Constants
 ****************************************/
#define TOKEN "BBFF-J7PJo14TCt2bEkOvLPj0M5RKoATGXy" // Your Ubidots TOKEN
#define WIFINAME "ZONG MBB-E8231-6E63" //Your SSID
#define WIFIPASS "08659650" // Your Wifi Pass
#define DEVICE_LABEL  "load_management"  // Put here your Ubidots device label
#define VARIABLE1  "light1"  // Put here your Ubidots variable label
#define VARIABLE2  "light2"  // Put here your Ubidots variable label
#define VARIABLE3  "light3"  // Put here your Ubidots variable label
#define VARIABLE4  "light4"  // Put here your Ubidots variable label

int load1 = D0; 
int load2 = D1; 
int load3 = D2; 
int load4 = D3;

int vresistor = A0; 
float value=0; // To store incoming value.

Ubidots client(TOKEN);

/****************************************
   Auxiliar Functions
 ****************************************/
 
// cast from an array of chars to float value.
float btof(byte * payload, unsigned int length) {
  char * demo = (char *) malloc(sizeof(char) * 10);
  for (int i = 0; i < length; i++) {
    demo[i] = payload[i];
  }
  float value = atof(demo);
  free(demo);
  return value;
}

// Callback to handle subscription

void callback(char* topic, byte* payload, unsigned int length) {
  value = btof(payload, length);
  Serial.println(value);

if (value == 0.00)
{
  Serial.println("Light1 turned off"); 
  digitalWrite(load1, LOW); 
  client.add("light1",0);

}

if (value == 1.00)
{
  Serial.println("Light1 turned ON"); 
  digitalWrite(load1, HIGH); 
    client.add("light1",1);

}


if (value == 2.00)
{
  Serial.println("Light2 turned off"); 
  digitalWrite(load2, LOW); 
  
}

if (value == 3.00)
{
  Serial.println("Light2 turned ON"); 
  digitalWrite(load2, HIGH); 
}



if (value == 4.00)
{
  Serial.println("Light3 turned off"); 
  digitalWrite(load3, LOW); 
}

if (value == 5.00)
{
  Serial.println("Light3 turned ON"); 
  digitalWrite(load3, HIGH); 
  
}



if (value == 6.00)
{
  Serial.println("Light4 turned off"); 
  digitalWrite(load4, LOW); 
}

if (value == 7.00)
{
  Serial.println("Light4 turned ON"); 
  digitalWrite(load4, HIGH); 
}
   
}

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

void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE1); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE2); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE3); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE4); //Insert the dataSource and Variable's Labels

 pinMode(load1, OUTPUT); 
 pinMode(load2, OUTPUT); 
 pinMode(load3, OUTPUT); 
 pinMode(load4, OUTPUT); 

pinMode(vresistor, INPUT); 

}

void loop() {
  // put your main code here, to run repeatedly:
  if (!client.connected()) {
    client.reconnect();
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE1); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE2); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE3); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE4); //Insert the dataSource and Variable's Labels

   client.ubidotsPublish("Monitoring");
  }

int data = analogRead(vresistor); 
  client.add("vresistor",data); 
  client.ubidotsPublish("Monitoring"); 
  
  client.loop();
}

Ubidots IoT platform setup:

For the Ubidots IoT platform setup watch video tutorial given below.

Watch Video Tutorial:

 

Another Ubidots IOT Platform based Project:

https://www.electroniclinic.com/ubidots-nodemcu-light-intensity-monitoring-using-gauges-and-charts/

 

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button