Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send data to windy.com #18

Open
HenrikBoChristoffersen opened this issue Jun 29, 2023 · 2 comments
Open

Send data to windy.com #18

HenrikBoChristoffersen opened this issue Jun 29, 2023 · 2 comments
Labels
[Feature] Extensibility and APIs All things related to new APIS, devices ... [Priority] Low [Type] Enhancement New feature or request
Milestone

Comments

@HenrikBoChristoffersen
Copy link

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
Copy link

iurban23 commented Jul 1, 2023

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

@HenrikBoChristoffersen
Copy link
Author

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

@jaz-on jaz-on self-assigned this Aug 1, 2023
@jaz-on jaz-on added [Type] Enhancement New feature or request [Feature] Extensibility and APIs All things related to new APIS, devices ... [Priority] Low labels Aug 3, 2023
@jaz-on jaz-on removed their assignment Aug 3, 2023
@jaz-on jaz-on added this to the 3.9 milestone Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility and APIs All things related to new APIS, devices ... [Priority] Low [Type] Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants