Combine two project into one for ESP8266 or NodeMCU

Hello
I had 2 separate projects that are working fine, each project connected to it own ESP8266. I want to combine 2 projects into one ESP8266 so I can add or subtract the value on each project to get a final output.

For example:

  • project 1 has output value for V1, V2, V3
  • Project 2 has output value for V7, v8, v9

==> I want the output to be V22 (which V22= V3-V7)

How do I archive this output?

With Blynk you could use the bridge to send the value to one ESP then calculate it in that ESP and send the result to the app. Or if you are using MQTT you could send both values to node Red to calculate there and send the value to the app. The last would require bode red knowledge and a bit of Java to calculate.

Hi Dave,

I just tried to use the bridge but still can’t read the data from second device, what did I do wrong, please advice.

Here is the code for my device A:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
ModbusMaster node;
SimpleTimer timer;
SoftwareSerial pzem(D5,D6);
double V_PZEM;
uint8_t result;  uint16_t data[6];
 
//start for OTA =================================
#include <ArduinoOTA.h>
char ssid[] = "xxx";                                  
char pass[] = "xxxxxxxxx";                            
char server[] = "192.168.1.20";                       
int port = 8080;                                      
#define USE_LOCAL_SERVER                              
#define OTA_HOSTNAME                    "PZEM-004_DeviceB"
#define AUTH  "c935d49ee5c140e3840a63d42266566c"     
//End  for OTA =================================
 

void setup()
{
  Serial.begin(115200); Serial.println("Start serial"); pzem.begin(9600); Serial.println("Start PZEM serial");
node.begin(1, pzem);  Serial.println("Start PZEM"); // 1 = ID MODBUS
 
//start  for OTA =================================
WiFi.mode(WIFI_STA);
#if defined(USE_LOCAL_SERVER)
WiFi.begin(ssid, pass);
Blynk.config(AUTH, server, port);
#else
Blynk.begin(AUTH, ssid, pass);
#endif
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
//End for OTA =================================
}

void updateBlynk() {
  Blynk.virtualWrite(V5, V_PZEM);
}
 
void loop()
{
  Blynk.run();
  ArduinoOTA.handle();      //Required for OTA  ====================================
  timer.run();
  result = node.readInputRegisters(0x0000, 10);
    if (result == node.ku8MBSuccess)  {
  V_PZEM      = (node.getResponseBuffer(0x00)/10.0f);
    }
  Serial.print("V_PZEM:     "); Serial.println(V_PZEM);
  updateBlynk();
  delay(1000);
}

Here is the code for device B:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define DHTPIN 0          // D3
#define DHTTYPE DHT11     // DHT 11
 
//start for OTA =================================
#include <ArduinoOTA.h>
char ssid[] = "xxx";                                  
char pass[] = "xxxxxxxx";                            
char server[] = "192.168.1.20";                       
int port = 8080;                                      
#define USE_LOCAL_SERVER                              
#define OTA_HOSTNAME                    "DHT11_DeviceA"
#define AUTH  "7a19c505ce524a1fb6cf545f580a125a"     
//End  for OTA =================================
 
DHT dht(DHTPIN, DHTTYPE);
double V_PZEM2, Combine;
BlynkTimer timer;

// Bridge widget on virtual pin0
WidgetBridge bridge1(V0);

BLYNK_CONNECTED() {
  bridge1.setAuthToken("c935d49ee5c140e3840a63d42266566c"); // Place the AuthToken of the second hardware here
}


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature() for Celcious
  Serial.print("Humidity =    "); Serial.println(h);  //% 
  Serial.print("Temperature = "); Serial.println(t); //Fahrenheit
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);
  bridge1.virtualWrite(V5, V_PZEM2);
  Combine = (t + V_PZEM2);
  Blynk.virtualWrite(V6, Combine);
}



void setup()
{
  // Debug console
  Serial.begin(9600);
 
//Key start  for OTA =================================
        WiFi.mode(WIFI_STA);
        #if defined(USE_LOCAL_SERVER)
        WiFi.begin(ssid, pass);
        Blynk.config(AUTH, server, port);
        #else
        Blynk.begin(AUTH, ssid, pass);
        #endif
        while (Blynk.connect() == false) {}
        ArduinoOTA.setHostname(OTA_HOSTNAME);
        ArduinoOTA.begin();
//Key End for OTA =================================

  dht.begin();
 
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}
 
void loop()
{
  Blynk.run();
  ArduinoOTA.handle();      //Required for OTA  ====================================
  timer.run();
}

the compile completed successful but I still can’t get the reading from my second device :frowning:

The combine reading for V5 (V_PZEM in device A) and V2 (temp in device B) still does not show up as it should be. What did I do wrong?

Please help

I’ve never used Bridge (because I use Node-Red and MQTT instead) but my understanding is that one device will be the receiver. This doesn’t need any Bridge code, other than a BLYNK_WRITE(vPin) which will be triggered when data is sent to it from the other device.

The other device has its’s own Auth code which will be different to the first one (you haven’t shown this in your second sketch, it just says “Put your Blynk local server IP address here”) and the Bridge Auth code which is the Auth code from the first device.

The second device sends data to the first one via the Bridge virtual pin (V23) via a Blynk.virtualWrite(V23, Value) command, which you are doing.

The other issue is that both of your devices valve code which does nothing for the vast majority of the time, because they have a delay(1000) in their void loops, blocking all code execution for one second. Blynk.virtualWrites don’t get processed until a Blynk.run is encountered, and neither do BLYNK_WRITE callbacks.
You’ll need to move the code out of your void loop and into a function that is called with a timer - in both sets of code, if you want this to work correctly.

Pete.

I have modified my sketch (see above) to make it simpler to troubleshoot but I still ran into the problem as the beginning that the reading on sketch A still not show up in sketch B and the combine reading still wrong.

I don’t think that you’ve taken on board anything I’ve said about how Bridge works and what you needed to do to make this work.

I’m not going to write your code for you, so I’ll take a step back and let others try to help you with this.

Pete.