-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmp180.py
42 lines (31 loc) · 1012 Bytes
/
bmp180.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import logging
import Adafruit_BMP.BMP085 as BMP085
# Setup logging
logging.basicConfig(filename='/home/pi/bmp180_error.log',
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
# Function for storing readings into MySQL
def insertDB(temperature,pressure):
try:
con = mdb.connect('localhost',
'pi_insert',
'xxxxxxxxxx',
'measurements');
cursor = con.cursor()
sql = "INSERT INTO bmp180(temperature, pressure) \
VALUES ('%s', '%s')" % \
( temperature, pressure)
cursor.execute(sql)
sql = []
con.commit()
con.close()
except mdb.Error, e:
logger.error(e)
# Get readings from sensor and store them in MySQL
sensor = BMP085.BMP085()
temperature = sensor.read_temperature()
pressure = sensor.read_sealevel_pressure(71)
insertDB(temperature,pressure)