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.
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.
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
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.
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.
Hey, thanks for your solution! I will count this as one of solutions… but, honestly, not the best ones. 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.
Sure, happy to help, although I usually try to give “better” (less crude) solutions. 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).
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.
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()?
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.
/* 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.*/
@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.
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.