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
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:
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 https://github.com/Witekio/iot-connect-raspberry-pi-demo.git
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 demo3.py <ENV_VALUE>
python3 demo3.py PROD
Then run this command in the terminal in the same directory as the demo3.py file. Note that if you need to use Python 2 you will have to run this command and work with demo.py instead:
python demo.py 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: