I have a couple of issues with this project. Line 89 Blynk.virtualWrite(0, counter);
V0 is a numerical input box. I cannot get this box to update. The notification I recieve on line 90 gives me the correct reading for ‘counter’ but it is not put into V0 correctly.
I also get disconnects from blynk after the 1 second timer has run. It seems random, not all the time but happens when pulsing = 0 (line 166) Blynk reconnects instantly
Any help would be appeciated
#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h> //blynk time server
char auth [] = "xxx";
WidgetRTC rtc;
// the timer object
SimpleTimer timerminute;
SimpleTimer timerhour;
volatile byte pulsing;
int pulsecount;
int noflow;
int blynkflowingvalue;
int blynknoflowvalue;
int state; //on/off state
const int pinReedSwitch = 2;
int counter;
int countminutes;
int counthours;
int lastValue;
int hournotify;
int minutenotify;
int secondnotify;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
rtc.begin(); //begin getting time-date
timerminute.setInterval(60000, CheckWater); // Miliseconds to seconds, every minute run function which counts time
timerhour.setInterval(3600000, UpdateHourly); // Miliseconds to seconds, every hour run function which counts time
pulsing = 0;
counter = 0;
countminutes = 0;
counthours = 0;
pulsecount = 0;
noflow = 0;
pinMode(pinReedSwitch, INPUT_PULLUP);
Blynk.syncVirtual(2); //This gets the virtual pin value from server. (Needed for example after power loss)
Blynk.syncVirtual(3);
Blynk.syncVirtual(4);
Blynk.syncVirtual(5);
Blynk.syncVirtual(7);
Blynk.syncVirtual(10);
}
void loop()
{
Blynk.run();
timerminute.run();
timerhour.run();
int value = !digitalRead(pinReedSwitch);
if (value != lastValue)
{
lastValue = value;
if (value)
{
pulsing = 1; //water is flowing set pulsing to 1
counter++;
countminutes++;
counthours++;
Serial.println("value: " + String(value));
Serial.println("lastvalue: " + String(lastValue));
Serial.println("Counter: " + String(counter));
}
}
if (hour() == hournotify && minute() == minutenotify && second() == secondnotify )
{
Blynk.virtualWrite(0, counter); //update how many counts/litres in last 24 hours and set it back to zero
Blynk.notify("Water mains Feed: " +String(counter) + " litres used in last 24 hours");
counter = 0;
}
}
BLYNK_WRITE(2)
{
blynkflowingvalue = param.asInt(); // Get value as integer
}
BLYNK_WRITE(3)
{
blynknoflowvalue = param.asInt(); // Get value as integer
}
BLYNK_WRITE(6) //Reset Button
{
noflow = 0;
pulsecount = 0;
pulsing = 0;
Blynk.virtualWrite(4, LOW); //Turn off no flow error button
Blynk.virtualWrite(5, LOW); //Turn off to much flow error
}
BLYNK_WRITE(7) //on/off button
{
if ( param.asInt() == 1 ) //Button high/pressed
{
state = 1;
}
else
{
state = 0;
}
}
BLYNK_WRITE(V10) //Time for notify
{
TimeInputParam t(param);
// Process start time
hournotify = t.getStartHour();
minutenotify = t.getStartMinute();
secondnotify = t.getStartSecond();
}
void CheckWater()
{
if (state == 1)
{
if (pulsing == 1)
{
pulsecount++;
Blynk.virtualWrite(1, HIGH); //Turn on flowing LED
if (pulsecount == blynkflowingvalue)
{
Blynk.virtualWrite(5, HIGH); //Turn on to much flow error button
Blynk.notify("Warning direct mains in has been flowing for " + String(blynkflowingvalue) + " minutes. Remember to Reset");
}
pulsing = 0; //We only want to see if the flow sensor pulsed at all in the one minute interval. It did, so set it back to 0
noflow = 0; //obviously there has been flow so reset to zero
Blynk.virtualWrite(8, countminutes); //update how many counts/litres in a minute and set it back to zero
countminutes = 0;
}
else
{
noflow++;
Blynk.virtualWrite(1, LOW); //Turn off flowing LED
if (noflow == blynknoflowvalue)
{
Serial.println("Warning, no flow");
Blynk.virtualWrite(4, HIGH); //Turn on no flow error button
Blynk.notify("Warning direct mains in has not been flowing for " +String(blynknoflowvalue) + " minutes. Remember to Reset");
}
pulsecount = 0; //in the one minute interval water stopped pulsing through sensor so put the pulse count to 0
Blynk.virtualWrite(8, countminutes); //update how many counts/litres in a minute and set it back to zero
countminutes = 0;
}
}
}
void UpdateHourly()
{
Blynk.virtualWrite(9, counthours); //update how many counts/litres in an hour and set it back to zero
counthours = 0;
}
A single BlynkTimer object can support up to 16 timer instances.
See the “Creating Interval Timers” section of this post:
Also, it’s usual (although not strictly necessary) to use the “V” prefix when referencing virtual pins in BLYNK_WRITE, Blynk.virtualWrite and Blynk.syncVirtual commands so that they’d look like this:
Blynk.virtualWrite(V0, counter);
This would make your code more readable from most Blynkers point of view.
Also, this in your void setup won’t work:
Instead, these Blynk.syncVirtual commands should be in a BLYNK_CONNECTED() callback function
See the “Synchronising the output state with the app at startup” section of this post for more info:
I’ve made some changes to the timers. My sync’s do work not being inside the BLYNK_CONNECTED() However I can add it inside that.
Updated code below. Can you see anywhere else I am spamming the Blynk server causing errors?
I’ve removed a piece out of the loop, but not sure how I would remove the piece which is checking for the reed sensor
#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h> //blynk time server
char auth [] = "";
WidgetRTC rtc;
// the timer object
SimpleTimer timer;
volatile byte pulsing;
int pulsecount;
int noflow;
int blynkflowingvalue;
int blynknoflowvalue;
int state; //on/off state
const int pinReedSwitch = 2;
int counter;
int countminutes;
int counthours;
int lastValue;
int hournotify;
int minutenotify;
int secondnotify;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
rtc.begin(); //begin getting time-date
timer.setInterval(60000, CheckWater); // Miliseconds to seconds, every minute run function which counts time
timer.setInterval(3600000, UpdateHourly); // Miliseconds to seconds, every hour run function which counts time
timer.setInterval(1000, DailyNotify); // Miliseconds to seconds, every hour run function which counts time
pulsing = 0;
counter = 0;
countminutes = 0;
counthours = 0;
pulsecount = 0;
noflow = 0;
pinMode(pinReedSwitch, INPUT_PULLUP);
Blynk.syncVirtual(V2); //This gets the virtual pin value from server. (Needed for example after power loss)
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
Blynk.syncVirtual(V5);
Blynk.syncVirtual(V7);
Blynk.syncVirtual(V10);
}
void loop()
{
Blynk.run();
timer.run();
int value = !digitalRead(pinReedSwitch);
if (value != lastValue)
{
lastValue = value;
if (value)
{
pulsing = 1; //water is flowing set pulsing to 1
counter++;
countminutes++;
counthours++;
Serial.println("value: " + String(value));
Serial.println("lastvalue: " + String(lastValue));
Serial.println("Counter: " + String(counter));
}
}
}
BLYNK_WRITE(V2)
{
blynkflowingvalue = param.asInt(); // Get value as integer
}
BLYNK_WRITE(V3)
{
blynknoflowvalue = param.asInt(); // Get value as integer
}
BLYNK_WRITE(V6) //Reset Button
{
noflow = 0;
pulsecount = 0;
pulsing = 0;
Blynk.virtualWrite(V4, LOW); //Turn off no flow error button
Blynk.virtualWrite(V5, LOW); //Turn off to much flow error
}
BLYNK_WRITE(V7) //on/off button
{
if ( param.asInt() == 1 ) //Button high/pressed
{
state = 1;
}
else
{
state = 0;
}
}
BLYNK_WRITE(V10) //Time for notify
{
TimeInputParam t(param);
// Process start time
hournotify = t.getStartHour();
minutenotify = t.getStartMinute();
secondnotify = t.getStartSecond();
}
void CheckWater()
{
if (state == 1)
{
if (pulsing == 1)
{
pulsecount++;
Blynk.virtualWrite(V1, HIGH); //Turn on flowing LED
if (pulsecount == blynkflowingvalue)
{
Blynk.virtualWrite(V5, HIGH); //Turn on to much flow error button
Blynk.notify("Warning direct mains in has been flowing for " + String(blynkflowingvalue) + " minutes. Remember to Reset");
}
pulsing = 0; //We only want to see if the flow sensor pulsed at all in the one minute interval. It did, so set it back to 0
noflow = 0; //obviously there has been flow so reset to zero
Blynk.virtualWrite(V8, countminutes); //update how many counts/litres in a minute and set it back to zero
countminutes = 0;
}
else
{
noflow++;
Blynk.virtualWrite(V1, LOW); //Turn off flowing LED
if (noflow == blynknoflowvalue)
{
Serial.println("Warning, no flow");
Blynk.virtualWrite(V4, HIGH); //Turn on no flow error button
Blynk.notify("Warning direct mains in has not been flowing for " +String(blynknoflowvalue) + " minutes. Remember to Reset");
}
pulsecount = 0; //in the one minute interval water stopped pulsing through sensor so put the pulse count to 0
Blynk.virtualWrite(V8, countminutes); //update how many counts/litres in a minute and set it back to zero
countminutes = 0;
}
}
}
void UpdateHourly()
{
Blynk.virtualWrite(V9, counthours); //update how many counts/litres in an hour and set it back to zero
counthours = 0;
}
void DailyNotify()
{
if (hour() == hournotify && minute() == minutenotify && second() == secondnotify )
{
Blynk.virtualWrite(V0, counter); //update how many counts/litres in last 24 hours and set it back to zero
Blynk.notify("Water mains Feed: " +String(counter) + " litres used in last 24 hours");
counter = 0;
}
}