Pretty stable with https library BUT #ifdef'd out actual https usage is is very temperamental ATM.

This commit is contained in:
Ray Jones 2020-05-03 21:59:56 +10:00
parent c74f0a76eb
commit 3129a88cb9
10 changed files with 570 additions and 445 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 206 KiB

After

Width:  |  Height:  |  Size: 206 KiB

View File

@ -121,6 +121,9 @@
#include "Utility/GetLine.h"
#include "Utility/DemandManager.h"
#include "Protocol/BlueWireTask.h"
#if USE_TWDT == 1
#include "esp_task_wdt.h"
#endif
// SSID & password now stored in NV storage - these are still the default values.
//#define AP_SSID "Afterburner"
@ -133,6 +136,18 @@ const int FirmwareSubRevision = 0;
const int FirmwareMinorRevision = 6;
const char* FirmwareDate = "26 Apr 2020";
/*
* Macro to check the outputs of TWDT functions and trigger an abort if an
* incorrect code is returned.
*/
#define TWDT_TIMEOUT_S 15
#define CHECK_ERROR_CODE(returned, expected) ({ \
if(returned != expected){ \
printf("TWDT ERROR\n"); \
abort(); \
} \
})
#ifdef ESP32
#include "Bluetooth/BluetoothESP32.h"
@ -185,7 +200,9 @@ CGPIOalg GPIOalg;
CMQTTsetup MQTTmenu;
CSecuritySetup SecurityMenu;
TaskHandle_t handleWatchdogTask;
TaskHandle_t handleBlueWireTask;
extern TaskHandle_t handleSSLTask;
// these variables will persist over a soft reboot.
__NOINIT_ATTR float persistentRunTime;
@ -269,7 +286,7 @@ char taskMsg[BLUEWIRE_MSGQUEUESIZE];
void checkBlueWireEvents()
{
// collect and report any debug messages from the blue wire task
if(BlueWireMsgBuf && xQueueReceive(BlueWireMsgBuf, taskMsg, 0))
if(BlueWireMsgQueue && xQueueReceive(BlueWireMsgQueue, taskMsg, 0))
DebugPort.print(taskMsg);
// check for complted data exchange from the blue wire task
@ -299,8 +316,7 @@ void checkBlueWireEvents()
// trap being in state 0 with a heater error - cancel user on memory to avoid unexpected cyclic restarts
if(RTC_Store.getCyclicEngaged() && (BlueWireRxData.getRunState() == 0) && (BlueWireRxData.getErrState() > 1)) {
const char* msg = "Forcing cyclic cancel due to error induced shutdown\r\n";
xQueueSend(BlueWireMsgBuf, msg, 0);
DebugPort.println("Forcing cyclic cancel due to error induced shutdown");
// DebugPort.println("Forcing cyclic cancel due to error induced shutdown");
RTC_Store.setCyclicEngaged(false);
}
@ -320,7 +336,8 @@ void parentKeyHandler(uint8_t event)
void interruptReboot()
{
ets_printf("%ld Software watchdog reboot......\r\n", millis());
esp_restart();
abort();
// esp_restart();
}
unsigned long WatchdogTick = -1;
@ -499,16 +516,27 @@ void setup() {
setupGPIO();
#if USE_TWDT == 1
DebugPort.println("Initialize TWDT");
//Initialize or reinitialize TWDT
CHECK_ERROR_CODE(esp_task_wdt_init(TWDT_TIMEOUT_S, true), ESP_OK); // invoke panic if WDT kicks
//Subscribe this task to TWDT, then check if it is subscribed
CHECK_ERROR_CODE(esp_task_wdt_add(NULL), ESP_OK);
CHECK_ERROR_CODE(esp_task_wdt_status(NULL), ESP_OK);
#else
#if USE_SW_WATCHDOG == 1 && USE_JTAG == 0
// create a high priority FreeRTOS task as a watchdog monitor
TaskHandle_t wdTask;
xTaskCreate(WatchdogTask,
"watchdogTask",
2000,
1024,
NULL,
configMAX_PRIORITIES-1,
&wdTask);
&handleWatchdogTask);
#endif
#endif
JSONWatchdogTick = -1;
WatchdogTick = -1;
@ -566,13 +594,12 @@ void setup() {
TempSensor.getDS18B20().mapSensor(2, NVstore.getHeaterTuning().DS18B20probe[2].romCode);
// create task to run blue wire interface
TaskHandle_t Task;
xTaskCreate(BlueWireTask,
"BlueWireTask",
2000,
1600,
NULL,
TASK_PRIORITY_BLUEWIRE,
&Task);
TASK_PRIORITY_HEATERCOMMS,
&handleBlueWireTask);
@ -592,7 +619,7 @@ void loop()
{
// DebugPort.handle(); // keep telnet spy alive
feedWatchdog(); // feed watchdog
feedWatchdog(); // feed watchdog
doStreaming(); // do wifi, BT tx etc
@ -601,7 +628,7 @@ void loop()
if(checkTemperatureSensors())
ScreenManager.reqUpdate();
checkDisplayUpdate();
checkDisplayUpdate();
checkBlueWireEvents();
@ -612,40 +639,43 @@ void loop()
bool checkTemperatureSensors()
{
long tDelta = millis() - lastTemperatureTime;
if(tDelta > MIN_TEMPERATURE_INTERVAL) { // maintain a minimum holdoff period
lastTemperatureTime = millis(); // reset time to observe temeprature
if(tDelta > MIN_TEMPERATURE_INTERVAL) { // maintain a minimum holdoff period
lastTemperatureTime = millis(); // reset time to observe temeprature
if(bReportStack) {
int stackdepth = uxTaskGetStackHighWaterMark(NULL);
DebugPort.printf("Stack : %d\r\n", stackdepth);
}
if(bReportStack) {
DebugPort.println("Stack high water marks");
DebugPort.printf(" Arduino: %d\r\n", uxTaskGetStackHighWaterMark(NULL));
DebugPort.printf(" BlueWire: %d\r\n", uxTaskGetStackHighWaterMark(handleBlueWireTask));
DebugPort.printf(" Watchdog: %d\r\n", uxTaskGetStackHighWaterMark(handleWatchdogTask));
DebugPort.printf(" SSL loop: %d\r\n", uxTaskGetStackHighWaterMark(handleSSLTask));
}
TempSensor.readSensors();
TempSensor.readSensors();
float fTemperature;
if(TempSensor.getTemperature(0, fTemperature)) { // get Primary sensor temperature
if(DS18B20holdoff) {
DS18B20holdoff--;
DebugPort.printf("Skipped initial DS18B20 reading: %f\r\n", fTemperature);
} // first value upon sensor connect is bad
else {
// exponential mean to stabilse readings
FilteredSamples.AmbientTemp.update(fTemperature);
if(TempSensor.getTemperature(0, fTemperature)) { // get Primary sensor temperature
if(DS18B20holdoff) {
DS18B20holdoff--;
DebugPort.printf("Skipped initial DS18B20 reading: %f\r\n", fTemperature);
} // first value upon sensor connect is bad
else {
// exponential mean to stabilse readings
FilteredSamples.AmbientTemp.update(fTemperature);
manageCyclicMode();
manageFrostMode();
manageHumidity();
}
}
else {
DS18B20holdoff = 3;
FilteredSamples.AmbientTemp.reset(-100.0);
}
manageCyclicMode();
manageFrostMode();
manageHumidity();
}
}
else {
DS18B20holdoff = 3;
FilteredSamples.AmbientTemp.reset(-100.0);
}
TempSensor.startConvert(); // request a new conversion, will be ready by the time we loop back around
TempSensor.startConvert(); // request a new conversion, will be ready by the time we loop back around
return true;
}
}
return false;
}
@ -940,6 +970,10 @@ void checkDebugCommands()
else if(rxVal == 'h') {
getWebContent(true);
}
else if(rxVal == '!') {
DebugPort.println("Invoking deliberate halt loop");
for(;;); // force watchdog reboot
}
else if(rxVal == ('b' & 0x1f)) { // CTRL-B Tst Mode: bluetooth module route
bTestBTModule = !bTestBTModule;
Bluetooth.test(bTestBTModule ? 0xff : 0x00); // special enter or leave BT test commands
@ -1145,6 +1179,10 @@ void ShowOTAScreen(int percent, eOTAmodes updateType)
void feedWatchdog()
{
#if USE_TWDT == 1
CHECK_ERROR_CODE(esp_task_wdt_reset(), ESP_OK); //Comment this line to trigger a TWDT timeout
#else
#if USE_SW_WATCHDOG == 1 && USE_JTAG == 0
// BEST NOT USE WATCHDOG WITH JTAG DEBUG :-)
// DebugPort.printf("\r %ld Watchdog fed", millis());
@ -1153,6 +1191,7 @@ void feedWatchdog()
#else
WatchdogTick = -1;
#endif
#endif
}
void doJSONwatchdog(int topup)
@ -1193,10 +1232,12 @@ void doStreaming()
KeyPad.update(); // scan keypad - key presses handler via callback functions!
#if USE_JTAG == 0
#if DBG_FREERTOS == 0
//CANNOT USE GPIO WITH JTAG DEBUG
GPIOin.manage();
GPIOout.manage();
GPIOalg.manage();
#endif
#endif
Bluetooth.check(); // check for Bluetooth activity
@ -1320,11 +1361,16 @@ void setName(const char* name, int type)
NVstore.save();
NVstore.doSave(); // ensure NV storage
if(type == 0) {
DebugPort.println("Restarting ESP to invoke new network credentials");
DebugPort.handle();
delay(1000);
ESP.restart();
}
DebugPort.println("Restarting ESP to invoke new network credentials");
DebugPort.handle();
// initiate reboot
const char* content[2];
content[0] = "AP reconfig reset";
content[1] = "initiated";
ScreenManager.showRebootMsg(content, 1000);
// delay(1000);
// ESP.restart();
}
}
void setPassword(const char* name, int type)
@ -1344,11 +1390,16 @@ void setPassword(const char* name, int type)
NVstore.save();
NVstore.doSave(); // ensure NV storage
if(type == 0) {
DebugPort.println("Restarting ESP to invoke new network credentials");
DebugPort.handle();
delay(1000);
ESP.restart();
}
DebugPort.println("Restarting ESP to invoke new network credentials");
DebugPort.handle();
// initate reboot
const char* content[2];
content[0] = "AP password";
content[1] = "changed";
ScreenManager.showRebootMsg(content, 1000);
// delay(1000);
// ESP.restart();
}
}

