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

DHT Sensor data to Thingworx using Arduino UNO & Ethernet

Highlighted
Newbie

DHT Sensor data to Thingworx using Arduino UNO & Ethernet

I am using a DHT22 Sensor on an Arduino UNO using an Ethernet Shield to send data to Thingsworx.

My account on Thingworx is being provided through my university.

I am able to get the data on SerialCom but unable to display on the Thingworx Platform.

Any help in this matter would be very helpful!

Attaching the screenshots and code for reference.

Thank you!

CODE:


#include <DHT.h>

#include <DHT_U.h>

#include <Wire.h>

#include <SPI.h>

#include <Ethernet.h>

//How many values you will be pushing to ThingWorx

#define propertyCount 2

#define DHTPIN 2

#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

// 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[] = "https://iupui.cloud.thingworx.com/Thingworx/Composer/";

EthernetClient client;

//ThingWorx App key which replaces login credentials)

char appKey[] = "d6b579cb-981a-402c-99c7-6992171e1fe5";

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

char thingName[] = "TempT_mskhirid";

//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[] = "TempNHumid";

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

void setup() {

  //shut down the SD Card pins

  pinMode(4, OUTPUT);

  digitalWrite(4, HIGH);

// start serial port:

Serial.begin(9600);

  dht.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] = dht.readTemperature();

  propertyValues[1] = dht.readHumidity();

// Aquire sensor values

  // Wait a few seconds between measurements.

  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!

  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

  float h = dht.readHumidity();

  // Read temperature as Celsius (the default)

  float t = dht.readTemperature();

  // Read temperature as Fahrenheit (isFahrenheit = true)

  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).

  if (isnan(h) || isnan(t) || isnan(f)) {

    Serial.println("Failed to read from DHT sensor!");

    return;

  }

  // Compute heat index in Fahrenheit (the default)

  float hif = dht.computeHeatIndex(f, h);

  // Compute heat index in Celsius (isFahreheit = false)

  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");

  Serial.print(h);

  Serial.print(" %\t");

  Serial.print("Temperature: ");

  Serial.print(t);

  Serial.print(" *C ");

  Serial.print(f);

  Serial.print(" *F\t");

  Serial.print("Heat index: ");

  Serial.print(hic);

  Serial.print(" *C ");

  Serial.print(hif);

  Serial.println(" *F");

  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, 443)) {

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

  }

}

SERIALCOM OUTPUT:


Capture.JPG

Tags (2)
2 REPLIES 2

Re: DHT Sensor data to Thingworx using Arduino UNO & Ethernet

Hi Mayur, are you running into some sort of issues e.g. any specific errors that are observed when you attempt to write the values to the ThingWorx? Or are you in the phase of figuring out how to send data to ThingWorx? There are quite a few blogs available in the community you might want to give them a try e.gDelivering Arduino Collected Data to ThingWorx using MQTT

Re: DHT Sensor data to Thingworx using Arduino UNO & Ethernet

It looks like you are trying to execute a custom service that you created. If your service takes HTML content as your header states, the content is not properly formatted. Why not use the services that are built into ThingWorx to set property values? Check out how to use the REST API here:

https://developer.thingworx.com/resources/guides/rest-api-how-guide/property-values-rest-api-how

And take  a look at a similar example:

https://developer.thingworx.com/resources/guides/connect-adafruit-feather/set-arduino-ide-and-board