-
Notifications
You must be signed in to change notification settings - Fork 0
/
readSerialTemp2.py
64 lines (39 loc) · 1.43 KB
/
readSerialTemp2.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
# -*- coding: iso-8859-1 -*-
import serial
import sys
import string
import time
import MySQLdb
from ConfigParser import SafeConfigParser
# read configuration file in (current directory) simple.ini
parser = SafeConfigParser()
parser.read('simple.ini')
# device parameters
device = parser.get('device', 'device_file')
baudrate = parser.get('device', 'baud_rate')
# database parameters
host = parser.get('database', 'host')
port = int(parser.get('database', 'port'))
user = parser.get('database', 'user')
password = parser.get('database','password')
database_name = parser.get('database','database_name')
# create database connection
db = MySQLdb.connect(host=host, port=port, user=user, db="home_temperatures")
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS %s(TimeStamp TIMESTAMP NOT NULL PRIMARY KEY, Temperature INT)",database_name);
# open serial connection with device, baudrate and timeout 1s
ser = serial.Serial(device,baudrate, timeout = 1)
# query string
query = "INSERT INTO temperatures(Temperature) VALUES(%s)"
notFound = True;
# total size of reading from the arduino is 61 bytes
while (notFound) :
s = ser.read(61)
# if 'C: ' is found in the buffer, save the [3-7] characters after it (which is the temperature)
if (s.find('C: ') >= 0):
currTemp = s[s.find('C: ') + 3:s.find('C: ')+7]
# store in db
cursor.execute(query,currTemp)
db.commit
# a little delay
time.sleep(10);