This project demonstrates how to use an ESP32 microcontroller to connect to a Wi-Fi network and communicate with an MQTT broker. It utilizes the WiFi and PubSubClient libraries to manage Wi-Fi connections and MQTT messaging.
- Connects to a Wi-Fi network using provided credentials.
- Connects to an MQTT broker.
- Subscribes to an MQTT topic and listens for incoming messages.
- Publishes a message to an MQTT topic upon connection.
Before running the code, ensure you have the following:
- An ESP32 microcontroller.
- The Arduino IDE installed with ESP32 support.
- The
WiFiandPubSubClientlibraries installed in the Arduino IDE.
-
Set Up the Arduino IDE for ESP32:
- Follow the instructions on the ESP32 Arduino GitHub page to install the necessary board definitions.
-
Install Required Libraries:
- Open the Arduino IDE and navigate to Sketch > Include Library > Manage Libraries.
- Search for and install the
PubSubClientlibrary.
-
Configure the Code:
- Open the provided code in the Arduino IDE.
- Update the Wi-Fi credentials:
const char* ssid = "your_wifi_ssid"; const char* password = "your_wifi_password";
- Update the MQTT broker credentials:
IPAddress server(192, 168, 178, 30); // Replace with your broker's IP address
-
Upload the Code:
- Connect your ESP32 to your computer.
- Select the correct board and port in the Arduino IDE.
- Upload the code to the ESP32.
WiFi.h: Manages the Wi-Fi connection.PubSubClient.h: Manages MQTT communication.
LED: GPIO 2 is used for an LED indicator.
callback(): Handles incoming MQTT messages.startWifi(): Connects the ESP32 to the Wi-Fi network.connectBroker(): Connects the ESP32 to the MQTT broker and subscribes to a topic.
- Publishing: The ESP32 publishes a message to the
outTopictopic upon connection. - Subscribing: The ESP32 subscribes to the
testtopic and processes incoming messages via thecallback()function.
- Start: After uploading the code, the ESP32 will connect to the specified Wi-Fi network.
- Connect: It will then connect to the MQTT broker and publish a message to the
outTopic. - Subscribe: The ESP32 will subscribe to the
testtopic and print incoming messages to the Serial Monitor.
- No Wi-Fi Connection: Ensure that the Wi-Fi credentials are correct and that the ESP32 is within range of the network.
- MQTT Connection Issues: Verify that the MQTT broker's IP address and credentials are correct and that the broker is reachable.
This project is licensed under the MIT License - see the LICENSE file for details.