Skip to content

Commit

Permalink
v1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
s60sc authored Jan 5, 2025
1 parent c00ff0a commit ffd7b95
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 191 deletions.
2 changes: 1 addition & 1 deletion ESP32-Tuya_Device.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ void setup() {
}

void loop() {
if (startedUp) heartBeat();
startedUp ? heartBeat() : delay(1000);
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Once this app has been configured with the specific Tuya DataPoints for the devi
Download GitHub files into the Arduino IDE sketch folder, removing `-main` from the application folder name.

Select the required module, eg `ESP32C3 Dev Module`.
Compile using arduino core v2.x or V3.x (min v3.0.3) with Partition Scheme: `Minimal SPIFFS (...)`.
Compile using arduino core min v3.1.0 with Partition Scheme: `Minimal SPIFFS (...)`.

To load the app on the ESP32-C3FN4 for the first time, use a pin compatible ESP8266 Code Burner shown in image above, connecting the IO15 header (for pin GPIO8) to 3V3.

Expand Down
12 changes: 6 additions & 6 deletions appGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#pragma once
#include "globals.h"
#if !CONFIG_IDF_TARGET_ESP32C3
#error "Must select ESP32C3 module"
#endif

/******************** User modifiable defines *******************/

Expand All @@ -17,6 +20,7 @@
#define USE_UART0 false // true if using ESP as tuya device, else false if used as external wifi module

#define ALLOW_SPACES false // set true to allow whitespace in configs.txt key values
#define USE_IP6 false

// web server ports
#define HTTP_PORT 80 // app control
Expand All @@ -32,10 +36,9 @@
#define DBG_ON false // esp debug output
#define DOT_MAX 50
#define HOSTNAME_GRP 0
#define USE_IP6 false

#define APP_NAME "ESP-TuyaDevice" // max 15 chars
#define APP_VER "1.6"
#define APP_VER "1.7"

#define HTTP_CLIENTS 2 // http, ws
#define MAX_STREAMS 0
Expand Down Expand Up @@ -67,9 +70,6 @@
#define INCLUDE_CERTS false // certificates.cpp (https and server certificate checking)
#define INCLUDE_WEBDAV true // webDav.cpp (WebDAV protocol)

#define IS_IO_EXTENDER false // must be false except for IO_Extender
#define EXTPIN 100

// to determine if newer data files need to be loaded
#define CFG_VER 3

Expand Down Expand Up @@ -108,7 +108,7 @@
#define UART_RTS UART_PIN_NO_CHANGE
#define UART_CTS UART_PIN_NO_CHANGE
#define TUYA_BAUD_RATE 9600
#define BUFF_LEN UART_FIFO_LEN * 2 // bigger than biggest tuya message
#define BUFF_LEN (UART_FIFO_LEN * 2) // bigger than biggest tuya message


/******************** Function declarations *******************/
Expand Down
2 changes: 1 addition & 1 deletion appSpecific.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ esp_err_t appSpecificWebHandler(httpd_req_t *req, const char* variable, const ch
httpd_resp_sendstr_chunk(req, numStr);
httpd_resp_sendstr_chunk(req, "°C</text></svg>");
httpd_resp_sendstr_chunk(req, NULL);
}
} else return ESP_FAIL;
return ESP_OK;
}

Expand Down
56 changes: 37 additions & 19 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
// s60sc 2021, 2022

#include "esp_arduino_version.h"

#if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3, 0, 3)
#error Must be compiled with arduino-esp32 core v3.0.3 or higher
#endif

#pragma once
// to compile with -Wall -Werror=all -Wextra
//#pragma GCC diagnostic error "-Wformat=2"
#pragma GCC diagnostic ignored "-Wformat-y2k"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
//#pragma GCC diagnostic ignored "-Wunused-variable"
//#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
//#pragma GCC diagnostic ignored "-Wignored-qualifiers"
//#pragma GCC diagnostic ignored "-Wclass-memaccess"
#if (ESP_ARDUINO_VERSION_MAJOR >= 3)
#pragma GCC diagnostic ignored "-Wvolatile"
#else
#pragma GCC diagnostic ignored "-Wformat"
#endif

/******************** Libraries *******************/

Expand All @@ -34,10 +37,8 @@
#include <Update.h>
#include <WiFi.h>
#include <HTTPClient.h>
////#include <NetworkClient.h> // v3.x only
////#include <NetworkClientSecure.h> // v3.x only
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <NetworkClient.h>
#include <NetworkClientSecure.h>
#include <esp_http_server.h>
#include <esp_https_server.h>

