Sample code for reading temperatures from DS18B20 sensors connected to a Raspberry Pi, and sending them to MQTT gateways in the cloud.
See the AdaFruit tutorial for useful instructions on connecting the DS18B20 sensors to the Pi.
This sample code can publish temperatures readings to both the AWS IoT and IBM Watson IoT platforms using the MQTT protocol.
This sample can publish temperature readings to the AWS IoT device gateway using the MQTT protocol. The Pi must be a registered thing on AWS IoT, it must be authorized to connect to AWS, and it must be allowed to publish messages to the AWS IoT device gateway. In short:
- create a thing on AWS IoT as a virtual representation of the Pi
- create a security certificate on AWS IoT, download the cert, private key and public key files, and copy them to the Pi
- attach the Pi thing to the certificate
- create a policy that allows publishing to any topic
- attach the policy to the certificate
This guide on getting started with AWS IoT and Raspberry Pi is useful.
This sample can publish temperature readings to the IBM Watson IoT Platform. The Pi must be a registered device on Watson IoT. In short, you must:
- register on IBM Bluemix
- create an Internet of Things Platform service, and obtain an organization ID
- create a Raspberry Pi device type
- register the Raspberry Pi as a device
- use the organization ID, device type, device ID, and device token to publish messages to the MQTT gateway
-
Copy the AWS certificate files and the root CA certificate file to the Pi.
-
Download this code to the Pi.
cd /home/pi
git clone https://github.com/jtyberg/pi-therm-sensor.git
-
Modify the certificate and key file paths, and the MQTT endpoints in
config.py
. -
Install Python package dependencies on the Pi. This sample uses the W1ThermSensor package to read from the sensors connected to the Pi, and the paho-mqtt package to send the data to AWS IoT endpoints.
pip3 install w1thermsensor paho-mqtt
- Run it
cd /home/pi/pi-therm-sensor
# just print the temperature readings to the console
python3 therm.py
# print the temperatures to the console and publish them to MQTT endpoints
python3 therm.py --publish aws --publish watson
To setup a cron job to read and publish temperatures every 15 minutes:
crontab -l | { cat; echo "0,15,30,45 * * * * /home/pi/pi-therm-sensor/therm.py --publish aws --publish watson"; } | crontab -