Skip to content

Commit

Permalink
Supports bot SD and SPIFFS filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
jordancrubin committed Oct 17, 2022
1 parent ea5bc25 commit 788e15a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 33 deletions.
6 changes: 4 additions & 2 deletions examples/testmeter/testmeter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
#include <Watermeter.h>

/*
WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],metervalue,saveInterval,debug)
WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],debounce,useFilesys,metervalue,saveinterval)
Using GPIO 15 [15]
Using internal pullup resistors [true]
This meter is designed as gallons [g]
Debounce delay in ms [100]
Use SD card to autosave meter to filesys [true]
The value of measurment added each time [.1] i.e. .1 gallons per pulse
The save interval to SD in seconds [30]
The save interval to FS in seconds [30] 0 disables autosave
Toggle some debug to console [true]
When saving to the filesystem, one type of filesystem must be uncommented in Watermeter.cpp USE_SPIFFS or USE_SD.
You MUST choose one, and ONLY one. Software arrives defaulted to USE_SPIFFS.
If SD card is set to true the programme will expect a fat32 formatted SD card installed in the unit.
It uses the default connections. SPIFFS hase been completely removed as of this version, given the
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Watermeter
version=1.0.8
version=1.0.9
author=Jordan Rubin <[email protected]>
maintainer=Jordan Rubin <[email protected]>
sentence=Control Library for reed switch pulse actuated water meters such as the DAE Water Meter Model V-100P
Expand Down
103 changes: 77 additions & 26 deletions src/Watermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
Watermeter.cpp - Control Library for reed switch pulse actuated water meters
This one designed around the DAE Water Meter Model V-100P
Most of these meters operate in the same manner, the difference
can be adjusted as parameters in the instance creation.
can be adjusted as parameters in the instance creation. Saving works
With either SPIFFS or SDFS as a compile time parameter.
https://www.youtube.com/c/jordanrubin6502
2020 - 2022 Jordan Rubin.
*/

#include <Arduino.h> // Required for Platform.io
#include <Watermeter.h>

// Choose only one below filesystem....... USE_SPIFFS or USE_SD
#define USE_SPIFFS 1
//#define USE_SD 1
////////////You Must choose one, and not both if saving to FS!
#if (USE_SPIFFS)
#include <SPIFFS.h>
#endif
#if(USE_SD)
#include <SD.h>
#include <FS.h>
#endif

int meterSignal;
bool gallon = 0;
Expand All @@ -22,14 +34,15 @@ long lastUpdate = 0;
double meter = 0;
float increment;
int saveint;
bool useSDcard;
bool useFilesys;

void IRAM_ATTR respondInterrupt();

//CONSTRUCTOR -----------------------------------------------------------
//WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr, int saveInterval, bool debug)
WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr, int saveInterval, bool debugger)
//WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useFS, float incr, int saveInterval, bool debug)
WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useFS, float incr, int saveInterval, bool debugger)
{
//----------------- initialize initial parameters
//----------------- initialize initial parameters --------------------------]
if (useInternalPullups){
pinMode(signalGPIOpin, INPUT_PULLUP);
}
Expand All @@ -43,13 +56,14 @@ WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure,
debug = debugger;
saveint = saveInterval * 1000;
debounceDelay = dbounce;
useSDcard = useSD;
useFilesys = useFS;
}
// ----------------------------------------------------------------------------]

