Hello, I’m new on Cayenne i follow a project flow meter, im made mine with a Esp32 and a water flow sensor FS300A.
Problems:
— I want to have a chart with the consume of water by days.
This is my code
{ /*
Application:
- Interface water flow sensor with ESP32 board.
Board:
- ESP32 Dev Module
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h> //Importar a “BlynkSimpleEsp32” library.
//Activa a Communicação MQTT da plataforma Cayenne:
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP32.h> //Importar a "CayenneMQTTESP32" library.
//Autenticação da nossa conta na aplicação Cayenne.
char username[] = "";
char password[] = "";
char clientID[] = "";
//Autenticação de acesso à rede Wi-Fi.
char ssid[] = "";//Colocar o nome da rede Wi-Fi.
char pass[] = ""; //Colocar a password do rede Wi-fi.
//Nota: Colocar "" caso seja uma rede Wi-Fi aberta.
#define SENSOR 21
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
void setup()
{
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, pass);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
//Iniciar a comunicação com a aplicação Cayenne:
Cayenne.loop();
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
// 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() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = 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 millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
// 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(totalMilliLitres / 1000);
Serial.println("L");
}
}
void Counter()
{
// Increment the pulse counter
pulseCount++;
}
CAYENNE_OUT(V0) //Flow rate:
{
Cayenne.virtualWrite(V0, flowRate);
}
CAYENNE_OUT(V1) //Current Liquid Flowing:
{
Cayenne.virtualWrite(V1, flowMilliLitres);
}
CAYENNE_OUT(V2) //Output Liquid Quantity:
{
Cayenne.virtualWrite(V2, totalMilliLitres);
}