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:
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Nodemcu Power Supply and Relay Interfacing Circuit Diagram:
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#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/