maandag 2 januari 2017

THE ELECTRONIC BAROMETER (2)



It is nice to read real time the changes in the pressure as an indication of changes in the weather. However, the observations once published are lost.  It would be much better to store the measurements as data in for example MYSQL and publish the data from the database in Wordpress.

WORDPRESS

The  Raspberry  weather station has a nice solution. It publishes measurements of sensors about temperature and humidity in a database and publishes the data on a Wordpress blog. To install just follow the software setup and the optional. Here are the basic steps
-          Install on the Raspberry: Apache, MYSQL and PHP;
-          Install Wordpress;
-          Create weather tables in the database Wordpress
-          Make changes in the Python script so it reads the sensors of the Sense Hat
-          Download the Raspberry plugin for Wordpress and create a blog page
-          Finally download the App for the Raspberry weather station.
Here is rewritten script for the sensors of the Sense Hat reading temperature, humidity and pressure.
#!/usr/bin/python
import sys
import subprocess
import re
import os
import time
from sense_hat import SenseHat
import MySQLdb as mdb
import datetime

databaseUsername="xxxxx"
databasePassword="xxxxxxx"
databaseName="wordpress" #do not change unless you named the Wordpress database with some other name
sense = SenseHat()
temperature = sense.get_temperature()
pressure = sense.get_pressure()
humidity = sense.get_humidity()
# formula see below
temp = round ( 0.0071*temperature*temperature+0.86*temperature-10.0,1)
humidity = round ( humidity*(2.5-0.029*temperature),1)
pressure = round(pressure, 1)


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, pressure) VALUES (%s,%s,%s,%s,%s)",(temperature,humidity,currentDate, minutes, pressure))

          print "Saved temperature, humidity and pressure"
          return "true"


def readInfo():

   #humidity, temperature = Adafruit_DHT.read_retry(sensor, pinNum)#read_retry - retry getting temperatures for 15 times

   print "Temperature: %.1f C" % temperature
   print "Humidity:    %s %%" % humidity
        print "Pressure:    %s hPa" % pressure
   if humidity is not None and temperature is not None:
          return saveToDatabase(temperature,humidity) #success, save the readings
   else:
          print 'Failed to get reading. Try again!'
          sys.exit(1)


#check if table is created or if we need to create one
try:
   queryFile=file("createTable.sql","r")

   con=mdb.connect("localhost", databaseUsername,databasePassword,databaseName)
        currentDate=datetime.datetime.now().date()

        with con:
          line=queryFile.readline()
          query=""
          while(line!=""):
                 query+=line
                 line=queryFile.readline()
         
          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")
  

except IOError:
   pass #table has already been created
  

status=readInfo() #get the readings
subprocess.call(os.path.dirname(os.path.realpath(__file__))+"/createXml.py %s %s %s" % (databaseUsername,databasePassword, databaseName), shell=True)

os.rename("/var/www/output.xml", "/var/www/html/output.xml")

In the following posting – the electronic barometer 3 – I am going to discuss:
-          Make a query of the MYSQL database export as .csv and import in Excel for analysis and visualization in Google charts of Plot.Ly
-          Make a connection between the database and R Project, query the data and make an analysis with R, visualizing with R using plot, ggplot, googleVis and plotly.

Geen opmerkingen:

Een reactie plaatsen

Opmerking: alleen leden van deze blog kunnen een reactie plaatsen.