Netatmo Weather Station is a popular weather station that provides us with information such as indoor and outdoor temperature, atmospheric pressure, CO2 level, humidity etc.

In this tutorial, integration with Grenton system is shown.

For the integration, it is required to obtain a token to authorize the sent request for measurements.

In the first step, install the weather station and create an account at: https://www.netatmo.com/en-us.


Once the account is active and the station returns measurements as below, you can move on to the next steps.


1. Token retrieving

Instructions for obtaining the token are available at: https://dev.netatmo.com/apidocumentation/oauth.


In the first step, create an application (https://dev.netatmo.com/apps/createanapp#form), where you can find the client ID and client secret, which will be needed in the further steps.

Pay attention on redirect URI field - fill it with https://api.netatmo.com/oauth2.

To obtain a token, a request must be sent. This can be done using a browser or Postman.

 

The data that the request should contain:

Host: https://api.netatmo.com

Path: /oauth2/token 

RequestBody:

- client_id - taken from App Technical Parameters

- client_secret - taken from App Technical Parameters 

- grant_type - password

- username - user name

- password - user password


Below is an example of a request and respond in Postman:

In response, there are:

- access token

- refresh token

- time after which the token will expire


2. Data retrieving 

Once you have the token, proceed to create the configuration on the HTTP Gate module.


In the first step, create a HttpRequest virtual object.

Host: https://api.netatmo.com

Path: /api/getstationsdata


Then prepare a script (netatmo_initial) that will execute the SendRequest method with the MAC address of the station and the current token. 

local params="device_id=70%3Aeg%3A52%3A33%3A8f%3A73&get_favorites=false"
local key="Authorization: Bearer **************|***************** \r\n"

Gate_HTTP->NetAtmo_Request_StationData->SetQueryStringParams(params)
Gate_HTTP->NetAtmo_Request_StationData->SetRequestHeaders(key)
Gate_HTTP->NetAtmo_Request_StationData->SendRequest()

In this example, MAC adres is 70:EG:52:33:8F:73 and is located in this line:

local params="device_id=70%3Aeg%3A52%3A33%3A8f%3A73&get_favorites=false"

the ":" mark is replaced with %3A,


If MAC address contains letters, in this script you need to use small letters (the big letters are reserved for %3A).


Paste your token to:

local key="Authorization: Bearer your token \r\n" 


Now, prepare the user features to which you will save the responses:


The last step is to create a script (netatmo_stationdata_response) that will pass the response to the user features. 

local resp = Gate_HTTP->NetAtmo_Request_StationData->ResponseBody 

Gate_HTTP->netatmo_Status=resp.status
Gate_HTTP->netatmo_StationName=resp.body.devices[1].station_name
Gate_HTTP->netatmo_StationPlace=resp.body.devices[1].place.city

Gate_HTTP->netatmo_Temperature=resp.body.devices[1].dashboard_data.Temperature
Gate_HTTP->netatmo_CO2=resp.body.devices[1].dashboard_data.CO2
Gate_HTTP->netatmo_Humidity=resp.body.devices[1].dashboard_data.Humidity
Gate_HTTP->netatmo_Noise=resp.body.devices[1].dashboard_data.Noise

Assign it to the OnResponse event of NetAtmo_Request_StationData virtual object.


When the script (netatmo_initial) is called, the values of user features are updated:


3. Token refreshing

The token expires after 10800s, so it needs to be updated periodically. For this purpose, it is useful to prepare a mechanism that will do this.

Below is an example of how it can be done.


Create a user feature whose initial value is refresh_token (found as a response in Postman):


And the feature to which you will pass the current token:


Then prepare a script (netatmo_refresh_token) where you enter client_id and client_secret.

local content = "&grant_type=refresh_token&refresh_token=" .. Gate_HTTP->refresh_token_netatmo .. "&client_id=620222ce0bc059762b5d5864&client_secret=sAixsO4MywWhymnYi7OqZcODm6PxM5cFtm9zgj1k0Rk"

Gate_HTTP->NetAtmo_Request_Token->SetRequestBody(content)
Gate_HTTP->NetAtmo_Request_Token->SendRequest()

In the next step, create a HttpRequest virtual object.

Host: https://api.netatmo.com

Path: /oauth2/token


 Assign the modified script (netatmo_initial) to the OnResponse event :


The modified script (netatmo_initial):

local resp = Gate_HTTP->NetAtmo_Request_Token->ResponseBody 
Gate_HTTP->access_token=resp.access_token



local params="device_id=70%3AEG%3A52%3A33%3A8F%3A73&get_favorites=false"
local key="Authorization: Bearer ".. Gate_HTTP->access_token.."  \r\n"

Gate_HTTP->NetAtmo_Request_StationData->SetQueryStringParams(params)
Gate_HTTP->NetAtmo_Request_StationData->SetRequestHeaders(key)
Gate_HTTP->NetAtmo_Request_StationData->SendRequest()

To continuously update the token, it is a good idea to create a Timer virtual object that will periodically run a script (netatmo_refresh_token).