Skip to content
Snippets Groups Projects
Commit 94557041 authored by fxk8y's avatar fxk8y :spider:
Browse files

add python flasher

parent eccbdba8
No related branches found
No related tags found
1 merge request!1Feature/mqtt ota
ota.py 0 → 100755
#!/usr/bin/python3
# TODO: read from header?
BROKER = "mqtt.c3pb.hack"
DEVICE = "my-device"
import sys
import paho.mqtt.client as mqtt
def on_connect(client, *ignored):
client.subscribe("device/" + DEVICE + "/ota/progress")
with open("build/qthing.bin", mode="rb") as f:
client.publish("device/" + DEVICE + "/ota/$firmware", payload=f.read())
def on_message(client, userdata, msg):
progress = int(msg.payload)
if progress == 0:
print("OTA progress:")
sys.stdout.write("\r[" + "#"*progress + " "*(100-progress) + "] " + str(progress) + "%")
sys.stdout.flush()
if progress == 100:
print("\nOTA successful")
if "--restart" in sys.argv:
print("restarting device")
client.publish("device/" + DEVICE + "/command", "restart")
else:
exit(0)
def on_publish(client, userdata, _ignored):
if userdata.first_pub_done:
exit(0)
userdata.first_pub_done = True
class Userdata(object):
first_pub_done = False
client = mqtt.Client(userdata=Userdata())
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.connect(BROKER)
client.loop_forever()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment