无法使用 Arduino IDE 和节点 mcu 通过 http 请求向 Autodesk tandem 发送数据

问题描述 投票:0回答:1

我希望有人可以帮助我更新下面的代码,以便使用 arduino ide 通过 http 请求将数据发送到 Autodesk Tandem 上的数字孪生流,请找到下面的代码

#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
}

我需要了解导致代码无法发送数据的问题,以及如何更新它。预先感谢

arduino esp8266 autodesk-tandem
1个回答
0
投票
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// Replace with your actual credentials
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;

// Generate a valid access token (see below for instructions)
String authToken = "Bearer xxx";  // Use "Bearer" for access token

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

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

  // Post data to Tandem
  String path = "/api/v1/timeseries/models/" + modelID + "/webhooks/generic";
  HTTPClient http;
  http.begin("https://" + server + path, authToken);  // Include authToken here
  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");
    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
}
**Access Token:
Replace authToken with a valid access token for your Tandem model.
Use the "Bearer" schema for access tokens, not "Basic".
Generate tokens using Autodesk Forge authentication methods.
Endpoint URL:
Ensure the path /api/v1/timeseries/models/{modelID}/webhooks/generic is correct for your setup.
HTTPS:
The code uses HTTPS for secure communication. Check for SSL/TLS compatibility on your device.
Error Handling:
The code now prints the HTTP response code and body if data sending fails.
Use this information to troubleshoot specific errors.
Debugging:
Use Serial output to monitor Wi-Fi connection, HTTP requests, and responses.enter code here**
© www.soinside.com 2019 - 2024. All rights reserved.