The objective of this tutorial is to publish temperature and humidity to a RabbitMQ message broker.
- Prerequisites
- Run RabbitMQ container
- Create a publisher on your Raspberry
- Create a consumer on your Raspberry
- Start publisher as Service
Prerequisites
- Set up a Raspberry PI 3
- Interacting with DHT22 Sensor
- A server or your own computer with Docker
- Install Git
Run RabbitMQ container
docker run -d --hostname my-rabbit --name my-rabbit -p 5672:5672 -p 8080:15672 rabbitmq:3-management
see more here
You can see RabbitMQ management interface on port 8080.
Create a publisher on your Raspberry
- Clone raspberry-scripts project
git clone git@github.com:jluccisano/raspberry-scripts.git
- Go to
cd raspberry-scripts/scripts
- Edit the _config.yml
Replace by your config
vim _config.yml
rabbitmq:
url: amqp://guest:guest@RABBIT_DOCKER_HOST:5672
gatewayId: raspberry_1
sendFrom: RASPBERRY_IP_ADDRESS
exchange: events
publish_interval: 60
queue: event
logPath: /var/log/dht22
- Start virtualenv (optional)
virtualenv -p /usr/bin/python2.7 ~/env2.7/
source ~/env2.7/bin/activate
see more here
Install dependencies:
pip install pika
pip install adafruit_python_dht
pip install pyyaml
see more here
- Start publisher
python publisher.py &
Show log:
tail -f /var/log/dht22/publisher.log
Note: code based on Asynchronous publisher example
Create a consumer on your Raspberry
- Start publisher
python consumer.py &
Show log:
tail -f /var/log/dht22/consumer.log
code based on Asynchronous consumer example
Start publisher as Service
Follow this tutorial
Edit publisher.py and change config.yml to /opt/dht22/_config.yml
- Start
sudo systemctl start dht22_publisher.service
- Check
sudo systemctl status dht22_publisher.service
(env2.7) pi@raspberrypi:/opt/dht22 $ sudo systemctl status dht22.service
● dht22.service - DHT22
Loaded: loaded (/lib/systemd/system/dht22.service; enabled)
Active: active (running) since Fri 2017-03-24 13:21:46 UTC; 1s ago
Main PID: 14457 (python)
CGroup: /system.slice/dht22.service
└─14457 /usr/bin/python /opt/dht22/publisher.py
Show log:
tail -f /var/log/dht22/publisher.log
Great publisher works fine !!
2017-03-24 13:21:48,032 Start publishing
2017-03-24 13:21:48,033 Scheduling next message for 60.0 seconds
2017-03-24 13:22:47,668 Received 0 heartbeat frames, sent 0
2017-03-24 13:22:47,669 Sending heartbeat frame
2017-03-24 13:22:47,784 Received heartbeat frame
2017-03-24 13:22:48,701 [] Published data {'expire': 38231000, 'data': {'humidity': 34.20000076293945, '@type': 'DHT22', 'temperature': 24.399999618530273}, 'created': 1490361768} # 1
2017-03-24 13:22:48,701 Scheduling next message for 60.0 seconds