Posting the sensor data online

Using the ESP8266, you can log sensor data to online servers for monitoring and control purposes. In this recipe, we will be looking at how to do that. We will use an ESP8266 board to post and store sensor data on dweet.io.

Getting ready

To do this tutorial, you will need an ESP8266 board and a USB cable among several other things, which include:

The hardware setup will resemble the one in the previous recipe.

In addition to setting up the hardware, you will set up the online platform where the sensor data will be posted. Luckily the platform we are using, dweet.io, is simple to use and requires no setup or signup. All you need to do is choose a name for your thing, which in this case is your ESP8266, then you can start publishing data to dweet.io.

How to do it…

Posting data to dweet.io is simple. It is done by calling a URL such as https://dweet.io/dweet/for/my-thing-name?hello=world. You are required to change my-thing-name to the name of your thing, hello to the name of the parameter that you are posting, and world to the value of the parameter that you are posting online.

In our case, we will call our thing garden-monitor-11447, and the names of the parameters we will be posting are humidity, temperature, and moisture. Our URL will look like this: https://dweet.io/dweet/for/garden-monitor-11447?humidity=<humidity value>&temperature=<temperature value>&moisture=<moisture value>.

Once you have successfully generated the URL:

  1. Connect the ESP8266 board to a Wi-Fi network that has Internet connection and then read the humidity, temperature, and soil moisture values from the sensors, as done in the previous recipe.
  2. Replace the <humidity value>, <temperature value>, and <moisture value> in the preceding URL with the readings obtained from the sensors. The URL will be used to send a http request to the dweet.io server.
  3. When the ESP8266 successfully sends the http request, the sensor data will be published on the dweet.io platform. To view the data, visit this URL from any web browser: https://dweet.io/follow/garden-monitor-11447:
    // Libraries
    #include <ESP8266WiFi.h>
    #include "DHT.h"
  4. ssid and password is set in this section:
    // Wi-Fi network SSID and password
    const char* ssid     = "your-ssid";
    const char* password = "your-password";
  5. Store the host name of the cloud server:
    // Host
    const char* host = "dweet.io";
  6. Define the pin that is connected to the DHT11 signal pin and the type of DHT sensor that we are using:
    #define DHTPIN 2 // what digital pin DHT11 is connected to
    #define DHTTYPE DHT11 // DHT 11 sensor
  7. Create a dht object:
    DHT dht(DHTPIN, DHTTYPE);
  8. Declare a variable that will hold the moistureReading:
    int moistureReading = 0; // holds value soil moisture sensor reading
  9. Initialize the serial interface and the DHT object. Configure the ssid and password of the Wi-Fi network and connect the ESP8266 to it:
    void setup() {
      Serial.begin(115200); // initialize serial interface
      dht.begin(); // initialize DHT11 sensor
      delay(10);
    
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      
      WiFi.begin(ssid, password);
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");  
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
  10. Delay for five seconds, then print the name of the host we are connecting to on the serial monitor:
    void loop() {
      delay(5000);
    
      Serial.print("connecting to ");
      Serial.println(host);
  11. Connect to the host server. Retry if not successful:
      // Use WiFiClient class to create TCP connections
      WiFiClient client;
      const int httpPort = 80;
      if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
      }
  12. Get readings from the DHT11 sensor and soil moisture sensor:
      // Read sensor inputs
      // get humidity reading
      float h = dht.readHumidity(); 
      // get temperature reading in Celsius
      float t = dht.readTemperature(); 
      // Check if any reads failed and exit early (to try again).
      while (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        delay(2000); // delay before next measurements
        //get the measurements once more
        h = dht.readHumidity(); 
        t = dht.readTemperature();
      }
      //get soil moisture reading
      moistureReading = analogRead(A0);
    • Generate a URL for the GET request we will send to the host server. The URL will include the sensor readings:
        // We now create a URI for the request
        String url = "/dweet/for/garden-monitor-11447?humidity=";
        url += String(h);
        url += "&temperature=";
        url += String(t);
        url += "&moisture=";
        url += String(moistureReading);
  13. Send the GET request to the server and check whether the request has been received, or if it has been timed out:
      // Send request
      Serial.print("Requesting URL: ");
      Serial.println(url);
      
      client.print(String("GET ") + url + " HTTP/1.1
    " +
                   "Host: " + host + "
    " + 
                   "Connection: close
    
    ");
      unsigned long timeout = millis();
      while (client.available() == 0) {
        if (millis() - timeout > 5000) {
          Serial.println(">>> Client Timeout !");
          client.stop();
          return;
        }
      }
    • Read incoming data from the host server line by line and display the data on the serial monitor. Close the connection after all the data has been received from the server:
        // Read all the lines from the answer
        while(client.available()){
          String line = client.readStringUntil('
      ');
          Serial.print(line);
        }
      
        // Close connecting
        Serial.println();
        Serial.println("closing connection");
      }
  14. Copy the sketch to your Arduino IDE and change the ssid in the code from your-ssid to the name of your Wi-Fi network, and the password from your-password to the password of your Wi-Fi network.
  15. Upload the sketch to your ESP8266 board. Open the serial monitor so that you can view the incoming data.

How it works…

The program connects to the Wi-Fi network using the provided password and ssid. It then proceeds to connect to the provided cloud/host server using the client.connect() function. Once the ESP8266 connects successfully, data from the sensors is read and a URL is generated that includes the updated sensor data. The URL is then sent to the host server using the client.print() function.

Once the data has been successfully sent, the sketch waits for a reply from the server. It does this with the client.available() function, which checks whether there is incoming data from the server. If there is data available, the sketch reads it and displays it on the serial monitor. The ESP8266 posts sensor data to dweet.io every five seconds.

See also

Once the sensor data is posted online, you will need to retrieve it for monitoring and control purposes. This can be done using the ESP8266, as explained in the next recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.10.116