Internet of Things with ESP8266
上QQ阅读APP看书,第一时间看更新

Reading data from a digital sensor

In this last section of this chapter, we are going to connect a digital sensor to our ESP8266 chip, and read data from it. As an example, we will use a DHT11 sensor, which can be used to get ambient temperature and humidity.

You will need to get this component for this section, the DHT11 sensor (https://www.adafruit.com/products/386)

Let's now connect this sensor to your ESP8266:

  1. First, place the sensor on the breadboard. Then, connect the first pin of the sensor to VCC, the second pin to pin 5 of the ESP8266, and the fourth pin of the sensor to GND.

    This is what it will look like at the end:

    Note

    Note that here I've used another ESP8266 board, the Adafruit ESP8266 breakout board. I will use this board in several chapters of this book.

    We will also use the aREST framework in this example, so it's easy for you to access the measurements remotely. aREST is a complete framework to control your ESP8266 boards remotely (including from the cloud), and we are going to use it several times in the book. You can find more information about it at the following URL: http://arest.io/.

  2. Let's now configure the board. The code is too long to be inserted here, but I will detail the most important part of it now.

    Note

    Note that you can find the code on the GitHub repository of the book:https://github.com/openhomeautomation/iot-esp8266-packt.

  3. It starts by including the required libraries:
    #include "ESP8266WiFi.h"
    #include <aREST.h>
    #include "DHT.h"
  4. To install those libraries, simply look for them inside the Arduino IDE library manager. Next, we need to set the pin that the DHT sensor is connected to:
    #define DHTPIN 5
    #define DHTTYPE DHT11
  5. After that, we declare an instance of the DHT sensor:
    DHT dht(DHTPIN, DHTTYPE, 15);
  6. As earlier, you will need to insert your own Wi-Fi name and password into the code:
    const char* ssid = "wifi-name";
    const char* password = "wifi-pass";
  7. We also define two variables that will hold the measurements of the sensor:
    float temperature;
    float humidity;
  8. In the setup() function of the sketch, we initialize the sensor:
    dht.begin();
  9. Still in the setup() function, we expose the variables to the aREST API, so we can access them remotely via Wi-Fi:
    rest.variable("temperature",&temperature);
    rest.variable("humidity",&humidity);
  10. Finally, in the loop() function, we make the measurements from the sensor:
    humidity = dht.readHumidity();
    temperature = dht.readTemperature();
  11. It's now time to test the project! Simply grab all the code and put it inside the Arduino IDE. Also make sure to install the aREST Arduino library using the Arduino library manager.
  12. Now, put the ESP8266 board in bootloader mode, and upload the code to the board. After that, reset the board, and open the Serial monitor. You should see the IP address of the board being displayed:
  13. Now, we can access the measurements from the sensor remotely. Simply go to your favorite web browser, and type:
    192.168.115.105/temperature

    You should immediately get the answer from the board, with the temperature being displayed:

    {
      "temperature": 25.00,
    "id": "1", 
    "name": "esp8266", 
    "connected": true
    }

    You can of course do the same with humidity.

Note

Note that here we used the aREST API, which we will use in several other chapters in this book. You can learn more about it at http://arest.io/.

Congratulations, you just completed your very first projects using the ESP8266 chip! Feel free to experiment with what you learned in this chapter, and start learning more about how to configure your ESP8266 chip.