Dear Blynk Community,
I am aware of the fact that this issue had been solved before on another topic. However for some reason that solution doesn’t work with my project. What I’m trying to do is to set a moisture sensor for flowerpots and when it’s moisture percentage is too low notify the user. The problem that I’m dealing with is since when it’s true, it’s true a gazillion times so it just sends countless notifications. Here is my code:
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
unsigned int notified = 0;
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
int soil=0;
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
void setup() {
Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass, "localipadress", 8442);
}
BLYNK_READ (V15) {
int sensorValue = analogRead(A0);
sensorValue = constrain(sensorValue, 485, 1023);
soil = map(sensorValue, 485, 1023, 100, 0);
if (soil < 35 && notified == 0) {
notified == 1;
Blynk.virtualWrite(V15, "Dry");
Blynk.notify("Your plants need to be watered.");
}
else if (soil<55 && soil > 35) {
notified == 0;
Blynk.virtualWrite(V15, "OK");
}
else if (soil>55) {
notified == 0;
Blynk.virtualWrite(V15, "Over wet");
}
else {
notified == 0;
}
Blynk.virtualWrite(V18, soil);
Serial.println(notified); // to see if notified has changed
}
void loop() {
Blynk.run();
}
The problem is it does show when it’s OK, dry and over wet but somehow notified keeps to be equal to 0 and because of that it keeps sending me notifications.
I can’t see a problem in my code so any help would be appreciated.
Thank you.
Btw: iPhone / local blynk server / arduino esp8266 shield.
wanek
January 28, 2018, 6:44pm
2
isiktantanis:
if (soil < 35 && notified == 0) {
notified == 1;
Blynk.virtualWrite(V15, “Dry”);
Blynk.notify(“Your plants need to be watered.”);
}
else if (soil<55 && soil > 35) {
notified == 0;
Blynk.virtualWrite(V15, “OK”);
}
else if (soil>55) {
notified == 0;
Blynk.virtualWrite(V15, “Over wet”);
}
else {
notified == 0;
}
you do not have the notified == 0
condition in the else if
statements, so the program will run them every time when the soil conditions are met. you should put that condition there too, like:
else if (soil < 55 && soil > 35 && notified == 0)
else if (soil > 55 && notified == 0)
you should also change the monitoring logic, so, when the state of the soil humidity changed, it should set notified to 1 again.
@wanek so I edited to code just like you mentioned and that didn’t do it? Any other ideas?
Costas
January 28, 2018, 8:23pm
4
wanek:
you should also change the monitoring logic, so, when the state of the soil humidity changed, it should set notified to 1 again.
@isiktantanis did you do this too?
Paste your formatted code.
@Costas I didn’t understand what do you mean by that but here is my code:
int sensorValue = analogRead(A0);
sensorValue = constrain(sensorValue, 485, 1023);
soil = map(sensorValue, 485, 1023, 100, 0);
if (soil < 35 && notified == 0) {
notified == 1;
Blynk.virtualWrite(V15, "Dry");
Blynk.notify("Your plants need to be watered.");
}
else if (soil<55 && soil > 35 && notified == 0) {
notified == 0;
Blynk.virtualWrite(V15, "OK");
}
else if (soil>55 && notified == 0) {
notified == 0;
Blynk.virtualWrite(V15, "Too Wet");
}
else {
notified == 0;
}
Costas
January 28, 2018, 9:07pm
6
The second row of each else if should be ==1 not ==0.
You are missing code for when sensor equals 35 and 55 i.e. you only have more than and less than.
So perhaps change first if to soil <= 35 and last else if to soil >=55.
I can’t see as you need the final if.
What you also need is the Blynk Timer to change notified from 1 to 0 as some set interval (1 min, 5 min etc).
so, like this right? @Costas
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
unsigned int notified = 0;
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
int soil=0;
BlynkTimer timer;
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
void setup() {
Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass, "ip", 8442);
timer.setInterval(1000L, resset);
}
BLYNK_READ (V15) {
int sensorValue = analogRead(A0);
sensorValue = constrain(sensorValue, 485, 1023);
soil = map(sensorValue, 485, 1023, 100, 0);
if (soil <= 35 && notified == 0) {
notified == 1;
Blynk.virtualWrite(V15, "Dry");
Blynk.notify("Your plants need to be watered.");
}
else if (soil<55 && soil > 35 && notified == 0) {
notified == 1;
Blynk.virtualWrite(V15, "OK");
}
else if (soil>= 55 && notified == 0) {
notified == 1;
Blynk.virtualWrite(V15, "Too wet");
}
Blynk.virtualWrite(V18, soil);
Serial.println(notified);
}
void resset () {
notified == 0;
}
void loop() {
Blynk.run();
timer.run();
}
2 Likes
Costas
January 28, 2018, 9:25pm
8
Looks good except this line. Needs to be 60000L (1min) or higher not 1000L (1s).
Yeah thank you. So helpful. I will try it tomorrow. @Costas
1 Like
wanek
January 28, 2018, 9:27pm
10
do not call resset too often, because you will get a notification every time it called.
i assume the state of the soil doesn’t changes every second… i would put once every 24 hours.