CURL : get rain data, save to file and read/process line by line Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

CURL : get rain data, save to file and read/process line by line

Post by marmachine »

Hi Folks,

I'm kinda stuck with the curl-part in the "IsItGonnaRain" function. (see this topic)
This scripts gets data from "Buienradar" and should save this into a tmp file
Next it should read this file line by line to process the data.

I am under the impression that the data/file is not saved actually.
Being no-expert in LUA, some help will be very welcome

here's part of my code

Code: Select all

function IsItGonnaRain(latitude,longitude,tempfilename,minutesinfuture,debug)

    local averagerain = 0
    local totalrain = 0
    local rainlines = 0
    
    -- construct the request URL
    local url = 'http://gps.buienradar.nl/getrr.php?lat='..latitude..'&lon='..longitude
    if (debug == 1) then print("| Request URL .............. : " .. url) end

    -- now get the data
    local trigger_action = 'curl -o '..tempfilename..' "'..url..'"' -- -s for Silent (no error messages) and -o for Write output to file instead of stdout.
    handle = os.execute(trigger_action)

    if (debug == 1) then
        print("| Trigger action ........... : " .. trigger_action)
        if handle then
            print("| CURL Data request ........ : Success")
        else
            print("! CURL Data request ........ : Failed")
        end
    end

    file = io.open(tempfilename, "r")

        -- now analyse the received lines, format is like 000|15:30 per line.
        while true do
            line = file:read("*line")
            if not line then break end
        
            if (debug == 1) then print("| Rain data (line) ......... : " .. line) end
        end
The debug result actually shows the content from the file, the content however isn't updated as you can see by the time.

Here's the debug result

Code: Select all

