ESP32 Tutorial 48/55 - Remote Temperature Monitoring and LED control MQTT | SunFounder's ESP32 IoT kit
In this tutorial, we will explore how to utilize the ESP32 module in conjunction with SunFounder's ESP32 extension board to monitor temperature and humidity remotely using the Adafruit IO platform. Additionally, we'll implement the functionality to control an LED via a web interface. By the end of this project, you'll be able to view real-time temperature and humidity data and toggle an LED on and off from your browser.
This project leverages the MQTT protocol for efficient communication between the ESP32 and the Adafruit IO service. MQTT is lightweight and well-suited for IoT applications, allowing us to easily publish sensor data and subscribe to commands for the LED control. For further clarification of the code and wiring, be sure to check the video accompanying this tutorial (in video at 00:00).
Hardware Explained
The main components for this project include the ESP32 microcontroller, a DHT11 temperature and humidity sensor, and an LED. The ESP32 is a powerful microcontroller that features built-in Wi-Fi and Bluetooth capabilities, making it an ideal choice for IoT projects. It can handle multiple tasks and connect to the internet seamlessly.
The DHT11 sensor is responsible for measuring temperature and humidity. It outputs digital signals that can be read by the ESP32. The LED serves as an indicator and can be controlled remotely to demonstrate the effectiveness of the MQTT protocol in managing devices over the internet.
Datasheet Details
| Manufacturer | Adafruit |
|---|---|
| Part number | DHT11 |
| Logic/IO voltage | 3.3 V |
| Supply voltage | 3.3 V |
| Output current (per channel) | 20 mA |
| Peak current (per channel) | 50 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3 V (low), 0.7 V (high) |
| Voltage drop / RDS(on) / saturation | N/A |
| Thermal limits | 0 to 50 °C |
| Package | 3-pin |
| Notes / variants | Use DHT22 for higher accuracy. |
- Ensure correct wiring to prevent damage.
- Use a 220 Ohm resistor with the LED for current limiting.
- Use pull-up resistors for the DHT11 data pin if necessary.
- Check Wi-Fi credentials for case sensitivity.
- Monitor serial output for debugging connection issues.
- Keep MQTT topics unique to avoid conflicts.
- Test sensor readings to ensure they are valid.
- Be cautious of the DHT11's response time; it may take time to stabilize readings.
LED = control LED; temperature = publish temperature data; humidity = publish humidity data.
Wiring Instructions
To wire the components, start by connecting the DHT11 sensor. Connect the left pin of the DHT11 to the 3.3V power rail on the breadboard using a red wire. The middle pin of the DHT11 should be connected to pin 13 on the ESP32 using a yellow wire. Finally, connect the right pin of the DHT11 to the ground rail using a blue wire.
Next, for the LED, connect the anode (longer leg) to pin 15 of the ESP32 through a 220 Ohm resistor. Connect the cathode (shorter leg) directly to the ground rail on the breadboard. Make sure all connections are secure and double-check for any loose wires.
Setting up the Dashboard
-
Visit Adafruit IO, then click on Start for free to create a free account.

-
Fill out the form to create an account.

-
After creating an Adafruit account, you’ll need to reopen Adafruit io. Click on the Dashboards, then click on New Dashboard.

-
Create a New Dashboard.

-
Enter the newly created Dashboard and create a new block.

-
Create 1 Toggle block.

-
Next, you’ll need to create a new feed here. This toggle will be used to control the LED, and we’ll name this feed “LED”.

-
Check the LED feed, then move to the next step.

-
Complete the block settings (mainly Block Title, On Text, and Off Text), then click on the Create block button at the bottom right to finish.

-
We also need to create two Text Blocks next. They will be used to display temperature and humidity. So, create two feeds named temperature and humidity.

-
After creation, your Dashboard should look something like this:

-
You can adjust the layout by using the Edit Layout option on the Dashboard.

-
Click on API KEY, and you will see your username and API KEY displayed. Note these down as you’ll need them for your code.

