You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Arduino UNO的C环境中调用Thingworx REST API执行PUT请求修改属性?

Got it, let's walk through how to send a PUT request to ThingWorx from your Arduino Uno to modify properties. I’ve tinkered with this setup before, so here’s a practical, step-by-step approach:

1. Hardware Setup

First, you’ll need:

  • Arduino Uno
  • Ethernet Shield (like the W5100) or a WiFi module (e.g., ESP8266 connected via serial—though Ethernet is more straightforward for Uno)
  • A stable connection to the same network as your ThingWorx server
2. Required Libraries

Use the official Ethernet Library (pre-installed with the Arduino IDE) to handle network communication. If you’re using a WiFi module, you’ll need the corresponding library (like ESP8266WiFi), but let’s focus on Ethernet first since it’s native to Uno.

3. Full Code Example

Here’s a complete snippet that connects to your network, sends a PUT request to update a ThingWorx property, and prints the server response:

#include <SPI.h>
#include <Ethernet.h>

// Replace with your network details
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192, 168, 1, 100); // Your ThingWorx server IP
EthernetClient client;

// ThingWorx credentials and target details
const char* appKey = "YOUR_THINGWORX_APP_KEY";
const char* thingName = "MyTestThing";
const char* propertyName = "Temperature";
int newPropertyValue = 25; // Example value to set

void setup() {
  Serial.begin(9600);
  while (!Serial) {} // Wait for serial monitor to open

  // Start Ethernet connection
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Optional: Set static IP if DHCP fails
    Ethernet.begin(mac, IPAddress(192, 168, 1, 101));
  }
  delay(1000);
  Serial.println("Connected to network");
}

void loop() {
  // Connect to ThingWorx server (port 80 is default for HTTP; use 443 for HTTPS, but Uno struggles with SSL)
  if (client.connect(server, 80)) {
    Serial.println("Connected to ThingWorx server");

    // Build PUT request header
    client.print("PUT /Thingworx/Things/");
    client.print(thingName);
    client.print("/Properties/");
    client.print(propertyName);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/json");
    client.print("App-Key: ");
    client.println(appKey);
    
    // Calculate request body length
    String requestBody = "{\"" + String(propertyName) + "\":" + String(newPropertyValue) + "}";
    client.print("Content-Length: ");
    client.println(requestBody.length());
    client.println(); // End of headers
    client.println(requestBody); // Send request body

    // Read server response
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
      }
    }
    client.stop();
    Serial.println("\nDisconnected from server");
  } else {
    Serial.println("Failed to connect to ThingWorx server");
  }

  delay(5000); // Wait 5 seconds before next update
}
4. Key Details to Note
  • App Key: Grab this from your ThingWorx instance—go to Security > Application Keys and create a new one with permissions to modify thing properties.
  • Request Format: ThingWorx expects JSON for property updates. Make sure the property name in the request body matches exactly what’s defined in your ThingWorx Thing.
  • HTTPS Warning: Arduino Uno doesn’t have enough memory to handle SSL/TLS natively, so stick with HTTP for this setup. If you need HTTPS, consider using an ESP8266/ESP32 as a co-processor to handle SSL.
  • Data Types: If your property is a string, wrap the value in quotes in the request body (e.g., "\"Hello World\"").
5. Troubleshooting Tips
  • Double-check your server IP, mac address, and App Key—typos here are the most common issue.
  • Use the serial monitor to debug: you’ll see if the connection succeeds and what the server response says (e.g., 200 OK means success, 401 Unauthorized means invalid App Key).
  • Ensure your ThingWorx server allows incoming HTTP requests from your Arduino’s IP address.

内容的提问来源于stack exchange,提问作者Trap Chavez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:25:38