#include "FS.h"
#include "LittleFS.h"
#define FSrecordsize 49
#define debugmode true
#define FORMAT_LITTLEFS_IF_FAILED true
String filename="/eeprom.dat";
String tempfile="/tempdata.dat";
byte WiFi_result[8]; //the current Wi-Fi result
byte Inet_result[8]; //the current Internet Connection result
int WiFi_RSSI[8];
byte WiFi_old_result[8];
byte Inet_old_result[8];
int WiFi_old_RSSI[8];
byte eventTimeStamp[6];
byte power_mask = 7;
byte current_power_reading; //we store the opto values in 0,1,2 and a copy of them in 3,4,5
byte last_power_read;
#define SEND_EMAIL 30
#define SEND_GPRS 31
#define SEND_SMS 32
#define SEND_SERIAL 33
#define SEND_ALL_FAILED -5
#define SEND_EMULATION 34
#define SEND_NOTHING_TO_SEND 35
void OLEDwrite(String temp,byte x,byte y) {
Serial.println("ÖLED :"+temp);
}
void OLEDupdateEP(byte y) {
Serial.printf("EP= %02d",y);
}
void encodeEEPROM() {
bool filefail=0;
File f = LittleFS.open(filename, "ab");
if (!f) {
Serial.println("file open failed for appending");
filefail=true;
}
if (filefail==false) {
writeFile(f);
} else {
#if (debugmode)
Serial.println("Unable to openfile "+filename);
#endif
}
f.close();
}
void writeFile(File f) { //encodeEEPROM() {
int lp;
OLEDwrite("WRITE FILE ",0,0);
#if (debugmode)
Serial.println("");
Serial.println("W Result");
#endif
//we can write to the file
for (lp = 0; lp < 8;lp++) {
f.write(WiFi_result[lp]);
#if (debugmode)
Serial.printf("%02d %04d ",lp, WiFi_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("WO Result");
#endif
//we can write to the file
for (lp = 0; lp < 8;lp++) {
f.write(WiFi_old_result[lp]);
#if (debugmode)
Serial.printf("%02d %04d ",lp, WiFi_old_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("I Result");
#endif
//we can write to the file
for (lp = 0; lp < 8;lp++) {
f.write(Inet_result[lp]);
#if (debugmode)
Serial.printf("%02d %04d ",lp, Inet_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("IO Result");
#endif
//we can write to the file
for (lp = 0; lp < 8;lp++) {
f.write(Inet_old_result[lp]);
#if (debugmode)
Serial.printf("%02d %04d ",lp, Inet_old_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("RSSI Result");
#endif
//we can write to the file
for (lp = 0; lp < 8;lp++) {
f.write(WiFi_RSSI[lp]);
#if (debugmode)
Serial.printf("%02d %04d ",lp, WiFi_RSSI[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("TimeStamp");
#endif
//we can write to the file
for (lp = 0; lp < 6;lp++) {
f.write(eventTimeStamp[lp]);
#if (debugmode)
Serial.printf("%02d %04d ",lp, eventTimeStamp[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("VARS DATA OUT");
Serial.printf("%04d ",power_mask);
Serial.printf("%04d ",current_power_reading);
Serial.printf("%04d ",last_power_read);
Serial.println();
#endif
f.write(power_mask); //The power mask for the opto card
f.write(current_power_reading); //The power mask for the opto card
f.write(last_power_read); //The power mask for the opto card
}
void processEEPROM() {
//Look and see if there is data waiting in the EEPROM
//if there is, process it .
//
//Firstly, we have to preserve the state of the variables as the decode routine will over-write them
//then we decode each item at a time and pass it to the "decide and send" routine.
//if we get "send success" we process the next one.
//Before we leave, we must put the data back in the variables.
//
int lp; //general loop variable
byte dataByte;
bool filefail;
int records;
int ctr;
unsigned int recptr;
unsigned int filesize;
bool tempfileused;
byte failfilectr;
byte copy_WiFi_result[8]; //the current Wi-Fi result
byte copy_Inet_result[8]; //the current Internet Connection result
int copy_WiFi_RSSI[8];
byte copy_WiFi_old_result[8];
byte copy_Inet_old_result[8];
int copy_WiFi_old_RSSI[8];
byte copy_eventTimeStamp[6];
byte copy_power_mask;
byte copy_current_power_reading;
byte copy_last_power_read;
int message_success;
int message_fail;
//Preserve the current variables regardless of what we are doing
OLEDwrite("READ EEPROM",0,0);
for (lp = 0; lp < 8;lp++) {
copy_WiFi_result[lp]=WiFi_result[lp]; // Bytes 1-8
copy_WiFi_old_result[lp]=WiFi_old_result[lp]; // Bytes 9-16
copy_Inet_result[lp]=Inet_result[lp]; // Bytes 17-24
copy_Inet_old_result[lp]=Inet_old_result[lp]; // Bytes 25-32
copy_WiFi_RSSI[lp]=WiFi_RSSI[lp]; // Bytes 33-40
}
for (lp = 0; lp <=6;lp++) {
copy_eventTimeStamp[lp]=eventTimeStamp[lp]; // Bytes 41-45
}
copy_power_mask=power_mask; // Bytes 46
copy_current_power_reading=current_power_reading; // Bytes 47
copy_last_power_read=last_power_read; // Bytes 48
#if (debugmode)
Serial.print("Is there any EEPROM data to send? ");
#endif
ctr=0;
recptr=0;
tempfileused=false;
failfilectr=0;
if (LittleFS.exists(filename)) {
LittleFS.remove(tempfile); //remove the temporary file that holds any failed calls.
File f = LittleFS.open(filename, "rb"); //Open for reading
filesize=f.size();
Serial.print("Yes");
Serial.println();
#if (debugmode)
Serial.printf("File Size %05d - Pointer %05d",filesize, recptr);
Serial.println();
#endif
while (recptr<filesize) {
message_fail=false;
readRecord(f); //decode the current slot number
message_success=decide_method_and_send(true,true); //so find out how to send it.
if ((message_success!=SEND_EMAIL) && (message_success!=SEND_GPRS)) { //the message did not send
#if (debugmode)
Serial.printf("---------->>>> File Data did not send %04d",recptr);
Serial.println();
tempfileused=true;
File failfile = LittleFS.open(tempfile, "ab"); //Open for append
writeFile(failfile);
failfilectr++;
failfile.close();
#endif
message_fail=true;
} else {
//the message was sent
#if (debugmode)
Serial.printf("Send Success %04d",recptr);
Serial.println();
#endif
}
ctr++;
recptr=ctr*FSrecordsize;
f.seek(ctr*FSrecordsize,SeekSet);
}
f.close();
}
OLEDupdateEP(failfilectr);
//Reinstate the variables before we leave
for (lp = 0; lp < 8;lp++) {
WiFi_result[lp]=copy_WiFi_result[lp];
WiFi_old_result[lp]=copy_WiFi_old_result[lp];
Inet_result[lp]=copy_Inet_result[lp];
Inet_old_result[lp]=copy_Inet_old_result[lp];
WiFi_RSSI[lp]=copy_WiFi_RSSI[lp];
}
for (lp = 0; lp <=6;lp++) {
eventTimeStamp[lp]=copy_eventTimeStamp[lp];
}
power_mask=copy_power_mask;
current_power_reading=copy_current_power_reading;
last_power_read=copy_last_power_read;
LittleFS.remove(filename); //we are finished with the file, so delete it.
if (tempfileused) {
LittleFS.rename(tempfile,filename); //if we have some that could not send, change the name.
}
}
void readRecord(File f) {
int lp;
int ctr;
byte savedata[33];
byte temp;
int read_addr;
int addr;
char rslt[1];
#if (debugmode)
Serial.printf("reading record %02d ",f.position()/FSrecordsize);
Serial.println();
// Serial.println("First Read Address " + String((whichslot*33)+2));
#endif
#if (debugmode)
Serial.println("W Read");
#endif
for (lp = 0; lp < 8;lp++) {
f.readBytes(rslt,1);
WiFi_result[lp]=rslt[0];
#if (debugmode)
Serial.printf("%02d %04d ",lp, WiFi_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("WO Read");
#endif
for (lp = 0; lp < 8;lp++) {
f.readBytes(rslt,1);
WiFi_old_result[lp]=rslt[0];
#if (debugmode)
Serial.printf("%02d %04d ",lp, WiFi_old_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("I Read");
#endif
for (lp = 0; lp < 8;lp++) {
f.readBytes(rslt,1);
Inet_result[lp]=rslt[0];
#if (debugmode)
Serial.printf("%02d %04d ",lp, Inet_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("IO Read");
#endif
for (lp = 0; lp < 8;lp++) {
f.readBytes(rslt,1);
Inet_old_result[lp]=rslt[0];
#if (debugmode)
Serial.printf("%02d %04d ",lp, Inet_old_result[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("RSSI Read");
#endif
for (lp = 0; lp < 8;lp++) {
f.readBytes(rslt,1);
WiFi_RSSI[lp]=rslt[0];
#if (debugmode)
Serial.printf("%02d %04d ",lp, WiFi_RSSI[lp]);
#endif
}
#if (debugmode)
Serial.println("");
Serial.println("Timestamp Read");
#endif
for (lp = 0; lp < 6;lp++) {
f.readBytes(rslt,1);
eventTimeStamp[lp]=rslt[0];
#if (debugmode)
Serial.printf("%02d %04d ",lp, eventTimeStamp[lp]);
#endif
}
f.readBytes(rslt,1);
power_mask=rslt[0];
f.readBytes(rslt,1);
current_power_reading=rslt[0];
f.readBytes(rslt,1);
last_power_read=rslt[0];
#if (debugmode)
Serial.printf("VARS - %04d ",power_mask);
Serial.printf("VARS - %04d ",current_power_reading);
Serial.printf("VARS - %04d ",last_power_read);
#endif
#if (debugmode)
Serial.println("");
#endif
}
byte decide_method_and_send(int x, int y) {
//do nothing
if (random(0,10000)>9500) {
return 999;
} else {
return SEND_EMAIL;
}
}
void createRandom(int startpoint) {
int lp;
for (lp = 0; lp < 8;lp++) {
WiFi_result[lp]=startpoint;
WiFi_old_result[lp]=9-startpoint;
Inet_result[lp]=-(lp>4);
Inet_old_result[lp]=!Inet_result[lp];
WiFi_RSSI[lp]=startpoint*10;
}
for (lp = 0; lp <=6;lp++) {
eventTimeStamp[lp]=lp+startpoint;
}
power_mask=200+startpoint;
current_power_reading=201+startpoint;
last_power_read=202+startpoint;
}
void setup(){
int llp;
int rnum;
Serial.begin(115200);
randomSeed(digitalRead(0));
if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
Serial.println("LittleFS Mount Failed");
return;
}
rnum=random(1,40);
Serial.printf("Writing %02d Records",rnum);
for (llp=0;llp<rnum;llp++) {
createRandom(llp);
encodeEEPROM();
}
Serial.println("Done - wait");
delay(1000);
processEEPROM();
Serial.println("--------------------------------------------------------------------------");
processEEPROM();
Serial.println("Finished");
}
void loop(){
}