GetDHTData is an ESP-IDF C firmware application (declared as project Paperdata in CMakeLists.txt) that samples temperature and humidity from a DHT11 single-wire sensor on an ESP32. A FreeRTOS task pipeline reads the sensor periodically, serializes measurements into JSON with cJSON, buffers payloads in a RAM pointer array while Wi-Fi is disconnected, and uploads them via HTTP POST using esp_http_client. For an I2C SHT31 variant of this firmware, see the sister repository GetSHT31Data.
+---------------+ worker_dht_json_handler +---------------+
| dht_sensor | (Queue, len 5, struct) |feature_extract|
| read_dht_data | ------------------------------> | dht_json_task |
| (Core 1, P7) | | (Core 1, P8) |
+---------------+ +---------------+
|
worker_json_net_handler |
(Queue, len 5, char *) v
+-------------------+ +---------------------+
| network.c | wifi_event_handler (BITS) | worker |
| event_handler & | ------------------------------> |json_network_retrieve|
| life_guard (C1,P9)| BIT0: SUCCESS / BIT1: FAIL | (Core 0, P7) |
+-------------------+ +---------------------+
| |
WIFI_CONNECT_FAIL | | WIFI_CONNECT_SUCCESS
(buffer up to 127) v v (drain buffer first,
+---------------+ | then live queue)
| json_buffer | |
| [128] (RAM) | --------+
+---------------+ |
v
+-------------------+
| http_build |
| http_upload_build |
| (max 3 attempts) |
+-------------------+
components/feature_extract (feature.c) performs cJSON serialization and RAM queue buffering rather than signal feature extraction.
| Directory | Files | Responsibility |
|---|---|---|
main |
main.c, Kconfig, CMakeLists.txt |
Initializes NVS flash (erasing and retrying on error), calls network_init(), initializes the HTTP client via http_init(), and starts the pipeline via worker_session(). |
components/common |
common.h, CMakeLists.txt |
Header-only shared constants (NORMAL_STACKSIZE, NETWORK_STACKSIZE, JSON_BUFFER_SIZE), dht_int_data_t, QueueGroupHandle_t, and workflow error codes (WORKFLOW_ERR_BASE + 1..3). |
components/dht_sensor |
dht11.c, dht11.h, Kconfig, CMakeLists.txt, idf_component.yml |
Polls DHT11 via esp-idf-lib/dht in read_dht_data(), converts float readings to tenths (int16_t), and sends dht_int_data_t structs to worker_dht_json_handler. |
components/feature_extract |
feature.c, feature.h, CMakeLists.txt, idf_component.yml |
Serializes dht_int_data_t to unformatted JSON strings using espressif/cjson in dht_json_task() and manages the RAM fallback array json_buffer via network_failed_buffer() and json_buffer_retrieve(). |
components/http_build |
http_build.c, http_build.h, Kconfig, CMakeLists.txt |
Configures esp_http_client for JSON HTTP POST in http_init() and runs bounded retry uploads in http_upload_build(). |
components/network |
network.c, network.h, Kconfig, CMakeLists.txt |
Configures Wi-Fi STA mode in network_init(), handles Wi-Fi/IP events updating wifi_event_handler, and runs the life_guard reconnect task. |
components/worker |
worker.c, worker.h, CMakeLists.txt |
Creates the two FreeRTOS queues and three pipeline tasks in worker_session(), and runs json_network_retrieve() for live upload, offline buffering, and buffer drain. |
| Function | Task name string | Stack size | Priority | Pinned core ID | Queues / event groups |
|---|---|---|---|---|---|
read_dht_data |
"READ SENSOR DATA" |
NORMAL_STACKSIZE (2048) |
7 |
1 |
Sends dht_int_data_t to worker_dht_json_handler (len 5) |
dht_json_task |
"JSON TO NETWORK" |
NORMAL_STACKSIZE (2048) |
8 |
1 |
Receives from worker_dht_json_handler (len 5); sends char * to worker_json_net_handler (len 5) |
json_network_retrieve |
"WIFI DATA TRANSFER" |
NETWORK_STACKSIZE (8192) |
7 |
0 |
Waits on wifi_event_handler (WIFI_CONNECT_SUCCESS | WIFI_CONNECT_FAIL); receives char * from worker_json_net_handler (len 5) |
life_guard |
"WIFI LIFE GUARD" |
NORMAL_STACKSIZE (2048) |
9 |
1 |
Waits on wifi_event_handler (WIFI_CONNECT_FAIL) |
-
Sampling interval and timing API (
components/dht_sensor/dht11.c):- On the first execution (
!initate_sensor),read_dht_data()reads once viadht_read_float_data(DHT_TYPE_DHT11, CONFIG_DHT_GPIO, &humidity, &tempreture)and drops that initial reading, setsinitate_sensor = true, and recordslastwaketime = xTaskGetTickCount(). - Each loop iteration sleeps with
xTaskDelayUntil(&lastwaketime, pdMS_TO_TICKS(DHT_WAKETIME))(DHT_WAKETIME = 1500ms) for fixed-period scheduling rather than relativevTaskDelay. - On
ESP_OK,tempretureandhumidityare multiplied by10and cast toint16_t,statusis set totrue, anddata_success_int()pushesdht_int_data_ttoworker_dht_json_handlerwithxQueueSend(..., pdMS_TO_TICKS(DHT_WAITTIME))(DHT_WAITTIME = 500ms, returningESP_ERR_QUEUE_FULLif full). - On sensor read error,
data_failed()setsstatus = falseand sendsdht_int_data_twith the same500ms queue timeout.
- On the first execution (
-
Queue capacities, Wi-Fi state, and buffering (
components/worker/worker.c,components/feature_extract/feature.c,components/network/network.c):worker_session()allocatesworker_dht_json_handler(5items ofdht_int_data_t) andworker_json_net_handler(5items ofchar *).dht_json_taskblocks withportMAX_DELAYreceiving fromworker_dht_json_handler, builds an unformatted JSON string withcJSON_PrintUnformatted(), and blocks withportMAX_DELAYsending thechar *pointer toworker_json_net_handler.network_init()starts Wi-Fi STA (WIFI_AUTH_WPA2_PSK, PMF capable, PMF not required). OnWIFI_EVENT_STA_DISCONNECTED,event_handler()clearsWIFI_CONNECT_SUCCESS(BIT0) and retriesesp_wifi_connect()immediately whilecurrent_retry < MAX_RETRY(MAX_RETRY = 3; thevTaskDelaybackoff usingNETWORK_WAKETIME = 2000ms is commented out). After3reconnect retries (oncecurrent_retryreaches3, on the next disconnect event),WIFI_CONNECT_FAIL(BIT1) is set;life_guardwakes onBIT1, waitsLIFE_GUARD_WAITTIME(3000ms) viavTaskDelay, and callsesp_wifi_connect().IP_EVENT_STA_GOT_IPresetscurrent_retry = 0and setsWIFI_CONNECT_SUCCESS(BIT0).IP_EVENT_STA_LOST_IPclearsWIFI_CONNECT_SUCCESS(BIT0) and callsesp_wifi_connect().json_network_retrievewaits viaxEventGroupWaitBits(wifi_event_handler, (WIFI_CONNECT_SUCCESS | WIFI_CONNECT_FAIL), pdFALSE, pdFALSE, portMAX_DELAY):- When
WIFI_CONNECT_FAILis active (andWIFI_CONNECT_SUCCESSis clear): it receives eachchar *fromworker_json_net_handler, setsis_failed_before = true, and stores the pointer instatic char * json_buffer[JSON_BUFFER_SIZE](JSON_BUFFER_SIZE = 128incommon.h) vianetwork_failed_buffer(). Becausenetwork_failed_buffer()rejects items once(*amount_pointer) >= JSON_BUFFER_SIZE - 1, capacity is127buffered JSON payloads (0..126). Once full, newer incoming payloads are freed immediately withcJSON_free()and dropped (ESP_ERR_QUEUE_FULL). - When
WIFI_CONNECT_SUCCESSis active: ifis_failed_before && queue_stored_amount != 0, it drainsjson_bufferin FIFO order starting atreal_data_position(0). Each successfulhttp_upload_build()frees the entry viacJSON_free(), sets the slot toNULL, and advancesreal_data_position. On HTTP failure, the drain loop breaks and keeps remaining items atreal_data_positionfor a subsequent pass. Oncereal_data_position == queue_stored_amount, it resetsqueue_stored_amount = 0,real_data_position = 0, andis_failed_before = false. - When
!is_failed_before && queue_stored_amount == 0: it receives a live pointer fromworker_json_net_handler, callshttp_upload_build(json_receive), and unconditionally freesjson_receivewithcJSON_free().
- When
-
HTTP client settings and retries (
components/http_build/http_build.c):http_init()configuresesp_http_client_config_twith:url:CONFIG_HTTP_SERVER_URLmethod:HTTP_METHOD_POSTtimeout_ms:4000event_handler:http_event_handler(logsHTTP_EVENT_ERROR,HTTP_EVENT_ON_DATA,HTTP_EVENT_DISCONNECTED)buffer_size:512buffer_size_tx:512keep_alive_enable:true
- Sets header
Content-Type: application/jsonviaesp_http_client_set_header(). http_upload_build()callsesp_http_client_set_post_field(http_handler, json_body, strlen(json_body))and executesesp_http_client_perform()up tomax_retry = 3total attempts with no delay between attempts.- On
ESP_OKfromesp_http_client_perform(), it checksesp_http_client_get_status_code():200 <= status_code < 300returnsESP_OK; any non-2xx status logs a warning ([WARN]Server returned non-2xx status code: %d) and continues until3total attempts complete. Any transport error (check != ESP_OK) also runs up to3total attempts before returningESP_FAIL.
| Symbol | Kconfig file / menu | Type | Default value | Description |
|---|---|---|---|---|
CONFIG_ESP_DEBUG_LOGV |
main/Kconfig (ESP_MAIN_SETTING) |
bool |
y |
"SHOW THE INFORMATION OF DEBUG" (defined in Kconfig; currently unused in .c files) |
CONFIG_ESP_DEVICE_ID |
main/Kconfig (ESP_MAIN_SETTING) |
string |
"ESP32_1" |
"DEVICE ID" sent as "device_id" in the JSON payload |
CONFIG_DHT_GPIO |
components/dht_sensor/Kconfig (DHT_GPIO) |
int |
None (must be set) | "specify DHT11 pin" connected to the DHT11 data line |
CONFIG_HTTP_SERVER_URL |
components/http_build/Kconfig (HTTP Setting) |
string |
None (must be set) | "SERVER_URL" passed to esp_http_client_config_t.url |
CONFIG_NETWORK_SSID |
components/network/Kconfig (NETWORK_CFG) |
string |
None (must be set) | "SSID" for Wi-Fi station connection |
CONFIG_NETWORK_PASSWORD |
components/network/Kconfig (NETWORK_CFG) |
string |
None (must be set) | "PASSWORD" for Wi-Fi station connection |
dht_body_build() in components/feature_extract/feature.c builds an unformatted JSON object using cJSON_PrintUnformatted() and sends it as an HTTP POST body with Content-Type: application/json:
{"device_id":"ESP32_1","temperature":240,"humidity":550,"status":true}device_id: string fromCONFIG_ESP_DEVICE_ID(default"ESP32_1").temperature:int16_tinteger scaled by10from the float reading indht11.c(e.g.,240for24.0deg C).humidity:int16_tinteger scaled by10from the float reading indht11.c(e.g.,550for55.0%RH).status: JSON boolean (trueon successfuldht_read_float_data()read,falseon read failure).
This repository contains only the ESP32 firmware; the companion HTTP JSON ingestion server is maintained in a separate repository.
CMakeLists.txt sets cmake_minimum_required(VERSION 3.22) and project(Paperdata). dependencies.lock (lockfile 3.0.0) records target: esp32, ESP-IDF 6.1.0, esp-idf-lib/dht 1.2.0 ("^1.2.0" in components/dht_sensor/idf_component.yml), esp-idf-lib/esp_idf_lib_helpers 1.4.0, and espressif/cjson 1.7.19~2 ("^1.7.19" in components/feature_extract/idf_component.yml, requiring idf >= 5.0).
idf.py set-target esp32
idf.py menuconfig
idf.py build
idf.py flash monitorSet CONFIG_DHT_GPIO, CONFIG_HTTP_SERVER_URL, CONFIG_NETWORK_SSID, CONFIG_NETWORK_PASSWORD, and optionally CONFIG_ESP_DEVICE_ID in idf.py menuconfig before building. Pass -p <PORT> to idf.py flash monitor if the serial port is not detected automatically.
- Volatile RAM-only offline buffer:
json_bufferis a static RAM array of128pointers (127usable slots before dropping new entries). All buffered payloads are lost on reboot or power loss. - Live HTTP failures while Wi-Fi is connected are discarded: In
components/worker/worker.c,json_network_retrieve()buffers payloads intojson_bufferonly in theWIFI_CONNECT_FAILbranch. WhenWIFI_CONNECT_SUCCESSis active andjson_bufferis empty,http_upload_build(json_receive)is followed unconditionally bycJSON_free(json_receive), so readings that fail HTTP POST across all3attempts (max_retry = 3) are lost. - Plaintext HTTP and missing TLS setup:
components/http_build/http_build.csets.url = CONFIG_HTTP_SERVER_URLwithout configuringcert_pem,crt_bundle_attach, or authentication headers. Usinghttp://sends payloads unencrypted, andhttps://URLs lack explicit server certificate verification configuration inesp_http_client_config_t. - Wi-Fi reconnect and
life_guarddefects incomponents/network/network.c:network_init()passeslife_guard_task_handler(NULL) by value instead of&life_guard_task_handlertoxTaskCreatePinnedToCore(), leavinglife_guard_task_handlerpermanentlyNULL. Consequently,vTaskSuspend(life_guard_task_handler)inWIFI_EVENT_STA_STARTis skipped, andeTaskGetState(life_guard_task_handler)onMAX_RETRYis called withNULL.IP_EVENT_STA_GOT_IPsetsWIFI_CONNECT_SUCCESS(BIT0) without clearingWIFI_CONNECT_FAIL(BIT1). Becauselife_guardwaits onWIFI_CONNECT_FAILwithxClearOnExit = false,BIT1stays set after reconnection andlife_guardcontinues waking every3000ms to callesp_wifi_connect().- The exponential delay in
WIFI_EVENT_STA_DISCONNECTED(//vTaskDelay(pdMS_TO_TICKS(NETWORK_WAKETIME*current_retry));) is commented out, so the initial3reconnect attempts run consecutively with no pause.
- DHT11 sensor characteristics and failure-path struct state:
- The DHT11 single-wire sensor has lower measurement resolution and slower response than I2C digital sensors (such as SHT31);
dht11.cpolls every1500ms, discards the first boot sample, and truncates(float * 10)toint16_t. - When
dht_read_float_data()fails,data_failed()setsstatus = falseand sendsdht_int_dataswithout resettingtempretureandhumidity. If the first post-init read fails, those struct fields hold uninitialized stack contents; on subsequent failures, they retain the previous successful sample.
- The DHT11 single-wire sensor has lower measurement resolution and slower response than I2C digital sensors (such as SHT31);
- Dual-core affinity, missing Kconfig defaults, and naming mismatches:
- Tasks pin explicitly to Core
0and Core1inworker.candnetwork.c, so single-core targets cannot run the unmodified code. CONFIG_DHT_GPIO,CONFIG_HTTP_SERVER_URL,CONFIG_NETWORK_SSID, andCONFIG_NETWORK_PASSWORDhave no Kconfig defaults.CONFIG_ESP_DEBUG_LOGVis declared inmain/Kconfigwithout any C source references,CMakeLists.txtdeclaresproject(Paperdata), andcomponents/feature_extractperforms JSON serialization and buffering rather than feature extraction.
- Tasks pin explicitly to Core