Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X
I'm using the Weather App with Arduino Uno to connect a DHT22 sensor to the thingworx platform, but i have been unable to show the data from the sensor in thingworx. On the serial monitor I can see the values of temperature and humidity been updated, but not in thingworx (Thingworx 7).
This is the code that I have been using in the arduino:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <Ethernet.h>
#define propertyCount 2
#define DHTPIN 2
#define DHTTYPE DHT22
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;
char appKey[] = "My AppKey";
char thingName[] = "HTD22ThingPrueba";
int timeBetweenRefresh = 5000;
char serviceName[] = "setTempAndHumid";
char* propertyNames[] = {"Temp", "Humid"};
double propertyValues[propertyCount];
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.begin(9600);
Serial.println("Temperatura y Humedad con Arduino");
while (!Serial) {
; }
dht.begin();
Ethernet.begin(mac, ip);
Serial.print("My IP address: ");
Serial.print(Ethernet.localIP());
Serial.println();
}
void loop() {
propertyValues[0] = dht.readTemperature();
propertyValues[1] = dht.readHumidity();
delay(5000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
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[])
{
char data[80];
strcpy(data, "?appKey=");
strcat(data, appKey);
strcat(data, "&method=post&x-thingworx-session=true");
if (client.connect(server, 443)) {
Serial.println("connected");
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();
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 {
Serial.println("the connection could not be established");
client.stop();
}
}
I appreciate your help, thanks.
Hi Yajseel did you check the log for detail. I may be missing this here but looks like you are not using any of the ThingWorx Edge SDK is that correct? Else you should check the Edge SDK's log. Else please check for the Applicationlog.log from ThingWorx
How is the Thing's property i.e. Temperature and Humidity setup is it setup to fetch every change from the device?
What does the full POST URL look like? Taking a quick glance through the code, it looks like there are raw <>
characters inserted in the URL just after printing out the `data` variable:
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(">");
POST requests typically send the parameters in the request body with a content type of application/json
specified. This link has more details:
REST API Overview and Examples
It might also be helpful to use the HttpClient library which would help simplify the code and perhaps make it easier to troubleshoot:
HTTPClient http;
http.begin("http://yourcoolurl.com/posts");
http.addHeader("Content-Type", "application/json");
http.POST("{'parameter': 1, 'anotherParameter': 'example'");
http.writeToStream(&Serial);
http.end();