ESP32 Tutorial 48/55 - Remote Temperature Monitoring and LED control MQTT | SunFounder's ESP32 IoT kit
SunFounder Documentation page ESP32 Learning Kit
Purchase SunFounder ESP32 Learning Kit
In this video we learn how to monitor temperature and humidity using ESP32 and turn ON/OFF LED over Adafruit IO.
Topics in this lesson
Use Chapters from timeline or click on the time
- 00:00 Introduction
- 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 Basic lesson 1 Watch video instruction for this code on YouTube https://youtu.be/M9BQweAsHJM Resource page for this lesson and code: http://robojax.com/course3 Tutoril by https://youTube.com/@robojax Adafruit MQTT Library ESP32 Adafruit IO SSL/TLS example Use the latest version of the ESP32 Arduino Core: https://github.com/espressif/arduino-esp32 Works great with Adafruit Huzzah32 Feather and Breakout Board: https://www.adafruit.com/product/3405 https://www.adafruit.com/products/4172 Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Tony DiCola for Adafruit Industries. Modified by Brent Rubell for Adafruit Industries MIT license, all text above must be included in any redistribution **********************************************************************/ /// ref: https://www.electronicwings.com/esp32/esp32-mqtt-client #include#include "WiFiClientSecure.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" /************************* WiFi Access Point *********************************/ #define WLAN_SSID "dars" #define WLAN_PASS "5152535455" /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "io.adafruit.com" // Using port 8883 for MQTTS #define AIO_SERVERPORT 8883 // Adafruit IO Account Configuration // (to obtain these values, visit https://io.adafruit.com and click on Active Key) // #define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME" // #define AIO_KEY "YOUR_ADAFRUIT_IO_KEY" #define AIO_USERNAME "robojax" #define AIO_KEY "aio_wCyL33EVKLcGoAotGNEQ4dGQYOLZ" /************ Global State (you don't need to change this!) ******************/ // WiFiFlientSecure for SSL/TLS support WiFiClientSecure client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); // io.adafruit.com root 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"; /****************************** Feeds ***************************************/ // Setup a feed called 'test' for publishing and 'test2' for subscription. // Notice MQTT paths for AIO follow the form: /feeds/ 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"); /*************************** Sketch Code ************************************/ // set pin numbers const int ledPin = 15; // the number of the led pin void setup() { Serial.begin(115200); delay(10); Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example")); // Connect to WiFi access point. Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); delay(1000); WiFi.begin(WLAN_SSID, WLAN_PASS); delay(2000); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Set Adafruit IO's root CA client.setCACert(adafruitio_root_ca); // register callback for feed LED.setCallback(ledCallback); // Setup MQTT subscription for time feed. mqtt.subscribe(&LED); // initialize the LED pin as an output pinMode(ledPin, OUTPUT); } // uint32_t x=0; void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); // indicatorPublish(); // wait 10 seconds for subscription messages mqtt.processPackets(10000); // wait a couple seconds to avoid rate limit //delay(2000); } void ledCallback(char* message, uint16_t len) { char messageBuffer[40]; snprintf(messageBuffer, sizeof(messageBuffer), "LED status is :: %s, len :: %u", message, len); Serial.println(messageBuffer); if (strcmp(message, "ON") == 0) { Serial.println("Turning ON LED"); digitalWrite(ledPin, LOW); } else { Serial.println("Turning OFF LED"); digitalWrite(ledPin, HIGH); } } // Function to connect and reconnect as necessary to the MQTT server. // 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!"); }