Basic Arduino API GET example - need one arduino to read another via API

Hello! Here goes - dont flame me just yet.

I know that Arduino does NOT yet support x-device access to Things/variables etc, and looking through the forum and the docs it seems that the best way is to use Arduino IoT Cloud API

Ive used GET requests with Arduino before to pull info from Dynamo, or weather data - does anyone have an example of pulling data using GET from arduino IOT - I cant even post my code as Ive deleted it, monkeyed with it so many times that I just dont want to get flamed!

Surely there must be a simlpe "heres how you get a value from Arduino IOT .. TO and arduino" (not to a webpage etc).

1 Like

Try this CURL

curl --REQUEST GET \
--url https://api2.arduino.cc/iot/v2/things/{THING_ID}/properties/
{PROPERTY_ID} \
--header 'Authorization: Bearer MY_TOKEN'

To obtain your token, you need to generate an API key. Then use this CURL:

curl --request POST \
--url 'https://api2.arduino.cc/iot/v1/clients/token' \
--header 'content-type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data 'client_id=YOUR_CLIENT_ID' \
--data client_secret=YOUR_SECRET_ID \
--data audience=https://api2.arduino.cc/iot
1 Like

Thanks! Especially for the speedy response!

Will give it a go!

M

OK - this has moved me massively forward -so thanks so much again! Curl worked fine with a slight tweak to the URL.

Two things:

  1. My token expiry is saying 300! that seems rediculously short - As im only looking to hit the API every 5mins - that means I have to generate a new token each time? Is there a way of increasing that?

  2. Are there any examples of this curl transposed into C for Arduino? or any libraries that do this? Ive fuond quite a few examples - but this surely must be common and standard? It has been a few years since I was a progammer by trade - so the old cobwebs are a bit in the way!

This is my declarations / libs (using NodeMCU):

#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <ESP8266HTTPClient.h>
char serverAddress[] = "api2.arduino.cc";  // server address
const char* _getLink = "https://api2.arduino.cc/iot/v1/clients/token";
WiFiClient client;
int status = WL_IDLE_STATUS;
String httpRequestData = "data=";

Setup is just the usual WIFI connect..

This is my loop code:

  HTTPClient http;

  httpRequestData += "grant_type:client_credentials&client_id:";
  httpRequestData += "MYID-REMOVEDFORTHISPOST";
  httpRequestData += "&client_secret:";
  httpRequestData += "MYSECRETREMOVEDFORTHISPOST";           
  httpRequestData += "&audience:https://api2.arduino.cc/iot";

  Serial.println("requesting token");
  if (client.connect("api2.arduino.cc",443))     
  { 
    String getStr = _getLink;
    Serial.println("port 443 OK");
    
    http.begin("https://api2.arduino.cc/iot/v1/clients/token");
    delay(1000);

    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    int httpCode = http.POST(httpRequestData);
    String payload = http.getString();
    Serial.println(httpCode);           // getting -1
    Serial.println(payload);            // getting nothing
    http.end();
  }
  
  client.stop();
  Serial.println("Waiting…");
  delay(35000);

Honestly I have hashed around with this so much!

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.