user72407: 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

user4143: 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.

72407: <p>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 <code>update.write</code>. In start ~150 packet are written successfully. But after that update.write started to give error <strong>Wrong Magic Byte</strong>.</p>
<p>Here is the code snippet which I am using inside ESP32</p>
<pre><code>#include &quot;mqtt_handler.h&quot;
StaticJsonDocument &lt;256&gt; root;
uint8_t temp[100] = {0};
size_t length ;
void otaHandler(byte *msg, unsigned int len)
{
deserializeJson(root,msg);
String command = root[&quot;command&quot;];
if (command.equals(&quot;ota.start&quot;))
{
    Serial.println(&quot;OTA.START via MQTT&quot;);
    if (!Update.begin(UPDATE_SIZE_UNKNOWN))
    {
        Update.printError(Serial);
    }
}
else if (command.equals(&quot;ota.write&quot;))
{
    Serial.println(&quot;OTA.WRITE via MQTT&quot;);
    memset(temp, 0, 100);
    length = root[&quot;length&quot;];
    String data = root[&quot;data&quot;]; 
    for (int i = 0; i &lt; length; i++)
    {
        temp[i] = data[i];
    }
    

    Serial.println(data);
    if (Update.write(temp, length) != length)
    {
        Update.printError(Serial);
    }
}
else if (command.equals(&quot;ota.end&quot;))
{
    Serial.println(&quot;OTA.END via MQTT&quot;);
    if (Update.end(true))
    {
        Serial.printf(&quot;Update Success \nRebooting...\n&quot;);
    }
    else
    {
        Update.printError(Serial);
    }
}
else
{
    Serial.println(&quot;Something went worng&quot;);
}
}
void callback(char* topic, byte* message, unsigned int length) {
otaHandler(message,length);
}
</code></pre>
<p>In this code <code>callback</code> is the method which is called when an MQTT message arrived.This is the relevant code, if required I can share the complete code.</p>
<p>Message send by python application looks like as below:</p>
<pre><code>{&quot;command&quot; : &quot;ota.start&quot;}
{&quot;command&quot;: &quot;ota.write&quot;, &quot;length&quot;: length, &quot;data&quot;: data})
{&quot;command&quot; : &quot;ota.end&quot;}
</code></pre>
<p>Below is the snapshot of <code>data</code> send by Python application in above ota.write message:</p>
<p><a href="https://i.stack.imgur.com/hNptB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hNptB.png" alt="enter image description here" /></a></p>
<p>Below is the snapshot how my firmware file looks like:</p>
<p><a href="https://i.stack.imgur.com/73OBP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/73OBP.png" alt="enter image description here" /></a></p>
<p>I am sharing this snapshot to show the data format in which I am publishing firmware file.</p>
<p>Below is the snapshot of serial monitor:
<a href="https://i.stack.imgur.com/hrURd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hrURd.png" alt="enter image description here" /></a></p>
<p>Please suggest me if I am missing something on this.</p>
<p>Thanks</p>
 4143: <p>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.</p>
<p>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 &quot;06&quot;), then when you receive a packet you must convert the content back into binary before sending it to the Update library.</p>
<p>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.</p>
