2
I need to enter in the database the result of this query through the temperature module.
The code is this.
Temperature and humidity are already returning, but not saved in the database the values.
# Carrega as bibliotecas
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
import Adafruit_DHT
import RPi.GPIO as GPIO
import time
db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="sensores")
cursor = db.cursor()
# Define o tipo de sensor
sensor = Adafruit_DHT.DHT11
#sensor = Adafruit_DHT.DHT22
GPIO.setmode(GPIO.BOARD)
# Define a GPIO conectada ao pino de dados do sensor
pino_sensor = 4
# Informacoes iniciais
print ("*** Lendo os valores de temperatura e umidade");
while(1):
# Efetua a leitura do sensor
umid, temp = Adafruit_DHT.read_retry(sensor, pino_sensor);
# Caso leitura esteja ok, mostra os valores na tela
if umid is not None and temp is not None:
print ("Temperatura = {0:0.1f} Umidade = {1:0.1f}\n").format(temp, umid);
cursor.execute("""INSERT INTO temperatura (temp, umidade) VALUES ('temp', 'umid')""")
db.commit()
print ("Aguarda 5 segundos para efetuar nova leitura...\n");
time.sleep(5)
else:
# Mensagem de erro de comunicacao com o sensor
print("Falha ao ler dados do DHT11 !!!")
Rename the title of your question so that its name specifies the content. Tip: "How to insert the result of a query into the database". It’s just a suggestion :)
– DiChrist
What a mistake Vinicius ?
– Jorge Vidinha
does not save the result to the database.
– Vinicius Fernandes
You’re passing the strings
'temp', 'umid'
usVALUES
ofINSERT
should not be the variablestemp
andumid
– drgarcia1986