-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
112 lines (93 loc) · 3.18 KB
/
main.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
106
107
108
109
110
111
import db
import mfrc522
import network
import speaker
import squeezebox
import squeezebox
import time
import ujson
from machine import Pin, PWM
def read(reader):
while True:
stat, tag_type = reader.request(reader.REQIDL)
if stat == reader.OK:
stat, raw_uid = reader.anticoll()
if stat == reader.OK:
return '%02x%02x%02x%02x' % (
raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3])
time.sleep(1)
def make_reader():
return mfrc522.MFRC522(14, 13, 12, 2, 15)
def load_config():
with open('config.json', 'r') as f:
return ujson.load(f)
def connect(client, ssid, password, sounder):
if not client.active():
print("Activating WIFI Station mode")
client.active(True)
if not client.isconnected():
print("Connecting to network")
client.connect(ssid, password)
while not client.isconnected():
pass
print("network config:", client.ifconfig())
speaker.ack_sound(sounder)
time.sleep(0.2)
speaker.ack_sound(sounder)
time.sleep(0.2)
speaker.ack_sound(sounder)
return client
def program_tracks(reader, sounder, config):
print("Programming tracks")
tracks = squeezebox.read_current_playlist(
config['host'], config['port'], config['player_id'])
print("tracks: %s" % tracks)
speaker.ack_sound(sounder)
card_id = None
while not card_id or card_id == config['master_card']:
card_id = read(reader)
db.save(card_id, {'tracks': tracks})
print("save to card %s" % card_id)
speaker.success_sound(sounder)
time.sleep(5)
def main():
config = load_config()
reader = make_reader()
sounder = PWM(Pin(4))
network.WLAN(network.AP_IF).active(False) # disable access point
client = network.WLAN(network.STA_IF)
connect(client, config['ssid'], config['password'], sounder)
while True:
try:
if not client.isconnected():
connect(client, config['ssid'], config['password'], sounder)
card_id = read(reader)
if card_id == config['master_card']:
print("master card detected")
program_tracks(reader, sounder, config)
elif card_id == config['pause_card']:
print("pausing")
squeezebox.pause(
config['host'],
config['port'],
config['player_id'])
speaker.success_sound(sounder)
else:
print("card %s read" % card_id)
data = db.load(card_id)
if data and 'tracks' in data:
print("playing %s" % data['tracks'])
squeezebox.play(
config['host'],
config['port'],
config['player_id'],
data['tracks'])
speaker.success_sound(sounder)
else:
speaker.fail_sound(sounder)
except Exception as e:
speaker.fail_sound(sounder)
print("EXCEPTION: %s" % e)
time.sleep(5)
if __name__ == '__main__':
main()