Skip to main content
1-Visitor
February 8, 2016
Solved

Unable to push the sensor output to the thingworx database

  • February 8, 2016
  • 2 replies
  • 4763 views

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?

    Best answer by vmihai

    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

    2 replies

    vmihai1-VisitorAnswer
    1-Visitor
    February 8, 2016

    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-211-VisitorAuthor
    1-Visitor
    February 9, 2016

    Restcall.gifserial Monitor 1.gifserial Monitor 2.gif

    Thanks alot Veronica

    dm-211-VisitorAuthor
    1-Visitor
    February 9, 2016

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

      }

    }

    1-Visitor
    April 13, 2017

    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