Netatmo /getpublicdata

Moderator: leecollings

Post Reply
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Netatmo /getpublicdata

Post by Toulon7559 »

Extraction of data from a specific Netatmo-station can be done using URL-calls /getstationsdata and/or /getmeasure.
A publicly released collection of summary data from Netatmo-stations over a selected area can be found using /getpublicdata.
Not as detailed as stationsdata, but enough to get an impression or overview for actual data.
Interesting to have a global look around without having a Netatmo-station yourself.
The demonstration url-call as shown at the Netatmo API-explanation for /getpublicdata produces a JSON-file, to be dissected to get to the data for specific stations within the selected area.

Tried the patatmo-setup and this 'Wolfram'-script, but stuck with errors:
apparently those scripts are not compatible with latest versions of Python3.x and/or not according the present oAuth-procedures of Netatmo?
.
This setup perhaps an alternative, workable PHP-approach?[/i]
Last edited by Toulon7559 on Friday 22 July 2022 19:55, edited 4 times in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Re: Netatmo /getpublicdata

Post by Toulon7559 »

The reward seems justifying some more effort .......

Modified the 'Wolfram'-script-components according to elements practically proven in other recent Python3-scripts.
Token-generation is by an sh-file named getNetatmokeyblog.sh,
filled with personal data in line with Netatmo-account and saved at Raspberry_with_RaspianBuster in folder /home/pi/Netatmo
(with fill-ins anonimized for this forummessage).
Attributes = 775 to ensure that the file is readable & executable by all.
Spoiler: show
curl -X POST -d "grant_type=password&client_id=<Client_ID>&client_secret=<Client_Secret>&username=<User_Name>&password=<User_Password>&scope=read_station" https://api.netatmo.com/oauth2/token
Embedded in the reader-script that as result produces the file /home/pi/Netatmo/keys.txt containing the temporary access-token(s):
attributes = 644 = file is writable by owner & readable by all.

Made a modifed reader-file named NetatmoPublicReader.py in folder /home/pi/Netatmo
Spoiler: show
import os
import requests
os.system('/home/pi/Netatmo/getNetatmokeyblog.sh > /home/pi/Netatmo/keys.txt');
keysfile = '/home/pi/Netatmo/keys.txt';
# accesstoken = StringSplit[StringSplit[keysfile, "\":\""][[2]], "\""][[1]];
URL = "https://api.netatmo.net/api/getpublicdata?access_token=" + keysfile + "&lat_ne=52.3&lon_ne=6.81&lat_sw=52.2&lon_sw=6.79&filter=False"
myfile = requests.get(URL)
open("/home/pi/NetatmoPublic.txt", 'wb').write(myfile.content)
a = myfile.text
print(a)
Attributes = 775 to ensure that the file is readable & executable by all.
In the reader-file commented the scriptline with "accesstoken = ", because StringSplit is not recognized by Python3.x of Domoticz,
and not clear what is the function of that line: seems superfluous, but see the next message
Now it works so far that
- the reader-file starts running under Python3 from Putty's CLI,
- keysfile is generated (with sensible contents, compatible with the info in the Apps-section of Netatmo.com),
- apparently communication with the Netatmo-server,
- but an errorcode pops up related to the access token.
Spoiler: show
sudo python3 /home/pi/Netatmo/NetatmoPublicReader_v2022a.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 403 0 214 100 189 769 679 --:--:-- --:--:-- --:--:-- 1454
{"error":{"code":2,"message":"Invalid access_token"}}
Related to Python3.x Reader-script:
anybody an idea which 'subtlety' has been missed in the Python-script for the insertion of access-token in the URL-string?
Last edited by Toulon7559 on Friday 22 July 2022 20:01, edited 4 times in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Re: Netatmo /getpublicdata

Post by Toulon7559 »

With a short lua-script have operationally crosschecked the keys in the file /home/pi/Netmo/keys.txt with the cURL-extraction as given in the Netamo-example and that proves that the access-token in the txt-file is accepted/applicable:
when manually inserting that access-token in the cURL-scriptline below, the result is a JSON-file as expected.

Code: Select all

curl -X GET "https://api.netatmo.com/api/getpublicdata?lat_ne=52.3&lon_ne=6.81&lat_sw=52.2&lon_sw=6.79&filter=false" -H "accept: application/json" -H "Authorization: Bearer 620f68710f862816fc5627ce|d89b76ab80dab8656096a9c22149ca15"
That seems to explain that the 'Wolfram'-scriptline with the dual Stringsplit-command is intended to extract the appropriate access-token by splitting the key-string.

Could continue with lua-script for the further data-extraction, but that is not intention.
It changes/directs the question in the previous message into:
what is an equivalent (short)scriptline in 'pure' Python3.x for the below 'Wolfram'-expression?

Code: Select all

accesstoken = StringSplit[StringSplit[keysfile, "\":\""][[2]], "\""][[1]];
Tried to make a very basic translation: seems equivalent, valid script,
but the string-find-lines both return -1, which implies that the textstrings access_token and refresh_token are reported as not present (although very visible for the observer!):
obviously the subsequent extraction of the access-token-string fails and the script ends with error "Invalid access_token".

Code: Select all

#!/usr/bin/python3

import os
import requests
os.system('/home/pi/Netatmo/getNetatmokeyblog.sh > /home/pi/Netatmo/keys.txt');
keysfile = '/home/pi/Netatmo/keys.txt';
# accesstoken = StringSplit[StringSplit[keysfile, "\":\""][[2]], "\""][[1]];
# translation back-to-basics for previous scriptline
x = keysfile.find("access_token")
print(x)
y = keysfile.find("refresh_token")
print(y)
at_start = x + 16
at_end = y - 3
at_string = keysfile[at_start:at_end]
print(at_string)
# end of translation
URL = "https://api.netatmo.net/api/getpublicdata?access_token=" + at_string + "&lat_ne=52.3&lon_ne=6.81&lat_sw=52.2&lon_sw=6.79&filter=False"
myfile = requests.get(URL)
open("/home/pi/NetatmoPublic.json", 'wb').write(myfile.content)
a = myfile.text
print(a)
What's wrong?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest