In the previous tutorial, ESP8266 module was used to design a Home Area Network to control an LED light from a remote PC. The PC client was used to send control signals to switch on and off the LED light in the Home Area Network. In this tutorial, the ESP module will be interfaced with DHT-11 temperature sensor and temperature data will be sent to the PC client for real-time temperature monitoring.
In this project, instead of LED, the DHT-11 sensor will be interfaced with the ESP8266 on ESP Client side. The Arduino sketch will be written to read temperature from the sensor and send it to the MQTT broker. A remote PC which will be configured as another MQTT client which will then receive the temperature and humidity readings and display them on a browser.
The IOT device designed in this project is a temperature monitor which is designed by interfacing the DHT-11 temperature and humidity sensor with the ESP-8266 Wi-Fi module. The Wi-Fi module as well as DHT-11 sensor are powered continuously with the help of a USB to Serial Converter. The Wi-Fi module needs to loaded with a firmware that could read temperature from the sensor and publish it to the MQTT broker. The Arduino UNO is used to flash the firmware code on the ESP8266 module. The ESP module can also be flashed with code using a FTDI converter like CP2102. The firmware itself is written in the Arduino IDE.
The remote PC acts as another IOT device in the project. The PC connects with ESP8266 based device via the MQTT broker. The PC connects with the Adafruit broker which will itself act as an MQTT Client.
In the previous tutorial, HiveMQ broker was used. In this tutorial, Adafruit broker will be used. Adafruit broker is highly secure MQTT broker and it supports TLS/SSL, authentication and authorization security. To use the Adafruit broker, an account need to be created on io.adafruit.com. The broker generates a random key for the user account. This is a broker’s private key which can be used to connect the device to the broker. So, only the devices which have the private key of broker can request for connection from the broker. In this way, it provides basic security. Adafruit broker can act as MQTT client simultaneously. In this project the Adafruit broker is configured as client as well.
Components Required –
Fig. 1: List of components required for MQTT Protocol on ESP8266 Module based IoT Sensor Data Monitor
Software Required –
• Arduino IDE
• Adafruit broker
Block Diagram –
Fig. 2: Block Diagram of Adafruit Broker controlled ESP8266 based Temeperature and Humidity Monitor
Circuit Connections –
First of all, the ESP8266 board needs to be loaded with the firmware code. In this tutorial, the firmware code is written using Arduino IDE. It is loaded to the ESP8266 board using the Arduino UNO. A generic ESP8266 board is used in this project. This board does not have any bootstrapping resistors, no voltage regulator, no reset circuit and no USB-serial adapter. The ESP8266 module operates on 3.3 V power supply with current greater than or equal to 250 mA. So, CP2102 USB to serial adapter is used to provide 3.3 V voltage with enough current to run ESP8266 reliably in every situation.
The ESP8266 Wi-Fi Module is a self contained SOC with integrated TCP/IP protocol stack that can access to a Wi-Fi network. The ESP8266 is capable of either hosting an application or off loading all Wi-Fi networking functions from another application processor. Each ESP8266 module comes pre-programmed with an AT command set firmware. The module comes available in two models – ESP-01 and ESP-12. ESP-12 has 16 pins available for interfacing while ESP-01 has only 8 pins available for use. The ESP-12 has the following pin configuration –
Fig. 3: Table listing pin configuration of ESP8266 ESP-12 Wi-Fi Modem
The ESP-01 model is used in the project. The ESP-01 model has the following pin configuration –
Fig. 4: Table listing pin configuration of ESP8266 ESP-12 Wi-Fi Modem
The Chip Enable and VCC pins of the module are connected to the 3.3 V DC while Ground pin is connected to the common ground. The chip enable pin is connected to VCC via a 10K pull up resistor. The RESET pin is connected to the ground via a tactile switch where the pin is supplied VCC through a 10K pull up resistor by default. The Tx and Rx pins of the module are connected to the RX and TX pins of the Arduino UNO. The GPIO-0 pin of the module is connected to ground via a tactile switch where the pin is supplied VCC through a 10K pull up resistor by default. These pull-up resistors act as voltage divider circuit which protects the ESP8266 board from high voltage. The Arduino board operates at 5V as well as 3.3 V while ESP8266 operates at 3.3 V. Though the Arduino board itself is powered via its 3.3 V supply pin, this voltage divider circuit further adds protection from any voltage surge. The use of pull-up resistors increases the stability of the circuit. With these circuit connections, the Arduino board itself act as USB to Serial Adaptor. The circuit connections between the Arduino UNO and ESP8266 module for boot loading can be summarized as follow –
Fig. 5: Table listing circuit connections between ESP8266 Modem and Arduino Uno
During compilation of the code, the GPIO0 and RESET switches must be pressed. For uploading program, the RESET switch must be released while the GPIO0 programming switch must be left pressed, so that the ESP can enter in programming mode. After uploading the code, the programming switch should also be released as well.
Write the firmware code in the Arduino IDE and connect the Arduino board with the PC via USB cable. Open Arduino IDE and go to Tools->Port and select the Arduino board (Arduino UNO). It may look like /dev/ttyABM0 (Arduino/Genuino Uno). Select the correct port name. The port name can be different in different IDE setups. Then Open Serial monitor in the Arduino IDE by navigating to Tools->Serial Monitor and set the baud rate to 115200 bauds per second. Pass ‘AT’ and ‘AT+GMR’ commands to test the connection between the Arduino and ESP-01 module. Try different settings for the ‘Line ending’ option of the serial monitor like Both NL & CR. Try different combinations until the ESP module starts interacting correctly with the serial monitor.
Download Python, PIP and ESPtool. Erase the pre-loaded firmware if the ESP module has any. Flash the firmware code to the ESP-01 module by writing the commands in serial monitor as instructed for the ESPtool. Check out the following link to write proper command for flashing the firmware code using ESPtool –
For uploading program, the RESET switch must be released while the GPIO0 programming switch must be left pressed. After loading the code, the programming switch should also be released as well. In this way, the firmware code can be loaded to the ESP8266 using the Arduino board as FTDI converter. It must be noted that every GND needs to be common to complete the circuit. The GPIO2 is alternative TX for bootloader mode.
Remove the connections. Now the ESP module is ready to be installed in the project circuit. In the project circuit, the ESP8266 is interfaced with the CP2102 adapter and DHT-11 Sensor.
The CP2102 is a single chip USB to UART Bridge. It is used to provide power supply to the ESP module. It can be used for bootloading as well. A CP2102 IC has the following pin configuration –
Fig. 6: Table listing pin configuration of CP2102 IC
Fig. 7: Table listing pin configuration of CP2102 IC
Fig. 8: Table listing pin configuration of CP2102 IC
The ESP board is connected to the CP2102 for bootloading mode and usage purpose. For bootloading, the ESP-01 TX pin is connected to RX pin of CP2102 and ESP’s RX pin is connected to the TX of CP2102 so that they both can send and receive the data. The ESP8266-01 needs 3.3 V power supply so ESP’s VCC is connected to the 3V3 of CP2102 and GNDs of both need to be connected to each other. The reset pin of ESP8266 along with 10k pull-up resistor (bootstrap resistor) is connected to the RTS pin of CP2102. The GPIO0 is a programming pin which is used to end the ESP-01 into programming mode. The GPIO0 along with 10K pull-up resistor is connected to DTR pin of CP2102. The CHPD pin of ES-01 is pulled-up with 10K resistor. The circuit connections between the ESP module and the CP2102 are summarized in the following table –
Fig. 9: Table listing circuit connections between ESP8266 ESP-01 Modem and CP2102 IC
After connecting the ESP module with the CP2102 adapter, the adaptor must be connected to the PC and the ESP module must be loaded with the firmware code in the similar manner as is demonstrated with the Arduino.
After loading the code, the ESP module will automatically access the Wi-Fi point with the SSID given in the firmware. The DHT-11 sensor is interfaced with the ESP module.
Fig. 10: Pin Diagram of DHT-11 Temperature and Humidity Sensor
The DHT11 sensor consists of two main components – one is Humidity sensing component and other is NTC temperature sensor (or Thermistor). The Thermistor is actually a variable resistor that changes its resistance with change in temperature. They both sense the temperature and humidity of area and give the output to the IC (which is placed on back side of sensor). The sensor has four pins – VCC, Ground, data Out and NC. The VCC and Ground pins are connected to the common VCC and Ground respectively. The Data Out pin of the sensor is connected to GPIO2 pin of the ESP module via 10K pull-up resistor. The circuit connections of the ESP module with the DHT sensor are summarized in the following table –
Fig. 11: Table listing circuit connections between ESP8266 Modem and DHT-11 Sensor
On the PC side Adafruit itself act as Client. The PC client should be assigned with unique client ID so that MQTT broker can easily identify which client is publishing and subscribing the topic and payload. Learn more about configuring PC as MQTT Client from the following tutorial –
Learn about creating and subscribing topics on an MQTT broker and publishing messages on them from the following tutorial –
In the similar way, the topics can be created on Adafruit broker and the MQTT Clients can be made to subscribe and publish messages on those topics. The Adafruit broker is highly scalable and reliable broker. To access this broker, user needs to create an account. The user account is given a unique private access key which can be used only for individual purpose. This private key can be shared only with trusted clients so that they can also connect with the broker. It should not be shared with any un-trusted client otherwise the data can be accessed by others also.
After creating the account, the topics can be created. The topics are called Feeds in the Adafruit environment. So, go to feed and create new feed like temperature and humidity feed for this project. The feeds must be created and registered with the MQTT broker. After creating the feeds, go to dashboard and create new dashboard. Dashboard are only for viewing data. Inside the dashboard, create new blocks for monitoring temperature and humidity values. With this Dashboard, the temperature and humidity readings can be viewed on Browser on the PC client.
The following precautions must be taken care while assembling the circuit –
1) The ESP8266 needs 3.3V power to work, so do not provide 5V.
2) Some people say that ESP8266 does not have 5V tolerant Inputs but when Arduino was used as USB to Serial Adapter it successfully provided the serial communication. According to the datasheet of ESP8266, It has also written that the ESP8266 GPIOs can tolerate more power up to 6V.
3) If the error of espcomm_sync-failed.md is shown while uploading the firmware code, then first check the selected port on which the ESP module is connected and then check if the module is entering in bootloading mode or not. This can be checked by connecting RX and GND pin of converter to the TX and GND pin of ESP. Then open a terminal and check if the following message is received on serial window or not.
ets Jan 8 2017,rst cause:2, boot mode:(x,y).
4) Check if the module pins are getting proper voltage or not using multimeter.
5) If the module is working but it starts to burn after sometime then there can be an issue of current requirement. In that case, use an external power supply to provide 3.3V to ESP8266.
Fig. 12: Prototype of Adafruit Broker controlled ESP8266 based Temeperature and Humidity Monitor
How the circuit works –
After loading firmware and circuit connections, the ESP8266 module is ready to publish data over MQTT protocol from the available Wi-Fi access point. The PC client is also ready to receive temperature and humidity values. The ESP8266 and PC clients communicate and send messages to each other via the MQTT broker which is Adafruit. The Adafruit also serve as Client on the PC side. In the previous tutorial, MQTTLens add-on was installed on chrome browser to configure PC as MQTT Client. With Adafruit, there is no need to install any add-on. The Adafruit itself act as Client on the PC side.
The ESP8266 client configures as publisher for the topic “temp” and “humidity”. The PC client configures as subscriber for these topics. The ESP8266 Client reads the temperature and humidity values from the DHT-11 Sensor. The DHT11 Temperature and Humidity Sensor is a digital sensor with inbuilt capacitive humidity sensor and Thermistor. It relays a real-time temperature and humidity reading every 2 seconds. The sensor operates on 3.5 to 5.5 V supply and can read temperature between 0° C and 50° C and relative humidity between 20% and 95%. The DHT11 detects water vapors by measuring the electrical resistance between the two electrodes. The humidity sensing component is a moisture holding substrate with electrodes applied to the surface. When water vapors are absorbed by the substrate, ions are released by the substrate which increases the conductivity between the electrodes. The change in resistance between the two electrodes is proportional to the relative humidity. Higher relative humidity decreases the resistance between the electrodes, while lower relative humidity increases the resistance between the electrodes. The DHT11 measures temperature with a surface mounted NTC temperature sensor (Thermistor) built into the unit.
The DHT 11 Sensor sends data in the digital form to the ESP module on one-wire protocol which must be implemented on firmware side. First the data pin is configured to input and a start signal is sent to it. The start signal comprises of a LOW for 18 milliseconds followed by a HIGH for 20 to 40 microseconds followed by a LOW again for 80 microseconds and a HIGH for 80 microseconds. After sending the start signal, the pin is configured to digital output and 40-bit data comprising of the temperature and humidity reading is latched out. Of the 5-byte data, the first two bytes are integer and decimal part of reading for relative humidity respectively, third and fourth bytes are integer and decimal part of reading for temperature and last one is checksum byte. The one-wire protocol is implemented on the firmware using an open-source library available for Arduino.
The read temperature and humidity values are sent as messages to the “temp” and “humidity” feeds (topics) respectively. The PC client receives messages on the feeds and display them on the Dashboard using temperature and humidity blocks.
Programming Guide –
You may also like:
Project Source Code
### //Program to /* File Name- PC_to_ESP8266_communication_with_adafruit_broker.ino Output pin of ESP-01 GPIO2-DHT11 sensor */ //Include libraries #include//Include library for ESP8266 configuration #include "Adafruit_MQTT.h" //Include library for MQTT #include "Adafruit_MQTT_Client.h" #include "DHT.h" //Include library for Temperature and humidity sensor configuration // DHT11 or DHT22 pin and Type declaration #define DHTPIN 2 //DHT11 ouptput connected to ESP-01 GPIO02 #define DHTTYPE DHT11 //DHT TYPE-DHT 11 or 22 DHT dht(DHTPIN, DHTTYPE, 15); //Create the instance for DHT sensor // WiFi parameters #define WLAN_SSID "Replace it with the SSID of the Wi-Fi Network" //SSID of Home's WiFi network #define WLAN_PASS "Replace it with the password of the Wi-Fi Network" //Password of Home's WiFi network // Adafruit IO Broker Initialization #define AIO_SERVER "io.adafruit.com" //Broker domain Name #define AIO_SERVERPORT 1883 //The port at which broker is listening #define AIO_USERNAME "priya22" //The username which we have initializaed during account creation at io.adafruit.com #define AIO_KEY "acd65714ad7f4217a6b1c1c4815fcfa4" //The private ley which will be alloted after creating account //Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient espclient; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&espclient, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); //Create the topic to be registered on MQTT broker //Notice MQTT paths for AIO follow the form: /feeds/ Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp"); Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity"); //Function declaration void MQTT_connect(); void setup() { Serial.begin(115200); //Start the serial communication for debugging delay(10); dht.begin(); //DHT sensor initialization //Connect to WiFi access point. Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); // Wait until the connection has been confirmed before continuing while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { MQTT_connect(); float temperature_data = 32.0; //dht.readTemperature(); //Read the current temperature float humidity_data = 51.0; //dht.readHumidity(); //Read the current humidity // Now we can publish stuff! Serial.print(F("nSending temperature value ")); Serial.print(temperature_data); Serial.print("..."); //Publish the temperature to the Adafruit broker if (! temp.publish(temperature_data)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } Serial.print(F("nSending Humidity value ")); Serial.print(humidity_data); Serial.print("..."); //Publish the humidity to the Adafruit broker if (! humidity.publish(humidity_data)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } delay(10000); } /* Function Name- MQTT_connect() Function to connect and reconnect as necessary to the MQTT server. It should be called in the loop function and it will take care if connecting. */ void MQTT_connect() { int8_t ret; //Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); } ###
Project Video
Filed Under: IoT tutorials, Tutorials
Filed Under: IoT tutorials, Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.