Call (HTTPS) API
Posted: Tue Feb 18, 2020 1:35 pm
Hi,
I’ve recently discovered the world of microcontrollers and made a few experimental projects so far which all work okay. I’m learning day by day, but I’ve got a puzzle now which I can’t seem to solve.
I’m want to read (and log) values from a home air quality monitor (Netatmo Home Coach). This can be done with a API. I’ve got a python script running which works, but I can’t convert the code to ‘Arduino IDE code’. One way (maybe?) might be to use micropython, but I’m using the Arduino IDE for all my other experiments/projects and prefer to stick to just one platform. I'm using a Wemos D1 pro board by the way.
This is the python script:
-------------------------------------------------------------------------------------------------------------------------------------
import requests
payload = {'grant_type': 'password',
'username': "………………………..",
'password': "………………………..",
'client_id':" ………………………..",
'client_secret': "………………………..",
'scope': 'read_homecoach'}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
print(response.json())
access_token=response.json()["access_token"]
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
params = {
'access_token': access_token,
'device_id': '………………………..'
}
try:
response = requests.post("https://api.netatmo.com/api/gethomecoachsdata", params=params)
response.raise_for_status()
data = response.json()["body"]
print(data)
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
-------------------------------------------------------------------------------------------------------------------------------------
I think I can figure out handling the JSON response. But anyone any idea what would be the code (Arduino IDE) to make a post request and retrieve values? The python script uses the ‘request’ library. Is there an Arduino equivalent?
Thanks in advance!
I’ve recently discovered the world of microcontrollers and made a few experimental projects so far which all work okay. I’m learning day by day, but I’ve got a puzzle now which I can’t seem to solve.
I’m want to read (and log) values from a home air quality monitor (Netatmo Home Coach). This can be done with a API. I’ve got a python script running which works, but I can’t convert the code to ‘Arduino IDE code’. One way (maybe?) might be to use micropython, but I’m using the Arduino IDE for all my other experiments/projects and prefer to stick to just one platform. I'm using a Wemos D1 pro board by the way.
This is the python script:
-------------------------------------------------------------------------------------------------------------------------------------
import requests
payload = {'grant_type': 'password',
'username': "………………………..",
'password': "………………………..",
'client_id':" ………………………..",
'client_secret': "………………………..",
'scope': 'read_homecoach'}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
print(response.json())
access_token=response.json()["access_token"]
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
params = {
'access_token': access_token,
'device_id': '………………………..'
}
try:
response = requests.post("https://api.netatmo.com/api/gethomecoachsdata", params=params)
response.raise_for_status()
data = response.json()["body"]
print(data)
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
-------------------------------------------------------------------------------------------------------------------------------------
I think I can figure out handling the JSON response. But anyone any idea what would be the code (Arduino IDE) to make a post request and retrieve values? The python script uses the ‘request’ library. Is there an Arduino equivalent?
Thanks in advance!