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

时间:2018-04-16 14:47:59

标签: c rest arduino thingworx

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

2 个答案:

答案 0 :(得分:0)

您可以使用Ethernet / Ethernet 2 library通过以太网或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 \r\n" +
    "Content-Type: application/json \r\n" +
    "Content-Length: " + payload.length() + " \r\n" +
    "\r\n" + 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();
}

答案 1 :(得分:-1)

Thingworx提供用于与Thingworx平台通信的“C Edge SDK”。 SDK提供了实用程序函数(twApi_WriteProperty)来读取/写入绑定Thing的属性。

链接到以下SDK文档:

属性更新的具体链接: