Wednesday 29 June 2016

Metpi #3: software

For the software side of the Metpi, I wanted to do two things:
- Log the data with the other pis from the Radpi system.
- Publish the data to the Weather Underground platform.

I've therefore split the work into two programs: one reads the data and makes it available to the Radpi server, the other sends the results to the Wu server.

Reading the sensor data is easiest done in python. I don't know python, but it's easily done with a few lines of code. The following reads the sensor data, displays it on the led screen of the Sensor Hat, and makes it available to the Radpi server (see Radpi setup to build the pi for that purpose):

from sense_hat import SenseHat
sense = SenseHat()

# Read the core's temperature
tFile = open('/sys/class/thermal/thermal_zone0/temp')
temp = float(tFile.read())
tempC = temp/1000

print "tempC=" + str(tempC)

# Read the Sense Hat's sensors
t1 = sense.get_temperature()
t2 = sense.get_temperature_from_pressure();
t3 = sense.get_temperature_from_humidity()
p = sense.get_pressure()
h = sense.get_humidity()

print "t1="+str(t1)
print "t2="+str(t2)
print "t3="+str(t3)
print "p="+str(p)

print "h="+str(h)

# Do some adjustments
avgtemp = (t1+t2+t3)/3
calctemp = avgtemp - ((tempC-avgtemp)/10)

calchum = h + 4
calcp = p + 6

print "calctemp=" + str(calctemp)
print "calchum=" + str(calchum)
print "calcp=" + str(calcp)

# Save the data for the Radpi server
f = open('/var/www/html/temp.html', 'w')
f.write(str(calctemp)+'!'+str(calchum)+'!'+str(calcp))
f.close()

# Display on the Sense Hat's LCD because we can
msg = "Temperature = %s, Pressure=%s, Humidity=%s" % (calctemp,calcp,calchum)

sense.show_message(msg, scroll_speed=0.05)

Note that the Sense Hat design has a major flaw: some of the sensors are very close to the pi's CPU and therefore are affected by it. It is mainly noticeable for the temperature, but humidity should also be affected. This is why the script contains a few adjustments based on values provided by another station nearby. As the effect of the CPU heat on the temperature sensor will be proportional to how busy/hot the pi is, we take all temperature sensors' readings, and substract a proportion of the difference with the pi's CPU temperature.





The script is scheduled to run every minute in crontab. I simply added another target to the Radpi central server to collect that data and store it in a table with temperature, humidity, and pressure fields. I also added the graph on the home temperature page.


The other script, in PHP this time since everything else is, reads the file created by the python script, and sends the information to the Wu server:

<?php

$data = file_get_contents("/var/www/html/temp.html");
$a = explode("!", $data);
$dew = $a[0] - ((100 - $a[1])/5);

$response = file_get_contents("http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?action=updateraw&ID=YOURIDHERE&PASSWORD=YOURPASSWORD&dateutc=".urlencode(gmdate("Y-M-d H:i:s"))."&humidity=".$a[1]."&tempf=".($a[0]*1.8+32)."&baromin=".($a[2]/1000*29.5)."&dewptf=".($dew*1.8+32));
print($response);

?>

The dew point is approximated* but that should be sufficiently accurate. All units are converted to imperial on the fly as that's what Wu requires. The script is scheduled to run every 5 minutes in crontab.

The data generated and uploaded by the station is now available on Wu.




* Simple dew point approximation: Lawrence, Mark G., 2005: The relationship between relative humidity and the dewpoint temperature in moist air: A simple conversion and applications. Bull. Amer. Meteor. Soc.86, 225-233. doi: http;//dx.doi.org/10.1175/BAMS-86-2-225

Next step: tweaks



No comments:

Post a Comment