Skip to content

Commit

Permalink
replaced spiffs with SD card support
Browse files Browse the repository at this point in the history
  • Loading branch information
jordancrubin committed Jul 21, 2022
1 parent 9201ab8 commit 3163553
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 57 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
https://www.youtube.com/c/jordanrubin6502


WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],debounce,spiffs,metervalue)
WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],debounce,sdcard,metervalue)

Using GPIO 15 [15]

Expand All @@ -18,13 +18,24 @@ This meter is designed as gallons [g]

Debounce delay in ms [100]

Use SPIFFS to autosave meter to filesys [true]
Use SDcard to autosave meter to filesys [true]

The value of measurment added each time [.1] i.e. .1 gallons per pulse

WATERMETER thisMeter(15,true,'g',100,true,.1,true);

Internal pullup resistors are available on the ESP32 but may not be on the Arduino or certain models requiring external resistors be provided for GPIO pins. Optimal debounce delay will need to be tested comparing the program to the physical meter accuracy. The SPIFFS initialization has autocorruption detection, should the file be found to be corrupted on init, it will pull in the meter value from the parameter. Verbose output is usefill for testing.

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.

CS -> GPIO 5
MOSI -> GPIO 23
CLK -> GPIO 18
MISO -> GPIO 19


Internal pullup resistors are available on the ESP32 but may not be on the Arduino or certain models requiring external resistors be provided for GPIO pins. Optimal debounce delay will need to be tested comparing the program to the physical meter accuracy. The SD card initialization has autocorruption detection, should the file be found to be corrupted on init, it will pull in the meter value from the parameter. Verbose output is usefull for testing.

This latest version attempts to reduce the footprint while making some improvment as I want all wasted space removed.

Expand Down
47 changes: 31 additions & 16 deletions examples/testmeter/testmeter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,37 @@
Most of these meters operate in the same manner, the difference
can be adjusted as parameters in the instance creation.
https://www.youtube.com/c/jordanrubin6502
2020 Jordan Rubin.
2020 - 2022 Jordan Rubin.
*/
#include <Arduino.h> //required for platformio
#include <Watermeter.h>

//WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],metervalue)
// Using GPIO 15 [15]
// Using internal pullup resistors [true]
// This meter is designed as gallons [g]
// Debounce delay in ms [100]
// Use SPIFFS to autosave meter to filesys [true]
// The value of measurment added each time [.1] i.e. .1 gallons per pulse
/*
WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],metervalue)
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
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.
CS -> GPIO 5
MOSI -> GPIO 23
CLK -> GPIO 18
MISO -> GPIO 19
*/

WATERMETER thisMeter(15,true,'g',100,true,.1);

void setup() {
Serial.begin(38400);
Serial.println("\n\n***Water Meter Test Programme***");
delay(3000);

//This allows the setting of the meter in the object to a specified value
//That might match tat of an analogue gauge on the metre
thisMeter.setMeter(32.2);

/*
Print the result codes out directly as the init is called
Serial.println(thisMeter.initFilesys());
Expand All @@ -37,18 +45,25 @@ void setup() {
const char * result = thisMeter.initFilesys();
Serial.println(result);


// Or use a methodology to test success directly
// if (strcmp(thisMeter.initFilesys(),"OK")==0){
// Serial.println("SPIFFS STARTED");
//}

/*
Or use a methodology to test success directly
if (strcmp(thisMeter.initFilesys(),"OK")==0){
Serial.println("SPIFFS STARTED");
}
The codes are:
OK - Everything worked as expected
ERR- The SPIFFS system was corrupted and couldnt be mounted
NEW- It found no Metre file and created a new one
RESET- The file had data errors and was re-initialized
NONE- The function was executed but the object has no param for SPIFFS support
This allows the setting of the meter in the object to a specified value
That might match tat of an analogue gauge on the metre
*/
// thisMeter.setMeter(32.2);

}

void loop() {
Expand Down
54 changes: 30 additions & 24 deletions src/Watermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
Most of these meters operate in the same manner, the difference
can be adjusted as parameters in the instance creation.
https://www.youtube.com/c/jordanrubin6502
2020 Jordan Rubin.
2020 - 2022 Jordan Rubin.
*/
#define FORMAT_SPIFFS_IF_FAILED true

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

int meterSignal;
bool gallon;
bool update;
long lastDebounce = 0; // Holds last debounce
long debounceDelay; // Between re-polling
long debounceDelay; // Between re-polling
double meter = 0;
float increment;
bool useSPIFFS;
bool useSDcard;
void IRAM_ATTR respondInterrupt();

//CONSTRUCTOR -----------------------------------------------------------
//WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],debouncedelay,useSPIFFS,incriment)
WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSPIF, float incr)

//WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr)
WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure, long dbounce ,bool useSD, float incr)
{
//----------------- initialize initial parameters
if (useInternalPullups){
Expand All @@ -39,7 +40,7 @@ WATERMETER::WATERMETER(int signalGPIOpin, bool useInternalPullups,char measure,
meterSignal = signalGPIOpin;
increment = incr;
debounceDelay = dbounce;
useSPIFFS = useSPIF;
useSDcard = useSD;
}
// ----------------------------------------------------------------------------]

Expand All @@ -64,20 +65,25 @@ int WATERMETER::getDebounce(void){

// FUNCTION - [initFilesys] - [Starts up the Meter File system and test--------]
const char* WATERMETER::initFilesys(void){
if (useSPIFFS){
if (!SPIFFS.begin(true)){
return "ERR";
}
if (useSDcard){
if(!SD.begin()){
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";}
}
//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";}
}
}
else {
return "NONE";
Expand All @@ -89,7 +95,7 @@ const char* WATERMETER::initFilesys(void){
// FUNCTION - [readFile] - [Opens and reads meter file into meter value--------]
bool WATERMETER::readFile(){
bool resetalarm=0;
File meterfile = SPIFFS.open("/meter.val","r");
File meterfile = SD.open("/meter/meter.val");
char test[20];
int i=0;
while (meterfile.available()) {
Expand All @@ -98,7 +104,7 @@ bool WATERMETER::readFile(){
i++;
if (i >=15) {
resetalarm=1;
SPIFFS.remove("/meter.val");
SD.remove("/meter/meter.val");
writeFile();
return resetalarm;
}
Expand Down Expand Up @@ -157,7 +163,7 @@ void WATERMETER::setMeter(double value){

// FUNCTION - [writeFile] - [Opens or creates meter file, writes value---------]
void WATERMETER::writeFile(){
File meterfile = SPIFFS.open("/meter.val","w");
File meterfile = SD.open("/meter/meter.val",FILE_WRITE);
char convert[10];
sprintf(convert, "%6.2f", meter);
meterfile.print(convert);
Expand Down
4 changes: 2 additions & 2 deletions src/Watermeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Most of these meters operate in the same manner, the difference
can be adjusted as parameters in the instance creation.
https://www.youtube.com/c/jordanrubin6502
2020 Jordan Rubin.
2020-2022 Jordan Rubin.
*/

// ensure this library description is only included once
Expand All @@ -15,7 +15,7 @@
class WATERMETER
{
////////////// user-accessible "public" interface
//WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],dbouncedelay,useSPIFFS,increment)
//WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],dbouncedelay,useSD,increment)
public:
WATERMETER(int,bool,char,long,bool,float);
bool updated(void);
Expand Down
37 changes: 25 additions & 12 deletions src/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,37 @@
Most of these meters operate in the same manner, the difference
can be adjusted as parameters in the instance creation.
https://www.youtube.com/c/jordanrubin6502
2020 Jordan Rubin.
2020 - 2022 Jordan Rubin.
*/
#include <Arduino.h> //required for platformio
#include <Watermeter.h>

//WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],metervalue)
// Using GPIO 15 [15]
// Using internal pullup resistors [true]
// This meter is designed as gallons [g]
// Debounce delay in ms [100]
// Use SPIFFS to autosave meter to filesys [true]
// The value of measurment added each time [.1] i.e. .1 gallons per pulse
/*
WATERMETER WATERMETER(SignalGPIOpin,useInternalPullupResistor,measure[g|l],metervalue)
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

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.

CS -> GPIO 5
MOSI -> GPIO 23
CLK -> GPIO 18
MISO -> GPIO 19
*/

WATERMETER thisMeter(15,true,'g',100,true,.1);

void setup() {
Serial.begin(38400);
Serial.println("\n\n***Water Meter Test Programme***");
delay(3000);

//This allows the setting of the meter in the object to a specified value
//That might match tat of an analogue gauge on the metre
thisMeter.setMeter(32.2);

/*
Print the result codes out directly as the init is called
Serial.println(thisMeter.initFilesys());
Expand All @@ -48,7 +56,12 @@ void setup() {
NEW- It found no Metre file and created a new one
RESET- The file had data errors and was re-initialized
NONE- The function was executed but the object has no param for SPIFFS support

This allows the setting of the meter in the object to a specified value
That might match tat of an analogue gauge on the metre
*/
// thisMeter.setMeter(32.2);

}

void loop() {
Expand Down

0 comments on commit 3163553

Please sign in to comment.