This is a demonstration to show how to connect an sensor on an IoT device (a Raspberry Pi) to ThingWorx using balenaCloud.
For this demonstration, I am using these components:
- Raspberry Pi 3 B+ with a MicroSD card
- Adafruit MCP9808 I2C temperature sensor
- balenaCloud account (you can sign up for a free account that lets you run 10 devices)
- PTC ThingWorx Foundation (you can download a 90 day trial)
balenaCloud allows me to manage the the Pi remotely and update everything on the Pi using containers. I can log in to the console to remotely run diagnostics and monitor the device. If you use Raspberry Pi or other resource-constrained IoT devices, I highly recommend giving balena a try. It's an excellent platform for managing devices and the team there is doing a lot of great stuff.
ThingWorx is an Industrial IoT (IIoT) platform that allows rapid development of applications for industrial uses. You can quickly create web applications that can interact and display data from disparate systems. In this application, I'll be pulling data from a web weather source along with the sensor from the Pi. In an production IIoT system, this could just as easily be data from a machine sensor along with production data from an MES system.
For this example, we are using the Rest API that is built into ThingWorx. For a larger scale system, I'd suggest either using the ThingWorx Edge SDK or the ThingWorx Edge Microserver.
Clone this repository to your own computer and let's get rolling...
First, you need to wire up the sensor. Per Adafruit's guide:
- Pi 3V3 to sensor VIN
- Pi GND to sensor GND
- Pi SCL to sensor SCK
- Pi SDA to sensor SDA
- After you've made an account with balena, be sure to add an SSH key from your machine (this is described here), then log into the dashboard and click "Create application".
- Type whatever application name that you'd like (mine is "thingworx"), select Raspberry Pi 3 as the device type, keep the application type to "Starter", then click "Create new application".
- Now click on "+ Add device".
- Keep the device, OS, and edition defaults. If you're using a Wifi connection on the Pi, select "Wifi + Ethernet" and enter in your Wifi credentials.
- Click "Download balenaOS" and follow the instructions on the right to burn the image and boot the Pi onto balena.
- After the device appears on the dashboard, look for
git remote add balena
text at the top right of the application view and copy that line. - In a command line in this repository directory on your computer, paste that line and run it.
- balena is all ready!
- After installing ThingWorx, login to the composer.
- Create a new Thing called "ExampleThing" based on the GenericThing template, and add a number property of "temperature" that is logged.
- Create a new user (the name is not important -- I called mine "piDataUser") that has no password and is enabled.
- Create a new application key (again, the name is not important -- I called mine "piKey") with an expiration date somewhere in the future and that uses the user you just created.
- Copy the Key ID that is created for the application key after hitting save, and update the
TWX_APPKEY
varaible insensor/scripts/measure_mcp9808.py
. - Update the
TWX_SERVER
variable as appropriate insensor/scripts/measure_mcp9808.py
. - After saving that file, run
git commit sensor/scripts/measure_mcp9808.py
and thengit push balena master
. This will run the build scripts on balena and push this container to your Pi. If all is successful, your temperature sensor will now be publishing data to the ExampleThing temperature propery.