Arduino ProjectsESP8266IOT Projects

Nodemcu with Arduino, Serial Communication, Control and Monitoring

Nodemcu With Arduino, Description:

 

Nodemcu with Arduino– In this tutorial, you will learn how to do Serial communication between Arduino and Nodemcu esp8266 wifi module. For the demonstration purpose in this project LDR and variable resistor will be monitored using the Blynk Application. The Variable Resistor and LDR values will be displayed on the Gauges using the Blynk Application.  Monitoring two Analog sensors is only possible due to the use of the Arduino Uno.

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 in a normal way. In this Tutorial, I will also explain why we need to connect the Nodemcu and Arduino Uno together. I will also explain when and when not use the Arduino Uno with the Nodemcu ESP8266 wifi module. Without any further delay, let’s get started!!!

Amazon Links:

12v Adaptor:

Arduino Uno

Arduino Nano

Nodemcu ESP8266 WiFi Module:

LM7805 Voltage Regulator:

470uf capacitor:

330-ohm resistor:

DC Female Power Jack:

Female Headers:

Male Headers:

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

DISCLAIMER:

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!



Arduino and Nodemcu ESP8266 together:

I know we can make the same project using only the Nodemcu ESP8266 Wifi module, and we can reduce the project size, power and building time, etc. You know very well the Nodemcu ESP8266 Wifi module has a very limited number of the IO pins, and there is only 1 analog Pin. I have been frequently asked by my followers on YouTube Channel “Electronic Clinic” that how to connect multiple analog sensors or how to increase the IO pins of the Nodemcu Module. My replay is, always use Arduino or Mega with the Nodemcu Module. We can also use a multiplexer, but that needs wiring and complicated programming which we really don’t want. So the easiest option is to use the Arduino Uno or Mega with the Nodemcu ESP8266 Wifi module.

So, that’s the reason I am using Arduino Uno with the Nodemcu Module. In this project, you will learn completely how to connect the Nodemcu module with the Arduino and how to establish Serial Communication. As this project is based on two-way communication, so, in this project we will be monitoring the sensors and controlling some electrical loads both at the same time using the Blynk Application. This project can be easily modified and can be used to monitor any type of sensors.

This Project is based on my previous two tutorials.

In this tutorial, you will learn how to install the Nodemcu esp8266 wifi board and how to download and use the blynk library and how to fix the USB UART driver error.

While in this tutorial you will learn how to make a power supply for Nodemcu esp8266 wifi module so that it can be easily powered up using a 12v adaptor or battery.

Nodemcu with Arduino Circuit Diagram:

Nodemcu with Arduino

This is the complete circuit diagram. This schematic is designed in the cadsoft eagle 9.1.0 version. If you want to learn how to make a schematics and PCB then watch my tutorial given above.

Let’s first start with the regulated Power Supply which is based on the famous LM7805 voltage Regulator. This Regulated Power Supply will be used to power up the Nodemcu ESP8266 wifi module. J1 is the female Power Jack and this is where we connect a 12v Adaptor or Battery or a Solar Panel. Two 470Uf capacitors are connected at the input and output of the voltage regulator. A 330 Ohm resistor is connected in series with a 2.5v LED. This is a current limiting resistor. This is a Power ON LED and it’s the only purpose is to show that the circuit is Turned ON.

The Output of the Regulated Power Supply is connected with the Vin pin of the Nodemcu ESP8266 Wifi Module and the ground is connected with the ground.


A variable resistor is connected with the A0 pin of Arduino, an LDR is connected in series with a 10k resistor, which makes a voltage divider and is connected with the 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 consist 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 the 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.

Nodemcu with Arduino

So this is the relay module, you can also use the readymade relay module.

Nodemcu with Arduino

these are 10k resistors connected with the base of 2n2222 NPN transistors,

Nodemcu with Arduino

these are the 1n4007 diodes connected across the relay coil pins,


Nodemcu with Arduino

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 Designing:

For the complete step by step application designing watch video tutorial given at the end of this article or following the instructions given below.

  • First of all, open the blynk application…..
  • set the project name as Control and monitor…..
  • Click on the chosen device and select nodemcu….
  • Make sure you set the connection type to wifi….
  • Then click on the create button, an authentication token will be sent on your email id, which will be then used in programming, simply copy and paste it in programming.
  • click on the screen and add a Gauge…..
  • Click on the gauge and set the name as variable resistor…..
  • Now click on the pin and select virtual and select v2….
  • Change the font….click on the push and select 1 second…..
  • Now add another gauge and repeat the same steps. After you are done with adding the Gauges on the screen, then start adding the numeric inputs.
  • Click on the screen and add numeric input……
  • Click on the numeric input, click the pin and select the virtual pin10.
  • Set the minimum value to 10 and the maximum value to 11. These 10 and 11 values will be used to control the relay connected with pin13 of the Arduino. finally set the name as relay1, if you want you can also change the font.
  • Now repeat the same steps for other numeric input and re-arrange the gauges and numeric inputs and change the color if you want.


