Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
Hi I have a problem, I have connected 1 temperature sensor to an Arduino card and I want the data to be recorded in a thing, the connection is made but in the properties there is no value, attached screenshot to see if I can support
#include <Wire.h>
#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
//How many values you will be pushing to ThingWorx
#define propertyCount 2
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// 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};
IPAddress ip(192,168,20,35);
char server[] = "3thmsdws.pp.vuforia.io";
int port = 443;
EthernetClient client;
//ThingWorx App key which replaces login credentials)
char appKey[] = "4d6fc02c-bf77-4da7-98db-e115c3771fcd";
// ThingWorx Thing name for which you want to set properties values
char thingName[] = "HTD22ThingPrueba";
//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("Temperatura y Humedad con Arduino");
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, ip);
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(5000);
// 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 {
// If you didn't get a connection to the server:
Serial.println("the connection could not be established");
client.stop();
}
}