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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Arduino Uno with HDC1008-Sensor: Thingworx receives no data

msiemering
2-Explorer

Arduino Uno with HDC1008-Sensor: Thingworx receives no data

I have tried for many hours to send data from the Arduino with HDC1008-Sensor (Temperature, Humidity) but at last i dont get any further. I tried to adjust the Weather App with Arduino Uno Project. The Thing is a Generic Thing and the properties (Temp, Humid) are from type Number.

I have created one Service:

The adjusted Code for the Arduino:


#include <HDC100X.h>
#include <Wire.h>
#include <SPI.h>
#include <WiFi101.h>

//How many values you will be pushing to ThingWorx
#define propertyCount 2

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local Network:

char server[] = "192.168.2.105";

char ssid[] = "xxxxxx";          //  your network SSID (name)
char pass[] = "xxxxxx";   // your network password
int status = WL_IDLE_STATUS;
WiFiClient client;

//ThingWorx App key which replaces login credentials)
char appKey[] = "d9e8e9de-f548-43e0-aeaa-2b9c6847c7dc";
// ThingWorx Thing name for which you want to set properties values
char thingName[] = "SteamSensor2";
//Interval of time at which you want the properties values to be sent to TWX server
int timeBetweenRefresh = 5000;
// ThingWorx service that will set values for the properties you need
// See the documentation for this tutorial for more information
char serviceName[] = "SetTempAndHumid";

//Initialize Properties Names and Values Arrays
char* propertyNames[] = {"Temp", "Humid"};
double propertyValues[propertyCount];

// last time you connected to the server, in milliseconds
unsigned long lastConnectionTime = 0;
// state of the connection last time through the main loop
boolean lastConnected = false;

//Initialize an HTU21D library object to read
// temperature and humidity data from your connected sensor
HDC100X hdc(0x43);

void setup() {
   
  // start serial port:
 
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  //initialize HTU21D object to read values from sensors
   hdc.begin(HDC100X_TEMP_HUMI,HDC100X_14BIT,HDC100X_14BIT,DISABLE);

   Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);

  status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    // don't do anything else:
    while(true);
  }
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.println("connected1");
    }
  }
}

void loop() {

  // Aquire sensor values
  propertyValues[0] = hdc.getTemp();
  propertyValues[1] = hdc.getHumi();

  // wait the established interval of time before
  // reading values from the sensor
  // and sending them to the TWX server again
  // delay(timeBetweenRefresh);
  if (millis() - lastConnectionTime > timeBetweenRefresh) {
    updateValues(propertyValues, client, server, appKey, thingName, serviceName, propertyNames);
  }
}

void updateValues(double values[] , WiFiClient &client, char server[], char appKey[], char thingName[], char serviceName[], char* sensorNames[])
{
  //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();

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

The generated Serial-Output:

Attempting to connect to WPA network...
SSID: xxxx
Connected to wifi

Starting connection...
connected1
connected2
POST /Thingworx/Things/SteamSensor2/Services/SetTempAndHumid?appKey=d9e8e9de-f548-43e0-aeaa-2b9c6847c7dc&method=post&x-thingworx-session=true<&Temp=24.45&Humid=32.41> HTTP/1.1
Host: 192.168.2.105
Content-Type: text/html

And I tried the HTTP-Request Manual what ended up like this:

for: http://192.168.2.105/Thingworx/Things/SteamSensor2/Services/SetTempAndHumid?appKey=d9e8e9de-f548-43e0-aeaa-2b9c6847c7dc&method=post&x-thingworx-session=true<&Temp=24.40&Humid=32.21>

I think that the Arduino has established a Connection to my local ThingWorx Server, but I have not found any proof for this like a log-file.

Because of the "Unable To Convert From Java.lang.String to NUMBER"-Advice I changed the Base-Type of the Properties to String, but the message was still the same.

Next I added to the Service "parseFloat", but there is still the same Problem.

5 REPLIES 5
supandey
19-Tanzanite
(To:msiemering)

Marius, first thing would be to check for the Thing's (from which you have shared the screenshot above) isConnected property once connected you'll see following highlighted value changed to true

Otherwise you can also navigate to the \\ThingworxStorage\logs and check for the applicationlog.log  the other option would be to check from the ThingWorx > Composer > Monitoring > RemoteThings

Excuse me for my late reaction.

I have a "Generic Thing" (described in the Tutorial) so I do not have the isConected Property and can not see the Thing in Monitoring->Remote Things.

The Application Log Shows the same error as described at the top of this discussion ("Unable To Convert..."):

BoundConnectedThings= 0, UnBoundConnectedThings= 0
----------------------------------------------------------------------------

2017-06-01 02:55:16.952+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-6] [context: com.thingworx.webservices.context.HttpExecutionContext@45945dd9][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:55:18.393+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-2] [context: com.thingworx.webservices.context.HttpExecutionContext@2fd149e][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:55:18.896+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-1] [context: com.thingworx.webservices.context.HttpExecutionContext@305c76f1][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:55:19.079+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-7] [context: com.thingworx.webservices.context.HttpExecutionContext@645bf386][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:55:19.289+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-8] [context: com.thingworx.webservices.context.HttpExecutionContext@461806f5][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:59:41.911+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-6] [context: com.thingworx.webservices.context.HttpExecutionContext@2f5bca3e][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:59:43.927+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-2] [context: com.thingworx.webservices.context.HttpExecutionContext@746997df][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 02:59:44.847+0200 [L: ERROR] [O: c.t.w.BaseService] [I: ] [U: Administrator] [S: ] [T: http-nio-80-exec-9] [context: com.thingworx.webservices.context.HttpExecutionContext@50d76f0f][message: Unable To Convert From java.lang.String to NUMBER]
2017-06-01 03:19:47.065+0200 [L: WARN] [O: c.t.s.s.p.PlatformSubsystem] [I: ] [U: SuperUser] [S: ] [T: pool-6-thread-1]  Thingworx System Metrics

supandey
19-Tanzanite
(To:msiemering)

When connecting your VirtualThing (which is running via your Arduino) you'd need a corresponding Thing created out of RemoteThing Template in ThingWorx Composer which will have required properties to bind and get data from Arduino. So I think instead of creating a Thing with GenericThing Template you should create one which is of type RemoteThing template. With remote thing you can see the property whether or not it's connected.

However since you appear to be not using any ThingWorx EDGE SDK, and rather just attempting to send the data over you may not see isConnected property as true. But you will see values being updated once there's successful connection.

There has been quite a few discussions around this you can try checking Arduino REST call Example ThingWorx and also DeliveringArduinoDataToThingworx.pdf if you haven't already.

The discussion you have linked (Arduino REST call Example ThingWorx) was my starting point. I adjusted the code to be able to connect with WiFi.

I tried the code I have posted at the top with a Remote Thing and ThingWorx got data (Property Values) but only once in 10 tries. So there ist no constant connection and also I can not find any proof in log files that the data was received.

But I have a theoretically question: Would it be possible to change the the value of the "lastConnection" Property manually through the code?


Tomorrow I will work on the linked PDF-File. My aim is to send constantly data to ThingWorx.


While trying to do the tutorial out of your pdf file the actual problem is "terminated compilation" because of "no such file or directory" of "SRAM.h"

Announcements


Top Tags