Code Examples & Walkthrough
The code begins by including the necessary libraries, setting up the Wi-Fi credentials, and defining the MQTT parameters. Key identifiers such as AIO_USERNAME and AIO_KEY are used to authenticate with the Adafruit IO service.
#define AIO_USERNAME "YourUsername"
#define AIO_KEY "YourKey"
These lines define your Adafruit IO username and key, which are essential for connecting to the MQTT broker. Ensure these values are accurate to establish a successful connection.
In the setup() function, the Wi-Fi connection is initialized, and the MQTT client is set up with the root CA certificate for secure communication.
WiFi.begin(WLAN_SSID, WLAN_PASS);
client.setCACert(adafruitio_root_ca);
This code connects the ESP32 to the specified Wi-Fi network and sets the root CA for secure MQTT connections. Properly handling these connections is critical for reliable data transmission.
Finally, the loop() function manages the MQTT connection and publishes temperature and humidity readings at regular intervals.
mqtt.processPackets(5000);
This line allows the ESP32 to process incoming messages for subscribed topics, ensuring that the device remains responsive to commands sent from the web interface.
For the complete code, please refer to the full program loaded below the article.
Demonstration / What to Expect
Upon successful setup, you should see real-time updates of temperature and humidity on your Adafruit IO dashboard. You can also toggle the LED on and off through the web interface. If the LED does not respond as expected, check your wiring and ensure that the MQTT topic names match those defined in the code.
Be aware that certain MQTT connection errors may occur due to expired certificates. Ensure you have the latest root CA certificate in your code to avoid these issues (in video at 15:30).
Video Timestamps
- 00:00 Start
- 1:50 Introduction to project
- 3:16 What is MQTT
- 6:36 Adafruit IO setup
- 11:13 wiring
- 13:38 Arduino code explained
- 22:03 Selecting ESP32 board and COM port
- 23:44 Project demonstration
- 27:05 Updating dashboard
/*
* Adafruit MQTT 库 ESP32 Adafruit IO SSL/TLS 示例
*
* 使用最新版本的 ESP32 Arduino 核心:
* https://github.com/espressif/arduino-esp32
*
* 与 Adafruit Huzzah32 Feather 和 Breakout Board 的兼容性很好:
* https://www.adafruit.com/product/3405
* https://www.adafruit.com/products/4172
*
* Adafruit 投入时间和资源提供这个开源代码,
* 请通过购买 Adafruit 的产品来支持 Adafruit 和开源硬件!
*
* 由 Tony DiCola 为 Adafruit Industries 编写。
* 由 Brent Rubell 为 Adafruit Industries 修改
* MIT 许可证,以上所有文本必须包含在任何再分发中
* /
*/
#include <WiFi.h>
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/*
* WiFi接入点 *********************************/
*
* #define WLAN_SSID "SSID"
* #define WLAN_PASS "PASSWORD"
*
* /************************* Adafruit.io设置
*/
#define AIO_SERVER "io.adafruit.com"
// 使用端口8883进行MQTTS
#define AIO_SERVERPORT 8883
// Adafruit IO 账户配置
// (要获取这些值,请访问 https://io.adafruit.com 并点击“Active Key”)
// #define AIO_USERNAME "您的_ADAFRUIT_IO_用户名"
// #define AIO_KEY "您的 Adafruit IO 密钥"
#define AIO_USERNAME "JimmyKernel"
#define AIO_KEY "aio_Mxiv12i74b5jKfKNSKLYjlhPmoHC"
/*
* 全球状态(您不需要更改此项!)******************/
*
* // WiFiFlientSecure用于SSL/TLS支持
* WiFiClientSecure client;
*
* // 通过传入WiFi客户端和MQTT服务器及登录详情设置MQTT客户端类。
* Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
*
* // io.adafruit.com 根CA
* const char* adafruitio_root_ca = \
* "-----BEGIN CERTIFICATE-----\n"
* "MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n"
* "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
* "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\n"
* "MjAeFw0xNzExMDIxMjIzMzdaFw0yNzExMDIxMjIzMzdaMGAxCzAJBgNVBAYTAlVT\n"
* "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
* "b20xHzAdBgNVBAMTFkdlb1RydXN0IFRMUyBSU0EgQ0EgRzEwggEiMA0GCSqGSIb3\n"
* "DQEBAQUAA4IBDwAwggEKAoIBAQC+F+jsvikKy/65LWEx/TMkCDIuWegh1Ngwvm4Q\n"
* "yISgP7oU5d79eoySG3vOhC3w/3jEMuipoH1fBtp7m0tTpsYbAhch4XA7rfuD6whU\n"
* "gajeErLVxoiWMPkC/DnUvbgi74BJmdBiuGHQSd7LwsuXpTEGG9fYXcbTVN5SATYq\n"
* "DfbexbYxTMwVJWoVb6lrBEgM3gBBqiiAiy800xu1Nq07JdCIQkBsNpFtZbIZhsDS\n"
* "fzlGWP4wEmBQ3O67c+ZXkFr2DcrXBEtHam80Gp2SNhou2U5U7UesDL/xgLK6/0d7\n"
* "6TnEVMSUVJkZ8VeZr+IUIlvoLrtjLbqugb0T3OYXW+CQU0kBAgMBAAGjggFAMIIB\n"
* "PDAdBgNVHQ4EFgQUlE/UXYvkpOKmgP792PkA76O+AlcwHwYDVR0jBBgwFoAUTiJU\n"
* "IBiV5uNu5g/6+rkS7QYXjzkwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsG\n"
* "AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB\n"
* "BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n"
* "HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n"
* "b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
* "HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
* "AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
* "c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
* "ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
* "jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
* "TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
* "7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
* "-----END CERTIFICATE-----\n";
*
* /****************************** 订阅 روانات
*/
// 设置一个名为'test'的发布订阅源和一个名为'test2'的订阅源。
// 注意,AIO的MQTT路径遵循以下形式:<username>/feeds/<feedname>
Adafruit_MQTT_Subscribe LED = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature");
/*
* Adafruit MQTT 基础课程 1
* 在 YouTube 上观看此代码的视频指导 https://youtu.be/M9BQweAsHJM
* 本课程和代码的资源页面:
* 📚⬇️ 下载和资源页面 https://robojax.com/RJT670
* 教程由 https://youTube.com/@robojax 提供
*
* Adafruit MQTT 库 ESP32 Adafruit IO SSL/TLS 示例
*
* 使用最新版本的 ESP32 Arduino 核心:
* https://github.com/espressif/arduino-esp32
*
* 与 Adafruit Huzzah32 Feather 和 Breakout Board 完美兼容:
* https://www.adafruit.com/product/3405
* https://www.adafruit.com/products/4172
*
* Adafruit 投入时间和资源提供此开源代码,
* 请通过购买 Adafruit 的产品来支持 Adafruit 和开源硬件!
*
* 由 Tony DiCola 为 Adafruit Industries 编写。
* 由 Brent Rubell 为 Adafruit Industries 修改
* MIT 许可证,以上所有文本必须包含在任何再分发中
* /
*
* /// 参考: https://www.electronicwings.com/esp32/esp32-mqtt-client
*/
#include <WiFi.h>
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/*
* WiFi接入点 *********************************/
*
* #define WLAN_SSID "dars"
* #define WLAN_PASS "5152535455"
*
* /************************* Adafruit.io 设置
*/
#define AIO_SERVER "io.adafruit.com"
// 使用端口8883进行MQTTS
#define AIO_SERVERPORT 8883
// Adafruit IO 账户配置
// (要获取这些值,请访问 https://io.adafruit.com 并点击“激活密钥”)
// #define AIO_USERNAME "您的Adafruit IO用户名"
// #define AIO_KEY "您的 Adafruit IO 密钥"
#define AIO_USERNAME "robojax"
#define AIO_KEY "aio_wCyL33EVKLcGoAotGNEQ4dGQYOLZ"
/*
* 全球状态(您不需要更改此内容!)******************/
*
* //支持SSL/TLS的WiFiFlientSecure
* WiFiClientSecure client;
*
* //通过传入WiFi客户端和MQTT服务器及登录详细信息来设置MQTT客户端类。
* Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
*
* // io.adafruit.com 根CA
* const char* adafruitio_root_ca =
* "-----BEGIN CERTIFICATE-----\n"
* "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n"
* "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
* "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n"
* "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n"
* "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
* "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n"
* "9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n"
* "CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n"
* "nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n"
* "43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n"
* "T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n"
* "gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n"
* "BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n"
* "TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n"
* "DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n"
* "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n"
* "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n"
* "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n"
* "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n"
* "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n"
* "-----END CERTIFICATE-----\n";
*/
// 设置一个名为“test”的发布Feed和一个名为“test2”的订阅Feed。
// 注意AIO的MQTT路径遵循以下格式:<username>/feeds/<feedname>
Adafruit_MQTT_Subscribe LED = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED");
// Adafruit_MQTT_Publish 湿度 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");
// Adafruit_MQTT_Publish 温度 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature");
Common Course Links
Common Course Files
资源与参考
-
文档ESP32 教程 48/55 - SunFounder 文档页面:使用 Adafruit IO 进行温湿度监控docs.sunfounder.com
文件📁
没有可用的文件。