Last active
September 10, 2016 08:11
-
-
Save jezman/de4f64e1b878f8f63d1b884dc94024fe to your computer and use it in GitHub Desktop.
RaspberryPi + DHT11 + PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import Adafruit_DHT | |
import psycopg2 | |
from time import strftime | |
DHT_MODEL = 11 | |
GPIO = 4 | |
humidity, temperature = Adafruit_DHT.read_retry(DHT_MODEL, GPIO) | |
insert = ('INSERT INTO week_temp (temp, humidity, time) VALUES\ | |
({:.0f}, {:.0f}, \'{}\'::timestamp);' | |
.format(temperature, humidity, strftime('%Y-%m-%d %H:%M:'))) | |
try: | |
connect = psycopg2.connect(database='db_name', user='user_name', | |
host='ip_addr', password='passwd') | |
cursor = connect.cursor() | |
cursor.execute(insert) | |
connect.commit() | |
connect.close() | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment