Web Weather > Weather Web Cam Links

Enter Weather Data On Webcam

<< < (2/2)

gianvi97:

--- Quote from: Daali on November 30, 2021, 09:18:49 PM ---
--- Quote from: gianvi97 on November 30, 2021, 01:21:19 PM ---Hi,
I have a weather station connected to wunderground that generates at this address

https://api.weather.com/v2/pws/observations/current?stationId=IMATER7&format=json&numericPrecision=decimal&units=m&apiKey=MIAAPIKEY

a string like this:


--- Code: ---{"observations":[{"stationID":"IMATER7","obsTimeUtc":"2021-11-30T17:34:06Z","obsTimeLocal":"2021-11-30 18:34:06","neighborhood":"Matera","softwareType":null,"country":"IT","solarRadiation":null,"lon":16.614463,"realtimeFrequency":null,"epoch":1638293646,"lat":40.742697,"uv":null,"winddir":292,"humidity":79.0,"qcStatus":1,"metric":{"temp":3.8,"heatIndex":3.8,"dewpt":0.5,"windChill":2.0,"windSpeed":7.1,"windGust":7.1,"pressure":1018.96,"precipRate":0.00,"precipTotal":0.00,"elev":395.0}}]}
--- End code ---

I want to use something, a script or other that sends this data (txt file) on my FTP in order to print it on the webcam.
For the webcam I have a php script on FTP to manage the shots from my Foscam and to print some writings that it takes from a txt file. The same script could take the txt file with the always updated weather data and put them on the last shot.
I don't know if anyone has tried this in the past and already uses something. Is it possible to do such a thing without having a PC running 24 hours a day?

Thanks

--- End quote ---

https://cron-job.org/en/ would work for your script without having a pc up.

--- End quote ---

I already use the cronjob. However, I would like a hand with the creation of the script. I made a draft which is this but it doesn't work ...


--- Code: ---<?php
    header("Refresh: 120; url=data.php")

$url = "https://api.weather.com/v2/pws/observations/current?stationId=IMATER7&format=json&numericPrecision=decimal&units=m&apiKey=mykey";
$parts = parse_url($url);
$output = [];
parse_str($parts['query'], $output);
echo $output['page'];

$file = fopen("file.txt", "w");
fwrite($file, $output['page']);
fclose($file);
?>
--- End code ---

I think the json format creates problems, maybe I should create arrays. I could get the data from the url like this:


--- Code: ---$json=fetchPageFromURL('myurl');
$data=json_decode($json,true);
print_r($data);
--- End code ---

The curl function might be fine too but I don't know how to set it up.

davidefa:
You can try something like this

--- Code: ---<?php
$url = "https://api.weather.com/v2/pws/observations/current?stationId=IMATER7&format=json&numericPrecision=decimal&units=m&apiKey=YOURAPIKEY";
$outFileName = "current.txt";
$debug = "1";

$json = file_get_contents($url);
$data = json_decode($json);
file_put_contents($outFileName, $data->observations[0]->obsTimeLocal."\n"); // first line without APPEND flag
file_put_contents($outFileName, $data->observations[0]->metric->temp."\n", FILE_APPEND); // subsequent lines with APPEND flag
file_put_contents($outFileName, $data->observations[0]->humidity."\n", FILE_APPEND);
// extend as required

/* available fields:
$data->observations[0]->obsTimeUtc
$data->observations[0]->obsTimeLocal
$data->observations[0]->solarRadiation
$data->observations[0]->uv
$data->observations[0]->windDir
$data->observations[0]->humidity
$data->observations[0]->metric->temp
$data->observations[0]->metric->heatIndex
$data->observations[0]->metric->dewpt
$data->observations[0]->metric->windChill
$data->observations[0]->metric->windSpeed
$data->observations[0]->metric->windGust
$data->observations[0]->metric->pressure
$data->observations[0]->metric->precipRate
$data->observations[0]->metric->precipTotal
*/

if ($debug)
   {
   // only for testing ( prints to video )
   print_r($data); echo "<br>";
   echo $data->observations[0]->obsTimeLocal."<br>";
   echo $data->observations[0]->metric->temp."<br>";
   echo $data->observations[0]->humidity."<br>";
   }
?>
--- End code ---

gianvi97:

--- Quote from: davidefa on December 01, 2021, 08:52:09 AM ---You can try something like this

--- Code: ---<?php
$url = "https://api.weather.com/v2/pws/observations/current?stationId=IMATER7&format=json&numericPrecision=decimal&units=m&apiKey=YOURAPIKEY";
$outFileName = "current.txt";
$debug = "1";

$json = file_get_contents($url);
$data = json_decode($json);
file_put_contents($outFileName, $data->observations[0]->obsTimeLocal."\n"); // first line without APPEND flag
file_put_contents($outFileName, $data->observations[0]->metric->temp."\n", FILE_APPEND); // subsequent lines with APPEND flag
file_put_contents($outFileName, $data->observations[0]->humidity."\n", FILE_APPEND);
// extend as required

