-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwizard.py
105 lines (93 loc) · 3.04 KB
/
wizard.py
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
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
import time
import click
import serial
from ampy import pyboard
from ampy.files import Files
from serial.tools import list_ports
@click.command()
def main():
click.echo(
"""
Setup for a ESP8266 based, Micropython powered, Spotify web API utilizing, IoT device.
There is a lot of setup ;) so let's get started!
For a new device you should go through all the steps.
"""
)
ports = list(list_ports.comports())
for port in ports:
click.echo(port)
click.echo('')
port = click.prompt(
'serial port',
type=click.Choice([p for p, _, _ in ports]),
)
click.echo(
"""
Erase the flash memory and write the Micropython with Spotify web API firmware.
(WiFi and Spotify credentials will be lost)
"""
)
if click.confirm('Erase and flash firmware?'):
import esptool
esptool.main(['--port', port, 'erase_flash'])
esptool.main(
[
'--port',
port,
'--baud',
'460800',
'write_flash',
'--flash_size',
'detect',
'0',
'spotify_web_api_micropython.bin',
]
)
serial.Serial(port).close()
click.echo('\n')
if click.confirm('Transfer application code (main.py)?'):
pyb = pyboard.Pyboard(port)
files = Files(pyb)
with open('main.py') as main_file:
main_code = main_file.read()
files.put('main.py', main_code.encode())
pyb.close()
if click.confirm('Updated WiFi settings (transfer boot.py)?'):
ssid = click.prompt('SSID')
password = click.prompt('password')
pyb = pyboard.Pyboard(port)
files = Files(pyb)
with open('boot.py') as boot_file:
boot_code = boot_file.read()
boot_code = boot_code.replace('<SSID>', ssid)
boot_code = boot_code.replace('<password>', password)
files.put('boot.py', boot_code.encode())
pyb.close()
if click.confirm('Setup Spotify web API credentials?'):
pyb = pyboard.Pyboard(port)
files = Files(pyb)
credentials_file_name = '/credentials.json'
for file_name in files.ls():
if file_name.startswith(credentials_file_name):
files.rm(credentials_file_name)
break
pyb.close()
ser = serial.Serial(port, 115200, timeout=10)
ser.write(b'\r\x03\x03\x04') # soft reboot
url = None
while True:
line = ser.readline()
if not line:
break
_, match, url = line.partition(b'Listening, connect your browser to')
if match:
url = url.decode().strip()
time.sleep(5)
click.echo('\nContinue setup in the browser {}'.format(url))
click.launch(url)
break
if not url:
click.echo('\nFailed to fetch url for the next part of the setup')
if __name__ == '__main__':
main()