Skip to content

Commit 96979d1

Browse files
authored
v2.5
1 parent e0612b1 commit 96979d1

File tree

9 files changed

+632
-206
lines changed

9 files changed

+632
-206
lines changed

appGlobals.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
/*********************** Fixed defines leave as is ***********************/
1616
/** Do not change anything below here unless you know what you are doing **/
1717

18-
//#define DEV_ONLY // leave commented out
1918
#define STATIC_IP_OCTAL "168" // dev only
2019
#define DEBUG_MEM false // leave as false
2120
#define FLUSH_DELAY 0 // for debugging crashes
@@ -25,7 +24,7 @@
2524
#define USE_IP6 false
2625

2726
#define APP_NAME "ESP32_AdBlocker" // max 15 chars
28-
#define APP_VER "2.4"
27+
#define APP_VER "2.5"
2928

3029
#define HTTP_CLIENTS 2 // http, ws
3130
#define MAX_STREAMS 0
@@ -53,9 +52,6 @@
5352
#define INCLUDE_CERTS false // certificates.cpp (https and server certificate checking)
5453
#define INCLUDE_WEBDAV true // webDav.cpp (WebDAV protocol)
5554

56-
#define IS_IO_EXTENDER false // must be false except for IO_Extender
57-
#define EXTPIN 100
58-
5955
// to determine if newer data files need to be loaded
6056
#define CFG_VER 3
6157

@@ -88,7 +84,6 @@
8884
#define LOG_PRI 1
8985
#define UART_PRI 1
9086
#define BATT_PRI 1
91-
#define IDLEMON_PRI 5
9287

9388
/******************** Function declarations *******************/
9489

appSpecific.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct {
3333
char* lastDomain;
3434
bool lastResult;
3535
} lastCheck;
36-
36+
3737
static void dnsTask(void* parameter) {
3838
// higher priority than loop()
3939
while (true) dnsServer.processNextRequest();
@@ -133,7 +133,7 @@ static void extractBlocklist() {
133133
static bool downloadBlockList() {
134134
// download blocklist file from web
135135
bool res = false;
136-
WiFiClientSecure wclient;
136+
NetworkClientSecure wclient;
137137
if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate, BLOCKLIST)) {
138138
HTTPClient https;
139139
size_t downloadSize = 0;
@@ -323,7 +323,7 @@ void buildAppJsonString(bool filter) {
323323
}
324324

325325
esp_err_t appSpecificWebHandler(httpd_req_t *req, const char* variable, const char* value) {
326-
return ESP_OK;
326+
return ESP_FAIL;
327327
}
328328

329329
esp_err_t appSpecificSustainHandler(httpd_req_t* req) {

globals.h

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
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 *******************/
@@ -26,18 +34,16 @@
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

@@ -81,7 +87,7 @@
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);
103109
bool calcProgress(int progressVal, int totalVal, int percentReport, uint8_t &pcProgress);
104110
bool changeExtension(char* fileName, const char* newExt);
105111
bool checkAlarm();
112+
bool checkAuth(httpd_req_t* req);
106113
bool checkDataFiles();
107114
bool checkFreeStorage();
115+
bool checkI2Cdevice(const char* devName);
108116
void checkMemory(const char* source = "");
109117
uint32_t checkStackUse(TaskHandle_t thisTask, int taskIdx);
110118
void debugMemory(const char* caller);
@@ -128,11 +136,13 @@ char* fmtSize (uint64_t sizeVal);
128136
void forceCrash();
129137
void formatElapsedTime(char* timeStr, uint32_t timeVal, bool noDays = false);
130138
void formatHex(const char* inData, size_t inLen);
139+
bool formatSDcard();
131140
bool fsStartTransfer(const char* fileFolder);
132141
const char* getEncType(int ssidIndex);
133142
void getExtIP();
134143
time_t getEpoch();
135144
size_t getFreeStorage();
145+
uint32_t getFrequency();
136146
bool getLocalNTP();
137147
float getNTCcelsius(uint16_t resistance, float oldTemp);
138148
void goToSleep(int wakeupPin, bool deepSleep);
@@ -147,24 +157,27 @@ void logPrint(const char *fmtStr, ...);
147157
void logSetup();
148158
void OTAprereq();
149159
bool parseJson(int rxSize);
160+
bool prepFreq(int maxFreq, int sampleInterval);
161+
bool prepI2C();
150162
void prepPeripherals();
151163
void prepSMTP();
152164
bool prepTelegram();
153165
void prepTemperature();
154-
void prepUart();
155166
void prepUpload();
156167
void reloadConfigs();
168+
float readInternalTemp();
157169
float readTemperature(bool isCelsius, bool onlyDS18 = false);
158170
float readVoltage();
159171
void 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);
162174
void remoteServerReset();
163175
void removeChar(char* s, char c);
164176
void replaceChar(char* s, char c, char r);
165177
void reset_log();
166178
void resetWatchDog();
167179
bool retrieveConfigVal(const char* variable, char* value);
180+
void runTaskStats();
168181
esp_err_t sendChunks(File df, httpd_req_t *req, bool endChunking = true);
169182
void setFolderName(const char* fname, char* fileName);
170183
void setPeripheralResponse(const byte pinNum, const uint32_t responseData);
@@ -187,12 +200,13 @@ void urlDecode(char* inVal);
187200
bool urlEncode(const char* inVal, char* encoded, size_t maxSize);
188201
uint32_t usePeripheral(const byte pinNum, const uint32_t receivedData);
189202
esp_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
192206
void startMqttClient();
193207
void stopMqttClient();
194208
void 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
197211
bool getTgramUpdate(char* response);
198212
bool sendTgramMessage(const char* info, const char* item, const char* parseMode);
@@ -238,6 +252,8 @@ extern bool doGetExtIP;
238252
extern bool usePing; // set to false if problems related to this issue occur: https://github.com/s60sc/ESP32-CAM_MJPEG2SD/issues/221
239253
extern bool wsLog;
240254
extern uint16_t sustainId;
255+
extern bool heartBeatDone;
256+
extern TaskHandle_t heartBeatHandle;
241257

242258
// remote file server
243259
extern char fsServer[];
@@ -300,11 +316,14 @@ extern char messageLog[];
300316
extern uint16_t mlogEnd;
301317
extern bool timeSynchronized;
302318
extern 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;
304321
extern const char* otaPage_html;
305-
extern SemaphoreHandle_t wsSendMutex;
322+
extern const char* failPageS_html;
323+
extern const char* failPageE_html;
306324
extern char startupFailure[];
307325
extern time_t currEpoch;
326+
extern bool RCactive;
308327

309328
extern UBaseType_t uxHighWaterMarkArr[];
310329

@@ -313,6 +332,10 @@ extern int sdMinCardFreeSpace; // Minimum amount of card free Megabytes before f
313332
extern int sdFreeSpaceMode; // 0 - No Check, 1 - Delete oldest dir, 2 - Upload to ftp and then delete folder on SD
314333
extern 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)

