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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Unable to push the sensor output to the thingworx database

dm-21
1-Newbie

Unable to push the sensor output to the thingworx database

Presently i'm working on an example IoT project (Temperature and Humidity measurement using Arduino Uno), I got the sensor output using Arduino IDE but unable to see the output in properties section in the Thingworx database. I have followed all the procedures given in the instruction manual. And the second thing is i'm unable to find the Mail Server Template in the Thing Template. Can anyone suggest me, what might be the problem?

ACCEPTED SOLUTION

Accepted Solutions
vmihai
1-Newbie
(To:dm-21)

Hi,

Can you please post a print screen of your Arduino IDE Serial Monitor with the REST calls that are being sent to ThingWorx ?

The Mail Server needs to be imported separately. You can download it from the market place at this url

ThingWorx IoT Marketplace » Mail

Thank you,

Veronica

View solution in original post

10 REPLIES 10
vmihai
1-Newbie
(To:dm-21)

Hi,

Can you please post a print screen of your Arduino IDE Serial Monitor with the REST calls that are being sent to ThingWorx ?

The Mail Server needs to be imported separately. You can download it from the market place at this url

ThingWorx IoT Marketplace » Mail

Thank you,

Veronica

dm-21
1-Newbie
(To:vmihai)

Restcall.gifserial Monitor 1.gifserial Monitor 2.gif

Thanks alot Veronica

dm-21
1-Newbie
(To:dm-21)

//#include <Dhcp.h>

//#include <Dns.h>

#include <Ethernet.h>

#include <EthernetClient.h>

#include <EthernetServer.h>

//#include <EthernetUdp.h>

#include <SPI.h>

#include "SparkFunHTU21D.h"

// #include <HTU21D.h>

#include <Wire.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:

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

char server[] = "ge2-4664.cloud.thingworx.com";

EthernetClient client;

//ThingWorx App key which replaces login credentials)

char appKey[] = "60cd9a5e-04e2-4e26-b7b9-f457cd91e774";

// ThingWorx Thing name for which you want to set properties values

char thingName[] = "HTU21DThing";

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

HTU21D myHumidity;

void setup() {

  //shut down the SD Card pins

  pinMode(4,OUTPUT);

  digitalWrite(4,HIGH);

 

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

   myHumidity.begin();

  // start the Ethernet connection:

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

  // Aquire sensor values

  propertyValues[0] = myHumidity.readTemperature();

  propertyValues[1] = myHumidity.readHumidity();

  // 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[] , EthernetClient &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.connect(server, 80)) {

    Serial.println("connected");

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

  }

}

vmihai
1-Newbie
(To:dm-21)

Hi,

I see that you are using ThingWorx 6.0  The tutorial was developed on ThingWorx 5.2 and there is an additional step to  be done on ThingWorx 6.0 to make it work. You need to enable REST calls  by following 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

Let me know if this fixes the issue,

Thank you,

Veronica

dm-21
1-Newbie
(To:vmihai)

Hai Good Morning Veronica,

It is working fine now,  i can able to see the updating values in my property.

Thank you so much for your great support veronica.

Dhivya

dm-21
1-Newbie
(To:dm-21)

Hai,

I have an other doubt veronica, my label chart is not showing the graph in the Mashup view page.

I have uploaded the .ino file again and i have done refresh also. Can u please help me?

dm-21
1-Newbie
(To:vmihai)

Hai,

I have an other doubt, my label chart is not showing the graph in the Mashup view page.

I have uploaded the .ino file again and i have done refresh also. Can u please help me?

vmihai
1-Newbie
(To:dm-21)

Hi Dhivya,

Can you please verify if your data has been properly logged in the value stream that you assigned to your HTU21DThing ? You can do this by going to the services section of the HTU21DThing and testing the QueryPropertyHistory General service.

If you can see the values for the logged properties  as shown below, than your Stream is properly set up.

If you cannot see any property, please first verify that your properties are being properly sent to ThingWorx, by running the sketch on your Arduino and going to the Properties section of the HTU21DThing and hitting the update button. If the properties are being updated, follow the instructions for setting up your logged properties described in this tutorial starting from page 2. http://www.thingworx.com/-/media/Files/PDFs/Academics/32AUHowtoCreateaMashupforArduinoUnoStep2.pdf?la=en

