That is expected behavior because original Blynk.run()
is a blocking call.
You can try this sketch, based on yours, using non-blocking Blynk_WM
to see how to solve the issue. This sketch has been tested and working OK, with or w/o Blynk / Internet.
#ifndef ESP8266
#error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting.
#endif
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
// Not use #define USE_SPIFFS => using EEPROM for configuration data in WiFiManager
// #define USE_SPIFFS false => using EEPROM for configuration data in WiFiManager
// #define USE_SPIFFS true => using SPIFFS for configuration data in WiFiManager
// Be sure to define USE_SPIFFS before #include <BlynkSimpleEsp8266_WM.h>
#define USE_SPIFFS true
// Force some params in Blynk, only valid for library version 1.0.1 and later
#define TIMEOUT_RECONNECT_WIFI 10000L
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5
// Those above #define's must be placed before #include <BlynkSimpleEsp8266_WM.h>
#include <BlynkSimpleEsp8266_WM.h> //https://github.com/khoih-prog/Blynk_WM
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <Ticker.h>
#define HTTP_PORT 80
WiFiClient client;
WiFiServer server(HTTP_PORT);
String command = ""; // Command received from Android device
// Set Relay Pins
#define RELAY1_PIN D0
Ticker relay_ticker;
#define PROGRAM_DEBUG true
void set_relay(byte status)
{
digitalWrite(RELAY1_PIN, status);
}
#define RELAY_CLICK_TIME_MS 400
void click_relay()
{
set_relay(HIGH);
relay_ticker.once_ms(RELAY_CLICK_TIME_MS, set_relay, (byte) LOW);
}
/* check command received from Android Device */
String checkClient (void)
{
while (!client.available())
delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}
/* send command echo back to android device */
void sendBackEcho(String echo)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println(echo);
client.println("</html>");
client.stop();
delay(1);
}
// Blynk PushButton
BLYNK_WRITE(V1)
{
if (param.asInt())
{
#if (PROGRAM_DEBUG)
Serial.println("Gate Open/Close by Blynk Button");
#endif
click_relay();
}
}
void WebServerHandle(void)
{
client = server.available();
if (!client)
return;
command = checkClient();
Serial.println(command);
if (command == "r1on" || command == "on" || command == "open" )
{
#if (PROGRAM_DEBUG)
Serial.println("Gate Open");
#endif
click_relay();
sendBackEcho(command);// send command echo back to android device
}
else if (command == "r1off" || command == "off" || command == "close")
{
#if (PROGRAM_DEBUG)
Serial.println("Gate Close");
#endif
click_relay();
sendBackEcho(command);// send command echo back to android device
}
command = "";
}
void setup()
{
pinMode(RELAY1_PIN, OUTPUT);
digitalWrite(RELAY1_PIN, LOW);
Serial.begin(115200);
Serial.println("\nStarting GateController-Server");
Blynk.begin("GateController-Server");
server.begin();
}
void loop()
{
Blynk.run();
WebServerHandle();
}
You certainly have to install Blynk_WM
library from Arduino IDE Library Manager.
Some improvements have been made such as :
- Use Ticker to eliminate using unwanted
delay()
inloop()
- Remove all unnecessary functions, etc.
- Remove unnecessary
OnDemand HotSpot
as newBlynk.run
will reconnect / start config portal if necessary. - Making it easier to read, easier to reconfigure by not using hardcoding if possible
You can add more and more functions later as needed.