Skip to content

Collect temperature readings from a Raspberry Pi and push them to MQTT topic.

Notifications You must be signed in to change notification settings

jtyberg/pi-therm-sensor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

pi-therm-sensor

Sample code for reading temperatures from DS18B20 sensors connected to a Raspberry Pi, and sending them to MQTT gateways in the cloud.

Raspberry Pi hardware setup

See the AdaFruit tutorial for useful instructions on connecting the DS18B20 sensors to the Pi.

MQTT setup

This sample code can publish temperatures readings to both the AWS IoT and IBM Watson IoT platforms using the MQTT protocol.

AWS IoT

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.

IBM Watson IoT

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

Run the code

  1. Copy the AWS certificate files and the root CA certificate file to the Pi.

  2. Download this code to the Pi.

cd /home/pi

git clone https://github.com/jtyberg/pi-therm-sensor.git
  1. Modify the certificate and key file paths, and the MQTT endpoints in config.py.

  2. 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
  1. 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 -

About

Collect temperature readings from a Raspberry Pi and push them to MQTT topic.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages