在 C 环境中制作 Thingworx REST API 命令



我目前正在尝试对Thingworx服务器进行PUT语句以更改属性。有没有人知道如何在C环境中更具体地为Arduino UNO执行此操作。

您可以使用以太网/以太网 2 库,以便通过以太网或 wifi 屏蔽从 Arduino 到 Thingworx 服务器进行 RESTful API 调用。

下面是一个示例(以太网(实现供您参考:

#include <Ethernet2.h>
#include <EthernetClient.h>
#include <EthernetUdp2.h>
#include <util.h>
IPAddress _ip(192, 168, 1, 12); // Client (Arduino) IP address
byte _mac[] = {0x90, 0xA2, 0xDA, 0x11, 0x3C, 0x69}; // Arduino mac address
char _server[] = "192.168.1.10"; // Server IP address
int _port = 9200; // Server port number
EthernetClient _client;
void setup() {    
  Ethernet.begin(_mac, _ip);
  delay(1000);
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());
  if (_client.connect(_server, _port)) {
    Serial.println("SUCCESS: Connected to the server!");
  } else {
    Serial.println("ERROR: Connection failed to the server!");
    return;
  }
  delay(1000);
}
void loop() {       
  // JSON formatted data package including sample UID and newValue 
  String payload = "{"uid": " + String(uid) + ", " +
                   ""data": " + String(newValue) + "}";
  String url = "/my-api/updatedata"; // API url hosted on the server
  // Finally, make an API call: PUT request
  _client.print("PUT " + url + " HTTP/1.1 rn" +
    "Content-Type: application/json rn" +
    "Content-Length: " + payload.length() + " rn" +
    "rn" + payload);
  delay(500); // Give the network some time
  // Read all the lines of the reply from server and 
  // print them to Serial to validate your API call
  while (_client.available()) {
    String reply = _client.readStringUntil('r');
    Serial.print(reply);
  }
  Serial.println();
}
Thingworx

提供了"C Edge SDK",用于与Thingworx平台进行通信。SDK提供实用程序函数(twApi_WriteProperty(来读/写绑定事物的属性。

链接到下面的 SDK 文档:

  • http://support.ptc.com/help/thingworx_hc/thingworx_edge_sdks_ems/#page/thingworx_edge_sdks_ems%2Fc_sdk%2Fc_csdk_general_introduction.html%23

属性更新的具体链接:

  • http://support.ptc.com/help/thingworx_hc/thingworx_edge_sdks_ems/#page/thingworx_edge_sdks_ems%2Fc_sdk%2Fc_csdk_write_property.html%23

最新更新