Connecting Your Raspberry Pi to IoT Connect

Homepage Connecting Your Raspberry Pi to IoT Connect

In this tutorial, I’ll show you how to connect a Raspberry Pi device to the Avnet IoT Connect Platform and how to use that platform to control the state of the Raspberry Pi remotely in order to turn off and on an LED.

Getting Started

Before starting this tutorial, you may want to review my previous tutorial on connecting your Raspberry Pi to AWS IoT. In that tutorial, I cover the setup process for your Raspberry Pi in a lot more detail and I provide you with a bunch of links to kits and hardware for you to get started.

Hardware Configuration

I’m going to assume that you already have the following:

  • A Raspberry Pi 4 with Raspberry Pi OS Installed on an SD card
  • A USB mouse/keyboard connected to your Raspberry Pi
  • A Micro HDMI cable from your monitor connected to the Raspberry Pi
  • Your power cable already connected

I’ll also be assuming that you have the same hardware and pin configuration that I describe in the previous AWS IoT Raspberry Pi post. Essentially, that you’ve got an LED and resistor all connected to GPIO 17 so that when you turn on GPIO 17 the LED comes on.

If you haven’t done this already then check out that previous post as I go through the entire setup process and link you to hardware options and a more detailed guide for the breadboard configuration.

Previous tutorial - connecting your Raspberry Pi to AWS IoT

Software and Account Setup

In addition to the hardware setup above, you’ll also need:

  • An IoT Connect Account –
  • An internet connection set up on your Raspberry Pi
  • Python 3 installed on your Raspberry Pi, this should come installed but to check if it is you can run:  python 3 --version
  • Git installed on your machine, this should also come installed but to check, run: git --version
  • The GPIO Python library, this should come installed but to test it, you can open the Python interpreter from the terminal and run: import RPi.GPIO as GPIO
  • If you don’t see any errors, then this should have worked as expected!

Confirming Hardware Setup

Now that you have a Raspberry Pi device setup and an LED connected to it let’s just make sure it’s working for the purposes of this demo. To do this, we’ll open the terminal up and enter into the Python 3 interpreter with:


From here, we’ll run a few GPIO commands to test that we can turn the LED off and on. Before proceeding, make sure that you have set up your hardware correctly as discussed in the guide linked above so that you don’t inadvertently damage the Raspberry Pi.

After that, we can run the following commands (without the >>> as part of them, that’s just to remind you we’re entering these in the Python interpreter):
>>> import RPi.GPIO as GPIO
>>> ledPin = 11
>>> GPIO.setmode(GPIO.BOARD)
>>> GPIO.setup(ledPin, GPIO.OUT)
>>> GPIO.output(ledPin, GPIO.LOW)
>>> GPIO.output(ledPin, GPIO.HIGH)

At this point, you should see the LED light on your breadboard turn on. If you aren’t seeing the LED light up, then you’ll need to double-check you have your breadboard and LED pin configuration. Make sure you turn off the device before you change anything.

You can turn the GPIO back off with:
>>> GPIO.output(ledPin, GPIO.LOW)

Creating our Device in IoT Connect

Our next step is to use the IoT Connect platform to register this device. Sign into the IoT Connect platform and then navigate to the “Device” section and click on “Templates”:

Even if we’re connecting just one IoT device, we must use this template to provide some basic context about the device template before we setup the details of the device itself.

In this section, click “Create Template”:

We’ll be using this section to create the template for our Raspberry Pi device. We’ll give this template a Template Code and Template Name of “raspi”. After that, we’ll have to select the authentication mechanism that we want to use with this device type:

There are a few different options for us to choose from:

  1. Key
  2. Self Signed Certificate
  3. CA Signed Certificate
  4. TPM

Each of these options is applicable to different use cases.

Key – With the Key option, your account’s CPID and the unique Device ID will be used to connect to the IoT Connect platform. At that time, the platform will generate credentials for an MQTTS connection your device will connect to.

Self-signed Certificates – You can also use a self-signed certificate to authenticate your device. You can do this by generating the self-signed certificate locally or using in the IoT connect platform. We’ll select this option and this in just a moment.

CA Signed Certificate – With this option, you can generate a unique certificate for your device that is signed by a certificate authority. This allows you to have more flexibility when provisioning devices as you can leverage the CA certificate rather than generating a unique self-signed certificate for every device.

TPM – One other option is to use a Trusted Platform Module or TPM. This is a type of hardware security module (HSM) that uses a chip containing keys to power cryptographic operations. This is a common solution for production applications.


We’ll be only provisioning this single device, so we’ll pick the “Self Signed Certificate” option and press “Save”. This will create the initial template for us.

From here, we can add device attributes. These are designed to be properties that we want to measure. For example, if we had a temperature sensor or light sensor we would configure attributes related to the measurement those sensors would take:

In this case, we’ll create one called “light” as the local name and the Data Type of String before pressing save and going to the next step.

In the Twin Properties section, we will make some changes. Twin properties are designed to hold values that will change back and forth between the device and the cloud. For example, for the LED we have connected to our Raspberry Pi we will create a new “light” property that we’ll be syncing back and forth from the device and the cloud:

We’ll leave the initial state as “OFF” and then hit Save. Later on, we’ll sync this Twin Property to our device.

Next, we need to create our specific device using the template we just created. At this point, I recommend that you are signed into IoT Connect while on your Raspberry Pi. The reason for this is that when we create the device we will configure a certificate for it and need to download that certificate to our device.

Once you’re logged into IoT Connect from the Raspberry Pi, Navigate to the Devices section and press the Create Device button:

In this section, we can give our device a unique ID and name. We’ll also have to pick the entity that we’re associating this device with and the Template we want to base this device off of. For this device I’ll make things simple and use the device ID and display name of “raspi”. I’ll also use the “raspi” template I just created:

After selecting these options, we have to create a certificate for our device. We can do this by pressing the gold certificate image to the right of the Template option. This will prompt us to enter in a password so that IoT Connect can generate a self-signed certificate for us:

After you enter a password and click the Generate button you should see a screen like this appear:

This contains all the information you’ll need in order to create the files you need locally to connect to IoT Connect Securely.

Make sure to secure these files properly and don’t share them! In this case I’m only showing a screenshot of this page with some test keys here that I’m about to delete!

Create three empty files on your Raspberry Pi called:

  • ca.crt.pem
  • client-certificate.pem
  • private.pem

Then, paste the contents of respective certificates and keys into the files. I suggest doing this by copying directly from the text box rather than using the copy button. If you do use the copy button you will need to make sure that when you paste the contents into your files that the “—–BEGIN CERTIFICATE—–” and “—–END CERTIFICATE—–” are each on their own lines with the content of the certificate between them on a line by itself too.

Once you’ve copied them all down then save the three files on your Raspberry Pi and copy the Thumbprint. Then you can press Ok. The primary thumbprint should be filled in for you automatically, if it isn’t, then paste the thumbprint you copied into the Primary Thumbprint section and press Save:

With this saved, we should be able to set up our Raspberry Pi to connect to IoT Connect!

Installing Dependencies on Our Raspberry Pi

This tutorial will use the IoT Connect SDK for Python to form a connection between the device and the cloud. However, there are a variety of other SDKs for other languages that you can leverage when working with IoT Connect. If you’d like to see more examples with other languages just reach out to us!

If you’re working with the Raspberry Pi OS, it should already have several dependencies we’ll need on it including Python 3. If you have another operating system installed on the Raspberry Pi, make sure to install Python 3 with at least version 3.5 or later from the Python website –

Want to see more examples

In addition to Python, you’ll need to use git to download some of the code for this tutorial. Raspberry Pi OS also comes with git, but if you’re using a different operating system make sure to install that too.

Let’s start by installing the IoT Connect SDK for Python. The easiest way to find it is to click on Resources while in the IoT Connect dashboard and click on SDK. From there, you should be taken to the Device SDK Flavors Page here.