Let me know if you are in need of further assistance.

Thank you,

Veronica

dm-21
1-Newbie
(To:vmihai)

Hai Veronica,

Thanks a lot for your precious guidance, i got the proper output with the graph.

Thank you,

Output.gif

Dhivya

fdasilva
1-Newbie
(To:dm-21)

Hi Veronica,

I am facing the same problem. The serial monitor shows the correct POST method but the properties are not being updated.

I am using the Composer 7.3.2.

The code is:

#include <DHT.h>

//#include <HTU21D.h>

#include <Wire.h>

/****************************************************************************

Arduino to Thingworx Ethernet Web Client using Ethernet Shield

This sketch reads sensor values and sends the values to a Thing on ThingWorx.

The sketch uses the Ethernet Repeating Web client (info below) and a custom ThingWorx library

as a starting point.  The user must choose a unique MAC address (the sketch right now

is set up as DHCP but you can add Static IP and DNS if you'd like.  Just make sure you

relay those changes in the Ethernet.begin() call).  On the Thingworx side make sure your

nameArray[] values, your thingName[], your serviceName[] and your server[] matches exactly.

Use your provided appKey that you recieve when you set up your Thing.

Create a service in your Thing or ThingShape and create inputs that will match the variable names you use

in your Arduino code.  In the JavaScript window type something similar to the following:

me.PropertyOne = parseFloat(InputOne);

me.PropertyTwo = parseFloat(InputTwo);

Where Property one is the name of your first Thing or ThingShape property and InputOne is the name of

your first input.  Everything is case sensitive.

Update sensorCount to reflect how many sensors (or whatever data variables) you want to

send.

created 3/5/2015

by Nick Milleson

Design Engineer

EAC Product Development Solutions

www.eacpds.com

nmilleson@eacpds.com

/****************************************************************************

  Repeating Web client

This sketch connects to a a web server and makes a request

using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or

the Adafruit Ethernet shield, either one will work, as long as it's got

a Wiznet Ethernet module on board.

This example uses DNS, by assigning the Ethernet client with a MAC address,

IP address, and DNS address.

Circuit:

* Ethernet shield attached to pins 10, 11, 12, 13

created 19 Apr 2012

by Tom Igoe

http://arduino.cc/en/Tutorial/WebClientRepeating

This code is in the public domain.

*****************************************************************************/

#include <SPI.h>

#include <Ethernet.h>

#define DHTPIN 2     // what digital pin we're connected to

#define DHTTYPE DHT11   // DHT 11

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

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

char server[] = "http://pp-20170404234723327.portal.ptc.io/Thingworx";

EthernetClient client;

//ThingWorx App key which replaces login credentials)

char appKey[] = "51884259-1003-444a-b361-485668d595ec";

// ThingWorx Thing name for which you want to set properties values

char thingName[] = "HTD11Thing";

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

//HTU21D myHumidity;

DHT myHumidity(DHTPIN, DHTTYPE);

void setup() {

  //shut down the SD Card pins

//pinMode(4,OUTPUT);

//digitalWrite(4,HIGH);

 

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

   myHumidity.begin();

  // start the Ethernet connection:

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

  // Aquire sensor values

  propertyValues[0] = myHumidity.readTemperature();

  propertyValues[1] = myHumidity.readHumidity();

  // 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[] , EthernetClient &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:

client.connect(server, 80);

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

    Serial.println("connected");

    Serial.println("HTD11Thing");

    Serial.println("setTempAndHumid");

    // send the HTTP POST request:

    client.print("POST /Thingworx/Things/");

    client.print("HTD11Thing");

    client.print("/Services/");

    client.print("setTempAndHumid");

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

    Serial.print("/Services/");

    Serial.print("setTempAndHumid");

    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 didn't get a connection to the server:

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

    Serial.println("HTD11Thing");

    Serial.println("setTempAndHumid");

    client.stop();

  }

}

Any ideas? I have read all information on internet and try everything.

Thanks in advance!

Felipe da Silva

Announcements


Top Tags