-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger-MYSQL+CSV
41 lines (35 loc) · 1.02 KB
/
logger-MYSQL+CSV
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import MySQLdb
import Adafruit_BMP.BMP085 as BMP085
sensor = BMP085.BMP085(mode=BMP085.BMP085_ULTRAHIGHRES)
# read pressure and convert from pa to hpa
p = sensor.read_pressure()
p = p / 100.00
#read temperature
t = sensor.read_temperature()
# write CSV (if needed)
#define datetime for CSV file (if needed)
#i = datetime.datetime.now()
#f = open("/mnt/i2c/test.csv","a") #opens file with name of "test.txt"
#f.write("{},{},{}\n".format(i,p,t))
#f.close()
# Open database connection
db = MySQLdb.connect("localhost","weather","weather","i2c" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sql = "INSERT INTO bmp180 (pressure, temperature_i) \
VALUES ('%s', '%s')" % \
(p,t)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()