33// s60sc 2021, 2022
44
55#include " esp_arduino_version.h"
6+
7+ #if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3, 1, 1)
8+ #error Must be compiled with arduino-esp32 core v3.1.1 or higher
9+ #endif
10+
611#pragma once
12+
13+ // #define DEV_ONLY // leave commented out
14+ #ifdef DEV_ONLY
715// to compile with -Wall -Werror=all -Wextra
16+ #pragma GCC diagnostic error "-Wformat=2"
17+ #pragma GCC diagnostic ignored "-Wformat-truncation"
18+ #pragma GCC diagnostic ignored "-Wformat-y2k"
819#pragma GCC diagnostic ignored "-Wunused-function"
920#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
1021// #pragma GCC diagnostic ignored "-Wunused-variable"
1122// #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
1223// #pragma GCC diagnostic ignored "-Wignored-qualifiers"
1324// #pragma GCC diagnostic ignored "-Wclass-memaccess"
14- #if (ESP_ARDUINO_VERSION_MAJOR >= 3)
1525#pragma GCC diagnostic ignored "-Wvolatile"
16- #else
17- #pragma GCC diagnostic ignored "-Wformat"
1826#endif
1927
2028/* ******************* Libraries *******************/
2634#include " ping/ping_sock.h"
2735#include < Preferences.h>
2836#include < regex>
29- #if !CONFIG_IDF_TARGET_ESP32C3
37+ #if ( !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32S2)
3038#include < SD_MMC.h>
3139#endif
3240#include < LittleFS.h>
3341#include < sstream>
3442#include < Update.h>
3543#include < WiFi.h>
3644#include < HTTPClient.h>
37- // //#include <NetworkClient.h> // v3.x only
38- // //#include <NetworkClientSecure.h> // v3.x only
39- #include < WiFiClient.h>
40- #include < WiFiClientSecure.h>
45+ #include < NetworkClient.h>
46+ #include < NetworkClientSecure.h>
4147#include < esp_http_server.h>
4248#include < esp_https_server.h>
4349
8187#define RAM_LOG_LEN (1024 * 7 ) // size of system message log in bytes stored in slow RTC ram (max 8KB - vars)
8288#define MIN_STACK_FREE 512
8389#define STARTUP_FAIL " Startup Failure: "
84- #define MAX_PAYLOAD_LEN 256 // set bigger than any websocket payload
90+ #define MAX_PAYLOAD_LEN 672 // set bigger than any incoming websocket payload (20ms audio)
8591#define NULL_TEMP -127
8692#define OneMHz 1000000
8793#define USECS 1000000
@@ -103,8 +109,10 @@ void buildJsonString(uint8_t filter);
103109bool calcProgress (int progressVal, int totalVal, int percentReport, uint8_t &pcProgress);
104110bool changeExtension (char * fileName, const char * newExt);
105111bool checkAlarm ();
112+ bool checkAuth (httpd_req_t * req);
106113bool checkDataFiles ();
107114bool checkFreeStorage ();
115+ bool checkI2Cdevice (const char * devName);
108116void checkMemory (const char * source = " " );
109117uint32_t checkStackUse (TaskHandle_t thisTask, int taskIdx);
110118void debugMemory (const char * caller);
@@ -128,11 +136,13 @@ char* fmtSize (uint64_t sizeVal);
128136void forceCrash ();
129137void formatElapsedTime (char * timeStr, uint32_t timeVal, bool noDays = false );
130138void formatHex (const char * inData, size_t inLen);
139+ bool formatSDcard ();
131140bool fsStartTransfer (const char * fileFolder);
132141const char * getEncType (int ssidIndex);
133142void getExtIP ();
134143time_t getEpoch ();
135144size_t getFreeStorage ();
145+ uint32_t getFrequency ();
136146bool getLocalNTP ();
137147float getNTCcelsius (uint16_t resistance, float oldTemp);
138148void goToSleep (int wakeupPin, bool deepSleep);
@@ -147,24 +157,27 @@ void logPrint(const char *fmtStr, ...);
147157void logSetup ();
148158void OTAprereq ();
149159bool parseJson (int rxSize);
160+ bool prepFreq (int maxFreq, int sampleInterval);
161+ bool prepI2C ();
150162void prepPeripherals ();
151163void prepSMTP ();
152164bool prepTelegram ();
153165void prepTemperature ();
154- void prepUart ();
155166void prepUpload ();
156167void reloadConfigs ();
168+ float readInternalTemp ();
157169float readTemperature (bool isCelsius, bool onlyDS18 = false );
158170float readVoltage ();
159171void remote_log_init ();
160- void remoteServerClose (WiFiClientSecure & sclient);
161- bool remoteServerConnect (WiFiClientSecure & sclient, const char * serverName, uint16_t serverPort, const char * serverCert, uint8_t connIdx);
172+ void remoteServerClose (NetworkClientSecure & sclient);
173+ bool remoteServerConnect (NetworkClientSecure & sclient, const char * serverName, uint16_t serverPort, const char * serverCert, uint8_t connIdx);
162174void remoteServerReset ();
163175void removeChar (char * s, char c);
164176void replaceChar (char * s, char c, char r);
165177void reset_log ();
166178void resetWatchDog ();
167179bool retrieveConfigVal (const char * variable, char * value);
180+ void runTaskStats ();
168181esp_err_t sendChunks (File df, httpd_req_t *req, bool endChunking = true );
169182void setFolderName (const char * fname, char * fileName);
170183void setPeripheralResponse (const byte pinNum, const uint32_t responseData);
@@ -187,12 +200,13 @@ void urlDecode(char* inVal);
187200bool urlEncode (const char * inVal, char * encoded, size_t maxSize);
188201uint32_t usePeripheral (const byte pinNum, const uint32_t receivedData);
189202esp_sleep_wakeup_cause_t wakeupResetReason ();
190- void wsAsyncSend (const char * wsData);
203+ void wsAsyncSendBinary (uint8_t * data, size_t len);
204+ bool wsAsyncSendText (const char * wsData);
191205// mqtt.cpp
192206void startMqttClient ();
193207void stopMqttClient ();
194208void mqttPublish (const char * payload);
195- void mqttPublishPath (const char * suffix, const char * payload);
209+ void mqttPublishPath (const char * suffix, const char * payload, const char *device = " sensor " );
196210// telegram.cpp
197211bool getTgramUpdate (char * response);
198212bool sendTgramMessage (const char * info, const char * item, const char * parseMode);
@@ -238,6 +252,8 @@ extern bool doGetExtIP;
238252extern bool usePing; // set to false if problems related to this issue occur: https://github.com/s60sc/ESP32-CAM_MJPEG2SD/issues/221
239253extern bool wsLog;
240254extern uint16_t sustainId;
255+ extern bool heartBeatDone;
256+ extern TaskHandle_t heartBeatHandle;
241257
242258// remote file server
243259extern char fsServer[];
@@ -300,11 +316,14 @@ extern char messageLog[];
300316extern uint16_t mlogEnd;
301317extern bool timeSynchronized;
302318extern bool monitorOpen;
303- extern const char * setupPage_html;
319+ extern const uint8_t setupPage_html_gz[];
320+ extern const size_t setupPage_html_gz_len;
304321extern const char * otaPage_html;
305- extern SemaphoreHandle_t wsSendMutex;
322+ extern const char * failPageS_html;
323+ extern const char * failPageE_html;
306324extern char startupFailure[];
307325extern time_t currEpoch;
326+ extern bool RCactive;
308327
309328extern UBaseType_t uxHighWaterMarkArr[];
310329
@@ -313,6 +332,10 @@ extern int sdMinCardFreeSpace; // Minimum amount of card free Megabytes before f
313332extern int sdFreeSpaceMode; // 0 - No Check, 1 - Delete oldest dir, 2 - Upload to ftp and then delete folder on SD
314333extern bool formatIfMountFailed ; // Auto format the file system if mount failed. Set to false to not auto format.
315334
335+ // I2C pins
336+ extern int I2Csda;
337+ extern int I2Cscl;
338+
316339#define HTTP_METHOD_STRING (method ) \
317340 (method == HTTP_DELETE) ? " DELETE" : \
318341 (method == HTTP_GET) ? " GET" : \
@@ -354,15 +377,18 @@ enum RemoteFail {SETASSIST, GETEXTIP, TGRAMCONN, FSFTP, EMAILCONN, EXTERNALHB, B
354377/* ********************** Log formatting ************************/
355378
356379// #define USE_LOG_COLORS // uncomment to colorise log messages (eg if using idf.py, but not arduino)
357- #ifdef USE_LOG_COLORS
380+ #ifdef USE_LOG_COLORS
381+ // ANSI color codes
358382#define LOG_COLOR_ERR " \033 [0;31m" // red
359383#define LOG_COLOR_WRN " \033 [0;33m" // yellow
360384#define LOG_COLOR_VRB " \033 [0;36m" // cyan
361- #define LOG_NO_COLOR " \033 [0m"
385+ #define LOG_COLOR_DBG " \033 [0;34m" // blue
386+ #define LOG_NO_COLOR
362387#else
363388#define LOG_COLOR_ERR
364389#define LOG_COLOR_WRN
365390#define LOG_COLOR_VRB
391+ #define LOG_COLOR_DBG
366392#define LOG_NO_COLOR
367393#endif
368394
@@ -375,6 +401,6 @@ enum RemoteFail {SETASSIST, GETEXTIP, TGRAMCONN, FSFTP, EMAILCONN, EXTERNALHB, B
375401#define LOG_ERR (format, ...) logPrint(ERR_FORMAT(format " ~" ), ##__VA_ARGS__)
376402#define VRB_FORMAT (format ) LOG_COLOR_VRB " [%s VERBOSE @ %s:%u] " format LOG_NO_COLOR " \n " , esp_log_system_timestamp(), pathToFileName(__FILE__), __LINE__
377403#define LOG_VRB (format, ...) if (dbgVerbose) logPrint(VRB_FORMAT(format), ##__VA_ARGS__)
378- #define DBG_FORMAT (format ) LOG_COLOR_ERR " [###### DBG @ %s:%u] " format LOG_NO_COLOR " \n " , pathToFileName(__FILE__), __LINE__
404+ #define DBG_FORMAT (format ) LOG_COLOR_DBG " [%s ### DEBUG @ %s:%u] " format LOG_NO_COLOR " \n " , esp_log_system_timestamp() , pathToFileName(__FILE__), __LINE__
379405#define LOG_DBG (format, ...) do { logPrint (DBG_FORMAT (format), ##__VA_ARGS__); delay (FLUSH_DELAY); } while (0 )
380406#define LOG_PRT (buff, bufflen ) log_print_buf((const uint8_t *)buff, bufflen)
0 commit comments