release_image.py
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import io
import serial
import time
######################################
# 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 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")
time.sleep(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))
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')
time.sleep(5)
# Run the rest of the mud setup
import release