I am trying to do OTA over MQTT using update.h library. I am publishing firmware file over MQTT in a specified chunk size. The file is receiving at ESP32 end. I am supplying the received data to update.write
. In start ~150 packet are written successfully. But after that update.write started to give error Wrong Magic Byte.
Here is the code snippet which I am using inside ESP32
#include "mqtt_handler.h"
StaticJsonDocument <256> root;
uint8_t temp[100] = {0};
size_t length ;
void otaHandler(byte *msg, unsigned int len)
{
deserializeJson(root,msg);
String command = root["command"];
if (command.equals("ota.start"))
{
Serial.println("OTA.START via MQTT");
if (!Update.begin(UPDATE_SIZE_UNKNOWN))
{
Update.printError(Serial);
}
}
else if (command.equals("ota.write"))
{
Serial.println("OTA.WRITE via MQTT");
memset(temp, 0, 100);
length = root["length"];
String data = root["data"];
for (int i = 0; i < length; i++)
{
temp[i] = data[i];
}
Serial.println(data);
if (Update.write(temp, length) != length)
{
Update.printError(Serial);
}
}
else if (command.equals("ota.end"))
{
Serial.println("OTA.END via MQTT");
if (Update.end(true))
{
Serial.printf("Update Success \nRebooting...\n");
}
else
{
Update.printError(Serial);
}
}
else
{
Serial.println("Something went worng");
}
}
void callback(char* topic, byte* message, unsigned int length) {
otaHandler(message,length);
}
In this code callback
is the method which is called when an MQTT message arrived.This is the relevant code, if required I can share the complete code.
Message send by python application looks like as below:
{"command" : "ota.start"}
{"command": "ota.write", "length": length, "data": data})
{"command" : "ota.end"}
Below is the snapshot of data
send by Python application in above ota.write message:
Below is the snapshot how my firmware file looks like:
I am sharing this snapshot to show the data format in which I am publishing firmware file.
Below is the snapshot of serial monitor:
Please suggest me if I am missing something on this.
Thanks