Expand Down Expand Up @@ -81,7 +82,7 @@
#define RAM_LOG_LEN (1024 * 7) // size of system message log in bytes stored in slow RTC ram (max 8KB - vars)
#define MIN_STACK_FREE 512
#define STARTUP_FAIL "Startup Failure: "
#define MAX_PAYLOAD_LEN 256 // set bigger than any websocket payload
#define MAX_PAYLOAD_LEN 672 // set bigger than any incoming websocket payload (20ms audio)
#define NULL_TEMP -127
#define OneMHz 1000000
#define USECS 1000000
Expand All @@ -103,8 +104,10 @@ void buildJsonString(uint8_t filter);
bool calcProgress(int progressVal, int totalVal, int percentReport, uint8_t &pcProgress);
bool changeExtension(char* fileName, const char* newExt);
bool checkAlarm();
bool checkAuth(httpd_req_t* req);
bool checkDataFiles();
bool checkFreeStorage();
bool checkI2Cdevice(const char* devName);
void checkMemory(const char* source = "");
uint32_t checkStackUse(TaskHandle_t thisTask, int taskIdx);
void debugMemory(const char* caller);
Expand Down Expand Up @@ -147,24 +150,26 @@ void logPrint(const char *fmtStr, ...);
void logSetup();
void OTAprereq();
bool parseJson(int rxSize);
bool prepI2C();
void prepPeripherals();
void prepSMTP();
bool prepTelegram();
void prepTemperature();
void prepUart();
void prepUpload();
void reloadConfigs();
float readInternalTemp();
float readTemperature(bool isCelsius, bool onlyDS18 = false);
float readVoltage();
void remote_log_init();
void remoteServerClose(WiFiClientSecure& sclient);
bool remoteServerConnect(WiFiClientSecure& sclient, const char* serverName, uint16_t serverPort, const char* serverCert, uint8_t connIdx);
void remoteServerClose(NetworkClientSecure& sclient);
bool remoteServerConnect(NetworkClientSecure& sclient, const char* serverName, uint16_t serverPort, const char* serverCert, uint8_t connIdx);
void remoteServerReset();
void removeChar(char* s, char c);
void replaceChar(char* s, char c, char r);
void reset_log();
void resetWatchDog();
bool retrieveConfigVal(const char* variable, char* value);
void runTaskStats();
esp_err_t sendChunks(File df, httpd_req_t *req, bool endChunking = true);
void setFolderName(const char* fname, char* fileName);
void setPeripheralResponse(const byte pinNum, const uint32_t responseData);
Expand All @@ -187,12 +192,13 @@ void urlDecode(char* inVal);
bool urlEncode(const char* inVal, char* encoded, size_t maxSize);
uint32_t usePeripheral(const byte pinNum, const uint32_t receivedData);
esp_sleep_wakeup_cause_t wakeupResetReason();
void wsAsyncSend(const char* wsData);
void wsAsyncSendBinary(uint8_t* data, size_t len);
bool wsAsyncSendText(const char* wsData);
// mqtt.cpp
void startMqttClient();
void stopMqttClient();
void mqttPublish(const char* payload);
void mqttPublishPath(const char* suffix, const char* payload);
void mqttPublishPath(const char* suffix, const char* payload, const char *device = "sensor");
// telegram.cpp
bool getTgramUpdate(char* response);
bool sendTgramMessage(const char* info, const char* item, const char* parseMode);
Expand Down Expand Up @@ -238,6 +244,8 @@ extern bool doGetExtIP;
extern bool usePing; // set to false if problems related to this issue occur: https://github.com/s60sc/ESP32-CAM_MJPEG2SD/issues/221
extern bool wsLog;
extern uint16_t sustainId;
extern bool heartBeatDone;
extern TaskHandle_t heartBeatHandle;

// remote file server
extern char fsServer[];
Expand Down Expand Up @@ -300,11 +308,14 @@ extern char messageLog[];
extern uint16_t mlogEnd;
extern bool timeSynchronized;
extern bool monitorOpen;
extern const char* setupPage_html;
extern const uint8_t setupPage_html_gz[];
extern const size_t setupPage_html_gz_len;
extern const char* otaPage_html;
extern SemaphoreHandle_t wsSendMutex;
extern const char* failPageS_html;
extern const char* failPageE_html;
extern char startupFailure[];
extern time_t currEpoch;
extern bool RCactive;

extern UBaseType_t uxHighWaterMarkArr[];

Expand All @@ -313,6 +324,10 @@ extern int sdMinCardFreeSpace; // Minimum amount of card free Megabytes before f
extern int sdFreeSpaceMode; // 0 - No Check, 1 - Delete oldest dir, 2 - Upload to ftp and then delete folder on SD
extern bool formatIfMountFailed ; // Auto format the file system if mount failed. Set to false to not auto format.

// I2C pins
extern int I2Csda;
extern int I2Cscl;

