#https://www.gctronic.com/files/ota/thymio3-esp32-firmware.bin

from ota32 import OTA, open_url
import gc
import machine
import thymio

thymio.disable_behaviors()

gc.collect()
print(gc.mem_free())

# url of webserver (e.g. iot49.local)
server = 'https://www.gctronic.com/files/'

# read the sha
s = open_url('{}/ota/thymio3-esp32-firmware.sha'.format(server))
sha = s.read(1024).split()[0].decode()
s.close()
             
gc.collect()

# explicitly state sha in case of memory error
# sha = '76617fb397c516c30d3959c5778a1fc61a7e5ec83f6128bc58feb84933f4d0c2'
url = '{}/ota/thymio3-esp32-firmware.bin'.format(server)

print("flashing {} with\nsha {}".format(url, sha))
ota = OTA(verbose=True)
ota.ota(url, sha)

#print("Restart (off=>on) the robot to finish the upgrade")
#machine.reset()
