I am building a monitor for my water usage that updates a mysql table with the stats from a flowmeter. I am currently using a button to simulate the pulse from the flowmeter for testing. I set up the pulse as an interrupt so I count all the pulses while code is running. However, the sketch misses any pulses that happen while it is sending the mysql data. This is not critical as I will only send the data periodically, but I would rather not miss pulses. Any advice?
Just FYI, the sketch also monitors how long the water has been running and shuts it off if it runs too long.
I am using an Arduino UNO R4. I am also very new to Arduino programming so any advice on other issues with my code is welcome.
Sketch code:
// Include the RTC library
#include "RTC.h"
//Include the NTP library
#include "arduino_secrets.h"
#include <NTPClient.h>
#include <WiFiS3.h>
#include <WiFiUdp.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include <ArduinoJson.hpp>
#include "wifiTime.h"
#include "Button.h"
#include "FlowCheck.h"
#include "ServerPing.h"
#include "mysqlSend.h"
#define BUTTON_PIN 5
Button button1(BUTTON_PIN); //define pin for tester pulde
byte sensorInterrupt = 2; //flow sensor interrupt
volatile byte pulseCount; //pulse counter
bool buttonStatus ;
flowCheck flowCheck; //establish flow check
serverPing serverPing("connected.homeseer.com"); //establish homeseer connection
int oldHour; //establish variable to check for new hour
int oldDay; //establish variable to check for new day
unsigned long timerCheckMs; //establish variable to check timer loop
unsigned long timerTimeMs = 30000; //set timer loop to 15 seconds
void setup() {
Serial.begin(9600);
while (!Serial); //wait for serial
connectToWiFi(); //connect to wifi
//start RTC and set time
RTC.begin();
time_update(-8);
RTC.getTime(currentTime);
Serial.println("The RTC is updated: " + String(currentTime));
//grab homeseer variables
String Pstatus = serverPing.statusProperty();
Serial.print("status property = : ");
Serial.println(Pstatus);
String vstatus = serverPing.statusMonitor();
Serial.print("status property = : ");
Serial.println(vstatus);
}
void loop() {
//check wifi
if (wifiStatus != WL_CONNECTED) {
Serial.println("Wifi DISCONNECTED. Trying to reconnect...");
connectToWiFi();
}
flowCheck.waterRunMaxSec[1] = 50;
pulseCount = flowCheck.check(pulseCount); //run flow check
///basic timer
if (millis() - timerCheckMs > timerTimeMs) {
timerCheckMs = millis();
Serial.println("");
Serial.println(">>>>>>>>TIMER LOOP>>>>>>>>");
String sqlColumns = "hour,mon_status,volume";
String sqlValues = "'1','val2','" + String(flowCheck.volumeDay) + "'";
querySend(sqlColumns,sqlValues); //send data to mysql table
Serial.println(">>>>>>>>TIMER LOOP>>>>>>>>");
}
//////TIME BLOCKS
RTCTime currenttime; //establish current time variable
RTC.getTime(currenttime); //set current time variable
//hour block
int nowHour = currenttime.getHour() ;
if( nowHour != oldHour ){
oldHour = nowHour;
///run hourly tasks
}
//day block
int nowDay = currenttime.getDayOfMonth() ;
if( nowDay != oldDay ){
oldDay = nowDay;
///run daily tasks
time_update(-8); //update RTC time once daily
}
///button pulse function
if (button1.isPressed()) {
if(buttonStatus){
}else{
pulseCount += 1;
buttonStatus = true;
}
}else{
buttonStatus = false;
}
}
mysqlSend.h that handles the mysql request
#include <Arduino.h>
#include <WiFiS3.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include "arduino_secrets.h"
char user[] = SECRET_MYSQL_USER;
char password[] = SECRET_MYSQL_PASS;
void querySend(String sqlColumns,String sqlValues)
{
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(XXX,XXX,XXX,XXX); // IP of the MySQL *server* here
// Sample query
String base_s = "INSERT INTO XXXXXXXXX.6556_Irr_Hr (" + sqlColumns + ") VALUES (" + sqlValues + ")";
const char *INSERT_SQL = base_s.c_str();
WiFiClient client;
MySQL_Connection conn((Client *)&client);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}
Thanks!