This is an extension from a previous thread that was getting lengthy and off the original topic. I have the following code which monitors the state of a float switch (high or low). I upload the sketch, turn on the serial monitor and manually move the float switch up and down. The serial monitor output represents what I'm doing with the switch. The issue is that I get a constant loop of blynk notifications, every 15 seconds or so. Furthermore, if i move the switch to it's low position, the first blynk notification i get is correct reading "low", the following notifications are "high" thereafter. When the switch is moved to the high position, blynk just loops stating it's "high". I know it's weird and took quite a while to even establish what is going on here. but the biggest issue is the loop of notifications every 15 seconds.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char auth[] = "710a4af9b1d82412fa477705bf642c634";
char ssid[] = "ssid";
char pass[] = "pwd";
int flag=0;
int floatSavedState = -1; // initial float low state indicates not yet known
void notifyOnButtonPress()
{
// pin D1 will be 1 if float has dropped due to low water level
// pin D1 will be 0 if water level is high enough
int floatLevelLow = digitalRead(D1); // read the float low indicator pin
if (floatLevelLow && floatLevelLow != floatSavedState) {
// send notification if this is first time or if the float level has
// changed since the last notification.
Serial.println("Resevoir Water is Low!");
Blynk.notify("Alert : Water Level low.");
floatSavedState = floatLevelLow;
} else if (floatLevelLow != floatSavedState)
// send notification if this is first time or if the float level has
// changed since the last notification.
Serial.println("Resevoir Water is High!");
Blynk.notify("Alert : Water level high");
floatSavedState = floatLevelLow;
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// Setup notification button on pin D1
pinMode(D1,INPUT_PULLUP);
timer.setInterval(16000L,notifyOnButtonPress);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
1 Answer 1
Your code:
timer.setInterval(16000L,notifyOnButtonPress);
Is calling this function every 16s.
This code:
if (floatLevelLow && floatLevelLow != floatSavedState) {
needs some parentheses to force your desired order-of-operations. As it stands now, you are asking the result of floatLevelLow && floatLevelLow
and comparing if that is not equal to floatSavedState
. That's likely not what you want.
Did you mean:
if (floatLevelLow && (floatLevelLow != floatSavedState)) {
EDIT:
Also, your if/else is missing brackets around the 2nd clause and should be:
} else if (floatLevelLow != floatSavedState) {
// send notification if this is first time or if the float level has
// changed since the last notification.
Serial.println("Resevoir Water is High!");
Blynk.notify("Alert : Water level high");
floatSavedState = floatLevelLow;
}
-
Thank you for your help with this! I added the parentheses and it is still acting in the same manner. If I comment out the timer function and the call for it, the sketch does not work at all once compiled.gfuller40– gfuller402018年02月02日 21:17:00 +00:00Commented Feb 2, 2018 at 21:17
-
Your code doesn't have brackets on the 2nd part of the
if
statement, so your code is callingSerial.println("Resevoir Water is High!"); if the
if` is true, but it's always calling the remaining lines. I will update my answer to address this also.jose can u c– jose can u c2018年02月02日 21:19:54 +00:00Commented Feb 2, 2018 at 21:19 -
You nailed it!!!! thank you so much, I've spent far too many hours on this!!!!gfuller40– gfuller402018年02月02日 21:31:32 +00:00Commented Feb 2, 2018 at 21:31