Update new Version

New createTable.sql with new rows for more sensors.
New scheduler.py put more sensor data into the new database design.
Update weather.py for import data into scheduler.py
This commit is contained in:
unixweb 2015-05-21 19:14:11 +02:00
parent 4392f8f185
commit 30d55ba1b5
3 changed files with 73 additions and 67 deletions

View File

@ -1,8 +1,15 @@
CREATE TABLE IF NOT EXISTS `temperatures` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`temperature` double NOT NULL,
`humidity` varchar(20) NOT NULL,
`dateMeasured` date NOT NULL,
`hourMeasured` int(128) NOT NULL,
`temperature_1` double NOT NULL,
`temperature_2` double NOT NULL,
`temperature_3` double NOT NULL,
`temperature_4` double NOT NULL,
`pressure` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`pressure_sea` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`humidity` varchar(20) NOT NULL,
`altitude` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

119
scheduler.py Normal file → Executable file
View File

@ -7,83 +7,76 @@ import sys
import time
import MySQLdb as mdb
import datetime
from weather import *
databaseUsername="XXXX"
databasePassword="XXXX"
databaseName="XXXX" #do not change unless you named the Wordpress database with some other name
databaseHost="XXXX"
def saveToDatabase(temperature,humidity):
con=mdb.connect("localhost", databaseUsername, databasePassword, databaseName)
currentDate=datetime.datetime.now().date()
now=datetime.datetime.now()
midnight=datetime.datetime.combine(now.date(),datetime.time())
minutes=((now-midnight).seconds)/60 #minutes after midnight, use datead$
with con:
cur=con.cursor()
cur.execute("INSERT INTO temperatures (temperature,humidity, dateMeasured, hourMeasured) VALUES (%s,%s,%s,%s)",(temperature,humidity,currentDate, minutes))
print "Saved temperature"
return "true"
def readInfo():
temperatureSaved="false" #keep on reading till you get the info
while(temperatureSaved=="false"):
# Run the DHT program to get the humidity and temperature readings!
output = subprocess.check_output(["/home/pi/myweather/wetter.py"]);
print output
matches = re.search("Temp =\s+([-]?[0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
temp = float(matches.group(1))
# search for humidity printout
matches = re.search("Press =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
humidity = float(matches.group(1))
#humidity=str(humidity)+"%"
print "Temperature: %.1f C" % temp
print "Pressure: %s %%" % humidity
return saveToDatabase(temp,humidity)
#check if table is created or if we need to create one
try:
queryFile=file("createTable.sql","r")
queryFile=file("createTable.sql","r")
con=mdb.connect("localhost", databaseUsername,databasePassword,databaseName)
currentDate=datetime.datetime.now().date()
con=mdb.connect(databaseHost,databaseUsername,databasePassword,databaseName)
currentDate=datetime.datetime.now().date()
with con:
line=queryFile.readline()
query=""
while(line!=""):
query+=line
line=queryFile.readline()
with con:
line=queryFile.readline()
query=""
while(line!=""):
query+=line
line=queryFile.readline()
cur=con.cursor()
cur.execute(query)
cur=con.cursor()
cur.execute(query)
#now rename the file, because we do not need to recreate the table everytime this script is run
queryFile.close()
os.rename("createTable.sql","createTable.sql.bkp")
#now rename the file, because we do not need to recreate the table everytime this script is run
queryFile.close()
os.rename("createTable.sql","createTable.sql.bkp")
except IOError:
pass #table has already been created
pass #table has already been created
def saveToDatabase(temp1, temp2, temp3, temp4, humidity, pressure, sea_pressure, altitude):
#print "SaveToDatabase Hit"
con=mdb.connect(databaseHost, databaseUsername, databasePassword, databaseName)
currentDate=datetime.datetime.now().date()
print currentDate
now=datetime.datetime.now()
midnight=datetime.datetime.combine(now.date(),datetime.time())
minutes=((now-midnight).seconds)/60 #minutes after midnight, use datead$
with con:
cur=con.cursor()
query = ("INSERT INTO `temperatures`(`temperature_1`, `temperature_2`, `temperature_3`, `temperature_4`, `humidity`, `dateMeasured`, `hourMeasured`, `pressure`, `pressure_sea`, `altitude`)"
"VALUES (%s,%s,%s,%s,%s,'%s',%s,%s,%s,%s)"% (temp1, temp2, temp3, temp4, humidity, currentDate, minutes, pressure, sea_pressure, altitude))
cur.execute(query)
print "Saved temperature"
return "false"
def readInfo():
#print "ReadInfo Hit"
temperatureSaved="false"
while(temperatureSaved=="false"):
temp1 = Temp1
temp2 = Temp2
temp3 = Temp3
temp4 = Temp4
humidity=Humidity
altitude = Altitude
pressure = Pressure
sea_pressure = Sea_pressure
#temperatureSaved="true"
return saveToDatabase(temp1, temp2, temp3, temp4, humidity, pressure, sea_pressure, altitude)
status=readInfo()
status="false"
while(status!="true"):
status=readInfo()

10
weather.py Normal file → Executable file
View File

@ -25,5 +25,11 @@ sensor = BMP085.BMP085()
# consumption are primarily the differences). The default mode is STANDARD.
#sensor = BMP085.BMP085(mode=BMP085.BMP085_ULTRAHIGHRES)
print 'Temp = {0:0.2f} *C'.format(sensor.read_temperature())
print 'Press = {0:0.2f} hPa'.format(sensor.read_pressure()/100)
Temp1 = format(sensor.read_temperature())
Temp2 = format(sensor.read_temperature())
Temp3 = format(sensor.read_temperature())
Temp4 = format(sensor.read_temperature())
Pressure = format(sensor.read_pressure()/100)
Sea_pressure = format(sensor.read_sealevel_pressure()/100)
Altitude = format(sensor.read_altitude())
Humidity= 57