- 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 sensorst1 = sense.get_temperature()
t2 = sense.get_temperature_from_pressure();
t3 = sense.get_temperature_from_humidity()
p = sense.get_pressure()
h = sense.get_humidity()
# 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')
# Display on the Sense Hat's LCD because we can
msg = "Temperature = %s, Pressure=%s, Humidity=%s" % (calctemp,calcp,calchum)
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:
$data = file_get_contents("/var/www/html/temp.html");
$a = explode("!", $data);
$dew = $a - ((100 - $a)/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."&tempf=".($a*1.8+32)."&baromin=".($a/1000*29.5)."&dewptf=".($dew*1.8+32));
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.
Next step: tweaks
Post a Comment