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

Weather App with Arduino Uno

SOLVED
Highlighted
Level 1

Weather App with Arduino Uno

I have been following this guide to connect a DHT22 sensor to the thingworx platform: Weather App with Arduino Uno - ThingWorx : ThingWorx

But i have been unable to show the data from the sensor in the thingworx platform, here 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>

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

char server[] = "http://52.44.210.91/Thingworx/Things/DHT22Thing/Properties/*";

EthernetClient client;

//ThingWorx App key which replaces login credentials)

char appKey[] = "097b0bba-c01c-4f10-a049-ff45c8c8017b";

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

char thingName[] = "DHT22Thing";

//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 only thing i change from the code in the guide is the URL, Appkey,  im using the DHT22 humidity and temperature sensor so the thing name  i created is called DHT22Thing , the variable myhumidty changed to dht and i included different libraries.

Here is the sketch running:

I´d apreciate if anyone can help me, thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Weather App with Arduino Uno

Hey Alvaro,

Can  you please try changing:

char server[] = "http://52.44.210.91/Thingworx/Things/DHT22Thing/Properties/*

to:

char server[] = "52.44.210.91'

or, possibly, char server[] = "http://52.44.210.91/Thingworx/​ and see if you have the same result?

It looks like you are trying to post the values to a service, but you have the host set as the update properties URL rather than the server name.

Thanks!

3 REPLIES 3

Re: Weather App with Arduino Uno

Hey Alvaro,

Can  you please try changing:

char server[] = "http://52.44.210.91/Thingworx/Things/DHT22Thing/Properties/*

to:

char server[] = "52.44.210.91'

or, possibly, char server[] = "http://52.44.210.91/Thingworx/​ and see if you have the same result?

It looks like you are trying to post the values to a service, but you have the host set as the update properties URL rather than the server name.

Thanks!

Re: Weather App with Arduino Uno

Thank you, it is showing the correct values now.

Re: Weather App with Arduino Uno

If I use https://academic.cloud.thingworx.com/Thingworx/Composer/   server, then what will be my ip address.