In looking at a cheap way to submit wetaher data to the Weather Underground and CWOP without the expense of setting up a full weather station, I created a PHP script which can read temperature & humidity data from a LaCrosse TX60U with a sensor gateway and submit the data to either the Weather Underground, CWOP or both. The sensor and gateway can be obtained for under $50 making it an economical alternative for weather monitoring.
It basically works like this:
1) The temperature is read on the sensor and is transmitted to the gateway
2) The gateway transmits the data to the weatherdirect.com site
3) The script, (loaded on a PHP enabled web host) runs as a cron job every 10 minutes, reads the data and forms a URL that is submitted to the WU or creates a connection to the CWOP servers and submits the data there.
I'm not a real programmer but I was able to work out the code that works and wanted to share with anyone else trying to do the same.
Here's the code:
<?php
// Temperature sensor section - Some code borrowed from another script
// Init $curl as a cURL object
$curl = curl_init();
// Tell cURL what URL we are going after - change this to your mobile URL
curl_setopt($curl, CURLOPT_URL, 'http://www.weatherdirect.com/TX60.aspx?&wusid=99999&bxid=9999999999');
// Tell cURL we would like headers as well
curl_setopt($curl, CURLOPT_HEADER, 1);
// Tell cURL we would like the results as a string instead of just dumping it on the screen
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute the cURL request
$data = curl_exec($curl);
// Close the cURL request
curl_close($curl);
$tempp= strpos($data, "Sensor Name"); // Change this to your sensors name
$temp = substr($data, $tempp + 213, 4) ; // You may need to adjust this to read the right temperature
$hump= strpos($data, "Sensor Name"); // Change this to your sensors name
$hum = substr($data, $hump + 610, 2) ; // You may need to adjust this to read the right humidity
// Since we know the temperature and humidity, we can calculate the dewpoint and submit that as well
// First, we convert the temp to celcius
$tempc = (($temp-32)/1.8);
// Knowing the temp in celcius we can calculate the dewpoint
$dpc = round((-430.22+237.7*log($hum*(6.11*pow(10, (7.5*$tempc/(237.7+$tempc))))/100))/(-log($hum*(6.11*pow(10, (7.5*$tempc/(237.7+$tempc))))/100)+19.08),1);
// Now we can convert the dewpoint in celcius to fahrenheit
$dpf = round((($dpc*1.8)+32),1);
//set POST variables - Be sure to change the station ID and password to yours!
$url = 'http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID=WUID&PASSWORD=PASSWORD';
$utc_wu = gmdate("Y-m-d+H:i:s", time());
$fields = array(
'dateutc'=>urlencode($utc_wu),
'tempf'=>urlencode($temp),
'humidity'=>urlencode($hum),
'dewptf'=>urlencode($dpf)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
// If you have a MADIS ID, insert that code below this line
?>
The CWOP portion is here:
//CWOP Section
$utc_cwop = gmdate("dHi", time());
$APRSTempR = round(($temp),0);
$APRSTemp = "0$APRSTempR";
$APRSHum = "$hum";
$APRSdata ="";
$APRSdata = "CW1234"; // CWOP ID or callsign -> change this to yours!!!!
$APRSdata = $APRSdata . ">APRS,TCPXX*:"; // network type
$APRSdata = $APRSdata . "/".$utc_cwop; // time of report
$APRSdata = $APRSdata . "z4157.47N/07218.38W"; // lat long -> change this to yours!!!!
$APRSdata = $APRSdata . "_...";
$APRSdata = $APRSdata . "/...";
$APRSdata = $APRSdata . "g...";
$APRSdata = $APRSdata . "t".$APRSTemp;
$APRSdata = $APRSdata . "r..."; // rain in last hour
$APRSdata = $APRSdata . "p..."; // rain in last 24
$APRSdata = $APRSdata . "P..."; // rain since midnight
$APRSdata = $APRSdata . "h".$hum;
$APRSdata = $APRSdata . "b...";
$APRSdata = $APRSdata . "ehomebrew";
// Change the user to your CWOP ID
$fp = fsockopen("cwop.aprs.net", 14580, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
$out = "user CW1234 pass -1 vers linux-1wire 1.00\r\n";
fwrite($fp, $out);
sleep(3);
$out = "$APRSdata\r\n`";
// echo "\n".$APRSdata;
fwrite($fp, $out);
sleep(3);
fclose($fp);
}
If you're on a budget and want to submit other data elements, you could also add components through a console and use the Cumulus software to submit a TXT file that you could read and format as well. I did this with a rain guage and because the console has a barometer, I could also submit that. The rain guage and base unit cost me about $60 and allowed me to have the temperature and rain guage in two different places. You can always add sensors as tme and money allows.
I hope this helps someone out!