You may need to sign in to IoT Connect to access these pages if you haven’t already. From this page, we’ll click on the Python page.

This page contains multiple options for the Python SDK. If you’re working with X.509 certificates like we are we can download the first option that says “SDK with Symantec and X.509 Auth Support”. Download this package to the Raspberry Pi and extract the contents. You can do this from the terminal with the unzip command.

After the file is unzipped, you can install the Python SDK by changing directories into the unzipped folder, and then into the iotconnect-sdk folder. From there, you can run this command to install whatever the version of the SDK is that happens to be in the folder:

pip3 install iotconnect-sdk-*.tar.gz

Or, if you’re using Python 2 then run:

pip install iotconnect-sdk-*.tar.gz

This should install the IoT Connect SDK for Python into your system. To test if the installation process worked correctly you can enter the Python 3 interpreter with:


From there, you can run:

import iotconnect

If this works without failures, then we should be ready to use the IoT Connect SDK. Now we can get the code for this tutorial with:

git clone

Syncing Between the Raspberry Pi and the IoT Connect Platform

Now that we have the code, we should move the certificate files to a place that the demo code expects them to save ourselves time.  Wherever you created your ca.crt.pem files and the other files from earlier, move them all to the Downloads folder. You can do this with:

mv <your-file-name> ~/Downloads/

Replace <your-file-name> with the actual name of the file for each of the three files.

We may also want to make sure we modify the permissions on these sensitive files to make them only readable to the current user. We can do this by running the following command for each of the files:

chmod 400 <your-file-name>

Remember to replace the section of the command with <this> inside of it.

In a normal production environment, we’d be more likely to use something like a Trusted Platform Module or TPM to securely store and use certificates like this. But for now, this should work fine for our demo purposes.

Now that we have those setup, change directories into the iot-connect-raspberry-pi-demo folder we cloned with git.

We need to run one of the demo scripts in this folder to start syncing with IoT Connect. To do this, first go to the IoT Connect dashboard and navigate to the Key Vault section under Settings. On this page, you should see an Environment and CPID value.

The Environment value might be something like “Prod” or “Dev”. Whatever it is, take that value, capitalize it and add it into the command below:

python3 <ENV_VALUE>

For example:

python3 PROD

Then run this command in the terminal in the same directory as the file. Note that if you need to use Python 2 you will have to run this command and work with instead:

python PROD

After running this command, you should see a prompt for the device serial number. Enter in “raspi” and press enter. Then you should see a prompt for the CPID, copy the CPID from the Key Vault page here and press enter. From here, you should see a message that includes something like:

— Twin Message Received —

From there, you can go into the Device > Devices section of IoT Connect and click on the “raspi” device and click on the Twin Property. Inside of that screen, it should look something like this:

On this page we can click the dropdown under Select Twin Property and select the “light” option. Then we can change the Desired Value to “ON” and press Execute Twin Property. Now if we look at our LED attached to the Raspberry Pi the script running on the Raspberry Pi should have registered a change and updated the state of the LED to on!

If we want to turn the LED back off, we select the “light” property again and set the desired value as “OFF” before pressing Execute Twin Property. It should prompt more text in our Raspberry Pi terminal then turn back off.

Once we’re done playing around, we can exit the script with Control + C.

And that’s it! We’ve managed to connect our Raspberry Pi device to IoT Connect and control it from the IoT Connect console. You should be able to continue to iterate on this project by taking advantage of additional twin properties as well as working with device attributes to communicate bidirectionally between the device and the cloud.

If you have any questions about this tutorial, or if you’d like to suggest ideas for the next one, don’t hesitate to contact us!

You might also like...
How to Connect Your Raspberry Pi to AWS IoT?

How to Connect Your Raspberry Pi to AWS IoT? For anyone getting started working with cloud services from a hardware background, it can be an intimidating journey that requires learning a lot of new concepts and best practices so I wanted to share my experience and help bridge the gap between those skillsets.

Read more
Fernando Medina Corey - Cloud Architect
26 July 2021