/* available fields:
$data->observations[0]->obsTimeUtc
$data->observations[0]->obsTimeLocal
$data->observations[0]->solarRadiation
$data->observations[0]->uv
$data->observations[0]->windDir
$data->observations[0]->humidity
$data->observations[0]->metric->temp
$data->observations[0]->metric->heatIndex
$data->observations[0]->metric->dewpt
$data->observations[0]->metric->windChill
$data->observations[0]->metric->windSpeed
$data->observations[0]->metric->windGust
$data->observations[0]->metric->pressure
$data->observations[0]->metric->precipRate
$data->observations[0]->metric->precipTotal
*/

if ($debug)
   {
   // only for testing ( prints to video )
   print_r($data); echo "<br>";
   echo $data->observations[0]->obsTimeLocal."<br>";
   echo $data->observations[0]->metric->temp."<br>";
   echo $data->observations[0]->humidity."<br>";
   }
?>
--- End code ---

--- End quote ---
Thank you very much.

Works well.

I tried putting a header to update it but it doesn't work. Maybe I need to use a cronjob?

Also in the txt file I would need to insert a space between the various data and what they refer to for example 11.4, I want to see: temperatures 11.4 C etc. and all on one line without the date. I tried to remove the line:

"file_put_contents ($ outFileName, $ data-> observations [0] -> obsTimeLocal." \ n ");"

but it does not work


--- Code: ---<?php
header("Refresh: 120; url=script.php")
$url = "https://api.weather.com/v2/pws/observations/current?stationId=IMATER7&format=json&numericPrecision=decimal&units=m&apiKey=mykey";
$outFileName = "current.txt";
$debug = "1";

$json = file_get_contents($url);
$data = json_decode($json);
file_put_contents($outFileName, $data->observations[0]->obsTimeLocal); // first line without APPEND flag
file_put_contents($outFileName, $data->observations[0]->metric->temp."C", FILE_APPEND); // subsequent lines with APPEND flag
file_put_contents($outFileName, $data->observations[0]->humidity."%", FILE_APPEND);


// extend as required

/* available fields:
$data->observations[0]->obsTimeUtc
$data->observations[0]->obsTimeLocal
$data->observations[0]->solarRadiation
$data->observations[0]->uv
$data->observations[0]->windDir
$data->observations[0]->humidity
$data->observations[0]->metric->temp
$data->observations[0]->metric->heatIndex
$data->observations[0]->metric->dewpt
$data->observations[0]->metric->windChill
$data->observations[0]->metric->windSpeed
$data->observations[0]->metric->windGust
$data->observations[0]->metric->pressure
$data->observations[0]->metric->precipRate
$data->observations[0]->metric->precipTotal
*/

if ($debug)
   {
   // only for testing ( prints to video )
   print_r($data); echo "<br>";
   echo $data->observations[0]->metric->temp."<br>";
   echo $data->observations[0]->humidity."<br>";
   }
?>
--- End code ---

davidefa:
This will create a 'one line' text file.
You have to call this script from your overlay script ( just put a require_once("wuCurrent.php"); ad the beginning of the overlay script, considering you called this script wuCurrent.php )


--- Code: (php) ---<?php
$url = "https://api.weather.com/v2/pws/observations/current?stationId=IMATER7&format=json&numericPrecision=decimal&units=m&apiKey=YOURAPIKEY";
$outFileName = "current.txt";
$debug = "1";

$json = file_get_contents($url);
$data = json_decode($json);
file_put_contents($outFileName, "temperature " . $data->observations[0]->metric->temp . "C  "); // first line without APPEND flag
file_put_contents($outFileName, "humidity "    . $data->observations[0]->humidity     . "%  ", FILE_APPEND); // subsequent lines with APPEND flag
// extend as required

/* available fields:
$data->observations[0]->obsTimeUtc
$data->observations[0]->obsTimeLocal
$data->observations[0]->solarRadiation
$data->observations[0]->uv
$data->observations[0]->windDir
$data->observations[0]->humidity
$data->observations[0]->metric->temp
$data->observations[0]->metric->heatIndex
$data->observations[0]->metric->dewpt
$data->observations[0]->metric->windChill
$data->observations[0]->metric->windSpeed
$data->observations[0]->metric->windGust
$data->observations[0]->metric->pressure
$data->observations[0]->metric->precipRate
$data->observations[0]->metric->precipTotal
*/

if ($debug)
   {
   // only for testing ( prints to video )
   print_r($data); echo "<br>";
   echo $data->observations[0]->metric->temp."<br>";
   echo $data->observations[0]->humidity."<br>";
   }
?>
--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version