mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-08 13:10:29 +00:00
cleanup cleanup
everybody everywhere
This commit is contained in:
parent
cb6ad63f39
commit
762ef6b0ed
@ -65,8 +65,6 @@ void beginFDRS()
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void handleCommands()
|
||||
{
|
||||
switch (theCmd.cmd)
|
||||
@ -87,27 +85,16 @@ void loopFDRS()
|
||||
{
|
||||
handleCommands();
|
||||
#if defined(USE_SD_LOG) || defined(USE_FS_LOG)
|
||||
if ((millis() - timeLOGBUF) >= LOGBUF_DELAY)
|
||||
{
|
||||
timeLOGBUF = millis();
|
||||
if (logBufferPos > 0)
|
||||
releaseLogBuffer();
|
||||
}
|
||||
handleLogger();
|
||||
#endif
|
||||
|
||||
while (UART_IF.available() || Serial.available())
|
||||
{
|
||||
getSerial();
|
||||
}
|
||||
handleSerial();
|
||||
#ifdef USE_LORA
|
||||
handleLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect_mqtt(1, true);
|
||||
}
|
||||
client.loop(); // for recieving incoming messages and maintaining connection
|
||||
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
handleMQTT();
|
||||
#endif
|
||||
|
||||
if (newData != event_clear)
|
||||
{
|
||||
switch (newData)
|
||||
|
@ -7,25 +7,35 @@
|
||||
#ifdef USE_FS_LOG
|
||||
#include <LittleFS.h>
|
||||
#endif
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
#if defined(USE_SD_LOG) || defined(USE_FS_LOG)
|
||||
#include <time.h>
|
||||
#endif
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
#if defined(USE_SD_LOG) || defined(USE_FS_LOG)
|
||||
char logBuffer[512];
|
||||
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
|
||||
uint32_t timeLOGBUF = 0;
|
||||
time_t last_mqtt_success = 0;
|
||||
time_t last_log_write = 0;
|
||||
void handleLogger()
|
||||
{
|
||||
if ((millis() - timeLOGBUF) >= LOGBUF_DELAY)
|
||||
{
|
||||
timeLOGBUF = millis();
|
||||
if (logBufferPos > 0)
|
||||
releaseLogBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
#if defined(USE_SD_LOG) || defined(USE_FS_LOG)
|
||||
void releaseLogBuffer()
|
||||
{
|
||||
#ifdef USE_SD_LOG
|
||||
DBG("Releasing Log buffer to SD");
|
||||
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
|
||||
if((logfile.size()/1024.0) < SD_MAX_FILESIZE){
|
||||
if ((logfile.size() / 1024.0) < SD_MAX_FILESIZE)
|
||||
{
|
||||
logfile.print(logBuffer);
|
||||
}
|
||||
logfile.close();
|
||||
@ -33,7 +43,8 @@ void releaseLogBuffer()
|
||||
#ifdef USE_FS_LOG
|
||||
DBG("Releasing Log buffer to internal flash.");
|
||||
File logfile = LittleFS.open(FS_FILENAME, "a");
|
||||
if((logfile.size()/1024.0) < FS_MAX_FILESIZE){
|
||||
if ((logfile.size() / 1024.0) < FS_MAX_FILESIZE)
|
||||
{
|
||||
logfile.print(logBuffer);
|
||||
}
|
||||
logfile.close();
|
||||
@ -43,18 +54,20 @@ void releaseLogBuffer()
|
||||
}
|
||||
#endif // USE_XX_LOG
|
||||
|
||||
uint16_t stringCrc(const char input[]){
|
||||
uint16_t stringCrc(const char input[])
|
||||
{
|
||||
uint16_t calcCRC = 0x0000;
|
||||
|
||||
for(unsigned int i = 0; i < strlen(input); i++) {
|
||||
calcCRC = crc16_update(calcCRC,input[i]);
|
||||
for (unsigned int i = 0; i < strlen(input); i++)
|
||||
{
|
||||
calcCRC = crc16_update(calcCRC, input[i]);
|
||||
}
|
||||
return calcCRC;
|
||||
}
|
||||
|
||||
void sendLog()
|
||||
{
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
#if defined(USE_SD_LOG) || defined(USE_FS_LOG)
|
||||
DBG("Logging to buffer");
|
||||
for (int i = 0; i < ln; i++)
|
||||
{
|
||||
@ -67,86 +80,110 @@ void sendLog()
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
outgoingString = outgoingString + " " + stringCrc(outgoingString.c_str()) + "\r\n";
|
||||
if (logBufferPos+outgoingString.length() >= (sizeof(logBuffer)/sizeof(char))) // if buffer would overflow, release first
|
||||
if (logBufferPos + outgoingString.length() >= (sizeof(logBuffer) / sizeof(char))) // if buffer would overflow, release first
|
||||
{
|
||||
releaseLogBuffer();
|
||||
}
|
||||
memcpy(&logBuffer[logBufferPos], outgoingString.c_str(), outgoingString.length()); //append line to buffer
|
||||
logBufferPos+=outgoingString.length();
|
||||
memcpy(&logBuffer[logBufferPos], outgoingString.c_str(), outgoingString.length()); // append line to buffer
|
||||
logBufferPos += outgoingString.length();
|
||||
}
|
||||
time(&last_log_write);
|
||||
#endif //USE_xx_LOG
|
||||
#endif // USE_xx_LOG
|
||||
}
|
||||
|
||||
void resendLog(){
|
||||
#ifdef USE_SD_LOG
|
||||
void resendLog()
|
||||
{
|
||||
#ifdef USE_SD_LOG
|
||||
DBG("Resending logged values from SD card.");
|
||||
File logfile = SD.open(SD_FILENAME, FILE_READ);
|
||||
while(1){
|
||||
while (1)
|
||||
{
|
||||
String line = logfile.readStringUntil('\n');
|
||||
if (line.length() > 0){ // if line contains something
|
||||
if (!client.publish(TOPIC_DATA_BACKLOG, line.c_str())) {
|
||||
if (line.length() > 0)
|
||||
{ // if line contains something
|
||||
if (!client.publish(TOPIC_DATA_BACKLOG, line.c_str()))
|
||||
{
|
||||
break;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
time(&last_mqtt_success);
|
||||
}
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
logfile.close();
|
||||
SD.remove(SD_FILENAME); // if all values are sent
|
||||
break;
|
||||
}
|
||||
}
|
||||
DBG(" Done");
|
||||
#endif
|
||||
#ifdef USE_FS_LOG
|
||||
#endif
|
||||
#ifdef USE_FS_LOG
|
||||
DBG("Resending logged values from internal flash.");
|
||||
File logfile = LittleFS.open(FS_FILENAME, "r");
|
||||
while(1){
|
||||
while (1)
|
||||
{
|
||||
String line = logfile.readStringUntil('\n');
|
||||
if (line.length() > 0){ // if line contains something
|
||||
if (line.length() > 0)
|
||||
{ // if line contains something
|
||||
uint16_t readCrc;
|
||||
char data[line.length()];
|
||||
sscanf(line.c_str(),"%s %hd",data,&readCrc);
|
||||
if(stringCrc(data)!=readCrc){continue;} // if CRCs don't match, skip the line
|
||||
if (!client.publish(TOPIC_DATA_BACKLOG, line.c_str())) {
|
||||
sscanf(line.c_str(), "%s %hd", data, &readCrc);
|
||||
if (stringCrc(data) != readCrc)
|
||||
{
|
||||
continue;
|
||||
} // if CRCs don't match, skip the line
|
||||
if (!client.publish(TOPIC_DATA_BACKLOG, line.c_str()))
|
||||
{
|
||||
break;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
time(&last_mqtt_success);
|
||||
}
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
logfile.close();
|
||||
LittleFS.remove(FS_FILENAME); // if all values are sent
|
||||
break;
|
||||
}
|
||||
}
|
||||
DBG(" Done");
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void begin_SD() {
|
||||
void begin_SD()
|
||||
{
|
||||
#ifdef USE_SD_LOG
|
||||
DBG("Initializing SD card...");
|
||||
#ifdef ESP32
|
||||
SPI.begin(SCK, MISO, MOSI);
|
||||
#endif
|
||||
if (!SD.begin(SD_SS)) {
|
||||
if (!SD.begin(SD_SS))
|
||||
{
|
||||
DBG(" Initialization failed!");
|
||||
while (1);
|
||||
} else {
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(" SD initialized.");
|
||||
}
|
||||
#endif //USE_SD_LOG
|
||||
#endif // USE_SD_LOG
|
||||
}
|
||||
|
||||
void begin_FS() {
|
||||
void begin_FS()
|
||||
{
|
||||
#ifdef USE_FS_LOG
|
||||
DBG("Initializing LittleFS...");
|
||||
|
||||
if (!LittleFS.begin())
|
||||
{
|
||||
DBG(" initialization failed");
|
||||
while (1);
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -518,9 +518,9 @@ void asyncReleaseLoRa(bool first_run)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_LORA
|
||||
void handleLoRa()
|
||||
{
|
||||
#ifdef USE_LORA
|
||||
if (operationDone) // the interrupt was triggered
|
||||
{
|
||||
enableInterrupt = false;
|
||||
@ -547,7 +547,7 @@ void handleLoRa()
|
||||
enableInterrupt = true;
|
||||
}
|
||||
}
|
||||
#endif // USE_LORA
|
||||
}
|
||||
#endif // USE_LORA
|
||||
|
||||
|
||||
|
@ -55,8 +55,6 @@ const char *mqtt_user = NULL;
|
||||
const char *mqtt_pass = NULL;
|
||||
#endif // FDRS_MQTT_AUTH
|
||||
|
||||
|
||||
|
||||
void reconnect_mqtt(short int attempts, bool silent)
|
||||
{
|
||||
if (!silent)
|
||||
@ -97,6 +95,16 @@ void reconnect_mqtt(int attempts)
|
||||
reconnect_mqtt(attempts, false);
|
||||
}
|
||||
|
||||
// Handles MQTT in loop()
|
||||
void handleMQTT()
|
||||
{
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect_mqtt(1, true);
|
||||
}
|
||||
client.loop(); // for recieving incoming messages and maintaining connection
|
||||
}
|
||||
|
||||
void mqtt_callback(char *topic, byte *message, unsigned int length)
|
||||
{
|
||||
String incomingString;
|
||||
@ -129,13 +137,14 @@ void mqtt_callback(char *topic, byte *message, unsigned int length)
|
||||
}
|
||||
}
|
||||
|
||||
void begin_mqtt(){
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect_mqtt(5);
|
||||
}
|
||||
client.setCallback(mqtt_callback);
|
||||
void begin_mqtt()
|
||||
{
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect_mqtt(5);
|
||||
}
|
||||
client.setCallback(mqtt_callback);
|
||||
}
|
||||
|
||||
void mqtt_publish(const char *payload)
|
||||
|
@ -53,3 +53,9 @@ void sendSerial() {
|
||||
#endif
|
||||
|
||||
}
|
||||
void handleSerial(){
|
||||
while (UART_IF.available() || Serial.available())
|
||||
{
|
||||
getSerial();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user