Copy Scripts to the Raspberry Pi (Optional)
Now we need to get some type of software on the Pi which can read from your (optional) sensors or the Pi itself and communicate back to the ThingWorx. If you so desire, please download each of the following to your computer:
• HC-SR04 Range Sensor—range_sensor.py
• DHT11 Temperature/Humidity Sensor—thingxpi_DHT11.py
• Connection to ThingWorx—thingxpi.py
These scripts are meant to get you started and are all done in a slightly different manner. For example, the thingxpi_DHT11.py script has hard-coded URL, Application Key, and thing name information which you’ll need to update to your specific settings. Whereas the range_sensor.py script communicates the HC-SR04’s information back to the thingxpi.py script. The thingxpi.py script then gathers some Raspberry Pi-specific information (CPU-temp, memory-usage, etc.) and sends the information as a bundle back to ThingWorx using command line arguments to denote the specific thing, URL, and app key. Each of the scripts must be modified (or written from scratch by you) in order to retrieve the information that you want.
How you get the scripts onto your Raspberry Pi is based on user discretion. For example, you could load them onto a USB drive, mount the drive using CLI on the Raspberry Pi, and then copy them over. Alternately, you could use apt-get to install an FTP server and transfer them that way. Python is required to run these various scripts. To confirm that Python is properly installed on your Raspberry Pi, please issue the following command:
python --version
|
You should have at least Python 2.7.3 as a response. Otherwise, please upgrade Python to a newer version.
|
Regardless, after your script(s) are loaded onto the Raspberry Pi, you must execute each one of them in their own SSH windows. If you are specifically using the HC-SR04 and DHT11 sensors, the commands in each SSH window will look similar to the following:
• sudo python ./range_sensor.py
• sudo python ./thingxpi_DHT11.py
• sudo python ./thingxpi.py RasperryPiThing <ThingWorx instance URL><App key>
Where <ThingWorx instance URL> is the URL for your ThingWorx instance, and <App key> is the application key that was created earlier in this tutorial. For example, the command should look similar to the following:
sudo python ./thingxpi.py RaspberryPiThing https://thingx-jdoe.abc1.thingworx.io/Thingworx d89bgc23-f3ce-42b5-b8d0-61gcd42489f8
|
Remember, the thingxpi_DHT11.py script must also be modified to include the correct URL, thing name, and app key before running it. An incorrect thing name, URL, or app key prevents the Python scripts from updating the thing properties.
|
Also note that the Python scripts that you’re importing can have dependencies that need to be installed. For example, both the Setuptools and Requests modules are used in the thingxpi.py file. You’ll need to manually install any missing modules as you attempt to run each of the scripts. To install the Python Setuptools and Requests modules, complete the following steps:
1. Install the Setuptools module.
b. Select and copy all content (Ctrl+A/Ctrl+C).
c. On your Raspberry Pi, issue a sudo touch ez_setup.py command to create the file.
d. Using a text editor on your Raspberry Pi, such as nano, paste the entire contents of ez_setup.py into the file. Save and exit the ez_setup.py file.
e. Run the following command: sudo python ez_setup.py
2. Install the Requests module.
a. Run the following commands:
▪ sudo git clone git://github.com/kennethreitz/requests.git
▪ cd requests (For example, change the directory into the folder that GIT has just added)
▪ sudo python setup.py install
After execution, each of the scripts continue to run, giving you printouts of the information being collected and sends the information to ThingWorx (this is the reason that three separate SSH windows are required for this method). With all of your scripts running on your Raspberry Pi, ThingWorx is regularly updated with this information. You can then use the stored information in your AR experience.
Verify Connection Between Raspberry Pi and ThingWorx
1. Open ThingWorx.
2. Click Things under MODELING. Search for the RaspberryPiThing and select it.
3. Click Properties under ENTITY INFORMATION.
4. Click the Refresh Properties icon at the top of the Value column.
5. Confirm that each time you press the Refresh Properties icon, some of the values change.