WXforum.net
May 18, 2013, 10:58:11 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
Members: 6609  •  Posts: 178206  •  Topics: 18100
Please welcome TargY, our newest member.
Welcome to the the new hosting for WXforum.net.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Submit weather data via PHP script  (Read 5351 times)
0 Members and 1 Guest are viewing this topic.
metherb
Member
*
Offline Offline

Posts: 8



« on: September 06, 2011, 02:09:48 PM »

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:

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($curlCURLOPT_URL'http://www.weatherdirect.com/TX60.aspx?&wusid=99999&bxid=9999999999');

// Tell cURL we would like headers as well
curl_setopt($curlCURLOPT_HEADER1);

// Tell cURL we would like the results as a string instead of just dumping it on the screen
curl_setopt($curlCURLOPT_RETURNTRANSFER1);

// Execute the cURL request
$data curl_exec($curl);

// Close the cURL request
curl_close($curl);

$temppstrpos($data"Sensor Name");  // Change this to your sensors name
$temp substr($data$tempp 2134) ;  // You may need to adjust this to read the right temperature

$humpstrpos($data"Sensor Name");  // Change this to your sensors name
$hum substr($data$hump 6102) ;  // 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:

Code:
//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!
Logged
iplay1515
Member
*
Offline Offline

Posts: 2



« Reply #1 on: January 14, 2012, 07:49:50 AM »

Is there any interest in capturing the data on the local network and avoiding the indirect approach?
Logged
weatherforyou
Forecaster
*****
Offline Offline

Posts: 461


My weather is on WeatherForYou.com. Is yours?


WWW
« Reply #2 on: January 14, 2012, 04:35:34 PM »

What about PWSweather.com?   Confused
Logged

Joe Torsitano

honnomr
Member
*
Offline Offline

Posts: 1



« Reply #3 on: November 11, 2012, 07:35:33 PM »

I have one of these Lacross Weather links that you hook up to a router, and would love to start up a simple temperature weatherstation on www.weatherunderground.com.  Unfortunatley, I'm no computer programmer, and don't understand the script you listed to gain access to the router temperature data.  Is there any possible way you can list how to make this happen using my wireless gateway from Lacross?  Thank you!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.098 seconds with 18 queries.