Hello !
@khoih @PeteKnight @guidotillema
i was going through the forum and found this interesting thread !!
After following the exact steps as you guys have mentioned above i still find that the button stops responding when connecting to the server !!
Scene 1
Device turns on
No Router available (button responds no lag)
Scene 2
Device turns on
router available but no internet
Now after the device is connected to wifi router it fails to connect to the server as there is no internet connection available the device keep trying to connect at set intervals… during this time the code as blocked !!
This is the code i am using
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define DEBOUNCE_TIME 25
#define LONG_BUTTON_PRESS_TIME_MS 10
#define DEBUG_ISR 0
#define VPIN1 V1
volatile unsigned long lastDebounceTime = 0;
volatile bool buttonPressed = false;
bool alreadyTriggered = false;
void lightOn1();
void lightOff1();
boolean LampState1 = 0;
boolean SwitchReset1 = true;
const int RelayPin1 = D2;
const int buttonPin = D1;
// Initialize Blynk
char authD1[] = "";
char ssid[] = "";
char pass[] = "";
IPAddress serverIP (00,000,000,000);
uint16_t serverPort = 8080;
BlynkTimer Timer;
unsigned int myServerTimeout = 3500L; // 3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout = 3200L; // 3.2s WiFi connection timeout (WCT)
unsigned int blynkInterval = 25000L; // 25.0s check server frequency (CSF)
unsigned int buttonInterval = 100L; // 0.1s check button state
void ICACHE_RAM_ATTR Falling();
void ICACHE_RAM_ATTR Rising();
void setup() {
Serial.begin(115200);
pinMode(RelayPin1, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);
Serial.println("Initialize Blynk");
if(WiFi.status() == 6){
Serial.println("WiFi not connected yet.");
}
Timer.setInterval(blynkInterval, checkBlynk);
unsigned long startWiFi = millis();
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED){
delay(500);
if(millis() > startWiFi + myWiFiTimeout){
Serial.println("Check the WiFi router. ");
break;
}
}
Blynk.config(authD1, serverIP, 8080);
Serial.println("This is HomeControl-TEST");
checkBlynk();
Timer.setInterval(buttonInterval, checkButton);
}
void checkBlynk() {
if (WiFi.status() == WL_CONNECTED)
{
unsigned long startConnecting = millis();
while(!Blynk.connected()){
Blynk.connect();
if(millis() > startConnecting + myServerTimeout){
Serial.print("Unable to connect to server. ");
break;
}
}
}
if (WiFi.status() != 3) {
Serial.print("No WiFi. ");
}
Serial.printf("Checking again in %is.\n", blynkInterval / 1000);
}
void Rising()
{
unsigned long currentTime = millis();
unsigned long TimeDiff;
#if (DEBUG_ISR > 0)
Serial.println("rising");
#endif
TimeDiff = currentTime - lastDebounceTime;
if ( digitalRead(buttonPin) && (TimeDiff > DEBOUNCE_TIME) )
{
#if (DEBUG_ISR > 0)
Serial.println("Button Released");
#endif
buttonPressed = false;
if (TimeDiff > LONG_BUTTON_PRESS_TIME_MS)
{
#if (DEBUG_ISR > 0)
Serial.printf("TimeDiff = %d ms => long Button Press (with release)\n", TimeDiff );
#endif
} else {
#if (DEBUG_ISR > 0)
Serial.println("Short Button Press");
#endif
}
lastDebounceTime = currentTime;
attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);
}
}
void Falling()
{
unsigned long currentTime = millis();
#if (DEBUG_ISR > 0)
Serial.println("falling");
#endif
if ( !digitalRead(buttonPin) && (currentTime > lastDebounceTime + DEBOUNCE_TIME))
{
lastDebounceTime = currentTime;
#if (DEBUG_ISR > 0)
Serial.println("Button Pressed");
#endif
buttonPressed = true;
attachInterrupt(digitalPinToInterrupt(buttonPin), Rising, RISING);
}
}
void loop() {
if (Blynk.connected()) {
Blynk.run();
}
Timer.run();
}
void checkButton()
{
if (!alreadyTriggered && buttonPressed && (millis() - lastDebounceTime > LONG_BUTTON_PRESS_TIME_MS))
{
alreadyTriggered = true;
#if (DEBUG_ISR > 0)
Serial.println("Long Button Press");
#endif
// Doing something here without waiting for button released (long press)
ButtonCheck1();
}
else if (!buttonPressed)
{
// Reset flag when button released to avoid triggered repeatedly
alreadyTriggered = false;
ButtonCheck1();
}
}
void ButtonCheck1() {
boolean SwitchState1 = (digitalRead(buttonPin));
if (!SwitchState1 && SwitchReset1 == true) {
if (LampState1) {
lightOff1();
} else {
lightOn1();
}
SwitchReset1 = false;
delay(50);
}
else if (SwitchState1) {
SwitchReset1 = true;
}
}
void ToggleRelay1() {
LampState1 = !LampState1;
if (LampState1) {
lightOn1();
}
else lightOff1();
}
void lightOn1() {
digitalWrite(RelayPin1, LOW);
LampState1 = 1;
Blynk.virtualWrite(VPIN1, HIGH);
}
void lightOff1() {
digitalWrite(RelayPin1, HIGH);
LampState1 = 0;
Blynk.virtualWrite(VPIN1, LOW);
}
BLYNK_WRITE(VPIN1) {
int SwitchStatus1 = param.asInt();
if (SwitchStatus1 == 2){
ToggleRelay1();
}
else if (SwitchStatus1){
lightOn1();
}
else lightOff1();
}
The whole purpose was to eliminate the blockage of the code during reconnection !! But the problem still exists !! MAY BE I AM GOING WRONG SOMEWHERE !!
Is there any thing missing in the code or is this the way it is supposed to work ??
EDITING TO ADD SERIAL MONITOR OUTPUT
WiFi not connected yet.
Check the WiFi router.
[3558]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.0 on NodeMCU
This is HomeControl-TEST
No WiFi. Checking again in 25s. *Button works without any lag [No router found]*
No WiFi. Checking again in 25s. *Button works without any lag [No router found]*
[50057] Connecting to 40.200.200.000 *Button stops working [router available, No internet]*
[55058] Connecting to 40.200.20.000 *Button stops working [router available, No internet]*
Unable to connect to server. Checking again in 25s. *Button works without any lag*
[75057] Connecting to 40.200.200.00
[75896] Ready (ping: 208ms).
Checking again in 25s. *Starts to work normally !!!!!!!*