release_image.py 3.07 KB
import os
import io
import serial
import time
import sys

######################################
# EDIT THIS TO MATCH YOUR SETTINGS
PORT = 'COM17'
VMID = '1e5fc8c'
ESSID = 'Volley'
WIFI_PASSWORD = '6198472223'
######################################

BAUDRATE = 115200

baked_files = [
  "weemud.py",
  "mudserver.py",
  "commandhandler.py",
  "wifiweb.py",
  "utils.py"
]

def countdown(t):
    for x in reversed(range(t)):
        sys.stdout.write('\r{}     '.format(x + 1))
        time.sleep(1)
    print('\r\n')

def run_command(sio, command, expected='>>>'):
    sio.write("{}\n".format(command))
    sio.flush() # it is buffering. required to get the data out *now*
    res = ''
    while expected not in res:
        try:
            res = sio.readline()
        except UnicodeDecodeError:
            return ''
    return res

# Compile vagrant image
for file in baked_files:
    os.system("vagrant scp {} {}:micropython/ports/esp8266/modules/{}".format(file, VMID, file))
os.system('vagrant ssh {} -c "cd micropython/ports/esp8266;make"'.format(VMID))
try:
  os.mkdir('image')
except FileExistsError:
  pass
os.system('vagrant scp {}:micropython/ports/esp8266/build/firmware-combined.bin image/firmware-combined.bin'.format(VMID))

os.system('esptool --port {} erase_flash'.format(PORT))
os.system('esptool --port {} --baud 460800 write_flash --flash_size=detect 0 image/firmware-combined.bin'.format(PORT))

print("Sleeping 10 seconds for reboot")
countdown(10)

with open('releasepw.conf', 'r', encoding='utf-8') as f:
  WEBREPL_PASS = f.read()

with serial.Serial(PORT, BAUDRATE, timeout=1) as ser:
    sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
    run_command(sio, '\x03')
    run_command(sio, 'import os')
    print('Setting up Webrepl')
    run_command(sio, "f = open('webrepl_cfg.py', 'w+').write(\"PASS = '{}'\\n\")".format(WEBREPL_PASS))
    run_command(sio, 'f = open("boot.py", "w+").write("# This file is executed on every boot (including wake-boot from deepsleep)\\n#import esp\\n#esp.osdebug(None)\\nimport gc\\nimport webrepl\\nwebrepl.start()\\ngc.collect()\\n")')

    print('Setting up Wifi Network')
    run_command(sio, 'import network')
    run_command(sio, 'sta_if = network.WLAN(network.STA_IF)')
    run_command(sio, 'ap_if = network.WLAN(network.AP_IF)')

    run_command(sio, 'sta_if.active(True)')

    print('Connecting to {}'.format(ESSID))
    run_command(sio, "sta_if.connect('{}', '{}')".format(ESSID, WIFI_PASSWORD))

    print('Waiting 15 seconds for network to connect.\r\n')
    countdown(15)

    waiting_for_ip = True
    while waiting_for_ip:
        try:
            ifconfig = eval(run_command(sio, "sta_if.ifconfig()", expected='('))
            
            if ifconfig[0] != '0.0.0.0':
                print("New IP Address: {}".format(ifconfig[0]))
                waiting_for_ip = False
        except SyntaxError:
            pass
        except NameError:
            pass

    print("Rebooting via machine reset")

    run_command(sio, 'import machine;machine.reset()')

print('Starting the squishy mud release\r\n')
countdown(5)

# Run the rest of the mud setup
import release