Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
script.py
Go to the documentation of this file.
1 import webiopi
2 
3 # import Serial driver
4 from webiopi.devices.serial import Serial
5 
6 # initialize Serial driver
7 serial = Serial("ttyACM0", 9600)
8 sensors = [0 for a in range(6)]
9 
10 def setup():
11  # empty input buffer before starting processing
12  while (serial.available() > 0):
13  serial.readString()
14 
15 def loop():
16  if (serial.available() > 0):
17  data = serial.readString() # read available data
18  lines = data.split("\r\n") # split lines
19  count = len(lines) # count lines
20 
21  lines = lines[0:count-1] # remove last item from split which is empty
22 
23  # process each complete line
24  for pair in lines:
25  cv = pair.split("=") # split channel/value
26  channel = int(cv[0])
27  value = int(cv[1])
28  sensors[channel] = value # store value
29 
30  webiopi.sleep(1)
31 
32 
33 # this macro scales sensor value and returns it as percent string
34 @webiopi.macro
35 def getSensor(channel):
36  percent = (sensors[int(channel)] / 1024.0) * 100.0
37  return "%.2f%%" % percent
38 
def loop
Definition: script.py:28
def getSensor
Definition: script.py:35
def setup
Definition: script.py:15