- 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)
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.
Next step: tweaks
No comments:
Post a Comment