Connecting a Raspberry Pi to the Cloud

This is an easy to make demo project that shows how to connect a simple sensor attached to a Raspberry Pi to the relayr cloud. It uses a digital temperature sensor, the very popular DS18B20, and two simple Python scripts - one to send the sensor data to the cloud, and the other to receive it for use in some other application.

The entire sample code is provided for you to get going in a GitHub repository.


Connect a sensor to the Raspberry Pi

First you need to connect the sensor to your Raspberry Pi. You can find a detailed description on how to connect the sensor (including a 4.7 K-Ohm resistor) on the Renewable Energy Website in the UK. It describes how to connect the sensor (using the 1wire bus) with three GPIO pins (power, ground and the actual data pin).


After that you must make sure that the 1wire communication device kernel module is loaded. The procedure for doing that is slightly different between Raspberry Pi versions before and after January 2015 when kernel 3.18.8 was included in Raspbian, the most widely used Linux distribution for the Raspberry Pi. In the recent updates you have to modify the file /boot/config.txt as described here:


# with a pre-3.18.8 kernel:
pi@raspberrypi ~ $ sudo modprobe w1-gpio && sudo modprobe w1_therm

# else:
pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux
pi@raspberrypi $ sudo nano /boot/config.txt
# add this line at the bottom (and then reboot):
# dtoverlay=w1-gpio


Now you can look for respective 1wire devices in your file system. Each DS18B20 sensor has a unique ID that appears in this devices directory, in our case 28-000004a365ef. Finding this unique ID is covered on the Renewable Energy Website link for connecting the sensor. The next section shows how to read the sensor data so that it can be published to the cloud.


Read the sensor data

Once you know the unique ID of your DS18B20, you can access the sensor data in a more reusable way

with a Python function like the one in


Running this code will also run a short loop to read and display the ambient temperature around the sensor. Try touching the sensor with your fingers to see how it affects the readings.


Now that the sensor is working and delivers data it's time to push that data into the relayr cloud as shown in the next section.


Create a device prototype in the relayr dashboard

If you don't have a relayr developer account, you'll need to create one. Once you have an account, you can create a sensor prototype simply by going to your relayr devices page and moving your mouse pointer on the big plus button in the lower right corner.


Then, scroll down and select “Continue without a model” to create the device. Now, switch the programming language to “Python” and copy the generated firmware code, which will be useful for the next section.


Publish your sensor data into the relayr cloud

You can publish your data using MQTT (a protocol for communicating messages from machine to machine). If this isn't isn't installed yet, you will have to get it set up on your Pi. The paho-mqtt package provides MQTT support for Python and can be easily installed as a Python package with pip like this (install pip first if you don't have it, yet):


pi@raspberrypi ~ $ sudo apt-get install python-pip
pi@raspberrypi ~ $ sudo pip install paho-mqtt==1.1


You'll know if you've successfully installed it if you can run this statement in Python without any error: import paho.


You can then copy the sample Python snippet from the dashboard prototype page that you've seen when creating a prototype. To do this, replace the while loop at the bottom of the code with the one in (available in the repository). Don’t forget to include the read_temperature function from above and also to add your unique sensor ID to the while loop (the one you found when setting up the sensor). Alternatively, you can use the code in, making sure to change the credentials (with the ones from your dashboard) and the device_id at the bottom of the page.


This will allow you to run an endless loop, reading temperature values and publishing them one per second to the relayr cloud.


Watch your sensor data on the relayr dashboard

As you push your data into the relayr cloud you can see the live values as they change in the relayr dashboard.





Fetch your sensor data from the relayr cloud

Watching your data in the dashboard as it changes is great, but at some point you'll want to extract the data to it to use it. For that purpose you can access your data via MQTT again by writing a simple script like the one named in the GitHub repository. If you run that script, it will show the live MQTT messages containing the data values as received.


To do this, use the relayr Python SDK by first installing the necessary packages (run the lines below on your pi):


sudo pip install git+­sdk

sudo pip install relayr ­­upgrade


If you are on a fresh Raspberry Pi, make sure you update its Debian package list and install some developer packages before installing the newest relayr package from GitHub like this:


pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install python-dev libffi-dev libssl-dev
pi@raspberrypi ~ $ pip install git+

You can now use the code in to receive data from the cloud. Make sure to change your device ID and access token (omitting the “Bearer” part of the token).



In this quick demo project, you've seen how you can connect a simple temperature sensor to a Raspberry Pi and publish its data in the relayr cloud. This allows you to view data live on the dashboard, or to export it for use in an application. You can use MQTT to publish and receive the sensor data, or use one of the relayr SDKs, like the Python SDK, to access the data more conveniently.


You can also use more exciting sensors and publish data values more complex than a single float (i.e. a list of three floats representing some geospatial information). Whenever you provide a reading known to the relayr dashboard it will show your data in a nice widget. And you can also publish something even more involved, like an object with deeper nesting levels. In that case the dashboard will show a generic widget. It's up to you and what you want to make!

This blog was updated in July 2016.