-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneralpower.py
executable file
·53 lines (43 loc) · 1.08 KB
/
generalpower.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
#!/usr/bin/python3
import serial
import cherrypy
import threading
import time
import datetime
import queue
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
blink=0
lastquery=0
def serialread(): #Continuous loop to read serial
global blink
while True:
line=ser.readline()
serRead=line.decode('utf-8')
if serRead == "b\r\n": #Start of frame
blink = blink + 1
print(blink)
def failsafe(): #If no web reading in last 15 seconds, delete data
while True:
global lastquery
global blink
if time.time() - lastquery > 12:
blink=0
print("reset")
time.sleep(0.1)
class Web(object):
@cherrypy.expose
def query(self,reset=0):
global blink
global lastquery
count = blink #Write an actual number if reset
if reset == "1":
lastquery = time.time()
blink=0
return str(count)
serialreadThread = threading.Thread(target=serialread)
serialreadThread.start()
failsafeThread = threading.Thread(target=failsafe)
failsafeThread.start()
cherrypy.server.socket_host = "0.0.0.0"
cherrypy.server.socket_port = 8081
cherrypy.quickstart(Web())