Arduino and Nodemcu two way communication, Control and Monitoring
Table of Contents
Arduino and Nodemcu Two-way communication Description:
Arduino Nodemcu two way communication – In this Tutorial, you will learn how to do two way communication between Arduino and Blynk through Nodemcu esp8266 wifi module. At the end of this tutorial, you will be able to monitor multiple analog sensors and you will also be able to control multiple loads. In this project, no multiplexers will be used. All the sensors and loads will be connected with the Arduino in a normal way while the Nodemcu esp8266 wifi module will be used as the bridge only. Let’s get started
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
This project is basically based on the Serial communication between the Arduino and Nodemcu esp8266 wifi module. As you know in Nodemcu esp8266 wifi module we have only one Analog pin and have less number of Input/Output pins, to increase the number of Analog pins and digital pins the best choice is to connect the Nodemcu module with the Arduino Uno or Mega.
Nodemcu Interfacing with Arduino Circuit Diagram:
Let’s have a look at the complete circuit diagram. This schematic is designed in cadsoft eagle 9.1.0 version. If you want to learn how to make schematics and PCB’s then watch my tutorial.
The power supply is already explained in my previous tutorials, you can use this power supply to power the Arduino by connecting its output with the Vin pin of the Arduino. But I will connect a 12v adaptor directly with Arduino and I will power up the Nodemcu esp8266 using the Arduino’s 5v and ground. As you can see the Arduino’s 5v is connected with Vin pin of the Nodemcu and the ground is connected with ground.
All the grounds are connected. A variable resistor is connected with A0 pin of the Arduino, an LDR is connected in series with a 10k resistor, which makes a voltage divider and is connected with A1 pin of the Arduino. The tx and Rx pin’s of the Nodemcu are connected with pin2 and pin3 of the Arduino. So the Nodemcu will communicate serially with Arduino Uno through pin2 and pin3. Pin2 is Rx and pin3 is tx, which will be defined in the programming using the software serial library.
For controlling these relays we will need relay drivers, the relay drivers simply consists of the 2n2222 NPN transistors, 10k ohm resistors, and diodes. As you can see a 10k resistor is connected with the base of 2n2222 NPN transistor as it’s a bjt “bipolar junction transistor” a current controlled device, that’s why we need a current limiting resistor. We usually use a 10k 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 12v. 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 the relay module, you can also use the 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 other circuits. This relay module consists of 7 relays but I will be using 2 relays. Connect 2 relays with pin number13…and …pin number12 of the Arduino Uno or mega And connect the relay module ground with the ground of Arduino.
Blynk Application:
For the step by step Blynk application designing watch Video tutorial.
This is how the Blynk application looks like.
Arduino and Nodemcu Programming:
Arduino Programming:
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 |
#include <SoftwareSerial.h> #include <Wire.h> #include <Adafruit_BMP085.h> SoftwareSerial nodemcu(2,3); // Connect VCC of the BMP180 / BMP085 sensor to 3.3V // Connect GND to Ground Adafruit_BMP085 bmp; long int data; int relay1 = 13; int relay2 = 12; int sdata1 = 0; // temperature data long int sdata2 = 0; // pressure data int sdata3 = 0; // altitude data String cdata; // complete data void setup() { Serial.begin(9600); nodemcu.begin(9600); pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); if (!bmp.begin()) { Serial.println("Could not find a valid BMP180 sensor, check wiring!"); while (1) {} } } void loop() { if(nodemcu.available() == 0 ) { Serial.print("Temperature = "); Serial.print(bmp.readTemperature()); sdata1 = bmp.readTemperature(); Serial.println(" *C"); Serial.print("Pressure = "); Serial.print(bmp.readPressure()); sdata2 = bmp.readPressure(); Serial.println(" Pa"); // Calculate altitude assuming 'standard' barometric // pressure of 1013.25 millibar = 101325 Pascal Serial.print("Altitude = "); Serial.print(bmp.readAltitude()); sdata3 = bmp.readAltitude(); Serial.println(" meters"); cdata = cdata + sdata1+","+sdata2+","+sdata3; Serial.println(cdata); nodemcu.println(cdata); delay(1000); // 100 milli seconds cdata = ""; } if ( nodemcu.available() > 0 ) { data = nodemcu.parseInt(); delay(100); Serial.println(data); if ( data == 10 ) { digitalWrite(relay1, LOW); } if ( data == 11 ) { digitalWrite(relay1, HIGH); } // relay2 if ( data == 12 ) { digitalWrite(relay2, LOW); } if ( data == 13 ) { digitalWrite(relay2, HIGH); } } } |
Nodemcu Programming:
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 |
#define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #include <SoftwareSerial.h> #include <SimpleTimer.h> int pinValue1; int pinValue2; int pinValue3; int pinValue4; char auth[] = "80a57c58dde24141834e87f459881ac3"; // Your WiFi credentials. // Set password to "" for open networks. char ssid[] = "ZONG MBB-E8231-6E63"; char pass[] = "08659650"; SimpleTimer timer; String myString; // complete message from Arduino, which consists of sensors data char rdata; // received charactors //for temperature , pressure and altitude int firstVal, secondVal,thirdVal; // sensors // This function sends Arduino's up time every second to Virtual Pin (1). // In the app, Widget's reading frequency should be set to PUSH. This means // that you define how often to send data to Blynk App. void myTimerEvent() { // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V1, millis() / 1000); } void setup() { // Debug console Serial.begin(9600); Blynk.begin(auth, ssid, pass); timer.setInterval(1000L,sensorvalue1); timer.setInterval(1000L,sensorvalue2); timer.setInterval(1000L,sensorvalue3); } void loop() { if (Serial.available() == 0 ) { Blynk.run(); timer.run(); // Initiates BlynkTimer } if (Serial.available() > 0 ) { rdata = Serial.read(); myString = myString+ rdata; // Serial.print(rdata); if( rdata == '\n') { // Serial.println(myString); // Serial.println("fahad"); // new code String l = getValue(myString, ',', 0); String m = getValue(myString, ',', 1); String n = getValue(myString, ',', 2); firstVal = l.toInt(); secondVal = m.toInt(); thirdVal = n.toInt(); myString = ""; // end new code } } } void sensorvalue1() { int sdata = firstVal; // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V2, sdata); } void sensorvalue2() { int sdata = secondVal; // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V3, sdata); } void sensorvalue3() { int sdata = thirdVal; // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V4, sdata); } String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = { 0, -1 }; int maxIndex = data.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } // in the Blynk app writes values to the Virtual Pin 10 BLYNK_WRITE(V10) { pinValue1 = param.asInt(); // assigning incoming value from pin V10 to a variable Serial.print(pinValue1); } // in Blynk app writes values to the Virtual Pin 11 BLYNK_WRITE(V11) { pinValue2 = param.asInt(); // assigning incoming value from pin V10 to a variable Serial.print(pinValue2); } |
Watch Video Tutorial:
Other Nodemcu and Arduino Related Projects:
YouTube Views and Subscribers count using Nodemcu ESP8266 & Arduino
Multiple analog sensors monitor using Nodemcu esp8266
IOT Water Quality monitoring using Arduino,pH Sensor,Nodemcu ESP8266
Nodemcu ESP8266 IOT Power Lines Mains frequency monitoring 50Hz
IOT light dimmer using Arduino and Nodemcu esp8266 wifi module
Nodemcu and BMP180 “temperature, pressure & Altitude” internet of things project “iot”
Arduino IOT Project: Nodemcu ESP8266 wifi Robot Car “L298N motor driver + Blynk + Joystick”
Hai,
I have problem from Arduino and Nodemcu two way communication, Control and Monitoring.
my problem is relay cannot control from blynk, the code same with tutorial.
any problem?
thanks