View File

@ -63,7 +63,7 @@ extern bool bReportOEMresync;
extern bool bReportBlueWireData;
extern sFilteredData FilteredSamples;
QueueHandle_t BlueWireMsgBuf = NULL; // cannot use general Serial.print etc from this task without causing conflicts
QueueHandle_t BlueWireMsgQueue = NULL; // cannot use general Serial.print etc from this task without causing conflicts
QueueHandle_t BlueWireRxQueue = NULL; // queue to pass down heater receive data
QueueHandle_t BlueWireTxQueue = NULL; // queue to pass down heater transmit data
SemaphoreHandle_t BlueWireSemaphore = NULL; // flag to indicate completion of heater data exchange
@ -74,8 +74,8 @@ void DebugReportFrame(const char* hdr, const CProtocol& Frame, const char* ftr,
void initBlueWireSerial();
void pushDebugMsg(const char* msg) {
if(BlueWireMsgBuf)
xQueueSend(BlueWireMsgBuf, msg, 0);
if(BlueWireMsgQueue)
xQueueSend(BlueWireMsgQueue, msg, 0);
}
void BlueWireTask(void*) {
@ -89,7 +89,7 @@ void BlueWireTask(void*) {
static unsigned long moderator = 50;
bool isBTCmaster = false;
BlueWireMsgBuf = xQueueCreate(4, BLUEWIRE_MSGQUEUESIZE);
BlueWireMsgQueue = xQueueCreate(4, BLUEWIRE_MSGQUEUESIZE);
BlueWireRxQueue = xQueueCreate(4, BLUEWIRE_DATAQUEUESIZE);
BlueWireTxQueue = xQueueCreate(4, BLUEWIRE_DATAQUEUESIZE);
BlueWireSemaphore = xSemaphoreCreateBinary();
@ -380,7 +380,13 @@ void BlueWireTask(void*) {
break;
} // switch(CommState)
#if DBG_FREERTOS == 1
digitalWrite(GPIOout1_pin, LOW);
#endif
vTaskDelay(1);
#if DBG_FREERTOS == 1
digitalWrite(GPIOout1_pin, HIGH);
#endif
}
}

View File

@ -25,7 +25,7 @@
#include <FreeRTOS.h>
#include "../Utility/UtilClasses.h"
extern QueueHandle_t BlueWireMsgBuf; // cannot use general Serial.print etc from this task without causing conflicts
extern QueueHandle_t BlueWireMsgQueue; // cannot use general Serial.print etc from this task without causing conflicts
extern QueueHandle_t BlueWireRxQueue; // queue to pass down heater receive data
extern QueueHandle_t BlueWireTxQueue; // queue to pass down heater transmit data
extern SemaphoreHandle_t BlueWireSemaphore; // flag to indicate completion of heater data exchange

View File

@ -39,8 +39,10 @@
#include "TempSense.h"
#include "BoardDetect.h"
#include "DemandManager.h"
#include "../OLED/ScreenManager.h"
extern CModerator MQTTmoderator;
extern CScreenManager ScreenManager;
char defaultJSONstr[64];
CModerator JSONmoderator;
@ -581,6 +583,7 @@ void sendJSONtext(const char* jsonStr)
}
void doJSONreboot(uint16_t PIN)
{
char jsonStr[20];
@ -589,15 +592,19 @@ void doJSONreboot(uint16_t PIN)
validate = random(1, 10000);
char jsonStr[20];
sprintf(jsonStr, "{\"Reboot\":%04d}", validate);
sprintf(jsonStr, "{\"Reboot\":\"%04d\"}", validate);
sendJSONtext( jsonStr );
}
else if(PIN == validate) {
strcpy(jsonStr, "{\"Reboot\":-1}");
strcpy(jsonStr, "{\"Reboot\":\"-1\"}");
sendJSONtext( jsonStr );
delay(1000);
ESP.restart(); // reboot
// initate reboot
const char* content[2];
content[0] = "Remote reset";
content[1] = "initiated";
ScreenManager.showRebootMsg(content, 1000);
}
}

