Skip to content

Instantly share code, notes, and snippets.

@jezman
Last active September 10, 2016 08:11
Show Gist options
  • Save jezman/de4f64e1b878f8f63d1b884dc94024fe to your computer and use it in GitHub Desktop.
Save jezman/de4f64e1b878f8f63d1b884dc94024fe to your computer and use it in GitHub Desktop.
RaspberryPi + DHT11 + PostgreSQL
#!/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