The project demonstrates the use of MQTT, where a publisher (ESP8266) is physically connected to an ultrasound sensor, while the subscriber (ESP8266) has an on-board LED light. The Raspberry Pi 4 acts as the MQTT broker, facilitating wireless communication between the publisher and subscriber.
The ultrasonic sensor measures the distance to an object, and based on the measured distance, it publishes messages to the MQTT broker (Raspberry Pi) using the topic "esp/distance". The subscriber ESP8266 board subscribes to the "esp/distance" topic and receives these distance messages. If the distance received is less than 20cm, the subscriber ESP8266 turns on its on-board LED. If the distance is greater than or equal to 20cm, the LED turns off.
The project can be applied to a range of use cases, such as security systems (indicating intrusion), object detection/avoidance, proximity sensing for machinery and environmental monitoring. Note, it is important to consider the limitations of components when implementing the project to your specific use case.
- 1x Raspberry Pi 4
- 2x ESP8266 (NodeMCU)
- 1x Ultrasonic Sensor (HC-SR04)
- 1x Breadboard
- 4x jumper wires for connection
- Connect ultrasonic sensor to publisher ESP8266 board
- Connect LED to subscriber ESP8266 board, if on-board light unavailable
- Set up Rasbperry Pi as broker, such as flashing Pi with Ubuntu Server and configure an MQTT broker software, I used Eclipse Mosquitto
- Write and upload code to the respective ESP8266 boards, I used the Arduino IDE
- Ensure that credentials for the Raspberry Pi (such as IP address, username, password etc.) is configured, and is updated in code written
- Turn on the Raspberry Pi and ESP8266 boards
- The publisher ESP8266 board reads the distance from the ultrasonic sensor and publishes the distance value to the "esp/distance" topic on the MQTT broker (Raspberry Pi).
- The subscriber ESP8266 board subscribes to the "esp/distance" topic and receives the distance messages.
- If the distance received is less than 20cm, the subscriber ESP8266 turns on the LED. If the distance is greater than or equal to 20cm, the LED turns off.
Ensure the MQTT broker (Raspberry Pi) is running and accessible to both publisher and subscriber ESP8266 boards for successful communication.