2016-08-29 23:56:00.509 LUA: +--------------------------------------------------+
2016-08-29 23:56:00.509 LUA: | =============== IS IT GONNA RAIN =============== |
2016-08-29 23:56:00.509 LUA: +--------------------------------------------------+
2016-08-29 23:56:00.509 LUA: | Request URL .............. : http://gps.buienradar.nl/getrr.php?lat=52.326080&lon=5.600445
2016-08-29 23:56:00.595 LUA: | Trigger action ........... : curl -o /home/pi/domoticz/scripts/tmp/rain.tmp "http://gps.buienradar.nl/getrr.php?lat=52.326080&lon=5.600445"
2016-08-29 23:56:00.596 LUA: | CURL Data request ........ : Success
2016-08-29 [color=#FF0000]23:56[/color]:00.596 LUA: | Rain data (line) ......... : 000|[color=#FF0000]19:45[/color]
2016-08-29 23:56:00.596 LUA: | Rain data # lines ........ : 0
2016-08-29 23:56:00.596 LUA: | Total rain ............... : 0
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: CURL : get rain data, save to file and read/process line by line

Post by Egregius »

The url isn't changed, my curl in php is still up and running.

But, when you paste the gps.buienradar url in a browser you're redirected to http://gadgets.buienradar.nl/data/raintext?lat=... so maybe you should use that as url.
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: CURL : get rain data, save to file and read/process line by line

Post by marmachine »

gordonb3 wrote:Ah!. My bad... :oops:
Just noticed that the uri in the wget output is incomplete, which happened to be due to the ampersand ('&') not being escaped. This is why wget did not return any output even though it followed the redirect. As it happens, curl does not do that unless you also specify '-L' as parameter. Without that it simply returns the output from the original uri, which is nothing.

But: this should also be what is in your output file and that is not the case because you have old content in it. Possibly your process does not have write access to that file?
Ohh the old content, actually the file entirely, is something i've put there to see if it finds and reads the file.
Also i tried to changes the file rights because i do expect the same issue;

Code: Select all

sudo chmod 777 /home/pi/domoticz/scripts/tmp/rain.tmp
However i am not sure if this is actually the case.
The CURL command in Putty also doesn't seem to work... (keep in mind i am unfamiliar with Linux and LUA)

Code: Select all

pi@raspberrypi:~$ curl -o /home/pi/domoticz/scripts/tmp/rain.tmp "http://gps.buienradar.nl/getrr.php?lat=52.326080&lon=5.600445"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
pi@raspberrypi:~$
Anyway, you're suggesting to try the -L parameter to the CURL and see what happens?
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: CURL : get rain data, save to file and read/process line by line

Post by marmachine »

Egregius wrote:The url isn't changed, my curl in php is still up and running.

But, when you paste the gps.buienradar url in a browser you're redirected to http://gadgets.buienradar.nl/data/raintext?lat=... so maybe you should use that as url.
Thanks Egregius! Changing the URL actually is the solution to this! I have my file content updated now! Woohoo :D me happy!

Edit: i've shared my updated version of the full script here
User avatar
rolandbreedveld
Posts: 157
Joined: Wednesday 09 March 2016 11:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: CURL : get rain data, save to file and read/process line by line

Post by rolandbreedveld »

Script isnt working since 29-aug-2016
after changing the url, it's working again:
url='http://gadgets.buienradar.nl/data/raint ... lon='..lon
Raspberry Pi(SSD)+UPS HAT,Dashticz on WallTablet,HomeBridge, P1, HarmonyHub,FritzBox,API to 123Solar+ABB-PowerOne+SMA,ESP-Leds(flashed LSC),Tasmota,Shelly,MQTT,Mosquito,Node-Red,http://wiki.breedveld.net/index.php?title=RPiWaterMeter, Mercedes-Me
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: CURL : get rain data, save to file and read/process line by line

Post by marmachine »

gordonb3 wrote:
marmachine wrote:Anyway, you're suggesting to try the -L parameter to the CURL and see what happens?
Either that or change the uri to the secondary one. If the primary uri - the one you are using - is the documented one I would stick to that

Anyway, this is what you'll see with the -L parameter added:

Code: Select all

curl -Lo /home/pi/domoticz/scripts/tmp/rain.tmp "http://gps.buienradar.nl/getrr.php?lat=52.326080&lon=5.600445"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   264  100   264    0     0   3307      0 --:--:-- --:--:-- --:--:--  3307
The first line with all the zero's is from the original uri, the second one is from the redirect.
Thanks Gordonb3, that makes sence!
Below is my Domoticz log:

Code: Select all

2016-08-30 18:10:00.200 LUA: +--------------------------------------------------+
2016-08-30 18:10:00.200 LUA: | =============== IS IT GONNA RAIN =============== |
2016-08-30 18:10:00.200 LUA: +--------------------------------------------------+
2016-08-30 18:10:00.200 LUA: | Request URL .............. : http://gps.buienradar.nl/getrr.php?lat=52.326080&lon=5.600445
2016-08-30 18:10:00.347 LUA: | CURL Data request ........ : Success
2016-08-30 18:10:00.348 LUA: | Rain data (line) ......... : 000|18:10
2016-08-30 18:10:00.348 LUA: | Rain data (line) ......... : 000|18:15
2016-08-30 18:10:00.348 LUA: | Rain data (line) ......... : 000|18:20
2016-08-30 18:10:00.348 LUA: | Rain data (line) ......... : 000|18:25
2016-08-30 18:10:00.351 LUA: | Rain data received # lines : 24
2016-08-30 18:10:00.352 LUA: | In timerange # lines ..... : 4
2016-08-30 18:10:00.352 LUA: | Total rain ............... : 0
2016-08-30 18:10:00.352 LUA: | Average rain calculated .. : 0
2016-08-30 18:10:00.352 LUA: | Rain text ................ : Geen regen verwacht binnen komende 15 minuten
2016-08-30 18:10:00.352 LUA: +--------------------------------------------------+
2016-08-30 18:10:00.352 LUA: | --------------------- END ---------------------- |
2016-08-30 18:10:00.352 LUA: +--------------------------------------------------+
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest