[SOLVED] Using Bridge function between WeMos D1 R2 ESP and MEGA

Hi

I’ve having so many problems trying to use Blynk Bridge widget - I just can’t figure out why its not working for me.

My setup:
I have a Mega with a Ethernet Shield and a 4 x Relay Shield that I have verified works all fine. I can turn Relay 3 (on Digital Pin 5) on and off easily and see Relay light and hear relay click so I know that works.

I also have a WeMos D1 R2 ESP board with a simple program that I want to send a HIGH and LOW do Digitalpin 5 on to my Mega so my Relay 3 turns on and off. But it doesn’t work - I have no idea why.

NOTE: I did read on Blynk.cc that : “when communicating between different device types, as Ardiono Core may refer to wrong pins in such cases.” IF this is the case could this be why I can’t get this to work ? and if so can Blynk programmers pls publish the CORE pin mappings ?

I am running all this on my local Blynk server.

My code below…any help would be very much appreciated. :slight_smile:

MEGA Board Code:

/*
 * This is MEGA Arduino with Ethernet Shield and Relay Shield
 * 
 */
 
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
// #include <ESP8266WiFi.h>
// #include <BlynkSimpleEsp8266.h>

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

#define RELAY1  7  // D7  [GPIO13 for ESP]
#define RELAY2  6  // D6  [GPIO12 for ESP]                     
#define RELAY3  5  // D5  [GPIO14 for ESP]  // We are trying to set this digitalPin HIGH and LOW from the WeMos D1 R2 ESP board using BLYNK Bridge widget
#define RELAY4  4   // D4  [GPIO4 for ESP]

long BaudRate = 9600, sysTick = 0;
char GotChar;

char auth[] = "xxxx";  // Blynk token for Mega Project on LOCAL SERVER 

void setup()
{
 // Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);       
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);       
  
  Serial.begin(9600);
  Blynk.begin(auth, IPAddress(xxx,xxx,xxx,xxx), 8442);
}  


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


 BLYNK_WRITE(V7)
{
 // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use:
 int i = param.asInt();
 int state;

 if (i==0) { digitalWrite(RELAY3,LOW);
  Serial.println("Relay 3 LOW = OFF"); } // turn ON Relay 2
 if (i==1) { digitalWrite(RELAY3,HIGH); // turn OFF Relay 2
 Serial.println("Relay 3 HIGH = ON"); 
 delay (500); } //

}


bool isFirstConnect = true;

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
    Serial.println ("Blynk First connected EXECUTED");   
  }
}

The WeMos D1 R2 ESP code is:

#include <SPI.h>

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

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


char rollerdoor_auth[] = "xxxx";  // Blynk token for ARDUINO Mega Project on LOCAL SERVER 


// Bridge widget on virtual pin 1
WidgetBridge bridge1(V20);

// Timer for blynking
SimpleTimer timer;

void setup()
 {
  Serial.begin(9600);
  Blynk.begin(auth, "xxx", "xxx", IPAddress(xxx,xxx,xxx,xxx)); 

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Call blynkAnotherDevice every second
  timer.setInterval(8000L, blynkAnotherDevice);
}

BLYNK_CONNECTED() {
  bridge1.setAuthToken(rollerdoor_auth);
  Serial.println ("Linked to RollerDoor");
}

static bool value = true;
void blynkAnotherDevice()
{
  // Send value to another device
  if (value) {
    bridge1.digitalWrite (5, HIGH);
    Serial.println("Set D5 to high");
  } else {
    bridge1.virtualWrite(5, LOW);
    Serial.println("Set D5 to low");
  }
  // Toggle value
  value = !value;
}

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

@mars you shouldn’t have any problems with the pin definitions of a Mega as it follows the Arduino standard and has been in use for years. WeMos is different but as you are going from WeMos to Mega it shouldn’t be a problem.

If I had your problem I would knock up the most basic system with Blynk cloud and see if the WeMos can control the Mega. Assuming this works you can then try the basic sketch on your local server.

I notice your pin definition on the Mega has D5 as output which is correct for the signal to the relay but I’m guessing you can’t send a signal from the WeMos to an output pin on the Mega. Send to V pin on Mega and set Mega to read V pin and activate D5.

HTH

thanks for reply Costas.

