1

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:

enter image description here

Below is the snapshot how my firmware file looks like:

enter image description here

I am sharing this snapshot to show the data format in which I am publishing firmware file.

Below is the snapshot of serial monitor: enter image description here

Please suggest me if I am missing something on this.

Thanks

4
  • The message basically means that you have not formatted your image data correctly. The first byte must always be 0xE9 (as in your firmware picture). I suspect you are sending some ASCII representation of the data and not converting that back to binary as you should.
    – Majenko
    Aug 17 '21 at 9:43
  • Looking at it more closely it looks like you're just blasting a decimal ASCII representation of the data that would be absolutely impossible to parse.
    – Majenko
    Aug 17 '21 at 9:44
  • Yes I am sending in decimal ASCII like 0XE9 is 233. In which format does it expect data ?? Binary ?? Aug 17 '21 at 9:52
  • It expects binary data. It is your responsibility to turn your text representation back into the binary format it started as. I would suggest using HEX as your transport layer not decimal - and use zero padding. You can't tell if 3224465 is 32 244 65 or 3 224 46 5, for example. Keep it as hex, and always 2 characters per byte, then in your C code take each pair of hex characters and convert them back to binary.
    – Majenko
    Aug 17 '21 at 9:55
1

You are just throwing an unparseable stream of ASCII decimal representations of the binary data at your code, then writing that ASCII data verbatim to the flash. That can never work.

Instead you need to pick an ASCII representation of your data that is actually parseable (I would suggest sticking to HEX representation and always 2 digits, so represent 0x6 as "06"), then when you receive a packet you must convert the content back into binary before sending it to the Update library.

As it is you take a byte (for example 0xE9) and convert that to ASCII decimal, which is 223. Then you write the 2, 2 and 3 as individual bytes to the flash as ASCII, resulting in 0x32,0x32,0x33 - or printing as decimal in an unparseable stream, 505051.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.