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 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import os
import re
import sys
import datetime
from datetime import timedelta
import json
import MySQLdb
import smtplib
import grovepi
import time
gas_sensor = 1
grovepi.pinMode(gas_sensor,"INPUT")
def getConfigurations():
path = os.path.dirname(os.path.realpath(sys.argv[0]))
configurationFile = path + '/valeurs/config.json'
configurations = json.loads(open(configurationFile).read())
return configurations
def databaseHelper(sqlCommand,sqloperation):
configurations = getConfigurations()
host = configurations["mysql"][0]["host"]
user = configurations["mysql"][0]["user"]
password = configurations["mysql"][0]["password"]
database = configurations["mysql"][0]["database"]
data = ""
db = MySQLdb.connect(host,user,password,database)
cursor=db.cursor()
if sqloperation == "Select":
try:
cursor.execute(sqlCommand)
data = cursor.fetchone()
except:
db.rollback()
elif sqloperation == "Insert":
try:
cursor.execute(sqlCommand)
db.commit()
except:
db.rollback()
sys.exit(0)
return data
while True:
try:
sensor_value = (float)(grovepi.analogRead(gas_sensor)*100)/1024
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sqlCommand = "INSERT INTO oxygene SET dateandtime='%s', oxygenedata='%s'" % (date, sensor_value)
databaseHelper(sqlCommand,"Insert")
print(sensor_value, date)
time.sleep(60)
except IOError:
print ("Error") |
Partager