- Out of the two boards, the ESP32 acts as the doorbell switch and the ESP8266 acts as the MQTT broker as well as the receiver unit for the doorbell.
- The ESP32 provides visual feedback by blinking an LED on the press of the doorbell button (connected to the board) and sends out the MQTT message, notifying the bell press.
- The ESP8266 runs the MQTT broker to circulate the MQTT message to subscribed clients, while also acting as the receiver unit for the doorbell, providing audio-visual feedback.
- All clients connected to the ESP8266 MQTT Broker will receive the message and alerts can be set up accordingly.
- Please ensure to modify the credentials on the respective codes
doorbell_receiver_mqttbroker [ESP8266]
-
Modify the SSID and Password
of your home Wi-Fi (or the Wi-Fi you want the connected devices on) -
Modify the MQTT UserID and MQTT Password
to the credentials you would like to use (all clients need to use these credentials to connect and communicate/ listen to messages) -
This code uses the sMQTTBroker library to convert an ESP8266 NodeMCU into an MQTT broker on a local network, connected by Wi-Fi to enable MQTT communications over the local network.
-
The ESP8266 will act as the MQTT broker, while also processing the incoming message for the doorbell press
-
Thus it acts as the doorbell's receiver unit, providing audio-visual feedback.
-
The audio is played using the ESP8266Audio libraries.
-
The audio data is directly fed into the doorbell.h header file as a hex code.
-
The audio file should be in WAV format, 16-bit PCM before converting the contents into Hex and pasting the contents into doorbell.h
doorbell_with_deep_sleep [ESP32]
-
This code uses the PubSubClient library to convert an ESP32 into an MQTT client that sends specific MQTT messages over the local network on the press of a button, which acts as the doorbell.
-
The ESP32 remains in deep sleep while not in use but comes online, connects to Wi-Fi & MQTT and sends the MQTT message to a broker on the local network using pre-defined credentials
-
Modify the SSID and Password
of your home Wi-Fi (or the Wi-Fi you want the connected devices on) -
Modify the MQTT UserID and MQTT Password
to whatever is set on the Broker code -
Modify the IP address to that of your MQTT Broker on the local network
The circuit includes an LED and a button. The ESP32 enters deep sleep when idle and wakes up when the button is pressed.
The circuit uses a transistor to amplify audio output for a speaker.
- Clone this repository to your local machine using Git:
Alternatively, you can click on the green Code button on the GitHub page and select Download ZIP. Then, extract the ZIP file to your computer.
git clone https://github.com/Ricky-001/MQTT_Doorbell.git
- Install the Arduino IDE if you don’t already have it.
- Open the
.ino
files for the ESP32 and ESP8266 codes:doorbell_with_deep_sleep.ino
for the ESP32 (doorbell switch).doorbell_receiver_mqttbroker.ino
for the ESP8266 (receiver and broker).
Before uploading the code to your boards, ensure the required libraries are installed. You can install these libraries via the Arduino Library Manager:
- Open Arduino IDE and navigate to
Sketch > Include Library > Manage Libraries
. - Search for and install the following libraries:
- For ESP32 (doorbell switch):
- PubSubClient: Used for MQTT communication.
- WiFi: Pre-installed with the ESP32 board package.
- For ESP8266 (MQTT Broker and receiver):
- sMQTTBroker: Used to set up the MQTT broker.
- ESP8266Audio: Used for audio playback.
- ESP8266WiFi: Pre-installed with the ESP8266 board package.
- For ESP32 (doorbell switch):
- Add ESP32 and ESP8266 Board URLs:
- Go to
File > Preferences
. - Add the following URLs under "Additional Boards Manager URLs":
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Go to
- Install Board Packages:
- Go to
Tools > Board > Boards Manager
. - Search for
ESP32
andESP8266
, then click Install.
- Go to
- Use a micro-USB cable to connect your ESP32 or ESP8266 to your computer. Ensure the cable supports data transfer (not just charging).
- In the Arduino IDE, navigate to
Tools > Board
and select:- ESP32 Dev Module for the ESP32.
- NodeMCU 1.0 (ESP-12E Module) for the ESP8266.
- Under
Tools > Port
, select the port to which your board is connected.
- Open the
.ino
file for the respective board (ESP32 or ESP8266). - Modify the following in the code:
- Wi-Fi credentials (SSID and password).
- MQTT Broker IP, username, and password (ensure they match across both devices).
- Click the Upload button in the Arduino IDE to flash the code to the board.
- Monitor the Serial Monitor (set to 115200 baud rate) for debugging and ensuring the board connects successfully to Wi-Fi and the MQTT broker.
-
Wi-Fi Connection Issues:
- Ensure your Wi-Fi SSID and password are entered correctly in both sketches.
- Check if the ESP32 and ESP8266 are within range of the Wi-Fi router.
- Ensure the Wi-Fi network operates on 2.4 GHz (not 5 GHz).
-
MQTT Communication Issues:
- Verify that the ESP8266 is running the MQTT broker successfully by subscribing to its IP using a client app (e.g., MQTT Explorer or MQTT.fx).
- Double-check the MQTT credentials in both the broker and the client code.
-
Audio Playback Issues on ESP8266:
- Ensure your speaker wiring matches the schematic.
- Verify that the audio file in
doorbell.h
is correctly converted to hex and is in the right format (16-bit PCM WAV).
-
Power Up the ESP8266:
- Once flashed, connect the ESP8266 to a 5V power source.
- It should start running the MQTT broker.
-
Power Up the ESP32:
- Press the button on the ESP32. The LED should blink, and an MQTT message should be sent.
-
Verify MQTT Message Reception:
- Use a mobile app like MQTT Alert or MQTT Explorer to subscribe to the MQTT broker on the ESP8266's IP address.
- Check if the doorbell message appears under the topic
home/doorbell
.
Apps like MQTT Alert can notify you on your smartphone when the doorbell is pressed:
- Install the app from the Play Store.
- Add a new connection with the following settings:
- Broker IP: IP address of the ESP8266.
- Port: 1883.
- Username/Password: As set in your ESP8266 MQTT Broker code.
- Topic:
home/doorbell
.
- Set up a notification tone to play when the message is received on the above topic.
- You should be good to go. You'll receive mobile notifications each time your doorbell is pressed!