forked from kevinmcaleer/bigmouth_wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbillyv2.py
131 lines (106 loc) · 3.53 KB
/
billyv2.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import network
import upip
from secret import ssid, password
from billy import Billy
import uos
from time import sleep, gmtime
from machine import RTC, reset
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print(".",end="")
sleep(0.5)
print(f"connected with ip {wlan.config}")
# check if phew installed
installed_libs = uos.listdir('lib')
if "phew" not in installed_libs:
print("Phew not installed, installing now")
upip.install("micropython-phew")
else:
print("phew already installed")
from phew import logging, server, template
logging.info("Starting up Billy")
billy = Billy()
page_views = 1
status = ""
from phew import server, connect_to_wifi
connect_to_wifi(ssid, password)
# if the clock isn't set then we need to fetch the time from an NTP
# server. this requires connecting to WiFi
# set clock
billy.update_rtc_from_ntp()
if not billy.clock_set():
logging.info("> clock not set, synchronise from ntp server")
if not billy.sync_clock_from_ntp():
# if we failed to synchronise the clock then turn on the warning
# led and go back to sleep for another cycle
logging.error("! failed to synchronise clock")
uptime = billy.what_time_is_it_mr_wolf()
if billy.low_disk_space():
# there is less than 10% of the filesystem available, time to truncate the log file
logging.error("! low disk space")
logging.truncate(8192)
@server.route("/", methods=["GET"])
def index(request):
global page_views
response = (template.render_template('index.html',page_views=page_views, uptime=uptime, status=status, fish_image=billy))
page_views += 1
return response
@server.route("/about", methods=["GET"])
def about(request):
global page_views
response = (template.render_template('about.html',page_views=page_views, uptime=uptime, status=status, fish_image=billy))
page_views += 1
return response
def redirect_and_respond(request):
global page_views
response = (template.render_template('index.html',page_views=page_views, uptime=uptime, status=billy.status, fish_image=billy))
page_views += 1
logging.info("redirecting")
return response
@server.route("/head/out", methods=["GET"])
def head_out(request):
billy.head_out()
return redirect_and_respond(request)
@server.route("/head/in", methods=["GET"])
def head_in(request):
billy.head_in()
return redirect_and_respond(request)
@server.route("/tail/out", methods=["GET"])
def tail_out(request):
billy.tail_out()
return redirect_and_respond(request)
@server.route("/tail/in", methods=["GET"])
def tail_in(request):
billy.tail_in()
return redirect_and_respond(request)
@server.route("/mouth/open", methods=["GET"])
def mouth_open(request):
billy.mouth_open()
return redirect_and_respond(request)
@server.route("/mouth/closed", methods=["GET"])
def mouth_close(request):
billy.mouth_close()
return redirect_and_respond(request)
@server.catchall()
def catchall(request):
return "Not found", 404
server.run(host="0.0.0.0", port=80)
wdt = WDT(timeout=8000)
while True:
if billy.low_disk_space():
# there is less than 10% of the filesystem available, time to truncate the log file
logging.error("! low disk space")
logging.truncate(8192)
# check wifi is connected:
if wlan.isconnected() == False:
print("wifi disconnected")
log.error("wifi disconnected")
log.info("Resetting device")
reset()
onboard.on()
wdt.feed()
sleep(0.25)
onboard.off()
sleep(5)