prefs.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
static fs::FS fp = STORAGE;
3131
static std::vector<std::vector<std::string>> configs;
3232
static Preferences prefs;
33-
char* jsonBuff = NULL;
3433
static char appId[16];
3534
static char variable[FILE_NAME_LEN] = {0};
3635
static char value[IN_FILE_NAME_LEN] = {0};
@@ -64,7 +63,7 @@ void reloadConfigs() {
6463
#if INCLUDE_MQTT
6564
if (mqtt_active) {
6665
buildJsonString(1);
67-
mqttPublishPath("config", jsonBuff);
66+
mqttPublishPath("status", jsonBuff);
6867
}
6968
#endif
7069
}
@@ -192,6 +191,9 @@ static bool savePrefs(bool retain = true) {
192191
#endif
193192
#if INCLUDE_MQTT
194193
prefs.putString("mqtt_user_Pass", mqtt_user_Pass);
194+
#endif
195+
#if INCLUDE_RTSP
196+
prefs.putString("RTSP_Pass", RTSP_Pass);
195197
#endif
196198
prefs.end();
197199
LOG_INF("Saved preferences");
@@ -222,6 +224,9 @@ static bool loadPrefs() {
222224
#endif
223225
#if INCLUDE_MQTT
224226
prefs.getString("mqtt_user_Pass", mqtt_user_Pass, MAX_PWD_LEN);
227+
#endif
228+
#if INCLUDE_RTSP
229+
prefs.getString("RTSP_Pass", RTSP_Pass, MAX_PWD_LEN);
225230
#endif
226231
prefs.end();
227232
return true;
@@ -237,7 +242,7 @@ void updateStatus(const char* variable, const char* _value, bool fromUser) {
237242
if (mqtt_active) {
238243
char buff[(IN_FILE_NAME_LEN * 2)];
239244
snprintf(buff, IN_FILE_NAME_LEN * 2, "%s=%s", variable, value);
240-
mqttPublish(buff);
245+
mqttPublishPath("state", buff);
241246
}
242247
#endif
243248

@@ -315,7 +320,20 @@ void updateStatus(const char* variable, const char* _value, bool fromUser) {
315320
else if (!strcmp(variable, "mqtt_user_Pass") && value[0] != '*') strncpy(mqtt_user_Pass, value, MAX_PWD_LEN-1);
316321
else if (!strcmp(variable, "mqtt_topic_prefix")) strncpy(mqtt_topic_prefix, value, (FILE_NAME_LEN/2)-1);
317322
#endif
318-
323+
#if INCLUDE_RTSP
324+
else if (!strcmp(variable, "RTSP_Name")) strncpy(RTSP_Name, value, MAX_HOST_LEN-1);
325+
else if (!strcmp(variable, "RTSP_Pass") && value[0] != '*')strncpy(RTSP_Pass, value, MAX_PWD_LEN-1);
326+
else if (!strcmp(variable, "rtsp00Video")) rtspVideo = streamVid = (bool)intVal;
327+
else if (!strcmp(variable, "rtsp01Audio")) rtspAudio = streamAud = (bool)intVal;
328+
else if (!strcmp(variable, "rtsp02Subtitles")) rtspSubtitles = streamSrt = (bool)intVal;
329+
else if (!strcmp(variable, "rtsp03Port")) rtspPort = intVal;
330+
else if (!strcmp(variable, "rtsp04VideoPort")) rtpVideoPort = intVal;
331+
else if (!strcmp(variable, "rtsp05AudioPort")) rtpAudioPort = intVal;
332+
else if (!strcmp(variable, "rtsp06SubtitlesPort")) rtpSubtitlesPort = intVal;
333+
else if (!strcmp(variable, "rtsp07Ip")) strncpy(RTP_ip, value, MAX_IP_LEN-1);
334+
else if (!strcmp(variable, "rtsp08MaxC")) rtspMaxClients = intVal;
335+
else if (!strcmp(variable, "rtsp09TTL")) rtpTTL = intVal;
336+
#endif
319337
// Other settings
320338
else if (!strcmp(variable, "clockUTC")) syncToBrowser((uint32_t)intVal);
321339
else if (!strcmp(variable, "timezone")) strncpy(timezone, value, FILE_NAME_LEN-1);
@@ -359,7 +377,10 @@ void updateStatus(const char* variable, const char* _value, bool fromUser) {
359377
saveConfigVect();
360378
} else {
361379
res = updateAppStatus(variable, value, fromUser);
362-
if (!res) LOG_VRB("Unrecognised config: %s", variable);
380+
if (!res) {
381+
if (fromUser) LOG_WRN("Unable to config %s as required cpp file not included", variable);
382+
else LOG_VRB("Unrecognised config: %s", variable);
383+
}
363384
}
364385
if (res) updateConfigVect(variable, value);
365386
}
@@ -381,7 +402,7 @@ void buildJsonString(uint8_t filter) {
381402
char timeBuff[20];
382403
strftime(timeBuff, 20, "%Y-%m-%d %H:%M:%S", localtime(&currEpoch));
383404
p += sprintf(p, "\"clock\":\"%s\",", timeBuff);
384-
formatElapsedTime(timeBuff, millis());
405+
formatElapsedTime(timeBuff, millis()); // rolls over after 49.7 days due to max uint32
385406
p += sprintf(p, "\"up_time\":\"%s\",", timeBuff);
386407
p += sprintf(p, "\"free_heap\":\"%s\",", fmtSize(ESP.getFreeHeap()));
387408
p += sprintf(p, "\"wifi_rssi\":\"%i dBm\",", WiFi.RSSI() );
@@ -408,6 +429,9 @@ void buildJsonString(uint8_t filter) {
408429
#endif
409430
#if INCLUDE_MQTT
410431
p += sprintf(p, "\"mqtt_user_Pass\":\"%.*s\",", strlen(mqtt_user_Pass), FILLSTAR);
432+
#endif
433+
#if INCLUDE_RTSP
434+
p += sprintf(p, "\"RTSP_Pass\":\"%.*s\",", strlen(RTSP_Pass), FILLSTAR);
411435
#endif
412436
}
413437
} else {
@@ -504,9 +528,6 @@ static bool checkConfigFile() {
504528
bool loadConfig() {
505529
// called on startup
506530
LOG_INF("Load config");
507-
if (jsonBuff == NULL) {
508-
jsonBuff = psramFound() ? (char*)ps_malloc(JSON_BUFF_LEN) : (char*)malloc(JSON_BUFF_LEN);
509-
}
510531
bool res = checkConfigFile();
511532
if (!res) res = checkConfigFile(); // to recreate file if deleted on first call
512533
if (res) {

0 commit comments

Comments
 (0)