A demo app that integrates an HCSR04 proximity sensor on a Raspberry PI with an MQTT publisher
The following items will need to be purchased to assemble this kit:
- Raspberry Pi 2,3 or 4 and necessary accessories (SD Card, Powersource, Keyboard, Mouse etc)
- HC-SR04 Sensor
- 400 Points Breadboard
- Female to Male Jumper wires
- Male to Male Jumper Wires
- 330 Ω Resistor
- 470 Ω Resistor
- Install NodeJS on your Raspberry PI
- Solace Cloud Account or access to a Solace PubSub+ Broker
Use the following diagram to connect the sensor to the Raspberry PI
(Note the diagram is based on a Raspberry Pi 2 - take note of the pin positions and map it to the Raspberry PI that you have purchased)
Clone the repo to your pi and install dependencies using the following commands:
git clone https://github.com/solacese/raspberry-pi-ultrasonic-sensor-mqtt.git
cd raspberry-pi-ultrasonic-sensor-mqtt
npm i
do the following:
- Rename .EDIT-ME.env to .env
- Fill in the Solace connectivity in the .env file with details from Solace Cloud Console's Connection tab
- Specify the range (in centimeters) that you would want to have an event be published onto Solace in the .env file as well
Use the following command to run the application,
sudo npm run start
If successful, you should see the following output:
=== Starting MQTT producer ===
Connecting MQTT client to Solace...
MQTT client connected to Solace.
Connecting to board...
1583858715516 Available Pi-IO
1583858715614 Connected Pi-IO
1583858715619 Repl Initialized
>> Connected to the board!
Enabling range of proxmity dection to be [10,50]cms
Distance measurement: {"cm":18.299,"centimeters":18.299,"in":7.14,"inches":7.14}
In addition, the distance will be published on the topic SOLACE/DISTANCE/MEASUREMENT
for applications to subscribe to.