BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk on 2 devices - How to check from the first that the second is alive


#1

Hi all,

I am building a thermostat project that consists of 2 nodemcu connected to blynk.
Both nodemcu work on the same project.
First nodemcu (Node1) is gathering temperature from a room and uploads it to cloud blynk server.
Second nodemcu (Node 2) opens a relay depending on temperature gathered from the first one.

My problem is that I would like to know in the code of “node 2” if “node 1” is online.
I do not want that “node 2” opens or closes the relay if “node 1” is not connected.

Thank you,
Andreas


#2

Any particular reason for not using a single MCU for both tasks?

As far as using multiple MCUs for a shared task, there are a few ways you can compare status and/or share tasks…

http://docs.blynk.cc/#blynk-main-operations-control-of-multiple-devices

http://docs.blynk.cc/#blynk-main-operations-devices-online-status

http://docs.blynk.cc/#widgets-other-bridge


#3

Hi @Gunner,

Any particular reason for not using a single MCU for both tasks?

Yes - nodemcu are going to be placed on different floors of a building.

Edit: Also I dont see anything in the docs that can help…

Andreas


#4

With s Bridge widget you can communicate between your devices. So, you can implement your own heartbeat mechanism (i.e. periodically send value from node1 to node2, node2 registers time last value received)


#5

Thanks Eugene.
This is what I was thinking also.
I think that Node 1 would update a virtual pin periodically and node 2 would read it from time to time.


#6

:wink:


#7

Create a bridge between the two devices so you can use the Eventor.

It’s a bit of a hack but unfortunately the Eventor only supports a single device.


#8

this my implementations… May be it helps you.

Main “Device1” which sends on/off information by increasing the number

void loop()
{
  if (currentMillis - previousMillis2 >= interval2) { //every 2 seconds
    previousMillis2 = currentMillis;
    Heartbeat++;
    bridge1.virtualWrite(V6, Heartbeat);

on device2 side,
if “device2” receive a new number, this means “device1” is working.
if the number does not change, this means “device1” is not working.

int Heartbeat = 0;
int HeartbeatV = 0;
bool HeartbeatHealt = true;

BLYNK_WRITE(V6) {
  Heartbeat = param.asInt(); // pinData variable will store value that came via Bridge
  Serial.println(Heartbeat);

}

void loop()
{
  if (currentMillis - previousMillis2 >= interval2) { //every 2.5 seconds  (in case of delay, this time value should bigger than device1's time value )
    previousMillis2 = currentMillis;
     if (Heartbeat != HeartbeatV) {
      HeartbeatV = Heartbeat;
      HeartbeatHealt = true;
      Serial.println("Dont Stop");
    } else {
      HeartbeatHealt = false;
      Serial.println("Stop");
    }

    if (HeartbeatHealt == false) {
      Serial.println("Turn Off Relay");
    }

i hope it helps.


#9

This is not good as you need to leave the main loop clear. Use timers instead and call your functions every X seconds…

void setup() {
  timer.setInterval(2000L, myFunc);  // call myFunc every 2 seconds
 }

void myFunc(){
//your code here
}


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

#10

Doesn’t both do the same thing in essence? of course usage of blynk timer is easier than my way. but is my example wrong?

unsigned long previousMillis1 = 0;        
const long interval1 = 30000;           

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis1 >= interval1) {
    previousMillis1 = currentMillis;
//things to do 
  }

#11

If you check the docs you’ll see that they suggest to leave the main loop as clean as possible… so… it’s up to you if you want to follow the specs or do it your way.
Oh, my post was meant if you want to use Blynk, otherwise it’s OK if you do it your way.


#12

Aside from responding to a year old topic :stuck_out_tongue_winking_eye: Please watch the time date stamps before posting, any answers over a couple of months is likely no longer relevant.

Not for bog standard Arduino coding. But because Blynk is an IoT App, and thus needs constant communication access… hogging the void loop() with code tends to lead to server disconnections, and with ESP8266, possible WDT issues. The solution is to split up your code into separate and quickly timed jobs, leaving the void loop() and subsequent background tasks in the Blynk library and ESP8266 WiFi control, free to maintain connection.


#13