cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Trouble updating Thing's properties using REST calls

tding1
1-Visitor

Trouble updating Thing's properties using REST calls

Hi, I have been following through the weather app demo for Arduino Uno and tweaked it for my academic project. But I can't seem to update my VendingMachineThing properties. I have used the exact same code apart from the server, appkey and name variables for a separate developer instance and it worked there. The service for my Thing is working properly. If it makes any difference, I am using an academic instance.

Here is the relevant code:

#include <Wire.h>

#include <Bridge.h>

#include <SPI.h>

#include <Ethernet.h>

#define propertyCount 2

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

char server[] = "nanyangpolytech5511.cloud.thingworx.com";

EthernetClient client;

char appKey[] = "da2a1216-6935-47ef-9c58-80a31e49ef99";

char thingName[] = "VendingMachineThing";

int timeBetweenRefresh = 5000;

char serviceName[] = "setTemperatureAndHumidity";

char* propertyNames[] = {"Temp", "Humid"};

double propertyValues[propertyCount];

unsigned long lastConnectionTime = 0;

// state of the connection last time through the main loop

boolean lastConnected = false;

void setup()

{

  //shut down the SD Card pins

  pinMode(4,OUTPUT);

  digitalWrite(4,HIGH);

  // start serial port

  Serial.begin(9600);

  delay(500);

  Serial.println("Trying to get an IP address using DHCP");

  Ethernet.begin(mac);

  Serial.print("My IP address: ");

  Serial.print(Ethernet.localIP());

  Serial.println();

}

void loop()

{

  propertyValues[0] = sensors.getTempCByIndex(0);

  propertyValues[1] = sensors.getTempCByIndex(0) + 50;

  if (millis() - lastConnectionTime > timeBetweenRefresh) {

    updateValues(propertyValues, client, server, appKey, thingName, serviceName, propertyNames);

  }

  Ethernet.maintain();

}

void updateValues(double values[] , EthernetClient &client, char server[], char appKey[], char thingName[], char serviceName[], char* sensorNames[])

{

  char data[80];

  strcpy(data, "?appKey=");

  strcat(data, appKey);

  strcat(data, "&method=post&x-thingworx-session=true");

  if (client.connect(server, 443)) {

    Serial.println("connected");

    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();

     client.stop();

    lastConnectionTime = millis();

    // 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 {

    // kf you didn't get a connection to the server:

    Serial.println("the connection could not be established");

    client.stop();

  }

}

Here is the output:

pic.png

I have also followed these steps:

1.       Click on Subsystems under SYSTEM within the left pane of Composer

2.       Click on PlatformSubsystem

3.       Click on Configuration

4.       Check the box next to Allow Request Method Switch to allow all REST API calls through a browser URL

5.       Uncheck Filter Content-Type

3 REPLIES 3
vmihai
12-Amethyst
(To:tding1)

Hi Timothy,

Thank you for the detailed information. 

There might be a problem with the https rest call. One first step would be  to change the protocol of your rest call I believe, from HTTP to HTTPS

    client.print(">");

    client.println("HTTP/1.1");

But I am not entirely sure about that . I will need to investigate it more and come back to you with an answer.

Also can you check to see that the properties for your Vending Machine are not read only ?

Try also building a rest call for your computer browser by copying and pasting the rest call from the Arduino Serial Monitor in your computer browser  ? This would allow you to be sure that the  rest call sent from Arduino is working properly. Don't forget to use https instead of http.

Hope this helps.

Thank you,

Veronica

tding1
1-Visitor
(To:vmihai)

Hi Veronica,

Unfortunately, changing the HTTP to HTTPS did not work, also the properties are not read-only. But using Postman, I was able to change the properties of my academic instance.

This is the generated code from Postman:

POST /Thingworx/Things/VendingMachineThing/Services/setTemperatureAndHumidity?appKey=da2a1216-6935-47ef-9c58-80a31e49ef99&amp;x-thingworx-session=false<&amp;Temp=25.34&amp;Humid=76.31> HTTP/1.1

Host: nanyangpolytech5511.cloud.thingworx.com

Cache-Control: no-cache

Postman-Token: 37c1d770-181b-32b6-b7a3-ddb940e07e58

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

I tried to use this code in Arduino and it still didn't work.

Strangely, I just tried the exact same code on a developer instance and it worked perfectly fine there.

mkhalil1
1-Visitor
(To:tding1)

Hello,Have you solved this issue? i have same problem

Announcements


Top Tags