boolean debug = true; // Change to false for no debug (serial off)
#define INT_PIN 13 // Interrupt pin number
#define BLYNK_PRINT Serial // Uncomment this for first run or debugging.
#include <ESP8266WiFi.h> // Library for ESP8266 WiFi functionality. Needs to be included if we want to connect to WiFi.
#include <BlynkSimpleEsp8266.h> // Blynk library. Needs to be included so we can send data to Blynk.
#include <SPI.h> // SPI library
#include <SD.h> // SD card library
#include “Wire.h” // I2C library
#include <MAX17043.h> // MAX17043 library
// System variables
unsigned int i = 0; // frequency conter
float flow = 0; // flow in l/min
float a_flow = 0; // average flow in l/min
float t_water = 0; // total amount of water
float voltage = 0; // battery voltage
float charge = 0; // battery charge
const int Ts = 2; // Sampling rate
unsigned int c = 0; // cycle counter
// Functions
void getFlow();
void logFlow();
void logFlowBlynk();
void checkBattery();
// ISR, Interrupt routine
void counter(){
i++;
}
// Blynk tokken
// You should get your Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “”;
// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “”;
char pass[] = “ma0506385325”;
MAX17043 batteryMonitor;
void setup()
{
// Initialize digital pin 2 as an output.
pinMode(2, OUTPUT); // Blue LED
pinMode(INT_PIN, INPUT); // Interrupt pin
if(debug){
Serial.begin(9600);
}
// I2C for Fuel gauge
Wire.begin(4,5); // Start I2C interface on Arduino pins 4 (SDA) and 5 (SCL)
batteryMonitor.reset();
batteryMonitor.quickStart();
delay(1000);
// SD card
if (!SD.begin(15)){
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“Card detected”);
// Blynk setup
Blynk.begin(auth, ssid, pass);
//wifi_set_sleep_type(MODEM_SLEEP_T);
}
void loop(){
getFlow();
logFlow();
if(c==26){
Blynk.run();
// Update server
checkBattery();
logFlowBlynk();
c=0;
// Total amount of water correction
t_water = t_water + (flow/60)Ts3;
delay(50);
}else{
c++;
delay(950);
}
}
in the monitor it connects to blynk but in the app its offline
void getFlow(){
i=0; // reset counter to zero
// Start interrupts on INT_PIN
attachInterrupt(digitalPinToInterrupt(INT_PIN), counter, RISING);
delay(1000); // count pulses in one seconds; i = frequency
detachInterrupt(digitalPinToInterrupt(INT_PIN));
flow = i / 7.5; // according to manufacturer this is how flow is calculated from frequency
t_water = t_water + (flow/60)*Ts;
a_flow = ( a_flow/2 ) + ((flow/60)/2 );
if(debug){
Serial.print(“Frequency: “);
Serial.print(i);
Serial.println(” Hz”);
Serial.print(“Flow: “);
Serial.print(flow);
Serial.println(” l/min”);
Serial.print(“Average flow: “);
Serial.print(a_flow);
Serial.println(” l/min”);
Serial.print(“Total amount of water: “);
Serial.print(t_water);
Serial.println(” l”);
}
}
void logFlow(){
// Prep data
String dataString = String(flow) + “,” + String(a_flow) + “,” + String(t_water);
// Open file
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);
// If the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
// Close file
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
}
}
void logFlowBlynk(){
// Send data
Blynk.virtualWrite(V0, a_flow); // Writing Average flow to Virtual pin V0.
Blynk.virtualWrite(V1, t_water); // Writing Total amound of water to Virtual pin V1.
Blynk.virtualWrite(V2, voltage); // Writing battery voltage level to Virtual pin V2.
Blynk.virtualWrite(V3, charge); // Writing battery charge status to Virtual pin V3.
// Send email alarm on battery voltage
if(voltage < 3.2){ // If voltage is below 3.2V, send alarm.
Blynk.email("my_email@example.com", “Battery low”, “Hey. Flow meter here. Please charge me!”);
}
Serial.println(“Update complete”);
}
void checkBattery(){
// Check battery charge status
voltage = batteryMonitor.getVCell();
Serial.print(“Voltage:\t\t”);
Serial.print(voltage, 4);
Serial.println(“V”);
// Check battery voltage
charge = batteryMonitor.getSoC();
Serial.print(“State of charge:\t”);
Serial.print(charge);
Serial.println("%");
}