forked from aarond10/powermeter_hub_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodemcu_server.py
executable file
·38 lines (31 loc) · 1.07 KB
/
nodemcu_server.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
import db
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import logging
import urlparse
class NodeMCUServer(SimpleHTTPRequestHandler):
"""Act similar to the fake sensornet server we emulate for the sake of the
efergy hub but this one uses regular HTTP and is designed for use with ESP8266
devices flashed with NodeMCU. Use GET instead of POST because I'm lazy.
"""
def do_GET(self):
url = urlparse.urlparse(self.path)
q = urlparse.parse_qs(url.query)
if url.path == '/data':
db.LogData(q['dev'][0], float(q['val'][0]))
logging.info("%s, %s" % (q['dev'][0], q['val'][0]))
self.send_response(200)
else:
self.send_response(404)
def do_POST(self):
return self.do_GET()
def run_server():
server_address = ('', 8079) # (address, port)
httpd = HTTPServer(server_address, NodeMCUServer)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
if __name__ == '__main__':
logging.basicConfig()
db.SetupDb('readings.db')
run_server()