-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebtest.py
More file actions
48 lines (40 loc) · 1.49 KB
/
webtest.py
File metadata and controls
48 lines (40 loc) · 1.49 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
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import serial
import time
import json
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
if self.path=="/temp.json":
ser.write('g')
bartemp = float(ser.readline())
barpres = int(float(ser.readline()))
humidity = int(float(ser.readline()))
humtemp = int(float(ser.readline()))
currenttime = time.time()
data = {'BarometerTemperature':bartemp, 'BarometerPressure':barpres, 'Humidity':humidity, 'HumidityTemperature':humtemp, 'CurrentTime':int(currenttime)}
self.send_response(200)
self.send_header('Content-type','application/json')
self.end_headers()
# Send the html message
self.wfile.write(json.dumps(data))
return
else:
self.send_error(404,'File Not Found: %s' % self.path)
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
ser=serial.Serial('/dev/ttyUSB0')
time.sleep(2)
#Wait forever for incoming http requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
ser.close()