Author Topic: how to use php to send data to WU  (Read 1636 times)

0 Members and 1 Guest are viewing this topic.

Offline m77

  • Forecaster
  • *****
  • Posts: 878
how to use php to send data to WU
« on: October 08, 2011, 02:40:10 PM »
hope this helps someone else.

credit to the original which i just tweaked for WU

// Met Office Weather Observations Website Upload Script
// Created by Andrew Jones
// www.bordersweather.co.uk


i use a cron job to auto send my clientraw file from Weather Display to WU

Quote
<?php
// WU upload script adapted and edited by Martin Sutton @meschoolweather
// http://www.school-portal.co.uk/GroupHomepage.asp?GroupID=910590
// but adapted from and originally written by
// Met Office Weather Observations Website Upload Script
// Created by Andrew Jones
// www.bordersweather.co.uk
 
//ini_set("display_errors",1);
//error_reporting(E_ALL);
date_default_timezone_set("Europe/London");
//CONFIGURABLE SECTION
$cumulus = 0; //set to 1 if use use cumulus, 0 otherwise.
$wd = 1; //set to 1 if you use weather display, 0 otherwise.
$datafile = "clientraw.txt"; //change to your own route to data
$softwaretype = "WeatherDisplay"; //change to your software
 
$siteid = "XXXXXXXXX"; //set to the site id on the WU site
$authenticationpin = "XXXXXXXX"; //set to the pin you set up
$useCURL = 1; //change to 1 if fopen doesn't work.
//END OF CONFIGURABLE SECTION
 
if (file_exists($datafile) === FALSE) { echo "Cannot find $datafile"; exit; }
$data = explode(" ",file_get_contents($datafile));
 
if ($cumulus == 1) {
$fdate = $data[0];
$ftime = $data[1];
$temp = $data[2];
$hum = $data[3];
$dew = $data[4];
$wspeed = $data[6];
$wgust = $data[40];
$winddir = $data[7];
$rainday = $data[9];
$baro = $data[10];
$windunit = $data[13];
$tempunit = $data[14];
$barounit = $data[15];
$rainunit = $data[16];
$rainhour = $data[47];
}
else {
$fdate = $data[74];
$ftime = "{$data[29]}:{$data[30]}:{$data[31]}";
$temp = $data[4];
$dew = $data[72];
$hum = $data[5];
$wspeed = $data[1];
$wgust = $data[140];
$winddir = $data[3];
$rainday = $data[7];
$baro = $data[6];
$windunit = "kts";
$tempunit = "c";
$barounit = "hpa";
$rainhour = 0; //cludge for now till I can find a better way to do it.
$rainunit = "mm";
}
 
if (stristr($tempunit,"c")) {
$temp = ConvertCtoF($temp);
$dew = ConvertCtoF($dew);
}
 
if (stristr($windunit,"kts")) {
$wspeed = KTStoMPH($wspeed,2);
$wgust = KTStoMPH($wgust,2);
}
 
if ((stristr($barounit,"hpa")) OR (stristr($barounit,"mb"))) {   
$baro = HPAtoINHG($baro);
}
 
if (stristr($rainunit,"mm")) {
$rainday = MMtoINCHES($rainday);
$rainhour = MMtoINCHES($rainhour);
}
 
$time = explode(":",$ftime);
$date = explode("/",$fdate);
$ourdate = mktime($time[0],$time[1],$time[2],$date[1],$date[0],$date[2]);
if (date('I') == 1) { $ourdate = strtotime("-1 hour",$ourdate); }
 
$oururl = "http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID={$siteid}&PASSWORD={$authenticationpin}";
$oururl .= "&dateutc=" .urlencode(date('Y-m-d G:i:s',$ourdate));
$oururl .= "&winddir={$winddir}&windspeedmph={$wspeed}&windgustmph={$wgust}&humidity={$hum}&dewptf={$dew}";
$oururl .= "&tempf={$temp}";
$oururl .= "&rainin={$rainhour}&dailyrainin={$rainday}&baromin={$baro}";
$oururl .= "&softwaretype={$softwaretype}&action=updateraw";
echo "Sending:<br/>$oururl";
 
if ($useCURL == 1) {
$ch = curl_init();
$timeout = 30;
curl_setopt($ch,CURLOPT_URL,$oururl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$ret = curl_exec($ch);
curl_close($ch);
} else {
$ret = file_get_contents($oururl);
}
 
 
function ConvertMPHtoKTS($mph) {
$kts = $mph * 0.868976242;
return $kts;
}
 
 
function ConvertCtoF($degc) {
$degf = round($degc = ((1.8* $degc) + 32),2);
return $degf;
}
 
 
// MMtoINCHES: converts mm to inches
function MMtoINCHES($value) {
  return round($value = (0.0393700787 * $value),2);
} // end function MMtoINCHES
 
 
// HPAtoINHg: converts hPa to inHg
function HPAtoINHG($value) {
  return round($value = (0.295300 * $value)/10,2);
} // end function HPAtoINHG
 
 
// KTStoMPH: converts KTS to MPH
function KTStoMPH($value, $precision) {
//  global $wind_prec;
  return round($value = (1.1507794 * $value),$precision);
} // end function KTStoMPH
 
 
?>


Weatherlink ip, Vantage Vue,

 

anything