Nodemcu with Arduino

Our basic application setup is completed.

Nodemcu with Arduino Programming:

This Project is based on two Programs, one program is written for the Arduino Uno while the other program is written for the Nodemcu ESP8266 Wifi Module. First let’s start with the Arduino Programming.


Arduino Programming:

#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:

#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 consistors of snesors 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 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);

}

For the programs explanation, watch the following Video. Don’t forget to Subscribe. Support my channel by liking this video and sharing this video.


Watch Video Tutorial:

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...

5 Comments

  1. hello, I followed his interesting video, I am trying to use the same communication method but I did not understand this part of the code, could you explain me what it does and how it works?
    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]) : "";
    

    }

    1. this code is used to split string messages. let’s say you have a message something like.
      34,454,235,45543,
      let’s say you want to split this message using comma as the delimiter. this is helpful when you have multiple sensors values and you want to split these values on the receiver side. you can use any character as the delimiter.

  2. Hello, i followed the code but ended up with this error

    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino: In function ‘void myTimerEvent()’:
    BLYNK_NODEMCU_VENAT:29:3: error: ‘Blynk’ was not declared in this scope
    29 | Blynk.virtualWrite(V1, millis() / 1000);
    | ^~~~~
    BLYNK_NODEMCU_VENAT:29:22: error: ‘V1’ was not declared in this scope; did you mean ‘D1’?
    29 | Blynk.virtualWrite(V1, millis() / 1000);
    | ^~
    | D1
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino: In function ‘void setup()’:
    BLYNK_NODEMCU_VENAT:40:3: error: ‘Blynk’ was not declared in this scope
    40 | Blynk.begin(auth, ssid, pass);
    | ^~~~~
    BLYNK_NODEMCU_VENAT:42:41: error: no matching function for call to ‘SimpleTimer::setInterval(long int, void (&)())’
    42 | timer.setInterval(1000L,sensorvalue1);
    | ^
    In file included from C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino:5:
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\libraries\SimpleTimer/SimpleTimer.h:23:10: note: candidate: ‘void SimpleTimer::setInterval(uint64_t)’
    23 | void setInterval(uint64_t interval);
    | ^~~~~~~~~~~
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\libraries\SimpleTimer/SimpleTimer.h:23:10: note: candidate expects 1 argument, 2 provided
    BLYNK_NODEMCU_VENAT:43:42: error: no matching function for call to ‘SimpleTimer::setInterval(long int, void (&)())’
    43 | timer.setInterval(1000L,sensorvalue2);
    | ^
    In file included from C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino:5:
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\libraries\SimpleTimer/SimpleTimer.h:23:10: note: candidate: ‘void SimpleTimer::setInterval(uint64_t)’
    23 | void setInterval(uint64_t interval);
    | ^~~~~~~~~~~
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\libraries\SimpleTimer/SimpleTimer.h:23:10: note: candidate expects 1 argument, 2 provided
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino: In function ‘void loop()’:
    BLYNK_NODEMCU_VENAT:51:3: error: ‘Blynk’ was not declared in this scope
    51 | Blynk.run();
    | ^~~~~
    BLYNK_NODEMCU_VENAT:52:9: error: ‘class SimpleTimer’ has no member named ‘run’
    52 | timer.run(); // Initiates BlynkTimer
    | ^~~
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino: In function ‘void sensorvalue1()’:
    BLYNK_NODEMCU_VENAT:86:3: error: ‘Blynk’ was not declared in this scope
    86 | Blynk.virtualWrite(V2, sdata);
    | ^~~~~
    BLYNK_NODEMCU_VENAT:86:22: error: ‘V2’ was not declared in this scope; did you mean ‘D2’?
    86 | Blynk.virtualWrite(V2, sdata);
    | ^~
    | D2
    C:\Users\Andrew Phiri\Desktop\MUST Design Studio\ARDUINO WORKS\VENAT\BLYNK_NODEMCU_VENAT\BLYNK_NODEMCU_VENAT.ino: In function ‘void sensorvalue2()’:
    BLYNK_NODEMCU_VENAT:94:3: error: ‘Blynk’ was not declared in this scope
    94 | Blynk.virtualWrite(V3, sdata);
    | ^~~~~
    BLYNK_NODEMCU_VENAT:94:22: error: ‘V3’ was not declared in this scope; did you mean ‘D3’?
    94 | Blynk.virtualWrite(V3, sdata);
    | ^~
    | D3
    exit status 1
    ‘Blynk’ was not declared in this scope

Leave a Reply

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

Back to top button