Import weather readings in *.php

(Translate)
Hello
Is it possible to import weather readings in *.php on Weather Display?

Serge

Hi there

In WD, Webfiles/Web Page Real Time FTP/WDL go to tab Webfiles Setup #1.
Halfway down there is a box you can tick "use PHP extension.

Not sure if that is what you’re after though.

Chris

I test via thingspeak. I don’t know if we can use I test via thingspeak with Weather Display

I’ve just had a quick look at ThingSpeak. The APi documentation say there are two ways to get data into a channel.

  1. Using the REST API
  2. Using MQTT

It should be possible to write some PHP that read the clientraw files and then sent the required data to your channel using the REST API. You’d need to run the PHP script regularly using something like cron. So this method is possible, but would need some work developing the script to read the data file and figuring out how to format data to send it through the REST API.

MQTT might be a better option for you. WD has been able to send data to MQTT for a number of years and I think some people are using it regularly to collect data. In this case you’d either need to install MQTT for yourself, or I think there are public MQTT servers that you can subscribe to where they run the server and you set up yout own feed and access to your data. You’d then need to configure WD to send the data to the MQTT server and also configure the server to publish the data you want into the ThingSpeak channel.

(Translate)

I already have a ThingSpeak station via the link that works in GPRS/3G/4G in a remote place in the mountains:
https://thingspeak.com/channels/1676216/feed.json?results=1

Below the code, I can not export file *.txt, I think I did the biggest however I can not solve this export.
If anyone can look into it?

<?php
$json = file_get_contents('https://thingspeak.com/channels/1676216/feed.json?results=1');
$decodedJson = json_decode($json, true);

// Ouvrir un nouveau fichier en écriture
$myFile = fopen(__DIR__."/".time()."lognivose/log.txt", "w");

foreach ($decodedJson['feeds'] as $feed) {
    $f3 = $feed['field3'];
    if (!empty($f3)) {
        // Écrire dans le fichier
        fwrite($myFile, "$f3\n"); // print "$d\t$f3\n";
    }
}

// Fermer le fichier
fclose($myFile);
?>

I think your problem is probably the directory you’re trying to write the log file to. If you use:

$myFile = fopen("log.txt","w");

AND then create the log file with full permissions

touch log.txt
chmod 666 log.txt

…then the value of field3 is correctly written to the log file (at least it is on my server).

You’re trying to create the log file in a dynamically named directory which includes the current time. I don’t think fopen() can create directories dynamically and in any case each file you write is very likely to be in a new directory which will rapidly create a lot of directories and probably make it difficult for you to find/use the log files that you’re creating. I’m not sure if you wanted to create a directory for each download, but I wouldn’t recommend it.

Even creating a new log file in the current (or an existing directory) may cause difficulties if the default permissions for new files don’t include ‘write’ for the web server process. That’s why I deliberately set the permissions for the log.txt file to 666 (rw-rw-rw-) to make sure that whatever process is running the web server can write to it. You might be able to just use user or group permissions to achieve the same thing avoiding any security issues with world permissions.

(Translate)

Thanks it works,
Just how is it possible with my script to reset every day at midnight? That is beyond my competence.
Thank you.

Firstly, are you trying to capture all the data for each day in the same log file? At the moment you’re using fopen() with “w” which truncates the file to empty each time it’s opened. If you want all the data for the same day in the file you should use “a” instead of “w”.

Secondly, do you want the log files for each day named with the date, e.g. 20230927-log.txt? If so, that can be done. I’m just doing some diagnostics work on the forum outage but I can look at the script when I’ve finished that if you can wait?

(Translate)

Thank you for your help.

Do you want the log files for each day named with the date, e.g. 20230927-log.txt : yes

Serge

This should do what you want.

<?php
$json = file_get_contents('https://thingspeak.com/channels/1676216/feed.json?results=1');
$decodedJson = json_decode($json, true);

// Ouvrir un nouveau fichier en écriture
$myFile = fopen(__DIR__."/".date("Ymd")."-log.txt", "a");
foreach ($decodedJson['feeds'] as $feed) {
    $f3 = $feed['field3'];
    if (!empty($f3)) {
        // Écrire dans le fichier
        fwrite($myFile, "$f3\n"); // print "$d\t$f3\n";
    }
}

// Fermer le fichier
fclose($myFile);
?>

This version writes the log file into the same directory the script runs from, so you’ll need to change the path to where you want the files to be stored. You’ll also need to make sure that the permssions on the directory where you’re storing your log files are correct to allow the web server process to write the files there. That’s not something easy for me to know. Using ‘777’ permissions on the destination directory would work, but using ‘777’ can be a security risk on shared servers so it’s not always safe to do!

(Translate)

I will test and keep you posted.
A question: does this code allow you to set to “0mm” at midnight?
Thank you.

Serge

The first reading after midnight goes into a new empty file. I don’t know if that’s what you mean by resetting to zero?

Example: my readings are at 23:59 = 20mm. Is it possible to reset to midnight 0mm.
Serge

The script doesn’t do that at the moment but it could be made to reset rain to zero on date change.

Edit: Having thought about it a little more, it’s not as easy to do this as I first thought.

(Translate)

“but it could be made to reset rain to zero on date change.” Yes it will be the best.
I guess it’s not easy.
Serge

(Translate)
Regarding setting to zero millimeters at midnight, I tried and it doesn’t work. I’m not specialized enough.
I would like to see if there are people who specialize in this area.
Serge