Code cleanup

This commit is contained in:
fvanroie 2020-11-22 16:59:35 +01:00
parent aa649e7c68
commit 8e150fca3a
23 changed files with 146 additions and 130 deletions

View File

@ -458,13 +458,6 @@ typedef void* lv_font_user_data_t;
#endif /*LV_SPRINTF_CUSTOM*/ #endif /*LV_SPRINTF_CUSTOM*/
//*********************** 7.0 BETA defines, Remove when final version is ready !!
#define LV_USE_TA 1 // TEMPORARY FIX for 7.0 beta
#define LV_USE_DDLIST 1 // TEMPORARY FIX for 7.0 beta
# define LV_DSLIST_DEF_ANIM_TIME 200
#define LV_USE_BTNM 1 // TEMPORARY FIX for 7.0 beta
/*=================== /*===================
* LV_OBJ SETTINGS * LV_OBJ SETTINGS
*==================*/ *==================*/

View File

@ -49,6 +49,8 @@ enum hasp_event_t { // even = released, odd = pressed
*/ */
void haspSetup(); void haspSetup();
void haspLoop(void); void haspLoop(void);
void haspReconnect(void);
void haspDisconnect(void);
lv_obj_t * get_page_obj(uint8_t pageid); lv_obj_t * get_page_obj(uint8_t pageid);
bool get_page_id(lv_obj_t * obj, uint8_t * pageid); bool get_page_id(lv_obj_t * obj, uint8_t * pageid);
@ -63,10 +65,8 @@ void haspBackground(uint16_t pageid, uint16_t imageid);
void hasp_set_group_objects(uint8_t groupid, uint8_t eventid, lv_obj_t * src_obj); void hasp_set_group_objects(uint8_t groupid, uint8_t eventid, lv_obj_t * src_obj);
void haspNewObject(const JsonObject & config, uint8_t & saved_page_id); // void haspNewObject(const JsonObject & config, uint8_t & saved_page_id);
void haspReconnect(void);
void haspDisconnect(void);
void haspWakeUp(void); void haspWakeUp(void);
void haspProgressVal(uint8_t val); void haspProgressVal(uint8_t val);

View File

@ -370,11 +370,11 @@ void configSetup()
} }
//#if HASP_USE_SPIFFS > 0 //#if HASP_USE_SPIFFS > 0
Log.notice(TAG_DEBG, F("Loading debug settings")); Log.trace(TAG_DEBG, F("Loading debug settings"));
debugSetConfig(settings[F("debug")]); debugSetConfig(settings[F("debug")]);
Log.notice(TAG_GPIO, F("Loading GUI settings")); Log.trace(TAG_GPIO, F("Loading GUI settings"));
guiSetConfig(settings[F("gui")]); guiSetConfig(settings[F("gui")]);
Log.notice(TAG_HASP, F("Loading HASP settings")); Log.trace(TAG_HASP, F("Loading HASP settings"));
haspSetConfig(settings[F("hasp")]); haspSetConfig(settings[F("hasp")]);
// otaGetConfig(settings[F("ota")]); // otaGetConfig(settings[F("ota")]);
@ -458,7 +458,7 @@ void configOutput(const JsonObject & settings, uint8_t tag)
Log.verbose(tag, output.c_str()); Log.verbose(tag, output.c_str());
} }
bool configClear() bool configClearEeprom()
{ {
#if defined(STM32F4xx) #if defined(STM32F4xx)
Log.notice(TAG_CONF, F("Clearing EEPROM")); Log.notice(TAG_CONF, F("Clearing EEPROM"));

View File

@ -7,6 +7,27 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "hasp_debug.h" // for TAG_CONF #include "hasp_debug.h" // for TAG_CONF
/* ===== Default Event Processors ===== */
void configSetup(void);
void configLoop(void);
void configEverySecond(void);
void configStart(void);
void configStop(void);
/* ===== Special Event Processors ===== */
void configWriteConfig(void);
void configOutput(const JsonObject & settings, uint8_t tag = TAG_CONF);
bool configClearEeprom(void);
/* ===== Getter and Setter Functions ===== */
bool configSet(int8_t & value, const JsonVariant & setting, const __FlashStringHelper * fstr_name);
bool configSet(uint8_t & value, const JsonVariant & setting, const __FlashStringHelper * fstr_name);
bool configSet(uint16_t & value, const JsonVariant & setting, const __FlashStringHelper * fstr_name);
/* ===== Read/Write Configuration ===== */
void configSetConfig(JsonObject & settings);
void configGetConfig(JsonDocument & settings);
/* json keys used in the configfile */ /* json keys used in the configfile */
const char F_CONFIG_STARTPAGE[] PROGMEM = "startpage"; const char F_CONFIG_STARTPAGE[] PROGMEM = "startpage";
const char F_CONFIG_STARTDIM[] PROGMEM = "startdim"; const char F_CONFIG_STARTDIM[] PROGMEM = "startdim";
@ -37,17 +58,4 @@ const char F_GPIO_CONFIG[] PROGMEM = "config";
const char HASP_CONFIG_FILE[] PROGMEM = "/config.json"; const char HASP_CONFIG_FILE[] PROGMEM = "/config.json";
void configSetup();
void configStop(void);
void configSetConfig(JsonObject & settings);
void configGetConfig(JsonDocument & settings);
void configWriteConfig();
void configOutput(const JsonObject & settings, uint8_t tag = TAG_CONF);
bool configSet(int8_t & value, const JsonVariant & setting, const __FlashStringHelper * fstr_name);
bool configSet(uint8_t & value, const JsonVariant & setting, const __FlashStringHelper * fstr_name);
bool configSet(uint16_t & value, const JsonVariant & setting, const __FlashStringHelper * fstr_name);
bool configClear();
#endif #endif

View File

@ -109,7 +109,7 @@ unsigned long debugLastMillis = 0;
uint16_t debugTelePeriod = 300; uint16_t debugTelePeriod = 300;
// Send the HASP header and version to the output device specified // Send the HASP header and version to the output device specified
void debugHaspHeader(Print * output) void debugPrintHaspHeader(Print * output)
{ {
if(debugAnsiCodes) output->println(TERM_COLOR_YELLOW); if(debugAnsiCodes) output->println(TERM_COLOR_YELLOW);
output->print(F("" output->print(F(""
@ -559,19 +559,19 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput); debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput);
debugPrintTimestamp(level, _logOutput); debugPrintTimestamp(level, _logOutput);
debugPrintHaspMemory(level, _logOutput); debugPrintHaspMemory(level, _logOutput);
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
debugPrintLvglMemory(level, _logOutput); debugPrintLvglMemory(level, _logOutput);
#endif #endif
switch(tag) {
case TAG_MQTT_PUB: if(tag == TAG_MQTT_PUB && level == LOG_LEVEL_NOTICE) {
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput); debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
break; } else if(tag == TAG_MQTT_RCV && level == LOG_LEVEL_NOTICE) {
case TAG_MQTT_RCV: debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput); } else {
break; debugPrintPriority(level, _logOutput);
default:
debugPrintPriority(level, _logOutput);
} }
_logOutput->print(F(" ")); _logOutput->print(F(" "));
debugPrintTag(tag, _logOutput); debugPrintTag(tag, _logOutput);
_logOutput->print(F(": ")); _logOutput->print(F(": "));
@ -627,7 +627,7 @@ void debugPreSetup(JsonObject settings)
// Print Header // Print Header
Serial.println(); Serial.println();
debugHaspHeader(&Serial); debugPrintHaspHeader(&Serial);
// Serial.println(debugHaspHeader()); // Serial.println(debugHaspHeader());
// Serial.println(); // Serial.println();
Serial.flush(); Serial.flush();
@ -637,15 +637,18 @@ void debugPreSetup(JsonObject settings)
} }
#if LV_USE_LOG != 0 #if LV_USE_LOG != 0
static uint32_t lastDbgLine; void debugLvglLogEvent(lv_log_level_t level, const char * file, uint32_t line, const char * funcname,
static uint16_t lastDbgFree; const char * descr)
void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr)
{ {
/* used for duplicate detection */
static uint32_t lastDbgLine;
static uint16_t lastDbgFreeMem;
lv_mem_monitor_t mem_mon; lv_mem_monitor_t mem_mon;
lv_mem_monitor(&mem_mon); lv_mem_monitor(&mem_mon);
/* Reduce the number of reepeated debug message */ /* Reduce the number of reepeated debug message */
if(line != lastDbgLine || mem_mon.free_biggest_size != lastDbgFree) { if(line != lastDbgLine || mem_mon.free_biggest_size != lastDbgFreeMem) {
switch(level) { switch(level) {
case LV_LOG_LEVEL_TRACE: case LV_LOG_LEVEL_TRACE:
Log.verbose(TAG_LVGL, descr); Log.verbose(TAG_LVGL, descr);
@ -659,8 +662,8 @@ void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const cha
default: default:
Log.notice(TAG_LVGL, descr); Log.notice(TAG_LVGL, descr);
} }
lastDbgLine = line; lastDbgLine = line;
lastDbgFree = mem_mon.free_biggest_size; lastDbgFreeMem = mem_mon.free_biggest_size;
} }
} }
#endif #endif

View File

@ -7,8 +7,7 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "lvgl.h" #include "lvgl.h"
void debugHaspHeader(Print * output); /* ===== Default Event Processors ===== */
void debugPreSetup(JsonObject settings); void debugPreSetup(JsonObject settings);
void debugSetup(); void debugSetup();
void debugLoop(void); void debugLoop(void);
@ -16,21 +15,20 @@ void debugEverySecond(void);
void debugStart(void); void debugStart(void);
void debugStop(void); void debugStop(void);
/* ===== Special Event Processors ===== */
void debugLvglLogEvent(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr);
void debugPrintHaspHeader(Print * output);
void debugStartSyslog(void); void debugStartSyslog(void);
void debugStopSyslog(void); void debugStopSyslog(void);
// void syslogSend(uint8_t log, const char * debugText);
void serialPrintln(String & debugText, uint8_t level); /* ===== Read/Write Configuration ===== */
void serialPrintln(const char * debugText, uint8_t level);
void syslogSend(uint8_t log, const char * debugText);
bool debugGetConfig(const JsonObject & settings); bool debugGetConfig(const JsonObject & settings);
bool debugSetConfig(const JsonObject & settings); bool debugSetConfig(const JsonObject & settings);
// void debugPrintPrefix(int level, Print * _logOutput); // void debugPrintPrefix(int level, Print * _logOutput);
// void debugPrintSuffix(int level, Print * _logOutput); // void debugPrintSuffix(int level, Print * _logOutput);
// void debugSendOuput(const char * buffer); // void debugSendOuput(const char * buffer);
void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr);
enum { enum {
TAG_MAIN = 0, TAG_MAIN = 0,

View File

@ -8,31 +8,31 @@
#define LOG_CMND_CTR "CMND: " #define LOG_CMND_CTR "CMND: "
bool is_true(const char * s); /* ===== Default Event Processors ===== */
void dispatchSetup(void); void dispatchSetup(void);
void dispatchLoop(void); void dispatchLoop(void);
void dispatchEverySecond(void);
void dispatchStart(void);
void dispatchStop(void);
// void dispatchCommand(const char * topic, const char * payload); // intenal /* ===== Special Event Processors ===== */
void dispatchConfig(const char * topic, const char * payload); void dispatchConfig(const char * topic, const char * payload);
void dispatchTopicPayload(const char * topic, const char * payload); void dispatchTopicPayload(const char * topic, const char * payload);
void dispatchTextLine(const char * cmnd); void dispatchTextLine(const char * cmnd);
// void dispatchParseJson(char * strPayload);
// void dispatchParseJsonl(char * strPayload);
void dispatchParseJsonl(Stream & stream); void dispatchParseJsonl(Stream & stream);
void dispatchClearPage(const char * page);
void dispatchPage(const char * page); void dispatchPage(const char * page);
void dispatchPageNext(); void dispatchPageNext();
void dispatchPagePrev(); void dispatchPagePrev();
void dispatchClearPage(const char * page);
void dispatchDim(const char * level); void dispatchDim(const char * level);
void dispatchBacklight(const char * payload); void dispatchBacklight(const char * payload);
void dispatchWebUpdate(const char * espOtaUrl); void dispatchWebUpdate(const char * espOtaUrl);
void dispatch_output_idle_state(const char * state);
void dispatchReboot(bool saveConfig); void dispatchReboot(bool saveConfig);
void dispatch_output_idle_state(const char * state);
void dispatch_output_statusupdate(void); void dispatch_output_statusupdate(void);
void dispatch_button(uint8_t id, const char * event); void dispatch_button(uint8_t id, const char * event);
@ -41,9 +41,15 @@ void dispatch_send_object_event(uint8_t pageid, uint8_t objid, uint8_t eventid);
void dispatch_send_group_event(uint8_t groupid, uint8_t eventid, bool update_hasp); void dispatch_send_group_event(uint8_t groupid, uint8_t eventid, bool update_hasp);
bool dispatch_get_event_state(uint8_t eventid); bool dispatch_get_event_state(uint8_t eventid);
bool is_true(const char * s);
void IRAM_ATTR dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * attribute, void IRAM_ATTR dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * attribute,
const char * data); const char * data);
/* ===== Getter and Setter Functions ===== */
/* ===== Read/Write Configuration ===== */
/* ===== Structs and Constants ===== */
struct haspCommand_t struct haspCommand_t
{ {
void (*func)(const char *); void (*func)(const char *);

View File

@ -95,9 +95,13 @@ void filesystemList()
#endif #endif
} }
bool filesystemSetup() bool filesystemSetup(void)
{ {
// no SPIFFS settings, as settings depend on SPIFFS // no SPIFFS settings, as settings depend on SPIFFS
// no Logging, because it depends on the configuration file
// Logging is defered until debugging has started
// FS success or failure is printed at that time !
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
@ -105,10 +109,10 @@ bool filesystemSetup()
#else #else
if(!HASP_FS.begin(true)) { if(!HASP_FS.begin(true)) {
#endif #endif
Log.error(TAG_FILE, F("SPI flash init failed. Unable to mount FS.")); // Log.error(TAG_FILE, F("SPI flash init failed. Unable to mount FS."));
return false; return false;
} else { } else {
Log.trace(TAG_FILE, F("SPI Flash FS mounted")); // Log.trace(TAG_FILE, F("SPI Flash FS mounted"));
return true; return true;
} }
#endif #endif

View File

@ -21,7 +21,7 @@ static AceButton * button[HASP_NUM_INPUTS];
// An array of button pins, led pins, and the led states. Cannot be const // An array of button pins, led pins, and the led states. Cannot be const
// because ledState is mutable. // because ledState is mutable.
hasp_gpio_config_t gpioConfig[HASP_NUM_GPIO_CONFIG] = { hasp_gpio_config_t gpioConfig[HASP_NUM_GPIO_CONFIG] = {
{2, 8, INPUT, LOW}, {3, 9, OUTPUT, LOW}, {4, 10, INPUT, HIGH}, {5, 11, OUTPUT, LOW}, {6, 12, INPUT, LOW}, // {2, 8, INPUT, LOW}, {3, 9, OUTPUT, LOW}, {4, 10, INPUT, HIGH}, {5, 11, OUTPUT, LOW}, {6, 12, INPUT, LOW},
}; };
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)

View File

@ -15,7 +15,7 @@
#include "tft_espi_drv.h" #include "tft_espi_drv.h"
#endif #endif
// Touch Driver // Select Touch Driver
//#include "indev/XPT2046_alt_drv.h" //#include "indev/XPT2046_alt_drv.h"
#include "indev/XPT2046.h" #include "indev/XPT2046.h"
@ -69,13 +69,14 @@ static uint16_t guiSleepTime2 = 120; // 1 second resolution
static uint8_t guiSleeping = HASP_SLEEP_OFF; static uint8_t guiSleeping = HASP_SLEEP_OFF;
static uint8_t guiTickPeriod = 20; static uint8_t guiTickPeriod = 20;
static uint8_t guiRotation = TFT_ROTATION; static uint8_t guiRotation = TFT_ROTATION;
#if ESP32 > 0 || ESP8266 > 0 // static TFT_eSPI tft; // = TFT_eSPI(); /* TFT instance */
static uint16_t calData[5] = {0, 65535, 0, 65535, 0};
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
static Ticker tick; /* timer for interrupt handler */ static Ticker tick; /* timer for interrupt handler */
#else #else
static Ticker tick(lv_tick_handler, LVGL_TICK_PERIOD); // guiTickPeriod); static Ticker tick(lv_tick_handler, LVGL_TICK_PERIOD); // guiTickPeriod);
#endif #endif
// static TFT_eSPI tft; // = TFT_eSPI(); /* TFT instance */
static uint16_t calData[5] = {0, 65535, 0, 65535, 0};
bool guiCheckSleep() bool guiCheckSleep()
{ {
@ -136,7 +137,6 @@ static void IRAM_ATTR my_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area,
/* Interrupt driven periodic handler */ /* Interrupt driven periodic handler */
static void ICACHE_RAM_ATTR lv_tick_handler(void) static void ICACHE_RAM_ATTR lv_tick_handler(void)
{ {
// Serial.print(".");
lv_tick_inc(LVGL_TICK_PERIOD); lv_tick_inc(LVGL_TICK_PERIOD);
} }
@ -562,7 +562,7 @@ void guiSetup()
#if LV_USE_LOG != 0 #if LV_USE_LOG != 0
Log.notice(TAG_LVGL, F("Registering lvgl logging handler")); Log.notice(TAG_LVGL, F("Registering lvgl logging handler"));
lv_log_register_print_cb(debugLvgl); /* register print function for debugging */ lv_log_register_print_cb(debugLvglLogEvent); /* register print function for debugging */
#endif #endif
/* Initialize the display driver */ /* Initialize the display driver */
@ -639,19 +639,23 @@ void guiSetup()
void IRAM_ATTR guiLoop() void IRAM_ATTR guiLoop()
{ {
lv_task_handler(); // process animations
#if defined(STM32F4xx) #if defined(STM32F4xx)
tick.update(); tick.update();
#endif #endif
lv_task_handler();
guiCheckSleep();
#if TOUCH_DRIVER == 1 #if TOUCH_DRIVER == 1
touch.loop(); touch.loop();
#endif #endif
} }
void guiEverySecond(void)
{
// check if we went to sleep, wake up is handled in the event handlers
guiCheckSleep();
}
void guiStart() void guiStart()
{ {
/*Initialize the graphics library's tick*/ /*Initialize the graphics library's tick*/

View File

@ -4,41 +4,33 @@
#ifndef HASP_GUI_H #ifndef HASP_GUI_H
#define HASP_GUI_H #define HASP_GUI_H
//#include "TFT_eSPI.h"
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "lvgl.h" #include "lvgl.h"
#define HASP_SLEEP_OFF 0 #define HASP_SLEEP_OFF 0
#define HASP_SLEEP_SHORT 1 #define HASP_SLEEP_SHORT 1
#define HASP_SLEEP_LONG 2 #define HASP_SLEEP_LONG 2
// #if defined(ARDUINO_ARCH_ESP8266) /* ===== Default Event Processors ===== */
// #include <ESP8266WebServer.h>
// void guiTakeScreenshot(ESP8266WebServer & client);
// #endif
// #if defined(ARDUINO_ARCH_ESP32)
// #include <WebServer.h>
// void guiTakeScreenshot(WebServer & client);
// #endif // ESP32
void guiTakeScreenshot();
void guiSetup(); void guiSetup();
void guiStart(void);
void guiLoop(void); void guiLoop(void);
void guiEverySecond(void);
void guiStart(void);
void guiStop(void); void guiStop(void);
/* ===== Special Event Processors ===== */
void guiCalibrate(); void guiCalibrate();
void guiTakeScreenshot(const char * pFileName); void guiTakeScreenshot(const char * pFileName); // to file
void guiTakeScreenshot(); // webclient
/* ===== Getter and Setter Functions ===== */
void guiSetDim(int8_t level); void guiSetDim(int8_t level);
int8_t guiGetDim(void); int8_t guiGetDim(void);
void guiSetBacklight(bool lighton); void guiSetBacklight(bool lighton);
bool guiGetBacklight(); bool guiGetBacklight();
bool guiCheckSleep(); bool guiCheckSleep();
/* ===== Read/Write Configuration ===== */
bool guiGetConfig(const JsonObject & settings); bool guiGetConfig(const JsonObject & settings);
bool guiSetConfig(const JsonObject & settings); bool guiSetConfig(const JsonObject & settings);

View File

@ -88,7 +88,7 @@ String esp32ResetReason(uint8_t cpuid)
} }
#endif #endif
void halRestart(void) void halRestartMcu(void)
{ {
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
ESP.restart(); ESP.restart();

View File

@ -6,7 +6,7 @@
#include <Arduino.h> #include <Arduino.h>
void halRestart(void); void halRestartMcu(void);
uint8_t halGetHeapFragmentation(void); uint8_t halGetHeapFragmentation(void);
String halGetResetInfo(void); String halGetResetInfo(void);
size_t halGetMaxFreeBlock(void); size_t halGetMaxFreeBlock(void);

View File

@ -130,7 +130,7 @@ bool httpIsAuthenticated(const __FlashStringHelper * fstr_page)
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
Log.notice(TAG_HTTP, F("Sending %S page to client connected from: %s"), fstr_page, Log.notice(TAG_HTTP, F("Sending %S page to client connected from: %s"), fstr_page,
webServer.client().remoteIP().toString().c_str()); webServer.client().remoteIP().toString().c_str());
#else #else
// Log.trace(TAG_HTTP,F("Sending %s page to client connected from: %s"), page, // Log.trace(TAG_HTTP,F("Sending %s page to client connected from: %s"), page,
// String(webServer.client().remoteIP()).c_str()); // String(webServer.client().remoteIP()).c_str());
@ -1642,7 +1642,7 @@ void httpHandleResetConfig()
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
if(resetConfirmed) { // User has confirmed, so reset everything if(resetConfirmed) { // User has confirmed, so reset everything
bool formatted = configClear(); bool formatted = configClearEeprom();
if(formatted) { if(formatted) {
httpMessage += F("<b>Resetting all saved settings and restarting device</b>"); httpMessage += F("<b>Resetting all saved settings and restarting device</b>");
} else { } else {
@ -1677,7 +1677,7 @@ void httpHandleResetConfig()
} }
} }
void webStart() void httpStart()
{ {
webServer.begin(); webServer.begin();
webServerStarted = true; webServerStarted = true;
@ -1688,7 +1688,7 @@ void webStart()
Log.trace(TAG_HTTP, F("Server started @ http://%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); Log.trace(TAG_HTTP, F("Server started @ http://%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
#else #else
Log.trace(TAG_HTTP, F("Server started @ http://%s"), Log.trace(TAG_HTTP, F("Server started @ http://%s"),
(WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str()));
#endif #endif
#else #else
IPAddress ip; IPAddress ip;
@ -1701,7 +1701,7 @@ void webStart()
#endif #endif
} }
void webStop() void httpStop()
{ {
webServer.stop(); webServer.stop();
webServerStarted = false; webServerStarted = false;
@ -1817,7 +1817,7 @@ void httpSetup()
webServer.collectHeaders(headerkeys, headerkeyssize); webServer.collectHeaders(headerkeys, headerkeyssize);
Log.trace(TAG_HTTP, F("Setup Complete")); Log.trace(TAG_HTTP, F("Setup Complete"));
webStart(); // webStart(); Wait for network connection
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1826,13 +1826,13 @@ void httpReconnect()
if(!httpEnable) return; if(!httpEnable) return;
if(webServerStarted) { if(webServerStarted) {
webStop(); httpStop();
} else } else
#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) #if HASP_USE_WIFI > 0 && !defined(STM32F4xx)
if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA) if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA)
#endif #endif
{ {
webStart(); httpStart();
} }
} }
@ -1845,7 +1845,7 @@ void httpLoop()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void httpEvery5Seconds() void httpEvery5Seconds()
{ {
if(httpEnable && !webServerStarted) httpReconnect(); // if(httpEnable && !webServerStarted) httpReconnect();
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -10,9 +10,11 @@
void httpSetup(); void httpSetup();
void httpLoop(void); void httpLoop(void);
void httpEvery5Seconds(void); void httpEvery5Seconds(void);
void httpReconnect(void); // void httpReconnect(void);
void httpStart(void);
void httpStop(void);
size_t httpClientWrite(const uint8_t *buf, size_t size); // Screenshot Write Data size_t httpClientWrite(const uint8_t * buf, size_t size); // Screenshot Write Data
bool httpGetConfig(const JsonObject & settings); bool httpGetConfig(const JsonObject & settings);
bool httpSetConfig(const JsonObject & settings); bool httpSetConfig(const JsonObject & settings);

View File

@ -6,11 +6,13 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
/* ===== Default Event Processors ===== */
void mdnsSetup(); void mdnsSetup();
void mdnsLoop(void); void mdnsLoop(void);
void mdnsStart(void); void mdnsStart(void);
void mdnsStop(void); void mdnsStop(void);
/* ===== Read/Write Configuration ===== */
bool mdnsGetConfig(const JsonObject & settings); bool mdnsGetConfig(const JsonObject & settings);
bool mdnsSetConfig(const JsonObject & settings); bool mdnsSetConfig(const JsonObject & settings);

View File

@ -323,7 +323,7 @@ void mqttSubscribeTo(const char * format, const char * data)
} }
} }
void mqttReconnect() void mqttStart()
{ {
char buffer[128]; char buffer[128];
char mqttClientId[64]; char mqttClientId[64];
@ -342,7 +342,7 @@ void mqttReconnect()
// Attempt to connect and set LWT and Clean Session // Attempt to connect and set LWT and Clean Session
snprintf_P(buffer, sizeof(buffer), PSTR("%sstatus"), mqttNodeTopic); snprintf_P(buffer, sizeof(buffer), PSTR("%sstatus"), mqttNodeTopic);
if(!mqttClient.connect(mqttClientId, mqttUser, mqttPassword, buffer, 0, false, "OFF", true)) { if(!mqttClient.connect(mqttClientId, mqttUser, mqttPassword, buffer, 0, false, "OFF", true)) { // Literal String
// Retry until we give up and restart after connectTimeout seconds // Retry until we give up and restart after connectTimeout seconds
mqttReconnectCount++; mqttReconnectCount++;
@ -398,12 +398,10 @@ void mqttReconnect()
mqttCommandTopic = prefix + F("/page"); mqttCommandTopic = prefix + F("/page");
mqttGroupCommandTopic = "hasp/" + mqttGroupName + "/page"; mqttGroupCommandTopic = "hasp/" + mqttGroupName + "/page";
mqttSensorTopic = prefix + F("/sensor");
mqttLightCommandTopic = prefix + F("/light/switch"); mqttLightCommandTopic = prefix + F("/light/switch");
mqttLightStateTopic = prefix + F("/light/state"); mqttLightStateTopic = prefix + F("/light/state");
mqttLightBrightCommandTopic = prefix + F("/brightness/set"); mqttLightBrightCommandTopic = prefix + F("/brightness/set");
mqttLightBrightStateTopic = prefix + F("/brightness/state"); mqttLightBrightStateTopic = prefix + F("/brightness/state");
mqttMotionStateTopic = prefix + F("/motion/state");
*/ */
// Set keepAlive, cleanSession, timeout // Set keepAlive, cleanSession, timeout
@ -469,7 +467,7 @@ void mqttLoop()
void mqttEvery5Seconds(bool networkIsConnected) void mqttEvery5Seconds(bool networkIsConnected)
{ {
if(mqttEnabled && networkIsConnected && !mqttClient.connected()) mqttReconnect(); if(mqttEnabled && networkIsConnected && !mqttClient.connected()) mqttStart();
} }
String mqttGetNodename() String mqttGetNodename()

View File

@ -9,8 +9,8 @@
void mqttSetup(); void mqttSetup();
void mqttLoop(); void mqttLoop();
void mqttEvery5Seconds(bool wifiIsConnected); void mqttEvery5Seconds(bool wifiIsConnected);
void mqttStart();
void mqttStop(); void mqttStop();
void mqttReconnect();
void IRAM_ATTR mqtt_send_state(const __FlashStringHelper * subtopic, const char * payload); void IRAM_ATTR mqtt_send_state(const __FlashStringHelper * subtopic, const char * payload);
void mqtt_send_input(uint8_t id, const char * payload); void mqtt_send_input(uint8_t id, const char * payload);

View File

@ -32,13 +32,13 @@ std::string otaUrl = "http://ota.netwize.be";
int16_t otaPort = HASP_OTA_PORT; int16_t otaPort = HASP_OTA_PORT;
int8_t otaPrecentageComplete = -1; int8_t otaPrecentageComplete = -1;
void otaProgress() static inline void otaProgress(void)
{ {
Log.verbose(TAG_OTA, F("%s update in progress... %3u%"), Log.verbose(TAG_OTA, F("%s update in progress... %3u%"),
(ArduinoOTA.getCommand() == U_FLASH ? PSTR("Firmware") : PSTR("Filesystem")), otaPrecentageComplete); (ArduinoOTA.getCommand() == U_FLASH ? PSTR("Firmware") : PSTR("Filesystem")), otaPrecentageComplete);
} }
void otaSetup() void otaSetup(void)
{ {
if(strlen(otaUrl.c_str())) { if(strlen(otaUrl.c_str())) {
Log.trace(TAG_OTA, otaUrl.c_str()); Log.trace(TAG_OTA, otaUrl.c_str());
@ -126,12 +126,12 @@ void otaSetup()
} }
} }
void otaLoop() void otaLoop(void)
{ {
ArduinoOTA.handle(); ArduinoOTA.handle();
} }
void otaEverySecond() void otaEverySecond(void)
{ {
if(otaPrecentageComplete >= 0) otaProgress(); if(otaPrecentageComplete >= 0) otaProgress();
} }

View File

@ -8,9 +8,12 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
void otaSetup(); /* ===== Default Event Processors ===== */
void otaSetup(void);
void otaLoop(void); void otaLoop(void);
void otaEverySecond(void); void otaEverySecond(void);
/* ===== Special Event Processors ===== */
void otaHttpUpdate(const char * espOtaUrl); void otaHttpUpdate(const char * espOtaUrl);
#endif #endif

View File

@ -60,7 +60,7 @@ void telnetClientDisconnect()
void telnetClientLogon() void telnetClientLogon()
{ {
telnetClient.println(); telnetClient.println();
debugHaspHeader(&telnetClient); debugPrintHaspHeader(&telnetClient);
// telnetClient.println(debugHaspHeader().c_str()); // Send version header // telnetClient.println(debugHaspHeader().c_str()); // Send version header
telnetLoginState = TELNET_AUTHENTICATED; // User and Pass are correct telnetLoginState = TELNET_AUTHENTICATED; // User and Pass are correct
telnetLoginAttempt = 0; // Reset attempt counter telnetLoginAttempt = 0; // Reset attempt counter

View File

@ -66,11 +66,11 @@ static void wifiConnected(IPAddress ipaddress)
Log.verbose(TAG_WIFI, F("Connected = %s"), WiFi.status() == WL_CONNECTED ? PSTR("yes") : PSTR("no")); Log.verbose(TAG_WIFI, F("Connected = %s"), WiFi.status() == WL_CONNECTED ? PSTR("yes") : PSTR("no"));
haspProgressVal(255); haspProgressVal(255);
debugStartSyslog();
// mqttReconnect();
// httpReconnect();
mdnsStart();
haspReconnect(); haspReconnect();
debugStartSyslog();
//mqttStart();
httpStart();
mdnsStart();
} }
static void wifiDisconnected(const char * ssid, uint8_t reason) static void wifiDisconnected(const char * ssid, uint8_t reason)
@ -81,6 +81,8 @@ static void wifiDisconnected(const char * ssid, uint8_t reason)
haspProgressMsg(F("Wifi Disconnected")); haspProgressMsg(F("Wifi Disconnected"));
debugStopSyslog(); debugStopSyslog();
mqttStop();
httpStop();
mdnsStop(); mdnsStop();
if(wifiReconnectCounter > 33) { if(wifiReconnectCounter > 33) {
@ -356,7 +358,7 @@ bool wifiShowAP(char * ssid, char * pass)
return true; return true;
} }
void wifiReconnect() static void wifiReconnect(void)
{ {
WiFi.disconnect(true); WiFi.disconnect(true);
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)

View File

@ -26,9 +26,9 @@ void setup()
eepromSetup(); // Don't start at boot, only at write eepromSetup(); // Don't start at boot, only at write
#endif #endif
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 // #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
filesystemSetup(); // filesystemSetup(); // Done in configSetup()
#endif // #endif
#if HASP_USE_SDCARD > 0 #if HASP_USE_SDCARD > 0
sdcardSetup(); sdcardSetup();
@ -154,13 +154,14 @@ void loop()
/* Timer Loop */ /* Timer Loop */
if(millis() - mainLastLoopTime >= 1000) { if(millis() - mainLastLoopTime >= 1000) {
/* Run Every Second */ /* Runs Every Second */
#if HASP_USE_OTA > 0 #if HASP_USE_OTA > 0
otaEverySecond(); otaEverySecond(); // progressbar
#endif #endif
debugEverySecond(); guiEverySecond(); // sleep timer
debugEverySecond(); // statusupdate
/* Run Every 5 Seconds */ /* Runs Every 5 Seconds */
if(mainLoopCounter == 0 || mainLoopCounter == 5) { if(mainLoopCounter == 0 || mainLoopCounter == 5) {
#if HASP_USE_WIFI > 0 #if HASP_USE_WIFI > 0
isConnected = wifiEvery5Seconds(); isConnected = wifiEvery5Seconds();
@ -171,11 +172,11 @@ void loop()
#endif #endif
#if HASP_USE_HTTP > 0 #if HASP_USE_HTTP > 0
httpEvery5Seconds(); // httpEvery5Seconds();
#endif #endif
#if HASP_USE_MQTT > 0 #if HASP_USE_MQTT > 0
mqttEvery5Seconds(isConnected); mqttEvery5Seconds(isConnected);
#endif #endif
} }
@ -188,5 +189,5 @@ void loop()
mainLastLoopTime += 1000; mainLastLoopTime += 1000;
} }
delay(2); delay(3);
} }