Connect two esp 8266 NodeMCU together and exchange sensor values

I want to read the virtual pin V6 of second nodemcu and if the temperature value exceeds 32 i need to communicate the first nodemcu and turn on the dc motor on pin D2.
Is this possible?Please Help.
Kindly note: *I have created a blynk project with 2 devices (Both of my Nodemcu)

Yes, its possible.
You need to search for “Bridge” in the forum for examples of how to exchange data between the two devices.
There’s also an example here:
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=Widgets%2FBridge

and some documentation here:

Pete.

thank you but how do i read the temperature value from V6 pin ??
(IN the example the value variable was a random boolean . I want it to be the V6 pin value)

When the temperature is written from the first board to V6, the BLYNK_WRITE(v6) callback on the second board will be triggered.
You then use the param.asInt() , or more probably param.asFloat(), command to capture the transmitted value and store it to a variable that you can then use elsewhere in your code.

Pete.

here `is my code for the first node mcu

#define BLYNK_PRINT Serial  
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>




// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "X***0j"; //Enter the Auth code which was send by Blynk

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "rahul";  //Enter your WIFI Name
char pass[] = "rahul254";  //Enter your WIFI Password
WidgetBridge bridge1(V1);
BlynkTimer timer;

BLYNK_CONNECTED() {
  bridge1.setAuthToken("2e***96"); // Place the AuthToken of the second hardware here
}


#define DHTPIN 2          // Digital pin 4

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);


// This function sends Arduino's up time every second to Virtual Pin (5).
// 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 sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
    float data = param.asFloat();
    if (data>32.0){
    bridge1.digitalWrite(D2, HIGH);  // Digital Pin 2  with the dc motor on the second board will be set HIGH
  }
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
 

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer

}

Sir, I am receiving “param was not declared in the scope error”

Please edit your post to add triple backticks at the beginning and end of your code.
Triple backticks look like this:
```

Pete.

Ive edited the code.The error is still not resolved

You’re getting the error because you’re not using the param.asFloat() command within a BLYNK_WRITE(vPin) callback.

Look at the example…

BLYNK_WRITE(V5)
{
  int pinData = param.asInt(); // pinData variable will store value that came via Bridge
}

Also, using “data” as a variable name probably isn’t a great idea, I think it’s probably a reserved word.

Pete.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>




// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XPl**8OJ"; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "rahul";  //Enter your WIFI Name
char pass[] = "rahul254";  //Enter your WIFI Password
WidgetBridge bridge1(V1);
BlynkTimer timer;

BLYNK_CONNECTED() {
  bridge1.setAuthToken("2e***96"); // Place the AuthToken of the second hardware here
}


#define DHTPIN 2          // Digital pin 4

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);


// This function sends Arduino's up time every second to Virtual Pin (5).
// 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 sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
   
}
BLYNK_WRITE(V6)
{
  int pindata = param.asInt(); // pinData variable will store value that came via Bridge
   if (pindata>31.0){
    bridge1.digitalWrite(D0, HIGH);  // Digital Pin 9 on the second board will be set HIGH
  }
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
 

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer

}

this code successfully compiled and uploaded but didnt work . Is there any existing issues in this code for the transmitting node mcu?

receiving node mcu has been coded this way:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "2e***96";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "rahul";
char pass[] = "rahul254";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
}

what changes are to be made here if any?

You don’t need the BLYNK_WRITE(V6) function on the sending device. and use the actual GPIO number, not the Dpin Number on the board. In this case D6 is GPIO 12

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

 if (t >31.0){
    bridge1.digitalWrite(12, HIGH);  // Digital Pin 9/GPIO 12 on the second board will be set HIGH
  }
   
}

No mater what, timer.setInterval(1000L, sendSensor); is no bueno!

The consensus on this forum is that a DHT-sensor shouldn’t be read that frequently! Try 5000L.

1 Like