Hello,
I work on my first DIY IoT small project - Flowmeter (basic 3 wires eBay unit with Hall Sensor).
I just started and my knowledge is limited but I try to modify online available codes and open them on NodeMCU and D1mini.
I’ve got the reading on the Serial Monitor under Arduino IDE software but I cannot see any readings on my Blynk app. In Blynk I use Gauge, V0 and 5sec refresh interval. I’ve got WiFi connection between Blynk and NodeMCU.
I would appreciate any suggestions and comments
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <SimpleTimer.h>
//Variable init
const int buttonPin = D2; //variable for D2 pin
//const int ledPin = D7;
int addr=0; //endereco eeprom
byte sensorInterrupt = 0; // 0 = digital pin 2
char auth[] = "XXXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXX";
char pass[] = "XXXX";
// 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;
float totalLitres;
SimpleTimer timer;
void setup(){
Blynk.begin(auth, ssid, pass);
Serial.begin(115200);
pinMode(buttonPin, INPUT);
//pinMode(ledPin, OUTPUT);
Serial.println();
flowMilliLitres = 0;
totalMilliLitres = 0;
totalLitres = 0;
oldTime = 0;
flowRate = 0.0;
timer.setInterval(1000L, showFlow);
attachInterrupt(digitalPinToInterrupt(buttonPin), pulseCounter, RISING);
}
void showFlow() //average the flow over averageperiod
{
detachInterrupt(sensorInterrupt); // Disable the interrupt while calculating flow rate and sending the value to the host
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
totalLitres = totalMilliLitres/1000;
unsigned int frac;
// 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("."); // Print the decimal point
// Determine the fractional part. The 10 multiplier gives us 1 decimal place.
frac = (flowRate - int(flowRate)) * 10;
Serial.print(frac, DEC) ; // Print the fractional part of the variable
Serial.print("L/min");
// Print the number of litres flowed in this second
Serial.print(" Current Liquid Flowing: "); // Output separator
Serial.print(flowMilliLitres);
Serial.print("mL/Sec");
// Print the cumulative total of litres flowed since starting
Serial.print(" Output Liquid Quantity: "); // Output separator
Serial.print(totalMilliLitres);
Serial.println("mL");
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void pulseCounter(){
// Increment the pulse counter
pulseCount++;
}
void loop(){
Blynk.run();
timer.run();
}
That’s because you don’t have any code to write the readings to Blynk. You’re missing a Blynk.virtualWrite(V0, value) command.
There are lots of other things wrong with your code, including lots of redundant code, but the main issues are:
you should use volatile variable types in your interrupt routine
move your Serial.begin to before the Blynk.begin command, that way you get Blynk connection data in your serial monitor
use BlynkTimer rather than SimpleTimer
This is not required.
These commands in your void setup are redundant, as the variable are automatically initialised with zero value. It is good programming practice to explicitly declare the initial value of every variable, but this can be done in the variable declaration.
I much appreciate your comments Pete - thank you for that. I will try to correct my code according to your advice.
In the meantime I paste a working code which I pull from the youtube video. Maybe this will help someone in the future who like me struggle with the first steps.
I’m having the same issue. Unable to see any changes in my blynk dashboard using a waterflow sensor. I’ve implemented two datastreams - flowrate and volume. The serial monitor displays the flowrate and volume correctly, my sensor shows that it is online, but nothing changes as water flows through the sensor.
I’ll appreciate any editions to the code :-
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
// Define the Blynk template ID and name to use for this project
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_TEMPLATE_NAME "xxxx"
// Replace with your Blynk auth token
char auth[] = "xxxx";
// Replace with your Wi-Fi network credentials
const char* ssid = "xxxx";
const char* password = "xxxxx";
// Define the pin on which the water flow sensor is connected
#define SENSOR D4
// Define variables to keep track of the water flow rate and total quantity of water
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 6.6;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long waterTime = millis();
unsigned long waterStartTime = 0;
bool waterIsOn = false;
// Interrupt service routine to count the number of pulses from the water flow sensor
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
void setup()
{
// Set up serial communication
Serial.begin(115200);
// Connect to Wi-Fi using the specified network credentials
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
// Print the local IP address on the Serial Monitor
Serial.println("");
Serial.println("Wi-Fi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Connect to Blynk using the specified auth token and Wi-Fi network credentials
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
// Set the water flow sensor pin to input mode with pull-up resistor enabled
pinMode(SENSOR, INPUT_PULLUP);
// Initialize the pulse count to zero
pulseCount = 0;
// Initialize the flow rate and total quantity of water to zero
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
// Initialize the previousMillis variable to the current time
previousMillis = 0;
// Attach an interrupt service routine to the falling edge of the water flow sensor pulse
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
// Get the current time in milliseconds
currentMillis = millis();
// If the specified interval has elapsed since the previous measurement, take a new measurement
if (currentMillis - previousMillis > interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
//flowRate = (millis() - previousMillis * pulse1Sec) / calibrationFactor;
previousMillis = millis();
flowMilliLitres = flowRate / 60 *1000;;
totalMilliLitres += flowMilliLitres;
Serial.print("Flow rate: ");
Serial.print(flowRate);
Serial.print("ML/min");
Serial.print("\t");
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.println("ML");
Blynk.virtualWrite(V4, flowRate, "APP_PUSH");
Blynk.virtualWrite(V1, totalMilliLitres);
}
// Handle OTA updates
ArduinoOTA.handle();
Blynk.run();
int pinValue = digitalRead(SENSOR);
if (pinValue == HIGH) {
if (!waterIsOn) {
waterIsOn = true;
waterStartTime = millis();
}
} else {
waterIsOn = false;
}
if (waterIsOn && millis() - waterStartTime > 180000) {
Blynk.logEvent("Water running for too long"); // do something, e.g. send a notification or turn off the water
}
} ```
@msmuringo this is a very old post. Its betterYou create a new post. Also before posting the code properly format it with triple backticks.
It looks like ```
@msmuringo Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
What is this line of code meant to achieve? – I was having an issue with displaying the flow rate, so I edited it to more or less force post the values (got it from an online solution) didn’t help though, but I just left it as is.
WaterVolume || Pin V1 || Data type: Double || Min 0, Max 100000
FlowRate || Pin V4 || Data type: Double || Min 0, Max 50
It seems like your code is thrown together from lots of sources without u dersanding what each of the pieces is meant to achieve, or together they work together or not.
Why are you doing all of the WiFi connect stuff, and the overly complicated Blynk.begin command?
Have you read my comments earlier in this topic about “keep your void loop clean”?
Of so, why is your void loop sl cluttered?
Why aren’t you simply using a BlynkTimer set at a 1 second interval to send the results to the serial monitor and to Blynk?
I’ve never seen that syntax used before in the 6 years that I’ve been using Blynk.