Simple php script to upload Tempest data to OpenWeather

Hi,

I am using the following php scripts, the first to create a station using OpenWeather API (run only once) and the second to grab data from my WeatherFlow Tempest station and then successfully upload them to OpenWeatherMap.org.
A cron job is used to run the second script every 10 minutes.
No additional weather software is needed.

Enjoy,
Kostas

<?php

/* 
 * This php script creates an OpenWeather station and returns its station_id
 * Run it only once.
 */

// Register (create) PWS with OpenWheather

$url1 = 'http://api.openweathermap.org/data/3.0/stations?appid=XXXXXXXXXXX';

$ch = curl_init( $url1 );
// Setup request to send json via POST - Use your own info
$payload = '{
  "external_id": "XXXXX_TEST001",
  "name": "XXXXXXXXXXXXX",
  "latitude": XX.XXXXX,
  "longitude": XX.XXXXX,
  "altitude": XXX
}';
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// Send request.
$result = curl_exec($ch);
curl_close($ch);

$result = json_decode($result);
$station_id = $result->ID;
echo 'station_id = ' . "$station_id";
<?php

// ================================ Step01: Download data from PWS ===============================

// Download data from WeatherFlow Tempest station (use your own Token)

$url = 'https://swd.weatherflow.com/swd/rest/observations/station/XXXXXX?token=XXXXXXXXXXXXXXXXXXX';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl)

// Decode data so they can be used
$data = json_decode($data);

// Calculate variables

$station_id = 'XXXXXXXXXXXXXXXXXXXX';   // this is known from station registration (creation)
$dt = $data->obs[0]->timestamp;
$temperature = $data->obs[0]->air_temperature;
$wind_speed = $data->obs[0]->wind_avg;
$wind_speed = 0.83 * ($wind_speed ** 1.5);  // convert from bft to m/s
$wind_speed = round($wind_speed, 1);
$wind_deg = $data->obs[0]->wind_direction;
$wind_gust = $data->obs[0]->wind_gust;
$wind_gust = 0.83 * ($wind_gust ** 1.5);    // convert from bft to m/s
$wind_gust = round($wind_gust, 1);
$humidity = $data->obs[0]->relative_humidity;
$dew_point =  $data->obs[0]->dew_point;
$pressure = $data->obs[0]->barometric_pressure;
$rain_1h = $data->obs[0]->precip_accum_last_1hr;
$rain_24h = $data->obs[0]->precip_accum_local_day;
$heat_index = $data->obs[0]->heat_index;

// =========================== Step02: Upload data to OpenWeather ===========================

$url3 = "https://api.openweathermap.org/data/3.0/measurements?station_id=$station_id&appid=XXXXXXXXXXXXXXXXXX";

$ch3 = curl_init();
// Setup request to send json via POST.

$payload = [
      "station_id" => $station_id,
      "dt" => $dt,
      "temperature" => $temperature,
      "wind_speed" => $wind_speed,
      "wind_deg" => $wind_deg,
      "wind_gust" => $wind_gust,
      "humidity" =>  $humidity,
      "dew_point" => $dew_point,
      "pressure" => $pressure,
      "rain_1h" => $rain_1h,
      "rain_24h" => $rain_24h,
      "heat_index" => $heat_index
];

$payload = json_encode( array($payload) );

curl_setopt( $ch3, CURLOPT_URL, $url3);
curl_setopt( $ch3, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch3, CURLOPT_POSTFIELDS, $payload );
// Return response instead of printing.
curl_setopt( $ch3, CURLOPT_RETURNTRANSFER, true );
// Send request.
$result3 = curl_exec($ch3);
curl_close($ch3);

4 Likes