im a newbie here. Does anyone know about E-ra IOT platform? I have carried out my small project on it. I have a trouble with my data. I wonder why my hourly flow rate does not reset in every hours? It still stayed at 14.9 L. 14.9L is hourly flow rate at 0h00. I thoght that hourly flow rate should reset in every hours and display on the chart. Unfortunately, it doesnt show hourly flow rate on the chart either since at the midnight.Im using esp32s, ds3231,YF-B1,… Please review my code, i dont know where i was wrong. Hope you guys help me, it means a lot to me. Thx all <33
#define ERA_DEBUG
#define DEFAULT_MQTT_HOST "mqtt1.eoh.io"
#define ERA_AUTH_TOKEN "16e5c4be-412f-42c2-af6c-0571be05829f"
#define FLOW_SENSOR_PIN 25
#define CALIBRATION_FACTOR 4.5
#define RAIN_SENSOR_PIN 36
#define RAIN_THRESHOLD_RAW 2000
#include <Arduino.h>
#include <ERa.hpp>
#include <Wire.h>
#include <ArtronShop_SHT3x.h>
#include <EEPROM.h>
#include <RTClib.h>
const char* myphone = "0812518637";
volatile int flowPulseCount = 0;
float flowRate = 0.0;
float totalFlow = 0.0;
unsigned long lastFlowTime = 0;
int rainSensorValue = 0;
bool isRaining = false;
unsigned long lastHourTime = 0;
float previousTotalFlow = 0.0;
float hourlyFlow = 0.0;
unsigned long lastDayTime = 0;
float previousDailyFlow = 0.0;
float dailyFlow = 0.0;
const char ssid[] = "Bo Cuoc Doi";
const char pass[] = "anhcao123";
RTC_DS3231 rtc;
ArtronShop_SHT3x sht3x(0x44, &Wire);
WiFiClient mbTcpClient;
void flowPulseCounter() {
flowPulseCount++;
}
void readEEPROM() {
totalFlow = EEPROM.readFloat(0);
previousTotalFlow = totalFlow;
}
void writeEEPROM(float value) {
EEPROM.writeFloat(0, value);
EEPROM.commit();
}
void handleRainSensor() {
rainSensorValue = analogRead(RAIN_SENSOR_PIN);
Serial.print("Rain Sensor Value: ");
Serial.println(rainSensorValue);
if (rainSensorValue > RAIN_THRESHOLD_RAW) {
if (!isRaining) {
Serial.println("Rain detected!");
ERa.virtualWrite(V6, 1);
isRaining = true;
}
} else {
if (isRaining) {
Serial.println("No rain detected.");
ERa.virtualWrite(V6, 0);
isRaining = false;
}
}
}
void calculateFlow() {
if (millis() - lastFlowTime >= 1000) {
flowRate = (flowPulseCount / CALIBRATION_FACTOR);
flowPulseCount = 0;
totalFlow += (flowRate / 60.0);
writeEEPROM(totalFlow);
Serial.print("Flow Rate: ");
Serial.print(flowRate, 2);
Serial.print(" L/mintTotal Flow: ");
Serial.print(totalFlow, 2);
Serial.println(" L");
ERa.virtualWrite(V4, flowRate);
ERa.virtualWrite(V5, totalFlow);
lastFlowTime = millis();
}
}
void handleHourlyFlow() {
if (millis() - lastHourTime >= 3600000UL) {
hourlyFlow = totalFlow - previousTotalFlow;
ERa.virtualWrite(V7, hourlyFlow);
Serial.print("Hourly Flow: ");
Serial.print(hourlyFlow, 2);
Serial.println(" L");
previousTotalFlow = totalFlow;
lastHourTime = millis();
}
}
void handleDailyFlow() {
DateTime now = rtc.now();
if (now.hour() == 0 && now.minute() == 0 && now.second() == 0) {
dailyFlow = totalFlow;
ERa.virtualWrite(V8, dailyFlow);
Serial.print("Daily Flow Reset: ");
Serial.print(dailyFlow, 2);
Serial.println(" L");
totalFlow = 0.0;
writeEEPROM(totalFlow);
previousDailyFlow = dailyFlow;
lastDayTime = millis();
if (hourlyFlow >= 14.9) {
Serial.println("Luu Luong Vuot qua quy dinh");
sendSMS(myphone,"Luu Luong vuot qua quy dinh");
}
}
}
void timerEvent() {
if (sht3x.measure()) {
Serial.print("Temperature: ");
Serial.print(sht3x.temperature(), 1);
ERa.virtualWrite(V2, sht3x.temperature());
ERa.virtualWrite(V3, sht3x.humidity());
Serial.print(" *CtHumidity: ");
Serial.print(sht3x.humidity(), 1);
Serial.print(" %RH");
Serial.println();
}
ERA_LOG(ERA_PSTR("Timer"), ERA_PSTR("Uptime: %d"), ERaMillis() / 1000L);
calculateFlow();
handleRainSensor();
handleHourlyFlow();
handleDailyFlow();
}
void setup() {
#if defined(ERA_DEBUG)
Serial.begin(115200);
Serial2.begin(115200); // Tốc độ baud của module SIM
#endif
ERa.setModbusClient(mbTcpClient);
ERa.setScanWiFi(true);
ERa.begin(ssid, pass);
EEPROM.begin(512);
readEEPROM();
pinMode(FLOW_SENSOR_PIN, INPUT_PULLUP);
pinMode(RAIN_SENSOR_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), flowPulseCounter, FALLING);
Serial2.println("AT");
delay(100);
if (Serial2.available()) {
Serial.println("Module SIM kết nối thành công!");
} else {
Serial.println("Không thể kết nối với module SIM.");
}
sendSMS(myphone, "Thiet Bi Da Khoi Dong!");
Wire.begin();
while (!sht3x.begin()) {
Serial.println("SHT3x not found !");
delay(1000);
}
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
ERa.addInterval(1000L, timerEvent);
lastHourTime = millis();
lastDayTime = millis();
}
void loop() {
ERa.run ();
}
void sendSMS(const char* phoneNumber, const char* message) {
Serial2.println("AT+CMGF=1"); // Chuyển sang chế độ nhắn tin văn bản
delay(100);
Serial2.print("AT+CMGS="");
Serial2.print(phoneNumber);
Serial2.println("""); // Số điện thoại nhận
delay(100);
Serial2.print(message); // Nội dung tin nhắn
delay(100);
Serial2.write(0x1A); // Ký tự kết thúc tin nhắn
delay(100);
}
void call(const char* phoneNumber) {
Serial2.print("ATD");
Serial2.print(phoneNumber);
Serial2.println(";"); // Thực hiện cuộc gọi
delay(100);
}
void endCall() {
Serial2.println("ATH"); // Kết thúc cuộc gọi
delay(100);
}
enter image description here
well, at first, i thought that maybe because my flow rate is not big enough to affect the hourly flow rate so the hourly flow rate still the same. But i recognize, if this case happens, the chart will show the same hourly flow rate in every hours but it didnt. The chart only shows the hourly flow rate at midnight.
Anh Cao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3