-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalog-mysql.py
53 lines (30 loc) · 1.37 KB
/
analog-mysql.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 spidev, time
import MySQLdb
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('/home/pi/pi-analog-reading/config.ini'))
channel = 0
db = MySQLdb.connect(host=config.get('MYSQL','host'), # your host, usually localhost
user=config.get('MYSQL','username'), # your username
passwd=config.get('MYSQL','password'), # your password
db=config.get('MYSQL','db')) # name of the data base
cur = db.cursor()
def log_data(channel,reading, voltage, temp):
try:
cur.execute("INSERT INTO `temp` (`id`, `reading`, `voltage`, `temp`, `created_at`) VALUES (%d, '%d', '%2.2f', '%2.2f', NOW()) ON DUPLICATE KEY UPDATE reading='%d', voltage='%2.2f', temp='%2.2f', created_at=NOW()" % (channel+1, reading, voltage,temp,reading, voltage,temp))
db.commit()
except:
db.rollback()
spi = spidev.SpiDev()
spi.open(0,0)
def analog_read(channel):
r = spi.xfer2([4 | 2 |(channel>>2), (channel &3) << 6,0])
adc_out = ((r[1]&15) << 8) + r[2]
return adc_out
while True:
reading = analog_read(channel)
voltage = reading * 4.3 / 4096
Temp = voltage * 99.5
log_data(channel, reading, voltage, Temp)
print("Reading=%d\tVoltage=%f\tTemp=%2.2f" % (reading, voltage,Temp))
time.sleep(1)