Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
Hello All,
As part of my internship, I am using Arduino to update remote thingworx properties. I implmented my first arduino program. I started in this program with pushing static values. But REST call from my arduino is not updating properties values. Although from browser it successfully updates properties.
I am using:
1- Thingworx 8.2.1-b140
2- Arduino 1.8.5 leonardo ETH
The output of my code is the following:
My IP address: *************** connected to the server Connexion: prop0: 11.00 prop1: 22.00 client: 1 server: pp-1805191146zt.devportal.ptc.io appKey: 9bc83f49-adab-46c5-9017-93fe992eaf6a thingName: myThing serviceName: myThingService nameArray0: arduino_value1 nameArray1: arduino_value2 connected2 POST /Thingworx/Things/myThing/Services/myThingService?appKey=9bc83f49-adab-46c5-9017-93fe992eaf6a&method=post&x-thingworx-session=true<&arduino_value1=11.00&arduino_value2=22.00> HTTP/1.1 Host: pp-1805191146zt.devportal.ptc.io Content-Type: text/html Connected
And when I put the following url in browser, it updates the properties values:
https://pp-1805191146zt.devportal.ptc.io/Thingworx/Things/myThing/Services/myThingService?appKey=9bc83f49-adab-46c5-9017-93fe992eaf6a&method=post&x-thingworx-session=true%3C&arduino_value1=11.00&arduino_value2=22.00%3E%20HTTP/1.1
Any pointers please on what I may be doing wrong?
The code I used is following:
#include <SPI.h> #include <Ethernet2.h> #include <ThingWorxEthernet.h> #define propertyCount 2 //How many values you will be pushing to ThingWorx // initialize the library instance: EthernetClient client; //Initialize network info and ThingWorx server and app key byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Choose a unique MAC address for the Arduino char server[] = "pp-1805191146zt.devportal.ptc.io"; //ThingWorx server (do not include http://) char appKey[] = "9bc83f49-adab-46c5-9017-93fe992eaf6a"; char thingName[] = "myThing"; //Name of your Thing in ThingWorx char serviceName[] = "myThingService"; //Name of your Service (see above) //Initialize Property Name Array char* nameArray[] = {"arduino_value1", "arduino_value2"}; double propertyValues[propertyCount]; unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds //Interval of time at which you want the properties values to be sent to TWX server int timeBetweenRefresh = 5000; ThingWorx myThing(propertyCount); // Initialize your ThingWorx Object void setup() { // start serial port: Serial.begin(9600); while(!Serial); // give the ethernet module time to boot up: delay(1000); // start the Ethernet connection: if (Ethernet.begin(mac)!= 0){ // print the Ethernet board/shield's IP address: Serial.print("My IP address: "); Serial.println(Ethernet.localIP()); } // if you get a connection, report back via serial: if (client.connect(server, 443)) { //myThing.UpdateValues(tab,client,server,appKey,thingName,serviceName,nameArray); Serial.println("connected to the server"); } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } // Aquire properties values propertyValues[0] = 11; propertyValues[1] = 22; // if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data: if (millis() - lastConnectionTime > timeBetweenRefresh) { Serial.println ("Connexion:"); Serial.print ("prop0: "); Serial.println(propertyValues[0]); Serial.print ("prop1: "); Serial.println(propertyValues[1]); Serial.print ("client: "); Serial.println(client); Serial.print ("server: "); Serial.println(server); Serial.print ("appKey: "); Serial.println(appKey); Serial.print ("thingName: "); Serial.println(thingName); Serial.print ("serviceName: "); Serial.println(serviceName); Serial.print ("nameArray0: "); Serial.println(nameArray[0]); Serial.print ("nameArray1: "); Serial.println(nameArray[1]); updateValues(propertyValues,client,server,appKey,thingName,serviceName,nameArray); lastConnectionTime = millis(); //Note the last connection time Serial.println ("Connected"); } } void loop() { } void updateValues(double propertyValues[] , EthernetClient &client, char server[], char appKey[], char thingName[], char serviceName[], char* propertyNames[]) { //build the String with the data that you will send //through REST calls to your TWX server char data[80]; strcpy(data, "?appKey="); strcat(data, appKey); strcat(data, "&method=post&x-thingworx-session=true"); // if you get a connection, report back via serial: if (client.connected()) { Serial.println("connected2"); // send the HTTP POST request: client.print("POST /Thingworx/Things/"); client.print(thingName); client.print("/Services/"); client.print(serviceName); client.print(data); client.print("<"); for(int idx = 0; idx < propertyCount; idx++) { client.print("&"); client.print(propertyNames[idx]); client.print("="); client.print(propertyValues[idx]); } client.print(">"); client.println(" HTTP/1.1"); client.print("Host: "); client.println(server); client.println("Content-Type: text/html"); client.println(); // print the request out Serial.print("POST /Thingworx/Things/"); Serial.print(thingName); Serial.print("/Services/"); Serial.print(serviceName); Serial.print(data); Serial.print("<"); for(int idx = 0; idx < propertyCount; idx++) { Serial.print("&"); Serial.print(propertyNames[idx]); Serial.print("="); Serial.print(propertyValues[idx]); } Serial.print(">"); Serial.println(" HTTP/1.1"); Serial.print("Host: "); Serial.println(server); Serial.println("Content-Type: text/html"); Serial.println(); } else { // if you couldn't make a connection: Serial.println("the connection could not be established"); client.stop(); } }
Hi @EmArduino bit confused as you have mentioned that properties are being updated, when you issue the REST calls, is there something else where you run into error?