I assume the digtialWrite command is coming from via the WeMos via BridgeWidget to the Blynk server directly to MEGA Blynk code on the h/w then the (MEGA) is doing the actual digitalWrite command then there should be no contention with the fact the pinmode on D5 is set to OUTPUT - is that logic correct ?

On your next point : if I send a bridge1.virtualWrite(Vx, HIGH) to the MEGA from my WeMos how do I most efficiently read that on my MEGA and know its state in order to send the command out to D5 ?

And one last question :slight_smile: when I setup on the Wemos the bridge Widget and pick a virtual pin e.g.

WidgetBridge bridge1(V20);

Is the V20 referred to hear relate to a virtual PIN on my WeMos or MEGA ?

Kind Regards

@mars I’m not entirely sure of the input / output logic used by Blynk and on many occasions, but not all, it doesn’t matter too much how the pins are defined. Logically it just looks wrong to have a pin defined as output when you are receiving an input from Blynk but you might be right as the ethernet connection is receiving the Blynk command to set D5 high or low.

Almost all that we do is based on virtual pins so we don’t have much experience with digital pins via Blynk.

I would have a value display on the Mega dashboard showing a value of 1 or 0 or high / low if it receives the command from the WeMos. With a timer I would read the value display at 1 second intervals and trigger the relay on or off accordingly. This way you can see visually if all is ok. Plus a few more Serial.println statements on the Mega to see what is happening in Serial Monitor.

I am guessing here, but on the WeMos the definition for WidgetBridge bridge1 will be a WeMos virtual pin assignment. Obviously the actually bridging is then done to the relevant Mega pin (virtual or otherwise) with bridge1.digitalWrite (5, HIGH) as you currently have it.

Hi Costas

I’m trying (as you recommended) to use virtualpin’s (V21 I am using) but still not working…am I using it correctly ? see below (on Serial port on WeMos I can see virtualpin information being set to HIGH and LOW but when I check Serial monitor on MEGA there is nothing displayed regarding V21 as part of my BLYNK_WRITE(V1) routing:

Code in WeMos ESP (sending virtualwrite via Bridge widget):

#include <SPI.h>

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

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


char rollerdoor_auth[] = "yyyyyy";  // Blynk token for ARDUINO Mega Project on LOCAL SERVER


// Bridge widget on virtual pin 1
WidgetBridge bridge1(V20);

// Timer for blynking
SimpleTimer timer;

void setup()
 {
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxx", "xxxx", IPAddress(xxx,xxx,xxx,xxx)); 

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Call blynkAnotherDevice every second
  timer.setInterval(8000L, blynkAnotherDevice);
}

BLYNK_CONNECTED() {
  bridge1.setAuthToken(rollerdoor_auth);
  Serial.println ("Linked to RollerDoor");
}

static bool value = true;
void blynkAnotherDevice()
{
  // Send value to another device
  if (value) {
    bridge1.virtualWrite (V21, HIGH);
    Serial.println("Set V21 to high");
  } else {
    bridge1.virtualWrite(V21, LOW);
    Serial.println("Set V21 to low");
  }
  // Toggle value
  value = !value;
}

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

Code in MEGA

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

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

#define RELAY1  7  // D7  [GPIO13 for ESP]
#define RELAY2  6  // D6  [GPIO12 for ESP]                     
#define RELAY3  5  // D5  [GPIO14 for ESP]  // We are trying to set this digitalPin HIGH and LOW from the WeMos D1 R2 ESP board using BLYNK Bridge widget
#define RELAY4  4   // D4  [GPIO4 for ESP]

long BaudRate = 9600, sysTick = 0;
char GotChar;

char auth[] = "xxxxx";  // Blynk token for Mega Project on LOCAL Blynk SERVER

void setup()
{
 // Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);       
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);       
  
  Serial.begin(9600);
  Blynk.begin(auth, IPAddress(xxx,xxx,xxx,xxx), 8442);
}  


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


BLYNK_WRITE (V21)
{
 int i = param.asInt();
 int state;

 if (i==0) { digitalWrite(RELAY3,LOW);
  Serial.println("FROM V21 Relay 3 LOW = OFF"); } // turn ON Relay 2
 if (i==1) { digitalWrite(RELAY3,HIGH); // turn OFF Relay 2
 Serial.println("FROM V21 Relay 3 HIGH = ON"); 
 delay (500); } //
 
}

 BLYNK_WRITE(V7)
{
 // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use:
 int i = param.asInt();
 int state;

 if (i==0) { digitalWrite(RELAY3,LOW);
  Serial.println("Relay 3 LOW = OFF"); } // turn ON Relay 2
 if (i==1) { digitalWrite(RELAY3,HIGH); // turn OFF Relay 2
 Serial.println("Relay 3 HIGH = ON"); 
 delay (500); } //

}


bool isFirstConnect = true;

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
    Serial.println ("Blynk First connected EXECUTED");   
  }
}

Looks ok from here.

Is V21 a value display on the Mega? I would send 1 or 0 to the value display or the WORD “high” / “low” from WeMos to see if this appears on the Mega value display.

You can worry about what to do after this once the text appears on the Mega dashboard. No text on the dashboard means you have a problem somewhere.

I might try WeMos to Wemos at this side and see what I get.

Nope - can’t get it to work. I’m completely stumped on this. :disappointed:

I recall on Blync Documentation the following:

“Also have in mind bridge.virtualWrite doesn’t send any value to mobile application, in order to do that you need call Blynk.virtualWrite”

I just can’t work out what I’m doing wrong either.

Costas - keen to see if you can get it working.

Knocked up a quick Wemos to Wemos and it is fine like this:

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

char auth[] = "xxxxxxxxxxxxxxx";

WidgetBridge bridge1(V1); // Bridge widget on virtual pin 1

SimpleTimer timer;

static bool value = true;

void blynkAnotherDevice()
{
  // Send value to another device
  if (value) {
    bridge1.virtualWrite(V5, 1);
    Serial.println("Sent High to other WeMos");
  } else {
    bridge1.virtualWrite(V5, 0);
    Serial.println("Sent Low to other WeMos");
  }
  
  value = !value;  // Toggle value
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, "xxxxxxxxxxx", "xxxxxxxxxx");
  //Blynk.begin(auth);

  while (Blynk.connect() == false) {
    // Wait until connected
  }


  timer.setInterval(1000L, blynkAnotherDevice);
}

BLYNK_CONNECTED() {
  bridge1.setAuthToken("xxxxxxxxxxxxxxxxx");
}



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

Without pressing anything on any device what the sketch does every second is ‘press’ V5 button on one WeMos from another Wemos. The WeMos being controlled is one of our ‘real’ projects and the virtual button “presses” are confirmed on the virtual LCD with thermostat time increasing at 5 minutes per button press (as set with the slider).

@mars do you have bridge Widget in both dashboards?
Do you have lots of Wemos to be able to do a cloud test from Wemos to Wemos?

Hi,

On your Mega, why don’t you use

   ` if (i==0) { digitalWrite(RELAY3.....
       .
       .
     else { digitalWrite(RELAY3...
       .
       .  `

or, are you sure when the WeMos is sending a HIGH, it is sending a 1 instead of a 255.

thanks Costas
I don’t have Bridge Widgets in any of the app screens (I believe this is optional and actually doesn’t do anything - correct ?)

I have another Wemos so I will do Wemos to Wemos.

Do I assume that on the receiving Wemos you would do a BLINK_WRITE(V5) routine to capture the the virtual write message from the sending Wemos ?

Kind Regards

The bridge widgets cost 100 energy units so I suspect the requirement not to have one has changed. They are not tied to any pins other than in the sketch but you should add them to both apps and retest.

A BLYNK_WRITE(V5) not a BLINK_WRITE(V5) but you probably don’t need to do that as you should be able to write directly to the digital pin, once you have the bridge widgets in your apps.

will take a look at that - thanks NickM

Hi All

Worked it out.
It all works fine when I use the public Blynk Server
BUT Bridge does NOT work when I user my Local Blynk Server

Costas, can you verify this on your end ?

Any other Blynk users seen same behaviour ? is this a Blynk Local Server bug or do I need to configure something on Blynk Local Server settings ?

I don’t use local servers, sorry.

no. should work. did you point your hardware to local server? did you using local server auth tokens?

Hi Mars
In your code is says
Blynk.begin(auth, IPAddress(xxx,xxx,xxx,xxx), 8442);
is that something to do with the bridge function?
Normally it would look like
Blynk.begin(auth, “xxxxx”, “xxxx”, IPAddress(xxx,xxx,xxx,xxx));
ie || SSID || Paswd || Blynk_server_IP||

@jt666 he is using ethernet on the Mega so no SSID and PWD.

Thanks Costas I didn’t understand that