thingsboard-client-sdk icon indicating copy to clipboard operation
thingsboard-client-sdk copied to clipboard

Thingsboard OTA Esp32 update issue

Open Nielajoubert1 opened this issue 1 year ago • 2 comments

Hi, I have the following code that I used to successfully upload to ESP32 but on the dashboard I cannot see the progress and the status is not updating to successful when completed, but on the terminal i can see my esp did upgrade to new firmware, I followed this tutorial on youtube: and also I downloaded the dashboard JSON file from here:

Here is the code I used: `#ifdef ESP8266 #include <ESP8266WiFi.h> #define THINGSBOARD_ENABLE_PROGMEM 0 #else #ifdef ESP32 #include <WiFi.h> #include <WiFiClientSecure.h> #endif #endif

#include <Arduino_MQTT_Client.h> #include <ThingsBoard.h>

#ifdef ESP8266 #include <Arduino_ESP8266_Updater.h> #else #ifdef ESP32 #include <Espressif_Updater.h> #endif #endif

constexpr char CURRENT_FIRMWARE_TITLE[] = "TEST"; constexpr char CURRENT_FIRMWARE_VERSION[] = "1.0.0"; constexpr uint8_t FIRMWARE_FAILURE_RETRIES = 12U; constexpr uint16_t FIRMWARE_PACKET_SIZE = 4096U; constexpr char WIFI_SSID[] = "YOUR_WIFI_SSID"; constexpr char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; constexpr char TOKEN[] = "YOUR_DEVICE_ACCESS_TOKEN"; constexpr char THINGSBOARD_SERVER[] = "demo.thingsboard.io"; constexpr uint16_t THINGSBOARD_PORT = 1883U; constexpr uint16_t MAX_MESSAGE_SIZE = 512U; constexpr uint32_t SERIAL_DEBUG_BAUD = 115200U;

WiFiClient espClient; Arduino_MQTT_Client mqttClient(espClient); ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);

#ifdef ESP8266 Arduino_ESP8266_Updater updater; #else #ifdef ESP32 Espressif_Updater updater; #endif #endif

bool currentFWSent = false; bool updateRequestSent = false;

void InitWiFi() { Serial.println("Connecting to AP ..."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected to AP"); }

bool reconnect() { if (WiFi.status() == WL_CONNECTED) { return true; } InitWiFi(); return true; }

void updatedCallback(const bool& success) { if (success) { Serial.println("Done, Reboot now"); #ifdef ESP8266 ESP.restart(); #else #ifdef ESP32 esp_restart(); #endif #endif } else { Serial.println("Downloading firmware failed"); } }

void progressCallback(const size_t& currentChunk, const size_t& totalChunks) { Serial.printf("Progress %.2f%%\n", static_cast(currentChunk * 100U) / totalChunks); }

void setup() { Serial.begin(SERIAL_DEBUG_BAUD); delay(1000); InitWiFi(); }

void loop() { delay(1000);

if (!reconnect()) { return; }

if (!tb.connected()) { Serial.printf("Connecting to: (%s) with token (%s)\n", THINGSBOARD_SERVER, TOKEN); if (!tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT)) { Serial.println("Failed to connect"); return; } }

if (!currentFWSent) { currentFWSent = tb.Firmware_Send_Info(CURRENT_FIRMWARE_TITLE, CURRENT_FIRMWARE_VERSION) && tb.Firmware_Send_State(FW_STATE_UPDATED); }

if (!updateRequestSent) { Serial.println("Firmware Update..."); const OTA_Update_Callback callback(&progressCallback, &updatedCallback, CURRENT_FIRMWARE_TITLE, CURRENT_FIRMWARE_VERSION, &updater, FIRMWARE_FAILURE_RETRIES, FIRMWARE_PACKET_SIZE); updateRequestSent = tb.Start_Firmware_Update(callback); }

tb.loop(); } `

I don't know if there is something else I'm missing?

Nielajoubert1 avatar Jul 04 '24 05:07 Nielajoubert1

Youtube tutorial: https://www.youtube.com/watch?v=L-eqJfSbzvc JSON : https://thingsboard.io/docs/user-guide/ota-updates/

Nielajoubert1 avatar Jul 04 '24 05:07 Nielajoubert1

Perhaps you've mistakenly downloaded the SW firmware dashboard. Ensure you downloaded the .json dashboard file from the FW section.

Because the implementation in the library should send all keys if you use the correct dashboard. You can check that if you click the device and see the fw_state key.

Additionally the progress is not very detailed, because the progress bar is created simply from a few states in the firmware. You can more or less only see once the firmware is received / started and then completly downloaded and flashed.

Furthermore the last state is sent by the user, this is normaly done once at the boot up of the device. Because according to the official API the UPDATED state is only sent once the device has successfully rebooted with the new firmware.

That is done by this section of your example code.

if (!currentFWSent) {
  currentFWSent = tb.Firmware_Send_Info(CURRENT_FIRMWARE_TITLE, CURRENT_FIRMWARE_VERSION) && tb.Firmware_Send_State(FW_STATE_UPDATED);
}

MathewHDYT avatar Jul 04 '24 07:07 MathewHDYT

So I got it to work, but I want to store the bin file on the esp32 and not install it I then want to transfer that file over painlessmesh to my other nodes, how can I save the Bin file to a SD card or onto the EEPROM. and access it ? can someone please advise.

Nielajoubert1 avatar Jul 24 '24 07:07 Nielajoubert1

So basically I want to know how I can write and read the BIN file send to the esp

Nielajoubert1 avatar Jul 24 '24 07:07 Nielajoubert1

Initalize the SD Card correctly and then you should be able to simply use the SDCard_Updater. Instead of the Espressif_Updater.

Example that uses only the Espressif framework can be found in the examples. Should be directly applyable to Arduino framework as well.

MathewHDYT avatar Jul 24 '24 07:07 MathewHDYT

Hi thanks, I only realized now that fuunction exist. Thanks allot

Nielajoubert1 avatar Jul 24 '24 08:07 Nielajoubert1