hey guys,
Hoping someone will have some free time to take a look at this code I have below. It compiles correctly but doesn’t function as planned. Its a mixture of a flow sensor code I found on an instructable, and some modifications I’ve made. I am still learning here, so I welcome any criticisms anyone might see that would teach me something or would be a better use of code.
Essentially its a simple water flow sensor, with a small caveat to open up a valve when a button is pressed. It seems like the interrupt code inst getting called, and i’m hoping to determine if its a code thing or a physical hardware thing.
I’m using a Wemos D1 Mini over WiFi, and a DigiTen flow sensor, and a PWM circuit for the valve opening. the valve opening works and the timers for that all operate effectively, but i’m not seeing any interrupt code being run.
Any help would be greatly appreciated.
Main Tab:
#include <Bounce2.h>
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Ticker.h> //for LED status
Ticker ticker;
char auth[] = "***";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";
char pass[] = "***";
char hostNamed[] = "WATERFLOW";
const int FW_VERSIONs = 1003;
int FW_VER=FW_VERSIONs;
const char* fwUrlBase = "***";
SimpleTimer timer;
WidgetRTC rtc;
WidgetBridge WaterBridge(V122);
byte sensorInterrupt = 14;
byte sensorPin = 14;
byte airPin = D6;
// The hall-effect flow sensor outputs approximately 4.5 pulses per second per litre/minute of flow.
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
#define averageperiod 5 // currently set to average the flow every 5 seconds
int countseconds = 0; // count up to averageperiod
int averageflow = 0; // used for averaging flow over averageperiod
bool notificationsent = false; // to ensure just one message for each flow start
bool airState = false; // air is OFF on start up
bool masterState = false; //
bool flowoffprintonce = false; // to ensure just one serial and terminal print for each flow stop
int rowIndex = 0;
String currentDate ;
String daybefore;
int rtctimer = 1; //check if RTC is OK and then disable / delete this #1 timer
int menu = 0;
int s;
int AirTimer;
int AirTime = 60000;
int AlertsVal = 0;
int disconnectCount = 0;
int debug = 0;
int virtualpin118 = 0; //UPDATE PIN
#define LED D4
#define SWITCHPIN D3
void setup()
{
Serial.begin(115200);
Serial.println();
pinMode(sensorPin, INPUT_PULLUP);
digitalWrite(sensorPin, HIGH);
pinMode(airPin, OUTPUT);
pinMode(SWITCHPIN, INPUT_PULLUP);
digitalWrite(airPin, LOW); // ACTIVE HIGH, air relay set to OFF on restart
Serial.println("FW: " + FW_VERSIONs);
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
flowRate = 0.0;
pulseCount = 0;
// Configured to trigger on a FALLING state change (transition from HIGH state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);//FALLING
// Configured to trigger on a CHANGE state change LOW to HIGH or HIGH to LOW
//attachInterrupt(airInterrupt, airToggle, CHANGE);
Blynk.begin(auth, ssid, pass);
Serial.print("Auth: "); Serial.println(auth);
Serial.print(hostNamed); Serial.println(" Booted");
WiFi.hostname(hostNamed);
ArduinoOTA.setHostname(hostNamed);
ArduinoOTA.setPassword((const char *)"Atlas123");
ArduinoOTA.begin();
rtc.begin();
Serial.println("Connected to Blynk");
Serial.println(WiFi.localIP());
timer.setInterval(1000L, showFlow);
timer.setInterval(2002L, Sent_serial);//send serial updates to blynk for debug
timer.setInterval(10000L, readSwitch); // check if air needs to be switched ON or OFF every 10s
rtctimer = timer.setInterval(2000L, checkRTC); // check every 2s if RTC is correct
checkForUpdates();
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V5);
Blynk.syncVirtual(V4);
WaterBridge.setAuthToken("8577a1df4f85451b9ae99869976764a6");
analogWrite(LED, 1023);
}
void showFlow() // average the flow over averageperiod
{
detachInterrupt(sensorInterrupt); // Disable the interrupt while calculating flow rate and sending the value to the host
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
// Note the time this processing pass was executed. Note that because we've
// disabled interrupts the millis() function won't actually be incrementing right
// at this point, but it will still return the value it was set to just before
// interrupts went away.
oldTime = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
// Add the ml passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
if (debug == 1) {// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL");
Serial.print("\t"); // Print tab space
Serial.print(totalMilliLitres / 1000);
Serial.println("L");
}
countseconds++;
if (countseconds > 0) { // used to skip the first rogue data flow reading
averageflow = averageflow + flowRate; // used to calculate the average flow over averageperiod cycles
}
if (countseconds == averageperiod) {
if (debug == 1) {
Serial.print("Average water flow in litres / M is ");
Serial.println(averageflow / averageperiod);
}
Blynk.virtualWrite(V2, int(averageflow) / averageperiod);
Blynk.virtualWrite(V9, int((averageflow) / averageperiod) * 255);
Blynk.virtualWrite(V1, totalMilliLitres / 3785.41);
countseconds = 0; // reset the counter
chkFlow();
averageflow = 0; // reset the average but only after chkFlow() function
}
attachInterrupt(sensorInterrupt, pulseCounter, FALLING); // Enable the interrupt again now that we've finished sending output
}
void chkFlow() {
if ((averageflow > 3) && (notificationsent == false)) { // guess of a decent water pressure
Serial.println("Water IS flowing.");
WaterBridge.virtualWrite(V18, 1);
if (AlertsVal == 1) {
Blynk.notify("Sensor: Water IS flowing.");
}
notificationsent = true; // stop getting messages until water stops flowing and starts again
flowoffprintonce = false; // when water stops flowing again we can restart serial and terminal print (once)
}
if ((averageflow <= 3) && (flowoffprintonce == false)) {
Serial.println("Water is NOT flowing.");
WaterBridge.virtualWrite(V18, 0);
notificationsent = false; // when water starts flowing again we can send another notification
flowoffprintonce = true; // stop serial and terminal prints after first pass of water stopping
s = 0;
}
}
void pulseCounter()
{
pulseCount++; // Increment the pulse counter
// Serial.println(pulseCount);
}
void airControl() // toggle air OFF and ON
{
if (airState == 1) {
Blynk.virtualWrite(V4, 1);
Blynk.virtualWrite(V8, "ON");
// Blynk.setProperty(V8, "color","#48E06B");
Serial.println("Air is ON");
}
else {
Blynk.virtualWrite(V4, 0);
Blynk.virtualWrite(V8, "OFF");
// Blynk.setProperty(V8, "color","#04C0F8");
Serial.println("Air is OFF");
}
}
void checkRTC() {
Serial.println("RTC check?");
if (year() != 1970) {
timer.disable(rtctimer); // disable rtctimer now RTC is ok
currentDate = String(day()) + "/" + month() + "/" + year(); // etc
Serial.println("RTC started");
daybefore = currentDate;
timer.setInterval(60000L, table); //start table() now RTC is OK
}
}
void table() //Saurabh
{
currentDate = String(day()) + "/" + month() + "/" + year(); // etc
if (currentDate != daybefore)
{
Blynk.virtualWrite(V10, "add", rowIndex, daybefore, totalMilliLitres / 1000 + String(" litre")); //Saurabh
Blynk.virtualWrite(V1, 0);
Serial.println("working");
flowMilliLitres = 0;
totalMilliLitres = 0;
daybefore = currentDate;
}
}
void STOPair() {
airState = 0;
digitalWrite(airPin, LOW); // turn AIR Off
airControl();
}
void readSwitch() {
int reading = digitalRead(SWITCHPIN);
if (debug == 2) {
Serial.println("Reading Switch");
Serial.print("Reading "); Serial.println(reading);
}
if (reading == 0) {
airState = 1;
digitalWrite(airPin, HIGH); // turn relay ON
WaterBridge.virtualWrite(V9, 0);
AirTimer = timer.setTimeout(AirTime, STOPair);
airControl();
}
}
BLYNK_WRITE(V3) { // reset with button in PUSH mode on virtual pin 3
int resetdata = param.asInt();
if (resetdata == 1) {
Serial.println("Clearing data");
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V1, 0);
averageflow = 0;
countseconds = 0;
flowMilliLitres = 0;
totalMilliLitres = 0;
}
}
BLYNK_WRITE(V4) { // Button in SWITCH mode on virtual pin 4 to control relay
int controlRelay = param.asInt();
if (controlRelay == 1) {
airState = 1;
if (AirTime != 0) {
if (timer.isEnabled(AirTimer)) {
timer.disable(AirTimer);
AirTimer = timer.setTimeout(AirTime, STOPair);
} else {
AirTimer = timer.setTimeout(AirTime, STOPair);
}
}
WaterBridge.virtualWrite(V9, 0);
digitalWrite(airPin, HIGH); // turn relay ON
}
else {
airState = 0;
digitalWrite(airPin, LOW); // turn relay OFF
if (timer.isEnabled(AirTimer)) {
timer.disable(AirTimer);
}
}
airControl();
}
BLYNK_WRITE(V5) //Alerts!
{
AlertsVal = param.asInt(); // read incoming value from pin V5
Serial.print("Alerts Value changed to " ); Serial.print(AlertsVal); Serial.println();
}
BLYNK_WRITE(V6) //time of air on
{
int pinValue = param.asInt(); // read incoming value from pin V5
AirTime = pinValue * 60000; // assign pin value to its own variable
if (AirTime != 0) {
if (timer.isEnabled(AirTimer)) {
timer.disable(AirTimer);
AirTimer = timer.setTimeout(AirTime, STOPair);
}
}
Serial.print("Air Timer Updated to " ); Serial.print(AirTime); Serial.println();
}
BLYNK_WRITE(V20) { // reset with button in PUSH mode on virtual pin 20
int resetdata = param.asInt();
if (resetdata == 1) {
Serial.println("Clearing table data");
Blynk.virtualWrite(V10, "clr");
}
}
BLYNK_WRITE(V100)//Reset the Board--------------------------------
{
int pinValue = param.asInt(); // read incoming value from pin V100
if (pinValue == 1) {
Serial.println("Reboot Called. We shall reboot now.");
//We will reset here
ESP.restart();
}
} BLYNK_WRITE(V101) //Debug Trigger--------------------------------
{
Serial.println("FW: " + FW_VER);
int pinValue = param.asInt(); // read incoming value from pin V100
if (pinValue == 1) {
Serial.println("Debug 1 Enabled.");
debug = 1;
} else if (pinValue == 2) {
Serial.println("Debug 2 Enabled.");
debug = 2;
} else if (pinValue == 3) {
Serial.println("Debug 3 Enabled.");
debug = 3;
} else {
Serial.println("Debug Disabled.");
debug = 0;
}
}
BLYNK_WRITE(V118)//Update the Board--------------------------------
{
int pinValue = param.asInt(); // read incoming value from pin V100
if (pinValue == 1) {
Serial.println("Update Called. We shall check for updates now.");
virtualpin118 = 1;
checkForUpdates();
}
}
void loop() {
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
Secondary Tab:
void CheckConnection() { // check every 11s if connected to Blynk server
if (!Blynk.connected()) {
++disconnectCount;
if (debug == 1) {
Serial.println("Not connected to Blynk server");
Serial.println("Disconnect Count: " + disconnectCount);
}
if (disconnectCount <= 20) {
Blynk.connect(); // try to connect to server with default timeout
} else if (disconnectCount > 20) {
delay(3000);
//ESP.restart();
Serial.println("Not connected to Blynk server, Unhappy Restart");
delay(5000);
}
}
else {
disconnectCount = 0;
if (debug == 1) {
Serial.println("Connected to Blynk server");
Serial.println("FW:" + FW_VERSIONs);
}
}
}
void checkForUpdates() {
detachInterrupt(13);
analogWrite(LED, 255);
String devID = hostNamed;
String fwURL = String( fwUrlBase );
fwURL.concat( hostNamed );
String fwVersionURL = fwURL;
fwVersionURL.concat( ".version" );
if (debug == 1) {
Serial.println( "Checking for firmware updates." );
Serial.print( "Device ID: " );
Serial.println( hostNamed );
Serial.print( "Firmware URL: " );
Serial.println( fwVersionURL );
}
HTTPClient httpClient;
httpClient.begin( fwVersionURL );
int httpCode = httpClient.GET();
if ( httpCode == 200 ) {
String newFWVersion = httpClient.getString();
if (debug == 1) {
Serial.print( "Current firmware version: " );
Serial.println( FW_VERSIONs );
Serial.print( "Available firmware version: " );
Serial.println( newFWVersion );
}
int newVersion = newFWVersion.toInt();
if ( newVersion > FW_VERSIONs ) {
Serial.println( "Preparing to update" );
String fwImageURL = fwURL;
fwImageURL.concat( ".bin" );
t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL );
Serial.print("FirmwareFile:");
Serial.println(fwImageURL);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
}
}
else {
if (debug == 1) {
Serial.println( "Already on latest version" );
}
}
}
else {
Serial.print( "Firmware version check failed, got HTTP response code " );
Serial.println( httpCode );
}
httpClient.end();
}
String TimeShowFormatted(int ms) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = ms / 1000; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the converted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the converted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the converted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
void Sent_serial() {
Blynk.virtualWrite(V0, TimeShowFormatted(millis()));
Blynk.virtualWrite(V19, WiFi.RSSI()); //signal strength on V19
// Sent serial data to Blynk terminal - Unlimited string readed
String content = ""; //null string constant ( an empty string )
char character;
while (Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content != "") {
String currentTime = String(hour()) + ":" + minute() + ":" + second();
content = " " + content;
content = currentTime + content;
Blynk.virtualWrite (V99, content);
}
}