Failed to send data by http request to Autodesk tandem using Arduino IDE and node mcu

88 Views Asked by At

How to update the code below in order to send data to my digital twin stream on Autodesk Tandem by http request using arduino ide?

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "xxxxx";
const char* password = "xxxxxxx";

const char* clientID = "xxx";
const char* clientSecret = "xxx";
const char* modelID = "urn:adsk.dtt:zy-xxx";

const char* server = "tandem.autodesk.com";
const int port = 443;

String authToken = "Basic xxx=";

void setup() {
  Serial.begin(115200);
  delay(10);

  // Connect to Wi-Fi
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

void postData() {
  // Construct payload
  const char* payload = "[{\"Temperature (C)\":25,\"id\":\"AQAAADKPYIggIUAVsiHJqUxjjNEAAAAA\"}]";

  // Post data to Tandem
  String path = "/api/v1/timeseries/models/" + String(modelID) + "/webhooks/generic?access_token=" + authToken;
  HTTPClient http;
  http.begin("https://" + String(server) + path);
  http.addHeader("Content-Type", "application/json");

  int httpCode = http.POST(payload);
  if (httpCode == 200) {
    Serial.println("Data sent successfully");
  } else {
    Serial.println("Failed to send data");
  }

  // Print HTTP response code and body
  Serial.print("HTTP Code: ");
  Serial.println(httpCode);
  Serial.print("Response: ");
  Serial.println(http.getString());

  http.end();
}

void loop() {
  postData();

  delay(60000);  // Wait for a minute before sending data again
}

i need to understand the issue that makes code fails to send data, and how to update it.

1

There are 1 best solutions below

4
Jan Liska On

The issue is that your request doesn't include authentication. Use following steps:

  1. In Tandem UI navigate to Streams then ... menu, then select Webhook Integration.
  2. Copy value for Authorization token.
  3. Use this for Authorization header:
http.addHeader("Authorization", "Basic xxx"); // this is value obtained in step #2 above

As alternative to Basic authentication is possible to use OAuth token provided by APS Authentication service. In this case the token needs to be have access to facility (data:write scope). When using 2-Legged Authentication the service needs to be added to the facility.

Hope it helps.