The community will undergo maintenance on October 16th at 10:00 PM PDT and will be unavailable for up to one hour.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <Ethernet.h>
//How many values you will be pushing to ThingWorx
#define propertyCount 2
#define DHTPIN 4 // what digital pin we're connected to
#define DHTTYPE DHT22
// 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://academic.cloud.thingworx.com/Things/salesforceThing_ahmed123/Properties/Temperature";
EthernetClient client;
//ThingWorx App key which replaces login credentials)
char appKey[] = "194b5fda-5be4-411a-8bcb-d20ccead7e55";
// ThingWorx Thing name for which you want to set properties values
char thingName[] = "salesforceThing_ahmed123";
//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
DHT dht(DHTPIN, DHTTYPE);
void setup() {
//shut down the SD Card pins
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// start serial port:
Serial.begin(9600);
Serial.println("DHT22 Library Demo");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//initialize HTU21D object to read values from sensors
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, 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();
}
}
The above code is as per my thing, I'm getting humidity and temperature in arduino serial monitor.
Could you please help me to update temperature and humidity value in thingworx as well.
Thanks,
Ahmed
Does the advice Adam gave in your other post help with this question as well?
Let us know.
Thanks,
-- Craig A.