thingsboard / thingsboard-client-sdk

Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
MIT License
145 stars 119 forks source link

Thingsboard OTA Esp32 update issue #208

Open Nielajoubert1 opened 5 days ago

Nielajoubert1 commented 5 days ago

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

define THINGSBOARD_ENABLE_PROGMEM 0

else

ifdef ESP32

include

include

endif

endif

include

include

ifdef ESP8266

include

else

ifdef ESP32

include

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 commented 5 days ago

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

MathewHDYT commented 5 days ago

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);
}