-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirServer.py
114 lines (92 loc) · 4.26 KB
/
irServer.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
#!/usr/bin/python
import serial
import json
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
class handler(BaseHTTPRequestHandler):
# All requests are just simple get requests - no request bodies, just
# calls to specific URLs
def do_GET(self):
# Status call - returns the current status of the AC that the arduino
# knows about. If changed have been made with the remote, the arduino
# won't know about these.
if self.path == "/status":
# AC STATUS
serialport.write(b'\xFF') # Ask the arduino for the current AC status
# first 4 bits are the temperature (from 0 -
# 13, representing temperatures from 17 to 30), second 4 bits are on
# and off (0 is on, 7 is off).
response = bytearray(serialport.read()) # Read the status response
temp = (response[0] >> 4) + 17
power = not bool(response[0]) & 7
serialport.flushInput() # Clear any left over bytes
# AMBIENT TEMP
serialport.write(b'\xF0') # Ask the arduino for the ambient temp
# Second Byte - Ambient Temp
response = bytearray(serialport.read())
ambientTempWhole = (response[0] & int('11111100', 2)) >> 2
ambientTempDecimal = response[0] & int('00000011', 2)
print "{0:b}".format(response[0])
print ambientTempWhole
print ambientTempDecimal
ambientTemp = (ambientTempWhole - 10.0) + (0.25 * ambientTempDecimal)
serialport.flushInput() # Clear any left over bytes
# Convert to JSON to be sent back to the requester - this can be
# extended in the future if there is more information that can be
# returned.
js = json.dumps({'temp': temp, 'on': power, 'ambientTemp':ambientTemp})
self.send_response(200)
self.send_header('Content-type','text/json')
self.end_headers()
self.wfile.write(js)
# ON call - turns the AC on with the most recent temperature
elif self.path == "/on":
# If bit 5 is high is a 'power' command. If the next four bits are
# high, it's an 'on' command
serialport.write(b'\x1F')
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write("Done!")
# OFF call - turns the AC on with the most recent temperature
elif self.path == "/off":
# If bit 5 is high is a 'power' command. If the next four bits are
# low, it's an 'off' command
serialport.write(b'\x10')
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write("Done!")
# Only other possibility is a temperature command, attempt to parse as
# an integer.
else:
try:
splitpath = self.path.split('/')
# If the route is a number between 17 and 30, and there are no
# other elements in the route
temp = int(splitpath[1])
if len(splitpath) == 2 and temp in range(17, 31):
# We have a valid temperature, send this to the arduino.
# Keep first 4 bits as low, to signal that this is a
# temperature command and now a power command
instruction = (temp - 17)
serialport.write(chr(instruction))
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write("Done!")
else:
self.send_error(404)
except:
self.send_error(404)
return
try:
server = HTTPServer(('', PORT_NUMBER), handler)
serialport = serial.Serial('/dev/cu.usbmodem1421', 9600)
print 'Started httpserver on port' , PORT_NUMBER
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()