#define HTTP_METHOD_STRING(method) \
(method == HTTP_DELETE) ? "DELETE" : \
(method == HTTP_GET) ? "GET" : \
Expand Down Expand Up @@ -354,15 +369,18 @@ enum RemoteFail {SETASSIST, GETEXTIP, TGRAMCONN, FSFTP, EMAILCONN, EXTERNALHB, B
/*********************** Log formatting ************************/

//#define USE_LOG_COLORS // uncomment to colorise log messages (eg if using idf.py, but not arduino)
#ifdef USE_LOG_COLORS
#ifdef USE_LOG_COLORS
// ANSI color codes
#define LOG_COLOR_ERR "\033[0;31m" // red
#define LOG_COLOR_WRN "\033[0;33m" // yellow
#define LOG_COLOR_VRB "\033[0;36m" // cyan
#define LOG_NO_COLOR "\033[0m"
#define LOG_COLOR_DBG "\033[0;34m" // blue
#define LOG_NO_COLOR
#else
#define LOG_COLOR_ERR
#define LOG_COLOR_WRN
#define LOG_COLOR_VRB
#define LOG_COLOR_DBG
#define LOG_NO_COLOR
#endif

Expand All @@ -375,6 +393,6 @@ enum RemoteFail {SETASSIST, GETEXTIP, TGRAMCONN, FSFTP, EMAILCONN, EXTERNALHB, B
#define LOG_ERR(format, ...) logPrint(ERR_FORMAT(format "~"), ##__VA_ARGS__)
#define VRB_FORMAT(format) LOG_COLOR_VRB "[%s VERBOSE @ %s:%u] " format LOG_NO_COLOR "\n", esp_log_system_timestamp(), pathToFileName(__FILE__), __LINE__
#define LOG_VRB(format, ...) if (dbgVerbose) logPrint(VRB_FORMAT(format), ##__VA_ARGS__)
#define DBG_FORMAT(format) LOG_COLOR_ERR "[###### DBG @ %s:%u] " format LOG_NO_COLOR "\n", pathToFileName(__FILE__), __LINE__
#define DBG_FORMAT(format) LOG_COLOR_DBG "[%s ### DEBUG @ %s:%u] " format LOG_NO_COLOR "\n", esp_log_system_timestamp(), pathToFileName(__FILE__), __LINE__
#define LOG_DBG(format, ...) do { logPrint(DBG_FORMAT(format), ##__VA_ARGS__); delay(FLUSH_DELAY); } while (0)
#define LOG_PRT(buff, bufflen) log_print_buf((const uint8_t*)buff, bufflen)
14 changes: 7 additions & 7 deletions prefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void reloadConfigs() {
#if INCLUDE_MQTT
if (mqtt_active) {
buildJsonString(1);
mqttPublishPath("config", jsonBuff);
mqttPublishPath("status", jsonBuff);
}
#endif
}
Expand Down Expand Up @@ -237,7 +237,7 @@ void updateStatus(const char* variable, const char* _value, bool fromUser) {
if (mqtt_active) {
char buff[(IN_FILE_NAME_LEN * 2)];
snprintf(buff, IN_FILE_NAME_LEN * 2, "%s=%s", variable, value);
mqttPublish(buff);
mqttPublishPath("state", buff);
}
#endif

Expand Down Expand Up @@ -359,7 +359,10 @@ void updateStatus(const char* variable, const char* _value, bool fromUser) {
saveConfigVect();
} else {
res = updateAppStatus(variable, value, fromUser);
if (!res) LOG_VRB("Unrecognised config: %s", variable);
if (!res) {
if (fromUser) LOG_WRN("Trying to config %s but feature not included", variable);
else LOG_VRB("Unrecognised config: %s", variable);
}
}
if (res) updateConfigVect(variable, value);
}
Expand All @@ -381,7 +384,7 @@ void buildJsonString(uint8_t filter) {
char timeBuff[20];
strftime(timeBuff, 20, "%Y-%m-%d %H:%M:%S", localtime(&currEpoch));
p += sprintf(p, "\"clock\":\"%s\",", timeBuff);
formatElapsedTime(timeBuff, millis());
formatElapsedTime(timeBuff, millis()); // rolls over after 49.7 days due to max uint32
p += sprintf(p, "\"up_time\":\"%s\",", timeBuff);
p += sprintf(p, "\"free_heap\":\"%s\",", fmtSize(ESP.getFreeHeap()));
p += sprintf(p, "\"wifi_rssi\":\"%i dBm\",", WiFi.RSSI() );
Expand Down Expand Up @@ -504,9 +507,6 @@ static bool checkConfigFile() {
bool loadConfig() {
// called on startup
LOG_INF("Load config");
if (jsonBuff == NULL) {
jsonBuff = psramFound() ? (char*)ps_malloc(JSON_BUFF_LEN) : (char*)malloc(JSON_BUFF_LEN);
}
bool res = checkConfigFile();
if (!res) res = checkConfigFile(); // to recreate file if deleted on first call
if (res) {
Expand Down
Loading

0 comments on commit ffd7b95

Please sign in to comment.