/
webServer.py
222 lines (186 loc) · 8.53 KB
/
webServer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Serve a Simple HTML page
# Web server based on an example found here
# https://how2electronics.com/raspberry-pi-pico-w-web-server-tutorial-with-micropython/
from machine import Pin, ADC, reset
from socket import socket
from time import sleep
# Home baked imports
import debug # My debuging routine
import constants
import counters
import dateTime
import fileTail
import diskFree
import picoTemp
from subprocess import playTone
import counters
# Constants
VERSION = constants.VERSION
DEBUG = constants.DEBUG
LOGTOFILE = constants.LOGTOFILE
EVENTDISPLAYLINES = constants.EVENTDISPLAYLINES
MAINGATEEVENTLOG = constants.MAINGATEEVENTLOG
EVENTDISPLAYLINES = constants.EVENTDISPLAYLINES
VOLUME = constants.VOLUME
LOWFREQUENCY = constants.LOWFREQUENCY
HIGHFREQUENCY = constants.HIGHFREQUENCY
DELAY = constants.DELAY
REPEAT = constants.REPEAT
GATEBATTERYLEVEL = "batteryLevel.txt"
HTMLLINEBREAK = "<BR>"
# Temperature Sensor
# sensor_temp = ADC(4)
# conversion_factor = 3.3 / (65535)
# Pin Definitions
ledPin = 5 # Physical Pin 7 Gnd = 8
led = Pin(ledPin, Pin.OUT) # Define pin as output
def main(sta_if,ip):
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.main() ", "webServer start",LOGTOFILE)
try:
if ip is not None:
connection=open_socket(ip)
serve(connection)
except KeyboardInterrupt:
reset()
#def temperature():
# temperature_value = sensor_temp.read_u16() * conversion_factor
# temperature_Celcius = 27 - (temperature_value - 0.706)/0.00172169/ 8
# if(DEBUG >=2):
# debug.debug(DEBUG, "webServer.temperature() ", "Temp="+str(temperature_Celcius),LOGTOFILE)
# sleep(1)
# return temperature_Celcius
def webpage(VERSION,houseTempValue,gateTempValue, batteryPercentage, batteryUpdateTime, eventDetails, freeBytesHouse, dateTimeHouse):
html = f"""
<!DOCTYPE html>
<html>
<body>
<H2>Gate Monitor</H2>
<p>Version={VERSION}</p>
<HR>
<LI>House Local Time is {dateTimeHouse}</LI>
<LI>House Pico Temperature is {houseTempValue} degrees Celsius</LI>
<LI>Gate Pico Temperature is {gateTempValue} degrees Celsius</LI>
<LI>House Free Disk Space is {freeBytesHouse} of 2048000 Bytes</LI>
<LI>Gate Battery Percentage is {batteryPercentage}</LI>
<LI>Gate Battery Update Time is {batteryUpdateTime}</LI>
<HR>
<p><H3>Gate Events Log Tail</H3></p>
<p>{eventDetails}</p>
<HR>
<p><h3>House Device LED Error Messages</h3></p>\
<li>1 long, 0 short = Device successfully started and running</li>\
<li>1 long, 1 short = Low voltage at mainGate.py (Less than value set in constants.py)</li>\
<li>1 long, 2 short = Unable to connect to Wifi at mainHouse.py</li>\
<li>1 long, 3 short = Unable to set system clock at mainHouse.py</li>\
<li>1 long, 4 short = Unable to open socket for inbound web page connection</li>\
<li>1 long, 5 short = Heartbeat message timed out</li>\
<HR>
<P><H3>Test</H3></p>
<form action="./led">
<input type="submit" value="Led On" />
</form>
<form action="./off">
<input type="submit" value="Led Off" />
</form>
<form action="./beep">
<input type="submit" value="Play Sound" />
</form>
<form action="./resetCounter">
<input type="submit" value="Reset Counter" />
</form>
</body>
</html>
"""
return html
def serve(connection):
while True:
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "Wait for web request",LOGTOFILE)
client = connection.accept()[0]
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "client="+str(client),LOGTOFILE)
request = client.recv(1024)
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "request="+str(request),LOGTOFILE)
request = str(request)
try:
request = request.split()[1]
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "request.split()[1]="+str(request),LOGTOFILE)
except IndexError:
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "IndexError",LOGTOFILE)
pass
if request == '/off?':
led.low()
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "Test set led off",LOGTOFILE)
elif request == '/led?':
led.high()
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "Test set led on",LOGTOFILE)
elif request == '/beep?':
# Play the sound
playTone(VOLUME, LOWFREQUENCY, HIGHFREQUENCY, DELAY, REPEAT)
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "Test play the sound",LOGTOFILE)
elif request == '/resetCounter?':
# Reset the counter
counters.openCount=0
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.serve(connection)", "Test reset the counter LED to 0",LOGTOFILE)
#Get the Temperature
temperature=(picoTemp.readTemperature())
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "temperature="+temperature,LOGTOFILE)
houseTempValue='%.2f'%(temperature)
# Read the most recent Main Gate Battery values from a file
fileHandle=open(GATEBATTERYLEVEL,"r")
batteryPercentage=fileHandle.readline()
batteryUpdateTimeTuple=eval(str(fileHandle.readline()))
gateTempValue=fileHandle.readline()
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "batteryUpdateTimeTuple="+ str(batteryUpdateTimeTuple),LOGTOFILE)
batteryUpdateTime=str(dateTime.formattedTimeString(batteryUpdateTimeTuple))
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "batteryUpdateTime="+ batteryUpdateTime,LOGTOFILE)
batteryUpdateDate=str(dateTime.formattedDateString(batteryUpdateTimeTuple))
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "batteryUpdateDate="+ batteryUpdateDate,LOGTOFILE)
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "gateTemp="+ str(gateTemp),LOGTOFILE)
fileHandle.close()
# Read the tail of the MaingGate Event Log
rawEventDetails = fileTail.tail(MAINGATEEVENTLOG,EVENTDISPLAYLINES)
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "rawEventDetails="+rawEventDetails, LOGTOFILE)
eventDetails = rawEventDetails.replace('\n', HTMLLINEBREAK + '\n')
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "eventDetails="+ eventDetails, LOGTOFILE)
freeBytesHouse = diskFree.diskFree()
timeHouse = dateTime.formattedTime()
dateHouse = dateTime.formattedDate()
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "timeHouse="+ timeHouse, LOGTOFILE)
debug.debug(DEBUG, "webServer.serve(connection)", "dateHouse="+ dateHouse, LOGTOFILE)
dateTimeHouse=str(dateHouse + " " + timeHouse)
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.serve(connection)", "dateTimeHouse="+ dateTimeHouse, LOGTOFILE)
html=webpage(VERSION,houseTempValue, gateTempValue, batteryPercentage, batteryUpdateDate + " " + batteryUpdateTime, eventDetails, freeBytesHouse, dateTimeHouse)
client.send(html)
client.close()
def open_socket(ip):
#<TODO> Add a try catch block here
# Received this error "OSError: [Errno 98] EADDRINUSE"
# On the connection.bind(address) line below
# Open a socket
if(DEBUG >=1):
debug.debug(DEBUG, "webServer.open_socket(ip) ", "Open a socket",LOGTOFILE)
address = (ip, 80)
connection = socket()
connection.bind(address)
connection.listen(1)
if(DEBUG >=2):
debug.debug(DEBUG, "webServer.open_socket(ip) ", "connection="+str(connection),LOGTOFILE)
return(connection)