Bonjour/Bonsoir, je me permet de vous contacter puisque je me trouve confronter à un soucis. Je suis en classe de Terminale STI2D et pour mon projet, j'utilise diverses capteurs donc le O2 Sensor de chez Grove. Pour en arriver au problème, j'ai fais fonctionner celui ci et donc récupérer sous forme de pourcentage le taux d'oxygène présent dans la pièce où se trouve le capteur, j'ai également stocker dans une autre variable la date et l'heure. J'ai essayer après cela de mettre tout ceci dans 2 tables de ma bases SQL mais quand je lance le programme sous RaspberryPi, rien ne s'ajoute à ma bdd, quelqu'un pourrait-il m'aider ? Merci.

Je vous joint le code python.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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")
config.json :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
{
"mysql":[{
    "host":"localhost",
    "user":"pi",
    "password":"paraponera",
    "database":"oxygene"
    }]
}