0

I'm trying to use a float switch to receive alerts via Blynk when a water level drops, and when it rises again. The code I have been using has been working for when the float drops (button pressed) and I could get it to work when the water comes back up, but it gets stuck in a loop. I'm new to Arduino programming and I'm sure it's some simple error in the way I'm writing/modifying this code...

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

char auth[] = "710a4af9b1d82412fa477705bf642c634";
char ssid[] = "SSID";
char pass[] = "PWD";
int flag=0;
void notifyOnButtonPress()
{
  int isButtonPressed = digitalRead(D1); //assignment here for pin
  if (isButtonPressed==1 && flag==0) {


    Blynk.notify("Alert : Water Level has dropped");
    flag=1;
  }
  else if (isButtonPressed==0 && flag==0)  
      Blynk.notify("Alert : Water level has risen");
  {
    flag=0;
  }


}
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
}

I believe the problem is in function notifyOnButtonPress.

  • Is the float connected to a switch on pin D1 such that if the water level drops, lowering the float, the switch connected to pin D1 is turned on and it stays on until the water level rises sufficiently, lifting the float, so that the float turns off the switch connected to pin D1? So the flag variable indicates if a message has been issued that the switch is turned on, low water level, or turned off, high water level? – Richard Chambers Feb 1 '18 at 17:46
  • Correct. The pin is assigned to D1. The water level is normally high. When it drops, the float drops and sends the "on" signal to the board. It remains in this state for around 15 minutes or so, then the water ebbs back up and opens the loop again (switching off). I'm trying to use "Flag" to be able to determine the last state and reset after a cycle of ON/OFF essentially with this switch... or i guess OFF/ON/OFF and repeat. – gfuller40 Feb 1 '18 at 20:04
0

It seems to me that the saved float level state needs to be initialized to what it's actual current level is rather than just starting it off as 0. That way conditions such as a power outage during which the float level has changed could be detected.

I also changed the text of your notifications, which may or may not be allowed, to more accurately describe the state of the system.

I haven't actually tested this so I would like feedback as to whether it works or not.

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.
       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.
     Blynk.notify("Alert : Water level high");
     floatSavedState = floatLevelLow;
  }
}

Or I suppose even simpler would be:

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 != floatSavedState) {
       // send notification if this is first time or if the float level has
       // changed since the last notification.
       if (floatLevelLow) {
           Blynk.notify("Alert : Water Level low.");
       } else {
           Blynk.notify("Alert : Water level high");
       }
       floatSavedState = floatLevelLow;
  }
}
  • Thank you. This gives me a single notification of "Water Level low" when powered up and float is in the HIGH position. Manually adjusting the float after has no effect. This holds true for both examples provided. – gfuller40 Feb 2 '18 at 1:10
  • @gfuller40 look at your pinMode(D1,INPUT_PULLUP) call. I think that makes pin D1 to be an INPUT pin and is attaching a pullup resistor to it so that if the line attached to the pin is floating, the pin will be pulled up to a HIGH value. I am a bit hazy on it but I am wondering if you have a wiring and setup problem. My impression, which may be wrong, is that the pin will always be HIGH. Could you check the value of the digitalRead(D1) that it returns a value of 1 if the float is pushed down, water level low, and a value of 0 if the float is up, water level high? – Richard Chambers Feb 2 '18 at 1:42
  • Thank you, Richard. I'm only using D1 and GND. This works as it should when i only check to see if the float has dropped in my initial programming int isButtonPressed = digitalRead(D1); if (isButtonPressed==1 && flag==0) { Serial.println("Resevoir Water is Low!"); Blynk.notify("Alert : Resevoir water is Low!"); flag=1; } else if (isButtonPressed==0) { flag=0; } ) but when I add to check the state again (high) is where I've been having trouble. I guess that's the only reason i have to believe it is set up correctly? – gfuller40 Feb 2 '18 at 1:56
  • @gfuller40 please update your posted question with a wiring schematic showing how you have things wired up. As part of that identify the actual components being used in the wiring. I am not sure what you mean by "only using D1 and GND" and how that wiring is done. You want to make sure that if the water level is low then the pin will definitely see a HIGH or 1 and if the water level is high then the pin will definitely see a LOW or 0. See playground.arduino.cc/CommonTopics/PullUpDownResistor or electronics-tutorials.ws/logic/pull-up-resistor.html – Richard Chambers Feb 2 '18 at 2:05
  • OK. I have a float switch (2 wires) one is connected to the GND pin on on the digital side of the MCU and the other is connected to D1 on the digital side as well. I just ran a test using a very simple function (void notifyOnButtonPress() { Serial.println(digitalRead(D1)); }) and I'm only seeing a value of "1" on the serial monitor (both positions). That explains things.... do i need a 3 wire float or something? or just use two floats? – gfuller40 Feb 2 '18 at 2:26
0

Change

else if (isButtonPressed==0 && flag==0)   
    Blynk.notify("Alert : Water level has risen");

to

else if ((isButtonPressed==0) && (flag==1))
{
    Blynk.notify("Alert : Water level has risen");
}

Otherwise, when flag == 1, it will never revert back to 0. Also it's best to add brackets, and use parenthesis.

Also, change the variable name flag to something more meaningfull.

  • His brackets are also wrong. flag always gets set to 0. – gre_gor Feb 1 '18 at 14:47
  • Are you saying I should also get rid of this: { flag=0; } – gfuller40 Feb 1 '18 at 15:03
  • No you have to change it to flag==1 Also use brackets (I will add it in my answer) – Michel Keijzers Feb 1 '18 at 15:42
  • Ok so this gets me stuck in a loop of water level LO and manually moving the float to HIGH has no effect. – gfuller40 Feb 2 '18 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.