Author Topic: Submit weather data via PHP script  (Read 11661 times)

0 Members and 1 Guest are viewing this topic.

Offline metherb

  • Member
  • *
  • Posts: 8
Submit weather data via PHP script
« 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: [Select]
<?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: [Select]
//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!

Offline iplay1515

  • Member
  • *
  • Posts: 2
Re: Submit weather data via PHP script
« 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?

Offline weatherforyou

  • Forecaster
  • *****
  • Posts: 545
  • My weather is on WeatherForYou.com. Is yours?
    • http://www.weatherforyou.com
Re: Submit weather data via PHP script
« Reply #2 on: January 14, 2012, 04:35:34 PM »
What about PWSweather.com?   :?
Joe Torsitano


Offline honnomr

  • Member
  • *
  • Posts: 1
Re: Submit weather data via PHP script
« 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!

Offline satmaster

  • Member
  • *
  • Posts: 13
Re: Submit weather data via PHP script
« Reply #4 on: February 26, 2015, 02:12:38 PM »
IM using this in my php script but ran into problems rounding rain data.

You can not use round with the rain data because it will trim the trailing zeros. And submit .30 as .03
You have to use number format.
Code: [Select]
$a = number_format($in, 2, '', '');
$out = substr("000$a", -3);

if ($solarrad>999) { $APRSdata = $APRSdata . "l".$solarradR;}
else{$APRSdata = $APRSdata . "L".$solarradR;}

Also if solar radiation is over 999 you have to switch to a lower case L
 

The dew point code doesn't work. It generates a dewpoint higher than the temp or sometimes the same as the temp.

example outdoor temp is 35.6 hum is  34% the result should be 13 something but the result is 35.6 or higher






« Last Edit: March 06, 2015, 10:59:40 AM by satmaster »

 

anything