Weather-Station-Software / live-weather-station

Display on your WordPress site, in many elegant ways, the meteorological data collected by public or personal weather stations.
https://weather.station.software/
GNU General Public License v2.0
7 stars 2 forks source link

Send data to windy.com #18

Open HenrikBoChristoffersen opened 1 year ago

HenrikBoChristoffersen commented 1 year ago

Hi Jason

Congratulation with taking over the source code for weather station it was long needed, that someone carried it on onwards. We have been using the weather station module for number of years now and have developed quite a page combining data from weather station with other data sources see more details on: https://www.gillelejesejlklub.dk/vejret/

I'm also sharing data from weather station to https://stations.windy.com/ using some code I have built.

You are free to use the source code below if you want to include the capability in weather station.

function ws_get_windy_token( $token = "") { $token = " Register your station on https://stations.windy.com/ to get key "; // <- Replace this with your own token return $token; }

//-----------------------> Post to windy.com <---------------------------------

function ws_dopost($url, $json) { $fields = (is_array($json)) ? http_build_query($json) : $json;

if($ch = curl_init($url)) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $json = curl_exec($ch);

  $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  $json =   curl_getinfo($ch, CURLINFO_HTTP_CODE); 

  curl_close($ch);

  return (int) $status;

} else { return false; } }

//-----------------------> Get the weatherlink data for you upload <---------------------------------

function ws_get_weatherlinkdata($message = "" ) {

$temp   = do_shortcode("[live-weather-station-textual device_id='wl:1d:0a:00:38:b2' module_id='m1:c0:00:00:00:01' measure_type='temperature' element='measure_value' format='computed' fx='none' color='#000000' speed='2000']");
$wind   = do_shortcode("[live-weather-station-textual device_id='wl:1d:0a:00:38:b2' module_id='m2:c0:00:00:00:01' measure_type='windstrength' element='measure_value' format='computed' fx='none' color='#000000' speed='2000']");
$windir = do_shortcode("[live-weather-station-textual device_id='wl:1d:0a:00:38:b2' module_id='m2:c0:00:00:00:01' measure_type='windangle' element='measure_value' format='raw' fx='none' color='#000000' speed='2000']");
$gust   = do_shortcode("[live-weather-station-textual device_id='wl:1d:0a:00:38:b2' module_id='m2:c0:00:00:00:01' measure_type='guststrength' element='measure_value' format='computed' fx='none' color='#000000' speed='2000']");
$rh     = do_shortcode("[live-weather-station-textual device_id='wl:1d:0a:00:38:b2' module_id='m1:c0:00:00:00:01' measure_type='humidity' element='measure_value' format='raw' fx='none' color='#000000' speed='2000']");

$curdatetime = date_format(date_create(),"Y-m-d").'T'.date_format(date_create(),"H:i:s");

$json =  '{"stations": [
                        {"station":0, "name":"Name of station", "lat":xx.xxxxxxx, "lon":xx.xxxxxxxx, "elevation":0, "tempheight":2, "windheight":10}        
                    ],
          "observations": [
                    {"station":0, "dateutc":"'.$curdatetime.'", "temp":'.$temp.', "wind":'.$wind.', "winddir":'.$windir.', "gust":'.$gust.', "rh":'.$rh.'}
                        ] }' ;

return $json;

}

//-----------------------> main code code for sending data to windy <---------------------------------

function ws_put_weatherdata_to_windy( ) {

$url   = "https://stations.windy.com/pws/update/".ws_get_windy_token();
$json =  ws_get_weatherlinkdata();
$status = ws_dopost($url, $json);

}

//-----------------------> adding the cron task for schedule job <--------------------------------- //-----------------------> remember to set the scedule to max 5 minutes <---------------------------------

add_action( 'init', function () {

$hook = 'run_windy_update';
$args = array();

if ( ! wp_next_scheduled( $hook, $args ) ) {
    wp_schedule_event( time(), 'five_minutes', $hook, $args );
}

} );

add_action( 'run_windy_update', function () {

ws_put_weatherdata_to_windy();

} );

iurban23 commented 1 year ago

hi Henrik, very interesting!!! where I have to put this code?

HenrikBoChristoffersen commented 1 year ago

Hi @iurban23

I'm using the plugin code snippet to manage to code. this creates a hook into the function.php so you dont need to change the code there. I also recommend to use Advanced Cron Manager to check of the cron scheduled job is added and running