Skip to content

Commit dd61cd5

Browse files
authored
v2.5
1 parent f838f2d commit dd61cd5

File tree

12 files changed

+1088
-822
lines changed

12 files changed

+1088
-822
lines changed

ESP32_SpecificGravity.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
// where tilt angle varies with wort density.
44
// Tilt Angle is calculated assuming device only has rotational (not linear) motion
55
// Specific Gravity is calculated from the tilt angle with reference to a nth order polynomial
6-
// - for this implementation only a first order is used with 2 data points
6+
// - for this implementation, only a first order is used with 2 data points
77
// See README.md for more info.
88
//
99
// Connections:
1010
// MPU6050 ESP32
11-
// ADO GND
11+
// INT n/c // interrupt
12+
// ADO 3V3 // address select
1213
// SDA GPIO 21 (green)
1314
// SCL GPIO 22 (yellow)
1415
// GND GND (brown)
@@ -39,7 +40,7 @@ void setup() {
3940

4041
// connect wifi or start config AP if router details not available
4142
startWifi();
42-
43+
4344
startWebServer();
4445
if (strlen(startupFailure)) LOG_WRN("%s", startupFailure);
4546
else {
@@ -50,5 +51,5 @@ void setup() {
5051
}
5152

5253
void loop() {
53-
if (startedUp) SGloop();
54+
startedUp ? SGloop() : delay(1000);
5455
}

appGlobals.h

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

17-
//#define DEV_ONLY // leave commented out
1817
#define STATIC_IP_OCTAL "123" // dev only
1918
#define DEBUG_MEM false // leave as false
2019
#define FLUSH_DELAY 0 // for debugging crashes
@@ -24,7 +23,7 @@
2423
#define USE_IP6 false
2524

2625
#define APP_NAME "ESP32_SG" // max 15 chars
27-
#define APP_VER "2.4"
26+
#define APP_VER "2.5"
2827

2928
#define HTTP_CLIENTS 2 // http, ws
3029
#define MAX_STREAMS 0
@@ -45,18 +44,21 @@
4544
#define WARN_ALLOC (16 * 1024) // low free max allocatable free heap block
4645
#define MAX_ALERT 1024
4746

48-
#define INCLUDE_FTP_HFS false // ftp.cpp (file upload)
49-
#define INCLUDE_SMTP false // smtp.cpp (email)
50-
#define INCLUDE_MQTT false // mqtt.cpp
51-
#define INCLUDE_TGRAM false // telegram.cpp
52-
#define INCLUDE_CERTS false // certificates.cpp (https and server certificate checking)
5347
#define INCLUDE_WEBDAV true // webDav.cpp (WebDAV protocol)
48+
#define INCLUDE_PERIPH true // peripherals.cpp
49+
#define INCLUDE_I2C true // periphsI2C.cpp
5450

55-
#define IS_IO_EXTENDER false // must be false except for IO_Extender
56-
#define EXTPIN 100
51+
// I2C devices
52+
#define USE_SSD1306 false
53+
#define USE_BMP280 false
54+
#define USE_MPU6050 true
55+
#define USE_MPU9250 false
56+
#define USE DS3231 false
57+
#define USE_LCD1602 false
58+
#define USE_DS18B20 false
5759

5860
// to determine if newer data files need to be loaded
59-
#define CFG_VER 3
61+
#define CFG_VER 4
6062

6163
#ifdef CONFIG_IDF_TARGET_ESP32S3
6264
#define SERVER_STACK_SIZE (1024 * 8)
@@ -89,33 +91,18 @@
8991
#define SERVO_PRI 1
9092
#define UART_PRI 1
9193
#define BATT_PRI 1
92-
#define IDLEMON_PRI 5
93-
94-
// devices requiring separate libraries
95-
#define USE_BMP280 false
96-
#define USE DS3231 false
97-
#define USE_SSD1306 false
98-
#define USE_DS18B20 false
99-
100-
// devices not requiring separate libraries
101-
#define USE_LCD1602 false
102-
#define USE_PCF8591 false
103-
#define USE_MPU6050 true
10494

10595
#define FILE_EXT ""
10696

97+
enum stepperModel {BYJ_48, BIPOLAR_8mm};
10798

10899
/******************** Function declarations *******************/
109100

110101
// global app specific functions
111102
bool SGsetup();
112103
void SGloop();
113-
bool checkMPU6050();
114-
float* readMPU6050();
104+
float* getMPU6050();
115105
bool sleepMPU6050(bool doSleep = true);
116-
float* getBMP280();
117-
bool checkI2Cdevices(bool showWarn = false);
118-
bool startI2C();
119106
void setLamp(uint8_t lampVal);
120107
void stepperDone();
121108

@@ -130,6 +117,3 @@ extern bool voltUse; // true to report on ADC pin eg for for battery
130117
extern int voltDivider;
131118
extern float voltLow;
132119
extern int voltInterval;
133-
134-
extern int I2C_SDA;
135-
extern int I2C_SCL;

appSpecific.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ char SGdata[150];
3737

3838
static void calculateSG() {
3939
// get data from MPU6050
40-
float* mpuData = readMPU6050();
40+
float* mpuData = getMPU6050();
4141
// axis used for pitch is whichever is linear to PETling length
4242
// generally this will be the X axis
4343
// get tilt angle (pitch) wrt to horizontal in degrees
@@ -126,7 +126,7 @@ bool SGsetup() {
126126
updateStatus("save", "1");
127127
}
128128
prepPeripherals();
129-
if (startI2C()) {
129+
if (prepI2C()) {
130130
// setup battery monitoring
131131
float voltage = readVoltage();
132132
LOG_INF("Battery voltage: %0.1f", voltage);
@@ -199,8 +199,8 @@ bool updateAppStatus(const char* variable, const char* value, bool fromUser) {
199199
else if (!strcmp(variable, "voltInterval")) voltInterval = intVal;
200200
else if (!strcmp(variable, "hostInterval")) hostInterval = intVal;
201201
else if (!strcmp(variable, "wsServerHost")) strcpy(wsServerHost, value);
202-
else if (!strcmp(variable, "I2C_SDA")) I2C_SDA = intVal;
203-
else if (!strcmp(variable, "I2C_SCL")) I2C_SCL = intVal;
202+
else if (!strcmp(variable, "I2Csda")) I2Csda = intVal;
203+
else if (!strcmp(variable, "I2Cscl")) I2Cscl = intVal;
204204
else if (!strcmp(variable, "timeAwake")) timeAwake = intVal;
205205
else if (!strcmp(variable, "timeAsleep")) timeAsleep = intVal;
206206
else if (!strcmp(variable, "LED_PIN")) LED_PIN = intVal;
@@ -250,7 +250,7 @@ void buildAppJsonString(bool filter) {
250250
}
251251

252252
esp_err_t appSpecificWebHandler(httpd_req_t *req, const char* variable, const char* value) {
253-
return ESP_OK;
253+
return ESP_FAIL;
254254
}
255255

256256
esp_err_t appSpecificSustainHandler(httpd_req_t* req) {
@@ -307,8 +307,8 @@ voltLow~3~1~N~Warning level for low voltage
307307
voltInterval~5~1~N~Voltage check interval (mins)
308308
voltPin~34~1~N~ADC Pin used for battery voltage
309309
voltUse~1~1~C~Use Voltage check
310-
I2C_SDA~21~1~N~I2C SDA pin
311-
I2C_SCL~22~1~N~I2C SCL pin
310+
I2Csda~21~1~N~I2C SDA pin
311+
I2Cscl~22~1~N~I2C SCL pin
312312
wsServerHost~192.168.1.122~0~T~IP address of remote client
313313
hostInterval~10~0~N~Remote client update interval (secs)
314314
LED_PIN~5~1~N~Pin for awake blink led

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)

0 commit comments

Comments
 (0)