Code isn't working without connected Blynk

Trying to implement my first IoT-device — WiFi-enabled light switch using ESP8266-enabled hardware.
D3 — FLASH-button on NodeMCU board I’m using to make prototype. Representing the main power button.
D4 — blue LED of main ESP8266 board. ON while light is off, OFF while light is on.
BUILTIN_LED — red LED of NodeMCU. Representing the Solid State Relay used to cut on/off power.

Everything work being connected to the Blynk network — I can switch on/off relay pin from the Blynk app or using the button, everything is synchronized. Troubles coming when I want to emulate work without Internet or WiFi access point, just turning on and off from the button — looks like it stucks on the Blynk.run(); and doesn’t move along.

The code:

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

char auth[] = "my_auth";
const int butPin = D3;
const int ledPin = D4;
const int relPin = BUILTIN_LED;
int butState;
int oldButState;
int x = 0;

void setup()
{
  Blynk.begin(auth, "my_wifi", "my_pass");
  pinMode(butPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(relPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  digitalWrite(relPin, HIGH);
}

void loop()
{
  Blynk.run();
  butState = digitalRead(butPin);
  if (butState == HIGH && oldButState == LOW){
    if (x == 0){//ON
    digitalWrite(relPin, LOW);
    digitalWrite(ledPin, HIGH);
    Blynk.virtualWrite(V0, HIGH);
    x = 1;
  }
  else{//OFF
    digitalWrite(relPin, HIGH);
    digitalWrite(ledPin, LOW);
    Blynk.virtualWrite(V0, LOW);
    x = 0;
  }
}
oldButState = butState;
}

BLYNK_WRITE(V0)
{
  int val = param.asInt();
  digitalWrite(ledPin, val);
  digitalWrite(relPin, !val);
  x = !x;
}

BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

The question is — how I can implement offline work of that device? Some way that main button code on void loop() can be executed always, even Blynk being offline.

you need internet!

or local server…

or physical button…

Even for something really simple as this?

butState = digitalRead(butPin);
  if (butState == HIGH && oldButState == LOW){
    if (x == 0){//ON
    digitalWrite(relPin, LOW);
    digitalWrite(ledPin, HIGH);
    Blynk.virtualWrite(V0, HIGH);
    x = 1;
  }
  else{//OFF
    digitalWrite(relPin, HIGH);
    digitalWrite(ledPin, LOW);
    Blynk.virtualWrite(V0, LOW);
    x = 0;
  }
oldButState = butState;

What do you mean by “physical button”? I will have it connected to ESP8266.

1 Like

how does your phone app connect to the ESP8266?

Through Wi-Fi.
I don’t want working Blynk app without Wi-Fi, I want logic part, that one I’ve written on the previous message. Be able to turn on and off light even without Wi-Fi and Blynk, just by pressing physical button.

1 Like

@G2Dolphin take a look at http://docs.blynk.cc/#blynk-firmware-connection-management

Your buttons could be set to disconnect / reconnect from / to Blynk as well as operate the relays.

Not tried it but you should be able to move Blynk.run() to a regularly called function which only gets called when Blynk is connected.

Thank you for that link. I’ve already read this, though didn’t understood how it can help me implement what I want.
I’ve tested now — ESP gets stuck after Blynk.auth().
The code

void setup()
{
  Serial.begin(9600);
  Serial.write("test1");
  Blynk.begin(auth, "ssid", "pass");
  Serial.write("test2");
  pinMode(butPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(relPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  digitalWrite(relPin, HIGH);
}

outputing only “test1” in console, so, looks like, it can’t even reach void loop().
Is there a timeout or something? Blynk.connect(timeout); seems like can’t work.

1 Like

Hi. I just started playing around with Blynk and an ESP8266 last week, so I’m very new to all of this. The one thing I noticed is that the Blink.begin() and Blink.run() commands can take a long time if the device cannot establish a link to the Blynk server. My guess is that is what is happening to you.

A crude solution would be to hang a switch off your ESP and use this switch as to decide if you should run the Blynk stuff or not. What you would have to do is wrap all of your Blynk.xxxx() commands in an ‘if’ statement. That is, if the switch is in the “yes, let’s talk to the Blynk server” position, then do the Blynk.xxxx() commands. If the switch is in the “no, let’s run without network” position, then don’t do the Blynk.xxxx() commands.

Crude, but it should work.

Hope this helps. :slight_smile:

Hey, thanks for your solution! I will count this as one of solutions… but, honestly, not the best ones. :slight_smile: I really think there’s something that can handle this situation… something like “ah, the Blynk server isn’t responding, let’s move further and ignore all Blynk functions” being disconnected and “oh, I can’t find this AP for X seconds, let’s move further and… yes” where there are just no AP on the sight. Thankfully this project is just being better using Blynk, and isn’t only possible with it… that’s I hope to. I want to be able to switch on/off the room light a traditional way too. :smiley:

1 Like

An extract from the setup() of one of our sketches:

  Blynk.config(blynk_token);
  Blynk.connect();
  float timeout = (millis() / 1000);
  while (Blynk.connect() == false) {
    if(((millis()/1000) - timeout) > 10){   // issue msg if not connected to Blynk in more than 10 seconds
      firstrow =  "Check the";  // for OLED
      secondrow = " Router  ";  // for OLED
      if(DEBUG){
        Serial.println("Check the router");
      }
      break; 
    }
  }
2 Likes

Sure, happy to help, although I usually try to give “better” (less crude) solutions. :slight_smile: Being new to Blynk, I’m not familiar with all the Blynk commands that are available. I realize it takes a while and potentially many steps to establish a Wifi connection plus a connection to the Blynk server, etc… It would be nice if Blynk could do this in multiple steps in Blynk.run().

The reason I say this is that my app runs as a state machine, and as part of that state machine I blink an LED (as a heartbeat to tell me that the device has not run off the rails). Well, if I power cycle the router that the ESP is talking to, because the ESP loses the wireless connection, the next time Blynk.run() is called it takes a long time to establish a connection, etc (which makes sense because the router is booting, etc) and my heartbeat LED stops blinking. But if this could be done asynchronously (i.e., small chunks of the entire "connect to network; do DNS lookup on the Blynk server; login; etc) in the Blynk.run() routine, then it wouldn’t hang the rest of the state machine. Maybe it is possible and I’m just not aware of it (I’m very ignorant on Blynk at this point).

Ok, enough of my ramblings… :slight_smile:

hello!

i have the same problem.
i have built a home automation system, the lights can be turned on / off from the blynk app, but also from physical buttons in the rooms. when the arduino is connected to the network, everything works fine and reliable (i can control the lights from my phone and also with the physical buttons).

the problem is, when the arduino is not connected to the network (for example the network cable is unplugged), the system is not working at all, i can not even use the physical buttons.

system hangs in the setup(), at the Blynk.begin(auth); function: waits ~60 seconds, then restarts the arduino, and everything begins again. it never enters in the main loop() function.

i would like to bypass somehow this problem: if no internet connection, than execute loop(), without blynk.
also there is another problem, if i unplug the network cable during loop() runtime, after a while the program hangs, it not responds to physical buttons. i must plug the cable, or reset the arduino.

i use arduino adk + enc28j60 + uipethernet + blynk.

// LIBRARY
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

thank you for your support!

@wanek do you see break in my sketch extract? With connection management and break you can skip all the Blynk stuff when your network is down.

ok. thank you. i will try it right now.

You also need to have a loop check for when your network comes back up so you can start using Blynk again.

i used the code in the example you gave, but now it hangs during the Blynk.connect() function WITH NETWORK CABLE PLUGGED.

if instead Blynk.begin(auth) i’m using the code you gave, maybe there are other things to manually configure? i did not find a detailed documentation how to configure manually, but here: http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynk_connected it says:
“You can also set up shields (WiFi, Ethernet) manually, and then call:
Blynk.config(auth);” so, maybe i’m missing something before call Blynk.config(auth)…

however, i do not understand, how could the “while” cycle measure the timeout, if it never gets executed, because the program hangs at Blynk.connect()?

this is the relevant part from my sketch:

// LIBRARY
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxx";

void setup()
{
  Serial.begin(9600);

  Serial.println("entering Blynk.config(auth);");
  Blynk.config(auth);

  Serial.println("entering Blynk.connect();");
  Blynk.connect();

  Serial.println("set float");
  float timeout = (millis() / 1000);

  Serial.println("entering while");
  while (Blynk.connect() == false) {
    if (((millis() / 1000) - timeout) > 10) {
      break;
    }
  }

  beep(50, 0);  // system ready
}

My sketch extract is based on WiFi and for devices that are already connected to the internet with WiFiManager.

You need to adapt for ethernet access and as I don’t use ethernet you would have to post your working ethernet sketch if you want us to advise what changes you need to make.

ok, here is my working sketch (only the relevant code):

// LIBRARY
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";

void setup()
{
  Blynk.begin(auth);

/* if network cable not plugged, timeout is ~60 seconds, after that it reboots. i would like to bypass this (instead rebooting -> go to loop()), if there is no network connection.*/

  beep(50, 0);
}

void loop()
{
  Blynk.run();

  checkOpto();
  checkUps();
  checkMotion();
  checkButton();
  checkTimers();
}

@Costas , i’m not allowed to post more than 3 post in the same topic, so i try to edit this one.

so, i have tried the EXACT code you provided.
now it passes the Blynk.config(auth, “blynk-cloud.com”, 8442) function, but hangs in the Blynk.connect() , with the network cable plugged!
it does not enter in the “while” loop.

I have installed the relevant libraries and set the board to Mega ADK but without the actual hardware it is difficult to check.

What does this do for you:

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

float timeout = (millis() / 1000);
char auth[] = "YourAuthToken";

void setup()
{
  Serial.begin(9600);
  Blynk.config(auth, "blynk-cloud.com", 8442);
  //Blynk.begin(auth);
  // You can also specify server.
  // For more options, see BoardsAndShields/Arduino_Ethernet_Manual example
  //Blynk.begin(auth, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8888);

  Blynk.connect();
  while (Blynk.connect() == false) {
    if(((millis()/1000) - timeout) > 10){   // issue msg if not connected to Blynk in more than 10 seconds
      break; 
    }
  }
}

void loop()
{
  if(Blynk.connect() == true){
    Blynk.run();
  }
}

Looks like I’ve solved the problem using this code:

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

char auth[] = "auth";
const char* ssid = "ssid";
const char* password = "pass";

void setup()
{
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  Blynk.config(auth);
  if (WiFi.status() == WL_CONNECTED) {
    Blynk.connect(5);
  }
}

void loop()
{
  if (WiFi.status() == WL_CONNECTED) {
    if (Blynk.connected() == true) {
      Blynk.run();
    }
    else {
      Blynk.connect(5);
      Blynk.run();
    }
  }
}

It connects longer than usual if anything is working, but connecting and syncing. If I deny ESP8266 connecting to AP, my code is working after 10 seconds approx. If I’ll allow — it reconnects and works again.
My code working from the start, if I reboot ESP8266, no more pauses on Blynk connecting.

The code needs polishing, though.

2 Likes