19 |
20 | ### Sniffer Mode
21 | The device needs to be opened up and the WBR3 module heat gunned off the board, and the ESP32-C3FN4 module soldered in its place, **after** it has been programmed with the app to allow subsequent changes to be made via OTA. Temporary jumpers need to be added to provide a 5V supply to the device and to connect UART1 on the ESP32-C3 (any two convenient pins) to UART0 on the WBR3.
22 |
23 | The WBR3 module needs to be placed in an ESP8266 code burner and the jumpers from ESP32-C3 connected to the TX and RX pins (UART0 TX -> UART1 RX etc). Do not power the code burner via USB port or the UART bridge chip will interfere with UART0. Supply power directly to the 5V and GND headers. Ensure that there is a common ground between the device and code burner power supplies. .
24 |
25 |
26 |
27 | When the device wakes up, it will output all of its current DataPoint values which will be shown on the sniffer web page - `TSN.htm`, see below.
28 | For each DataPoint, the output format is:
29 | * source > [command] DP id: data type ( value )
30 |
31 |
32 |
33 | Use the Tuya App to send commands to the device to request all data points. For each command sent, note the output on the Sniffer log, to match up the DataPoints, eg DP 2 is the target temperature * 10. Each command will cause the WBR3 module to output a particular DataPoint to the device. Continue sending commands until each DataPoint has been identified.
34 |
35 | ### Controller Mode
36 | Once this app has been configured with the specific Tuya DataPoints for the device (as in `appSpecific.cpp`), the jumpers can be removed and the device reassembled. The controller uses the device specific web page, in this case `Thermo.htm`.
37 |
38 | ## Installation
39 |
40 | Download GitHub files into the Arduino IDE sketch folder, removing `-main` from the application folder name.
41 |
42 | Select the required module, eg `ESP32C3 Dev Module`.
43 | Compile using arduino core min v3.1.0 with Partition Scheme: `Minimal SPIFFS (...)`.
44 |
45 | 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.
46 |
47 | On first installation, the application will start in wifi AP mode - connect to SSID: **ESP-TuyaDevice_...**, to allow router and password details to be entered via the web page on 192.168.4.1. The configuration data file (except passwords) is automatically created, and the application web pages automatically downloaded from GitHub to the flash **/data** folder when an internet connection is available.
48 |
49 | Subsequent updates to the application, or to the **/data** folder contents, can be made using the **OTA Upload** tab. The **/data** folder can also be reloaded from GitHub using the **Reload /data** button on the **Edit Config** tab.
50 |
51 | ## Configuration
52 |
53 | The Tuya Device web page **Edit Config** tab has the following buttons:
54 |
55 | * **Wifi Config**: WiFi and webserver settings
56 | .
57 | * **Save**: Make configuration changes persist over ESP reboots.
58 |
59 | * **Reboot ESP**: Restart the ESP to apply some configuration changes.
60 |
61 | * **Reload /data**: Reload the **/data** folder contents from GitHub.
62 |
63 | * **Clear NVS**: Clear current passwords.
64 |
65 |
--------------------------------------------------------------------------------
/appGlobals.h:
--------------------------------------------------------------------------------
1 | // Global TuyaDevice declarations
2 | //
3 | // s60sc 2022
4 |
5 | #pragma once
6 | #include "globals.h"
7 | #if !CONFIG_IDF_TARGET_ESP32C3
8 | #error "Must select ESP32C3 module"
9 | #endif
10 |
11 | /******************** User modifiable defines *******************/
12 |
13 | #define MCU_TX_PIN 21 // must be UART0 TX pin if controller is ESP, eg pin 21 for ESP32-C3
14 | #define MCU_RX_PIN 20 // must be UART0 RX pin if controller is ESP, eg pin 20 for ESP32-C3
15 |
16 | // sniffer
17 | #define USE_SNIFFER true // false to be tuya device, true when sniffing and need to connect ESP to external wifi eg a removed WBR3 Tuya wifi module
18 | #define WIFI_TX_PIN 4 // only when USE_SNIFFER is true
19 | #define WIFI_RX_PIN 3 // only when USE_SNIFFER is true
20 | #define USE_UART0 false // true if using ESP as tuya device, else false if used as external wifi module
21 |
22 | #define ALLOW_SPACES false // set true to allow whitespace in configs.txt key values
23 | #define USE_IP6 false
24 |
25 | // web server ports
26 | #define HTTP_PORT 80 // app control
27 | #define HTTPS_PORT 443 // secure app control
28 |
29 | /*********************** Fixed defines leave as is ***********************/
30 | /** Do not change anything below here unless you know what you are doing **/
31 |
32 | #define STATIC_IP_OCTAL "163" // dev only
33 | #define DEBUG_MEM false // leave as false
34 | #define FLUSH_DELAY 0 // for debugging crashes
35 | #define DBG_ON false // esp debug output
36 | #define DOT_MAX 50
37 | #define HOSTNAME_GRP 0
38 |
39 | #define APP_NAME "ESP-TuyaDevice" // max 15 chars
40 | #define APP_VER "1.8"
41 |
42 | #define HTTP_CLIENTS 2 // http, ws
43 | #define MAX_STREAMS 0
44 | #if USE_SNIFFER
45 | #define INDEX_PAGE_PATH DATA_DIR "/TSN" HTML_EXT
46 | #else
47 | #define INDEX_PAGE_PATH DATA_DIR "/Thermo" HTML_EXT
48 | #endif
49 | #define FILE_NAME_LEN 64
50 | #define IN_FILE_NAME_LEN 128
51 | #define JSON_BUFF_LEN (1024 * 2)
52 | #define MAX_CONFIGS 70 // > number of entries in configs.txt
53 | #define GITHUB_PATH "/s60sc/ESP32-Tuya_Device/main"
54 |
55 | #define STORAGE LittleFS // One of LittleFS or SD_MMC
56 | #define RAMSIZE (1024 * 8)
57 | #define CHUNKSIZE (1024 * 4)
58 | #define MIN_RAM 8 // min object size stored in ram instead of PSRAM default is 4096
59 | #define MAX_RAM 4096 // max object size stored in ram instead of PSRAM default is 4096
60 | #define TLS_HEAP (64 * 1024) // min free heap for TLS session
61 | #define WARN_HEAP (32 * 1024) // low free heap warning
62 | #define WARN_ALLOC (16 * 1024) // low free max allocatable free heap block
63 | #define MAX_ALERT 1024
64 |
65 | #define INCLUDE_FTP_HFS false // ftp.cpp (file upload)
66 | #define INCLUDE_SMTP false // smtp.cpp (email)
67 | #define INCLUDE_MQTT false // mqtt.cpp
68 | #define INCLUDE_TGRAM false // telegram.cpp
69 | #define INCLUDE_CERTS false // certificates.cpp (https and server certificate checking)
70 | #define INCLUDE_WEBDAV true // webDav.cpp (WebDAV protocol)
71 |
72 | // to determine if newer data files need to be loaded
73 | #define CFG_VER 3
74 |
75 | #ifdef CONFIG_IDF_TARGET_ESP32S3
76 | #define SERVER_STACK_SIZE (1024 * 8)
77 | #define DS18B20_STACK_SIZE (1024 * 2)
78 | #define STICK_STACK_SIZE (1024 * 4)
79 | #else
80 | #define SERVER_STACK_SIZE (1024 * 4)
81 | #define DS18B20_STACK_SIZE (1024)
82 | #define STICK_STACK_SIZE (1024 * 2)
83 | #endif
84 | #define BATT_STACK_SIZE (1024 * 2)
85 | #define EMAIL_STACK_SIZE (1024 * 6)
86 | #define FS_STACK_SIZE (1024 * 4)
87 | #define LOG_STACK_SIZE (1024 * 3)
88 | #define MIC_STACK_SIZE (1024 * 4)
89 | #define MQTT_STACK_SIZE (1024 * 4)
90 | #define PING_STACK_SIZE (1024 * 5)
91 | #define SERVO_STACK_SIZE (1024)
92 | #define SUSTAIN_STACK_SIZE (1024 * 4)
93 | #define TGRAM_STACK_SIZE (1024 * 6)
94 | #define TELEM_STACK_SIZE (1024 * 4)
95 | #define UART_STACK_SIZE (1024 * 2)
96 |
97 | // task priorities
98 | #define HTTP_PRI 5
99 | #define TGRAM_PRI 1
100 | #define EMAIL_PRI 1
101 | #define FTP_PRI 1
102 | #define LOG_PRI 1
103 | #define UART_PRI 1
104 | #define BATT_PRI 1
105 |
106 | #define UART_RTS UART_PIN_NO_CHANGE
107 | #define UART_CTS UART_PIN_NO_CHANGE
108 | #define TUYA_BAUD_RATE 9600
109 | #define BUFF_LEN (UART_FIFO_LEN * 2) // bigger than biggest tuya message
110 |
111 |
112 | /******************** Function declarations *******************/
113 |
114 | // global app specific functions
115 | void heartBeat();
116 | void prepUarts();
117 | void processMCUcmd();
118 | void processTuyaMsg(const char* wsMsg) ;
119 |
120 |
121 | /******************** Global app declarations *******************/
122 |
123 | extern const char* appConfig;
124 | extern bool uartReady;
125 |
126 | /************************** structures ********************************/
127 |
128 | struct tuyaStruct {
129 | uint8_t tuyaCmd;
130 | uint8_t tuyaDP;
131 | int32_t tuyaInt;
132 | uint8_t tuyaData[200]; // bigger than max message size from tuya MCU
133 | };
134 | extern tuyaStruct mcuTuya;
135 |
--------------------------------------------------------------------------------
/webDav.cpp:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | Using the WebDAV server:
4 | Windows 10:
5 | - Windows file explorer, in address bar enter: | Wifi setup.. | ||
|---|---|---|
| SSID | 246 |247 | | 248 | |
| Password | 251 |252 | | 253 | |
|
256 | | ||