-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_listener.py
53 lines (47 loc) · 1.65 KB
/
serial_listener.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
import serial
import sys
import MySQLdb
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser # ver. < 3.0
config = ConfigParser() # instantiate
config.read('mysql.config.ini') # parse existing file
# read values from a mysql.config.ini
myhost = config.get('mysql', 'host')
myuser = config.get('mysql', 'user')
mypasswd = config.get('mysql', 'passwd')
mydb = config.get('mysql', 'db')
# check that we have usb connected
try:
serialport = serial.Serial("/dev/ttyUSB0", 57600, timeout=0.5)
except Exception, e:
print >> sys.stderr, "Exception: %s" % str(e)
print >> sys.stderr, "No valid usb found from dev"
sys.exit(1)
# use config settings to db connection
db = MySQLdb.Connect(host=myhost, # your host, usually localhost
user=myuser, # your username
passwd=mypasswd, # your password
db=mydb) # name of the data base
cur = db.cursor()
# loop
while True:
command = serialport.readline()
s = command.strip()
suffix = ";"
if s.endswith(suffix) == False: # in line is not ending with ; forget it
x = "x" # do nothing
elif not s: # if s is empty forget it
print "tyhja"
else:
s = s[:-1] # remove tailing ;
node, value = s.split(":") # split message and assing variables
if node == '1Node':
cur.execute("INSERT INTO lampo1 (node, lampo) VALUES (%s, %s) ", (node, value))
db.commit()
print command
else:
cur.execute("INSERT INTO rele1 (node, tila) VALUES (%s, %s) ", (node, value))
db.commit()
print command