View File

@ -486,4 +486,14 @@ void DecodeCmd(const char* cmd, String& payload)
void setHoldoff(unsigned long& holdoff, unsigned long period)
{
holdoff = (millis() + period) | 1;
}
}
void hexDump(uint8_t* pData, int len, int wrap)
{
for(int i=0; i<len; ) {
for(int j=0; j<wrap && i<len; j++) {
DebugPort.printf("%02X ", pData[i++]);
}
DebugPort.println("");
}
}

View File

@ -193,5 +193,6 @@ enum eOTAmodes {
};
void setHoldoff(unsigned long& holdoff, unsigned long period);
void hexDump(uint8_t* pData, int len, int wrap=16);
#endif // __UTIL_CLASSES_H__

File diff suppressed because it is too large Load Diff

View File

@ -103,12 +103,18 @@ sBrowserUpload::begin(String& filename, int filesize)
}
int
sBrowserUpload::fragment(HTTPUpload& upload, HTTPResponse * res)
sBrowserUpload::fragment(HTTPUpload& upload, httpsserver::HTTPResponse * res)
{
if(isSPIFFSupload()) {
// SPIFFS update (may be error state)
if(DstFile.file) {
// file is open, add new fragment of data to file opened for writing
#ifdef REPORT_FILE_BYTES
DebugPort.println("Upload bytes...");
hexDump(upload.buf, upload.currentSize, 32);
#endif
if(DstFile.file.write(upload.buf, upload.currentSize) != upload.currentSize) { // Write the received bytes to the file
// ERROR! write operation failed if length does not match!
DstFile.file.close(); // close the file (fsUploadFile becomes NULL)

View File

@ -24,6 +24,7 @@
// Place Holder Config File - User config vars and defines to be moved here
#define USE_JTAG 0
#define DBG_FREERTOS 0
//////////////////////////////////////////////////////////////////////////////
// Configure bluetooth options
@ -46,6 +47,7 @@
#define USE_OTA 1
#define USE_WEBSERVER 1
#define USE_MQTT 1
#define USE_HTTPS 0
#define USE_PORTAL_TRIGGER_PIN 0