// FUNCTION - [updated] - [Checks and responds to the update flag--------------]
bool WATERMETER::updated(void){
if (update){
if (!useFilesys){update = 0; return 1;}
if(saveint == 0){return 1;}
if ((millis() - lastUpdate) > saveint) {
writeFile();
Expand All @@ -70,25 +84,42 @@ int WATERMETER::getDebounce(void){

// FUNCTION - [initFilesys] - [Starts up the Meter File system and test--------]
const char* WATERMETER::initFilesys(void){
if (useSDcard){
if(!SD.begin()){
return "ERR";
}
//DELETE FOR TESTING
//SD.remove("/meter/meter.val");
File root = SD.open("/meter");
if(!root.isDirectory()){
SD.mkdir("/meter");
}
root.close();
if(!SD.exists("/meter/meter.val")){
writeFile();
return "NEW";
}
else {
bool reset = readFile();
if (reset){return "RESET";}
}
if (useFilesys){
#if (USE_SPIFFS)
if (!SPIFFS.begin(true)){
return "ERR";
}
//DELETE FOR TESTING
//SPIFFS.remove("/meter.val");
if(!SPIFFS.exists("/meter.val")){
writeFile();
return "NEW";
}
else {
bool reset = readFile();
if (reset){return "RESET";}
}
#endif
#if (USE_SD)
if(!SD.begin()){
return "ERR";
}
//DELETE FOR TESTING
//SD.remove("/meter/meter.val");
File root = SD.open("/meter");
if(!root.isDirectory()){
SD.mkdir("/meter");
}
root.close();
if(!SD.exists("/meter/meter.val")){
writeFile();
return "NEW";
}
else {
bool reset = readFile();
if (reset){return "RESET";}
}
#endif
}
else {
return "NONE";
Expand All @@ -99,8 +130,18 @@ const char* WATERMETER::initFilesys(void){

// FUNCTION - [readFile] - [Opens and reads meter file into meter value--------]
bool WATERMETER::readFile(){
if (!useFilesys){
if(debug){Serial.print("No FS Enabled");
return 1;
}
}
bool resetalarm=0;
#if(USE_SPIFFS)
File meterfile = SPIFFS.open("/meter.val","r");
#endif
#if(USE_SD)
File meterfile = SD.open("/meter/meter.val");
#endif
char test[20];
int i=0;
while (meterfile.available()) {
Expand All @@ -109,11 +150,16 @@ bool WATERMETER::readFile(){
i++;
if (i >=15) {
resetalarm=1;
#if(USE_SPIFFS)
SPIFFS.remove("/meter.val");
#endif
#if(USE_SD)
SD.remove("/meter/meter.val");
#endif
writeFile();
return resetalarm;
}
}
}
test[i] = '\0';
meterfile.close();
char* end;
Expand Down Expand Up @@ -168,7 +214,12 @@ void WATERMETER::setMeter(double value){

// FUNCTION - [writeFile] - [Opens or creates meter file, writes value---------]
void WATERMETER::writeFile(){
#if(USE_SPIFFS)
File meterfile = SPIFFS.open("/meter.val","w");
#endif
#if(USE_SD)
File meterfile = SD.open("/meter/meter.val",FILE_WRITE);
#endif
char convert[10];
sprintf(convert, "%6.2f", meter);
meterfile.print(convert);
Expand Down
3 changes: 2 additions & 1 deletion src/Watermeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
class WATERMETER
{
////////////// user-accessible "public" interface
//WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],dbouncedelay,useSD,increment,saveinterval,debug)
//WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],dbouncedelay,useFS,increment,saveinterval,debug)
public:
WATERMETER(int,bool,char,long,bool,float,int,bool);
bool updated(void);
bool useFS;
int getDebounce(void);
const char* initFilesys(void);
bool isUpdated(void);
Expand Down
9 changes: 6 additions & 3 deletions src/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
#include <Watermeter.h>

/*
WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],metervalue,saveInterval,debug)
//WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useFS, float incr, int saveInterval, bool debug)
Using GPIO 15 [15]
Using internal pullup resistors [true]
This meter is designed as gallons [g]
Debounce delay in ms [100]
Use SD card to autosave meter to filesys [true]
The value of measurment added each time [.1] i.e. .1 gallons per pulse
The save interval to SD in seconds [30]
The save interval to FS in seconds [30] 0 disables autosave
Toggle some debug to console [true]

When saving to the filesystem, one type of filesystem must be uncommented in Watermeter.cpp USE_SPIFFS or USE_SD.
You MUST choose one, and ONLY one. Software arrives defaulted to USE_SPIFFS.

If SD card is set to true the programme will expect a fat32 formatted SD card installed in the unit.
It uses the default connections. SPIFFS hase been completely removed as of this version, given the
constant barrage of read and saves to the ESP32 thousands of times over is unhealthy for the device.
Expand Down Expand Up @@ -51,7 +54,7 @@ void setup() {
/*
Or use a methodology to test success directly
if (strcmp(thisMeter.initFilesys(),"OK")==0){
Serial.println("SPIFFS STARTED");
Serial.println("FS STARTED");
}
The codes are:
OK - Everything worked as expected
Expand Down

0 comments on commit 788e15a

Please sign in to comment.