Skip to content

Instantly share code, notes, and snippets.

@jamesmyatt
Created June 1, 2017 07:52
Show Gist options
  • Save jamesmyatt/c81d51e67a41c12d33101de0af810ad3 to your computer and use it in GitHub Desktop.
Save jamesmyatt/c81d51e67a41c12d33101de0af810ad3 to your computer and use it in GitHub Desktop.
OTA firmware upload for Homie ESP8266
from __future__ import (
division,
absolute_import,
print_function,
unicode_literals
)
import hashlib
import base64
import time
from paho.mqtt.client import Client
# Input parameters
device_base = 'homie/<device_name>'
firmware_file = '.pioenvs/nodemcuv2/firmware.bin'
broker_host = 'mqtt-broker.local'
with open(firmware_file, 'rb') as f:
firmware = f.read()
checksum = hashlib.md5(firmware)
# print(repr(firmware))
print('length:', len(firmware))
print('checksum:', checksum.hexdigest())
client_id = 'homie_upload'
c = Client(client_id, clean_session=True)
c.connect(broker_host, 1883, 60)
def on_message(mqttc, obj, msg):
print(msg.topic + " (q" + str(msg.qos) + ", r" + str(msg.retain) + ") " + repr(msg.payload))
c.on_message = on_message
c.loop_start()
c.subscribe(device_base + '/$fw/#', qos=1)
c.subscribe(device_base + '/$implementation/ota/#', qos=1)
time.sleep(10)
msg = c.publish(device_base + '/$implementation/ota/firmware',
bytearray(firmware), qos=0, retain=True)
msg.wait_for_publish()
msg = c.publish(device_base + '/$implementation/ota/checksum',
checksum.hexdigest(), qos=0, retain=True)
msg.wait_for_publish()
time.sleep(60)
msg = c.publish(device_base + '/$implementation/ota/firmware',
None, qos=1, retain=True)
msg.wait_for_publish()
msg = c.publish(device_base + '/$implementation/ota/checksum',
None, qos=1, retain=True)
msg.wait_for_publish()
c.disconnect()
time.sleep(1)
c.loop_stop()
print('DONE!')
@lesjaw
Copy link

lesjaw commented Aug 22, 2017

hei Nzbuu, I am trying your script, but no OTA run in my homie firmware.. here is the log

lesjaw@cloud:~/Olmatix-OTA$ python test_ota.py
length: 359392
checksum: f21427224720c210d713d4298f97d7fc
devices/5ccf7f80ee17/$fw/checksum (q1, r1) '16da18406f464ff1cdccbc752c99a0fc'
devices/5ccf7f80ee17/$fw/name (q1, r1) 'smartadapter1ch'
devices/5ccf7f80ee17/$fw/version (q1, r1) '2.1.0'
devices/5ccf7f80ee17/$implementation/ota/enabled (q1, r1) 'true'
[Errno 104] Connection reset by peer
DONE!
lesjaw@cloud:~/Olmatix-OTA$

I wonder why the checksum is always different even though I have the same name firmware from the same arduino sketch, I only change version number incremantlly

@lesjaw
Copy link

lesjaw commented Aug 22, 2017

my mistake, it is now working.. my broker setup message limit is 64Kb, the firmware is 350KB... now it seem working, i can see the firmware run..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment