Skip to content

Commit eac8412

Browse files
committed
Fix OTA disconnect issue #253 and adjust examples to changes. Add new connection state example
1 parent 274d742 commit eac8412

File tree

22 files changed

+853
-26
lines changed

22 files changed

+853
-26
lines changed

.github/workflows/espidf-compile-v4.4.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ jobs:
5454
esp_idf_version: v4.4
5555
target: esp32
5656
path: 'examples/0018-espressif_esp32_provision_device'
57+
- name: Build process connection state example on v4.4
58+
uses: espressif/esp-idf-ci-action@v1
59+
with:
60+
esp_idf_version: v4.4
61+
target: esp32
62+
path: 'examples/0020-espressif_esp32_connection_state'

.github/workflows/espidf-compile-v5.1.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ jobs:
5454
esp_idf_version: v5.1
5555
target: esp32
5656
path: 'examples/0018-espressif_esp32_provision_device'
57+
- name: Build process connection state example on v5.1
58+
uses: espressif/esp-idf-ci-action@v1
59+
with:
60+
esp_idf_version: v5.1
61+
target: esp32
62+
path: 'examples/0020-espressif_esp32_connection_state'

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ class Custom_MQTT_Client : public IMQTT_Client {
631631
return MQTT_Connection_Error::NONE;
632632
}
633633
634-
void set_connect_cycle_callback(Callback<void(MQTT_Connection_State, MQTT_Connection_Error)>::function callback) override {
634+
void subscribe_connection_state_changed_callback(Callback<void, MQTT_Connection_State, MQTT_Connection_Error>::function callback) override;
635635
// Nothing to do
636636
}
637637

examples/0014-espressif_esp32_send_data/main/0014-espressif_esp32_send_data.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ extern "C" void app_main() {
156156
tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT);
157157
}
158158

159+
while (!tb.connected()) {
160+
vTaskDelay(1000 / portTICK_PERIOD_MS);
161+
}
162+
159163
tb.Send_Telemetry_Data(TEMPERATURE_KEY, esp_random());
160164
tb.Send_Telemetry_Data(HUMIDITY_KEY, esp_random());
161165

examples/0015-espressif_esp32_process_OTA_MQTT/main/0015-espressif_esp32_process_OTA_MQTT.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ extern "C" void app_main() {
273273
tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT);
274274
}
275275

276+
while (!tb.connected()) {
277+
vTaskDelay(1000 / portTICK_PERIOD_MS);
278+
}
279+
276280
if (!currentFWSent) {
277281
currentFWSent = ota.Firmware_Send_Info(CURRENT_FIRMWARE_TITLE, CURRENT_FIRMWARE_VERSION);
278282
}

examples/0016-espressif_esp32_rpc/main/0016-espressif_esp32_rpc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ extern "C" void app_main(void) {
214214
tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT);
215215
}
216216

217+
while (!tb.connected()) {
218+
vTaskDelay(1000 / portTICK_PERIOD_MS);
219+
}
220+
217221
if (!subscribed) {
218222
const std::array<RPC_Callback, MAX_RPC_SUBSCRIPTIONS> callbacks = {
219223
// Requires additional memory in the JsonDocument for the JsonDocument that will be copied into the response

examples/0017-espressif_esp32_process_shared_attribute_update/main/0017-espressif_esp32_process_shared_attribute_update.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ extern "C" void app_main(void) {
191191
tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT);
192192
}
193193

194+
while (!tb.connected()) {
195+
vTaskDelay(1000 / portTICK_PERIOD_MS);
196+
}
197+
194198
if (!subscribed) {
195199
// Shared attributes we want to request from the server
196200
constexpr std::array<const char*, MAX_ATTRIBUTES> SUBSCRIBED_SHARED_ATTRIBUTES = {FW_CHKS_KEY, FW_CHKS_ALGO_KEY, FW_SIZE_KEY, FW_TAG_KEY, FW_TITLE_KEY, FW_VER_KEY};

examples/0018-espressif_esp32_provision_device/main/0018-espressif_esp32_provision_device.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ void provision_device(void *pvParameters) {
256256
tb.disconnect();
257257
}
258258

259+
// Wait for the provisioning device to disconnect
260+
while (tb.connected()) {
261+
vTaskDelay(1000 / portTICK_PERIOD_MS);
262+
}
263+
259264
// Connect to the ThingsBoard server, as the provisioned client
260265
tb.connect(THINGSBOARD_SERVER, credentials.username.c_str(), THINGSBOARD_PORT, credentials.client_id.c_str(), credentials.password.c_str());
261266

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# For more information about build system see
2+
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
# Set project version
6+
set(PROJECT_VER "0.1.1")
7+
8+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
9+
project(ESP32_CONN_STATE)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Detecting and reacting to connection state
2+
3+
## Devices
4+
| Supported Devices |
5+
|-------------------|
6+
| ESP32 |
7+
8+
## Framework
9+
10+
Espressif IDF
11+
12+
## ThingsBoard API
13+
-
14+
15+
## Feature
16+
Allows detecting and react to changes in the connection state. Especially useful when using the `Espressif_MQTT_Client`, because both the `disconnect` and `connect` method are non-blocking.
17+
This means that directly calling `connect` and then attempting to send data to ThingsBoard will cause the calls to fail, because the connection has not been established yet.
18+
19+
To fix this we can either read the current state of the device, so whether it is still connecting or already connected. Alternatively, we can subscribe a callback that will automatically inform once the connection state changes, so we are also informed if the device is now connected.

0 commit comments

Comments
 (0)