4 Replies Latest reply on Sep 19, 2017 12:46 PM by asanordontri RSS
    asanordontri Newbie

    Weather App with ESP8266

    Weather App with ESP8266

    I try to connect a DHT22 sensor by ESP82266 and send the data to the Thingworx.

     

    I tried to edit the Weather App with Arduino Uno Codes. I can connect the Thingworx server.

     

    But I have been unable to show the data from the sensor in the Thingworx platform,

    The Serial Monitor show the information as below

     

    ################################################################

    Connected to AndroidAP

    IP address: 192.168.43.142

    Thingwork Server connected

    POST /Thingworx/Things/HTU21DThing/Services/setTempAndHumid?appKey=287b9c77-983c-4abf-9f75-4fc273c7183a&method=post&x-thingworx-session=true<&Temp=25.20&Humid=45.80> HTTP/1.1

    Host: ohimvo0g.studio-trial.thingworx.io

    Content-Type: text/html

     

    Thingwork Server connected

    POST /Thingworx/Things/HTU21DThing/Services/setTempAndHumid?appKey=287b9c77-983c-4abf-9f75-4fc273c7183a&method=post&x-thingworx-session=true<&Temp=25.20&Humid=45.70> HTTP/1.1

    Host: ohimvo0g.studio-trial.thingworx.io

    Content-Type: text/html

     

    ################################################################

     

    The Original Code.

    https://www.thingworx.com/ecosystem/academic-program/iot-projects/weather-app-arduino-uno/

     

     

    Here is my code that I have been using in ESP8266:

    ------------------------------------------------------------------------------------------------------------------------

    // Temp and Humid

      #include "DHT.h"

      #define DHTPIN 10     // what digital pin we're connected to

      #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

    //WiFi

      #include <ESP8266WiFi.h>

      const char* ssid = "AndroidAP";

      const char* password = "nslc3250x5589";

     

    // The IP address will be dependent on your local network:

     

    char server[] = "ohimvo0g.studio-trial.thingworx.io";

     

    WiFiClient client;

     

    //ThingWorx App key which replaces login credentials)

    char appKey[] = "287b9c77-983c-4abf-9f75-4fc273c7183a";

     

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

    char thingName[] = "HTU21DThing";

     

    // ThingWorx service that will set values for the properties you need

    // See the documentation for this tutorial for more information

    char serviceName[] = "setTempAndHumid";

     

     

    //Initialize an HTU21D library object to read

    // temperature and humidity data from your connected sensor

    DHT dht(DHTPIN, DHTTYPE);

     

    void setup() {

     

      // start serial port:

     

      Serial.begin(9600);

      while (!Serial) {

        ; // wait for serial port to connect. Needed for Leonardo only

      }

     

    //initialize HTU21D object to read values from sensors

       dht.begin();

     

      // start the Wifi connection:

    Serial.println("Trying to get an IP address using DHCP");

      WiFi.begin(ssid, password);

    Serial.println("");

     

    // Wait for connection

      while (WiFi.status() != WL_CONNECTED) {

        delay(500);

    Serial.print(".");

      }

    Serial.println("");

    Serial.print("Connected to ");

    Serial.println(ssid);

    Serial.print("IP address: ");

    Serial.println(WiFi.localIP());

     

    }

     

    void loop() {

     

      // Aquire sensor values

      float Temp =  dht.readTemperature();

      float Humid = dht.readHumidity();

     

      //through REST calls to your TWX server

      // if you get a connection, report back via serial:


      if (client.connect(server, 8443)) {

        Serial.println("Thingwork Server connected");

        // send the HTTP POST request:

    client.print("POST /Thingworx/Things/");

    client.print(thingName);

    client.print("/Services/");

    client.print(serviceName);

    client.print("?appKey=");

    client.print(appKey);

    client.print("&method=post");

    client.print("&x-thingworx-session=true");

    client.print("<");

    client.print("&");

    client.print("Temp");

    client.print("=");

    client.print(Temp);

    client.print("&");

    client.print("Humid");

    client.print("=");

    client.print(Humid);

    client.print(">");

    client.println(" HTTP/1.1");

    client.print("Host: ");

    client.println(server);

    client.println("Content-Type: text/html");

        client.println();

     

        client.stop();

     

        // print the request out

    Serial.print("POST /Thingworx/Things/");

    Serial.print(thingName);

    Serial.print("/Services/");

    Serial.print(serviceName);

    Serial.print("?appKey=");

    Serial.print(appKey);

    Serial.print("&method=post");

    Serial.print("&x-thingworx-session=true");

    Serial.print("<");

    Serial.print("&");

    Serial.print("Temp");

    Serial.print("=");

    Serial.print(Temp);

    Serial.print("&");

    Serial.print("Humid");

    Serial.print("=");

    Serial.print(Humid);

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

      } 

     

    }

     

     

    ----------------------------------------------------------------------------------------------

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

     

    esp8266-thingwork.jpg

      • Re: Weather App with ESP8266
        ytella Apprentice

        Hi Adam Sanordontri,

        Could you please try the following and see if it works?

         

        Find the line char server[] = "ohimvo0g.studio-trial.thingworx.io"; in the code and update to char server[] = "ohimvo0g.studio-trial.thingworx.io:8443";


        Save and upload the program to your ESP8266 board.

        Go back to your HTU21D Thing properties section, click on the refresh properties button and see if the values are being updated constantly.

        If this doesn't solve the issue you can revert to original settings.

          • Re: Weather App with ESP8266
            asanordontri Newbie

            I tried yo add :8443 for the server port .

             

            The Serial Monitor show the information as below

             

            Connected to AndroidAP

            IP address: 192.168.43.142

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

            the connection could not be established

              • Re: Weather App with ESP8266
                ytella Apprentice

                Hi Adam Sanordontri,

                I apologize for the inconvenience. Could you please revert the settings to the default, run the code on ESP8266 board and see if you can find any errors in the application log.

                  • Re: Weather App with ESP8266
                    asanordontri Newbie

                      #include "DHT.h"

                      #define DHTPIN 10     // what digital pin we're connected to

                      #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

                     

                     

                      #include <Wire.h>

                      #include <SPI.h>

                      #include <Ethernet.h>

                     

                     

                      //WiFi

                     

                      #include <ESP8266WiFi.h>

                      #include <WiFiClient.h>

                      const char* ssid = "AndroidAP";

                      const char* password = "nslc3250";

                     

                     

                     

                     

                    char server[] = "13.114.212.97";

                     

                     

                    WiFiClient client;

                     

                     

                    //How many values you will be pushing to ThingWorx

                    #define propertyCount 2

                     

                     

                    //ThingWorx App key which replaces login credentials)

                    char appKey[] = "4ae58966-c96d-4f07-ace6-43d9775347a3";

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

                    char thingName[] = "HTU21DThing";

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

                      while (!Serial) {

                        ; // wait for serial port to connect. Needed for Leonardo only

                      }

                     

                     

                      //initialize HTU21D object to read values from sensors

                       dht.begin();

                      

                     

                    // start the Wifi connection:

                      Serial.println("Trying to get an IP address using DHCP");

                      WiFi.begin(ssid, password);

                      Serial.println("");

                     

                     

                      // Wait for connection

                      while (WiFi.status() != WL_CONNECTED) {

                        delay(500);

                        Serial.print(".");

                      }

                      Serial.println("");

                      Serial.print("Connected to ");

                      Serial.println(ssid);

                      Serial.print("IP address: ");

                      Serial.println(WiFi.localIP());

                     

                    }

                     

                     

                    void loop() {

                     

                     

                    // Aquire sensor values

                      propertyValues[0] = dht.readTemperature();

                      propertyValues[1] = dht.readHumidity();

                     

                     

                     

                      // wait the established interval of time before

                      // reading values from the sensor

                      // and sending them to the TWX server again

                      // delay(timeBetweenRefresh);

                      if (millis() - lastConnectionTime > timeBetweenRefresh) {

                        updateValues(propertyValues, client, server, appKey, thingName, serviceName, propertyNames);

                      }

                    }

                     

                     

                    void updateValues(double values[] , WiFiClient &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 Serial Monitor show the information as below

                     

                    Connected to AndroidAP

                    IP address: 192.168.43.142

                     

                     

                    the connection could not be established

                    the connection could not be established

                    the connection could not be established

                    the connection could not be established

                     

                    ###############################################

                     

                    I used 30 days Thingwork trial sever  for testing because It use port No.80 to conect.

                     

                    I´d appreciate if anyone can help me or give the example code for esp8266, thanks.