├── .gitignore ├── production ├── createTable.sql └── getInfo.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /production/createTable.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `temperatures` ( 2 | `id` int(255) NOT NULL AUTO_INCREMENT, 3 | `temperature` double NOT NULL, 4 | `humidity` varchar(20) NOT NULL, 5 | `dateMeasured` date NOT NULL, 6 | `hourMeasured` int(128) NOT NULL, 7 | PRIMARY KEY (`id`) 8 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raspberry Weather DHT22 2 | 3 | ## Introduction 4 | 5 | This code helps you read temperatures along with humidity and save both in your database. You need to configure your weather station in order to use this code. Read more on www.raspberryweather.com. 6 | 7 | The PressureWind branch (http://goo.gl/JXv3Ht) created by A. Catorcini is an expansion to the basic functionalities. Take a look if you want to display wind speed and barometric pressure! 8 | 9 | ## Installation 10 | 11 | First, you need to install the DHT22 library by Adafruit and some additional software: 12 | 13 | > git clone https://github.com/adafruit/Adafruit_Python_DHT.git 14 | 15 | > cd Adafruit_Python_DHT 16 | 17 | > sudo apt-get update 18 | 19 | > sudo apt-get install build-essential python-dev python-openssl 20 | 21 | > sudo python setup.py install 22 | 23 | If everything is wired correctly, test the setup with 24 | > sudo ./AdafruitDHT.py 22 4 25 | 26 | Where 22 is the DHT22 sensor and 4 represents pin #4 27 | -------------------------------------------------------------------------------- /production/getInfo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import sys 3 | import Adafruit_DHT 4 | 5 | import subprocess 6 | import re 7 | import os 8 | import time 9 | import MySQLdb as mdb 10 | import datetime 11 | 12 | databaseUsername="YOUR USERNAME, USUALLY ROOT" 13 | databasePassword="YOUR PASSWORD!" 14 | databaseName="WordpressDB" #do not change unless you named the Wordpress database with some other name 15 | 16 | sensor=Adafruit_DHT.DHT22 #if not using DHT22, replace with Adafruit_DHT.DHT11 or Adafruit_DHT.AM2302 17 | pinNum=4 #if not using pin number 4, change here 18 | 19 | def saveToDatabase(temperature,humidity): 20 | 21 | con=mdb.connect("localhost", databaseUsername, databasePassword, databaseName) 22 | currentDate=datetime.datetime.now().date() 23 | 24 | now=datetime.datetime.now() 25 | midnight=datetime.datetime.combine(now.date(),datetime.time()) 26 | minutes=((now-midnight).seconds)/60 #minutes after midnight, use datead$ 27 | 28 | 29 | with con: 30 | cur=con.cursor() 31 | 32 | cur.execute("INSERT INTO temperatures (temperature,humidity, dateMeasured, hourMeasured) VALUES (%s,%s,%s,%s)",(temperature,humidity,currentDate, minutes)) 33 | 34 | print "Saved temperature" 35 | return "true" 36 | 37 | 38 | def readInfo(): 39 | 40 | humidity, temperature = Adafruit_DHT.read_retry(sensor, pinNum)#read_retry - retry getting temperatures for 15 times 41 | 42 | print "Temperature: %.1f C" % temperature 43 | print "Humidity: %s %%" % humidity 44 | 45 | if humidity is not None and temperature is not None: 46 | return saveToDatabase(temperature,humidity) #success, save the readings 47 | else: 48 | print 'Failed to get reading. Try again!' 49 | sys.exit(1) 50 | 51 | 52 | #check if table is created or if we need to create one 53 | try: 54 | queryFile=file("createTable.sql","r") 55 | 56 | con=mdb.connect("localhost", databaseUsername,databasePassword,databaseName) 57 | currentDate=datetime.datetime.now().date() 58 | 59 | with con: 60 | line=queryFile.readline() 61 | query="" 62 | while(line!=""): 63 | query+=line 64 | line=queryFile.readline() 65 | 66 | cur=con.cursor() 67 | cur.execute(query) 68 | 69 | #now rename the file, because we do not need to recreate the table everytime this script is run 70 | queryFile.close() 71 | os.rename("createTable.sql","createTable.sql.bkp") 72 | 73 | 74 | except IOError: 75 | pass #table has already been created 76 | 77 | 78 | status=readInfo() #get the readings 79 | --------------------------------------------------------------------------------