Everything about Internet of Things and more

Blynk BRIDGE – Another way to Communicate between two ESP8266 modules

Gearbest  promotion

A new excellent method to communicate between Internet of Things modules, regardless the network or position on globe, is available now with the Blynk BRIDGE Technology provided by the IoT Blynk platform. You can now interconnect all your IoT projects using the Cloud Blynk Server as a live central transaction manager and the best part is that this feature is free. Also a big advantage is the simplicity and speed of development process thanks to the Blynk library which does all the hard work.

ESP8266 to ESP8266 Communication with Blynk Bridge

I know we already covered much of this topic in the esp talk each other article, but I just bumped into this awesome Blynk platform and I am going to get the most of it just because is mostly free and seems reliable. If you are new and want to get started with Blynk app you should read this article first:

Internet of Things for non-programmers with Arduino, Blynk and ESP8266-12F

Also if this is your first contact with the ESP8266 WiFi Module  you can read a complete tutorial in this article:

ESP8266 Arduino tutorial – WiFi module complete review

Now that we covered introduction let’s step into this project.

Project Goal and expectations

The two ESP8266-01 modules used in this project should be able to communicate each other even they both connect to the internet using different networks. One module will be connected using the home WiFi router and the other one will connect using my 4G Vodafone mobile connection via Hot-spot.

The first ESP-01 module will measure temperature and humidity using a DHT22 Temperature and humidity sensor. The measurements will be periodically pushed to the Blynk app dashboard and displayed via Blynk Gauge Widget. We will set a temperature alarm point at 28 Celsius degrees. When this alarm threshold is fired, the first ESP will open a 220V Relay attached to the second ESP-01, and also update a virtual LED on the second ESP Blynk dashboard.

Hardware parts and wiring diagram

Blynk BRIDGE - Another way to Communicate between two ESP8266 modules

Yes, ordinary simple! For the sake of demonstrating the working principle, we need to keep it simple and easy to make it. Therefore in this project the following parts were used:

The working principle – how it works?

Because we have two Internet of Things modules, we also need to create two Blynk projects in the Blynk App. Every IoT module, in our case the ESP-01, will be associated with a unique Blynk Authorization Token. We will use this unique tokens in order to gain access to other Internet of Things modules connected.

If you already read our first Blynk turorial, you should know how a simple Blynk project works. The Blynk Server opens a bidirectional communication channel with the IoT module isolating it with the unique Authorization Token, allowing transactions from both sides. With Blynk Bridge, we can now ask the Blynk server to give us access to other communication channels in exchange of proper tokens.

Blynk BRIDGE - Another way to Communicate between two ESP8266 modules

Once we’ve gained access to the second IoT module, we can now command its GPIOs as simple as we do it for the current one. Blynk Bridge library provides easy methods which are similar with Arduino organic code.

Code for the second ESP module. The one which will be controlled by the temperature alarms

/**************************************************************
   Blynk Bridge - Communication between ESP8266
   Sketch code for the receiver module (module which is controlled)
   www.geekstips.com
 **************************************************************/

#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[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

// This code will update the virtual port 5
BLYNK_WRITE(V5) {
  int pinData = param.asInt();
}

void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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

As you can see, this code has nothing special, is almost as simple as the standalone default sketch. The only differences are in the BLYNK_WRITE function call which we use to forward the updates for the VIRTUAL Ports. Other than that, this “slave” module won’t know what hit him :). It will receive commands from both the first ESP or directly trough the Blynk App in a totally transparent way.

Code for the first ESP module. The one which will control the second ESP according to the temperature alarms

/**************************************************************
   Blynk Bridge - Communication between ESP8266
   Sketch code for the master module (module which will command others)
   www.geekstips.com
 **************************************************************/
#define BLYNK_PRINT Serial

#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[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

#define DHTPIN 2 // What digital pin we're connected to

//#define DHTTYPE DHT11   // DHT 11
  #define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
WidgetBridge bridge1(V1);

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

void sendSensor(){
  // get readings from the DHT22 sensor
  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;
  }

  // Send command to the second ESP
  // WHEN Temperature IS OVER 28 C
  // in order to open the 220V Relay
  // Also update the VIRTUAL port 5 
  // on the second ESP
  if(t > 28){
     bridge1.digitalWrite(2, 1000);
     bridge1.virtualWrite(V5, 1000); 
  }else{
     bridge1.digitalWrite(2, 0);
     bridge1.virtualWrite(V5, 0); 
  }

  // Send temperature and humidity to Blynk App
  // on VIRTUAL ports 5 and 6 in order to 
  // display on Gauge Widget
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();

  timer.setInterval(1000L, sendSensor);
}

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

This sketch code is just a little bit more complex, but no nothing to worry. Some pieces of code were added over the standalone Blynk sketch in order to gain control over the second esp. Beside the Blynk related code, we also need to add DHT22 Arduino library and the specific code to initialize and make the readings. If you haven’t had the chance to play with DHT temperature and humidity sensors you can find everything you need to know in this arduino temperature example.

In the code piece below you can see how the Blynk Bridge functionality is integrated in the sketch. The BLYNK_CONNECTED function contains the Authorization Token for the second ESP to be controlled. Above this function we declare the bridge1, a variable to hold the BRIDGE associated with Virtual 1 Port (V1). We can actually open as many bridges as virtual ports are available in order to control multiple devices.

WidgetBridge bridge1(V1);

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

Once we defined the Bridge remote authentication token, we now can command both physical or virtual GPIOs on the second ESP via the bridge1 instance created in the above steps. Code below demonstrates how easy we can do that:

if(t > 28){
     bridge1.digitalWrite(2, 1000);
     bridge1.virtualWrite(V5, 1000); 
}else{
     bridge1.digitalWrite(2, 0);
     bridge1.virtualWrite(V5, 0); 
}

In this example you can see how the first ESP sends integer value 1000 to both digital and virtual ports on the second ESP, if the temperature gets over 28 Celsius degrees. On the ELSE road it also resets with value 0 when drops under 28C. I used value 1000 because ESP8266 has PWM on almost all GPIOs and sending 1 or 0 as ON/OFF doesn’t always work.

Starting from this example you can easily build more complex Internet of Things projects and set your devices free. You can find the original Blynk Bridge INO sketch on the Blynk github widgets branch. Also you can study the blynk bridge documentation on the official blynk website.

Conclusion

If you ask me, this Blynk platform is probably the most valuable resource available for Internet of Things projects. Considering the benefits it provides it really I wouldn’t mind to pay for it. It can really help you in making advanced projects with a high availability and extremely powerful live reports and dashboards. I confess that I spent weeks working to solve issues which Blynk features fixed in minutes, and for that I really recommend you to give it a try if you didn’t already.

If you want to see more methods of how to make esp8266 module talk each other you definitely need to checkout this great ESP8266 topology created by a geek farmer from Europe.

Hoping that this article inspired you, i kindly invite you share this articlesubscribe my YouTube channel and join the communities on social networks. Please feel free to comment or send suggestions / remarks so i can improve the content quality

Gearbest  promotion

Share on: Facebook Twitter Google+

Leave a Reply

Your email address will not be published.