How to read Temperature and Humidity from Am2302 sensor using Thingworx Edge java SKD for Raspberry Pi Revison History
Revision # Date ThingWorx Revision Changes Owner 1.0 2.0 21-11-14 3.0 17-12-14 4.0 19-12-14 Used you instead of we, added copyright and page cover, modified footer Modified Cover Page font, footer, review table and copyright paragraph position in the document at Andy s suggestion 5.0 26.01.15 Added a note concerning the App key DeAnna Robear Veronica MIHAI Veronica MIHAI Veronica MIHAI Veronica MIHAI Raspberry Pi Temperature and Humidity Sensor Integration Example with Thingworx PART ONE: On your Raspberry Pi 1. Install the following packages on your Pi before you begin; you will need to have established an internet connection on your Raspberry Pi before proceeding. It is also recommended that you SSH into your pi using a program like PuTTy. sudo apt-get update: A complete software update for the pi sudo apt-get install oracle-java7-jdk: Java JDK 7 is required by ThingWorx so that you can run the Java Virtual Machine (JVM) and other components which enable your pi to run applets and applications written in Java code 2. Create a directory to hold your project on the pi. The command for creating a new directory is mkdir. Run the following command : mkdir projects 3. Navigate to the new directory you just created using this command: cd 4. Perform the necessary wiring for the AM2302 sensor and download the necessary software and libraries. The AM2302 sensor has three wires that need to be connected to RPI pins. This Adafruit tutorial guides you through performing the connections and installing a python library that will allow you to read data from AM2302 temperature and humidity sensor through a simple command line like sudo./adafruitdht.py 2302 4, where 2302 is the name of the sensor and 4 is the number of the pin to which the data wire of the sensor is connected. Make sure you run the git clone https://github.com/adafruit/adafruit_python_dht.git command to install the Adafruit_Python_DHT library while in your projects folder, because the path to the library is hardcoded in the EMS jar file. on Raspberry Pi PTC Inc. 2
NOTE: If you re using a mac computer, you can copy and paste these commands like you would copy and paste anything else. If you are using a windows computer you can still copy using CTRL + C, but to be able to paste into the PuTTy window, you have to do a left mouse click. In the next step you will download an EMS project that will serve in pushing data from the Am2302 sensor into the Thingworx platform. The EMS project is basically a java project with the following structure: The reading of the temperature and humidity values from the sensor is done in the file TempAndHumidityThing.java with the help of the Process java class. The result containing the temperature and humidity values in the format Temp=29.0 *C Humidity=36.0% (see picture below) is then stored in a string that will later be parsed to obtain the numeric values of the properties. on Raspberry Pi PTC Inc. 3
Below you can find a snippet of the gettemperature() method of the same class mentioned above. private Double gettemperature() { String consoleoutput; if (simulated!=null&&simulated.equals("simulated")) { consoleoutput = getsimulatedconsoleoutput(); } else { consoleoutput = getcommandresults("sudo " + PI_HOME + PATH_TO_TEMP_HUMID_COMMAND + " 2302 4"); // line with the pin number to which the data wire of the sensor is connected on the RPI } Double temperature = parsetemperaturefromstring(consoleoutput); return temperature; } Note that if you choose to connect your Am2302 sensor to a different data pin you ll need to update the above command too, otherwise the data won t be properly read from the sensor. consoleoutput = getcommandresults("sudo " + PI_HOME + PATH_TO_TEMP_HUMID_COMMAND + " 2302 4"); // replace 4 with the pin number on which you connected your Am2302 sensor data wire Later on, you will use maven to package this java project into a runnable jar file that you ll name temperature-thing-jar-with-dependencies and then you will deploy it to you Raspberry Pi. If you are using a DHT22 sensor instead of an Am2302 sensor, you will need to replace the 3202 with 22 in the String given to the getcommandresults method, both in the gettemperature method as in the gethumidity method. on Raspberry Pi PTC Inc. 4
You will then need to repackage and redeploy the jar file to the Rasperry Pi. For more information on how to do this using maven read the remaining of this tutorial. PART TWO: On your Computer 1. Download and extract the EMS and project files from here. Make sure that you extract them to your home directory. To find out what is part of your home directory open up a command window and type echo %HOME% (if this doesn t work try %userprofile%), press enter and then type ls (if this doesn t work try dir) and again press enter: 2. Download and install java JDK7 here needed for the JVM and other components (see above) on Raspberry Pi PTC Inc. 5
3. Download and install Apache Maven here - used to manage the building/testing/deploying processes, it also handles all the downloading, installing, and version updating of project dependencies 4. Maven should be added to your PATH environment variable so that it can be utilized from the command line: Go to start, then find and click Computer. Right click on Computer and select properties: Select Advanced System Settings on Raspberry Pi PTC Inc. 6
When you reach the System Properties window, select Environment Variables : Scroll down under System Variables and find Path, then click edit: Select all of the text and copy it into a program like Notepad or Word so you can see the entire text: on Raspberry Pi PTC Inc. 7
The highlighted portion above is the part that needs to be added to your path, you can find the location by opening Documents Systems (C:) Program File (x86) apachemaven-3.2.1 bin. Then click the white space at the top to get a copy-able location: 5. Add Java to User Variables so you can utilize Java from the command line as well: Go back to Environment Variables: Under User variables for select New then type the name JAVA_HOME (make sure it is in all capitols) and then paste the location of Java into the variable value field: on Raspberry Pi PTC Inc. 8
NOTE: The Location of Java is Computer System (C:) Program Files Java jdk1.7.0_60: PART THREE: Building the example 1. To build the EMS, open up your command line and go to the directory where you have extracted your files to. To do this type cd then use the following command: mvn package: this will create the release jar in target/temperature-thing-jar-withdependencies.jar which enables the EMS to push data to ThingWorx on Raspberry Pi PTC Inc. 9
PART FOUR: Deploying your example to a Raspberry PI 1. Once your jar is built, you can automatically deliver the resulting jar to your Raspberry Pi for testing. To do this you must copy settings.xml from location Computer Systems(C:) Program File (x86) apache-maven-3.2.1 conf Paste this into the.m2 directory at location Data (D:) Users your user login name.m2 NOTE: If settings.xml already exists here, do no overwrite it on Raspberry Pi PTC Inc. 10
2. The settings file is what tells maven the username and password for your pi. Open settings.xml in a program like notepad or word and enter your raspberry pi username and password, the default is username pi and password raspberry. Make sure you save before exiting. 3. Before continuing to the next step, you ll have to generate an application key. An application key is tasked with identifying the calling program (temp and humidity data) and the user (you) to the website (ThingWorx Composer). To generate an application key: Go to the ThingWorx Composer On the left hand side, there is a pane with drop down menus for Modeling, Visualization, Data Storage, Collaboration, Security and System. Select the drop down menu for Security : Under Security hover over Application Keys and click the green plus sign: In the name section, enter your own name; make sure to follow naming conventions, click here to view the guidelines In the tag section, click the wand, select, then using Applications for the vocabulary and in the New Term field, enter a name for your tag (usually something related to your project i.e. TempHumidityProject): When you re done, click Add Term on Raspberry Pi PTC Inc. 11
Finally, fill in the User Name Reference field by again clicking the wand and selecting yourself from the list of users, when you re done it should look like this: When you re done, go to the top of the page and hit You ll notice that the keyid field now has a series of letters and numbers in it, this is your generated application key: Note: When creating the Application key, make sure that in the User Name Reference field you select the same user you use to log into Thingworx to follow this tutorial. Otherwise the data sent from the Thingworx java SDK program won t be received by Thingworx. The remote thing will appear online, but no data will be displayed for the Temperature and Humidity properties and no message will be shown to let you know what caused this situation. on Raspberry Pi PTC Inc. 12
4. Next, edit the pom.xml file in this project s main directory (aka wherever you saved it). Under properties update the URL indicating where you want the jar to be copied to. Now from your computer s command line run: `mvn deploy` This will deliver the jar to your PI automatically. PART FIVE: Running the example The following command will start your edge server from the target directory On your Pi s command line: java -jar./temperature-thing-jar-with-dependencies.jar <ThingWorx Server URL> <Application Key> <Optional Simulated> On windows command line: java -jar temperature-thing-jar-with-dependencies.jar <ThingWorx Server URL> <Application Key> <Optional Simulated> Note: When running the jar file on your PC, make sure you are running the command from your temperature-thing/target folder, where your jar file is located. Also use the simulated option, as no sensor from which to read data from is connected to your PC. To stop the running application, type CTRL+C in the command line. The parameters required to start the example will be different for everyone. This is how you determine them. on Raspberry Pi PTC Inc. 13
<ThingWorx Server URL> - If your thingworx server is on https://maker01.ptcmanaged.com/thingworx for example then your server URL will be wss://maker01.ptcmanaged.com:443/thingworx/ws. If you are not using https then wss: should be changed to just ws and the port to 80: <Application Key> - An API key is a string that your edge client can use to log in as you without having your username or password. You must log into ThingWorx as you and generate one. 1. From the home tab in the composer, find the section called Application Keys and push the + button. 2. Create a key called something like TemperatureThingKey, tag it and save it. Once saved copy the generated key id value and use it for this parameter. <Optional Simulated> - If the third parameter is provided that is the word simulated then no hardware will be required to run the example. It will use randomly generated temperature and humidity values. Here is an example command: java -jar./temperature-thing-jar-with-dependencies.jar wss://maker01.ptcmanaged.com:443/thingworx/ws 7c73215e-ff40-47ee-9c18-d29a0c5310e0 simulated This will start the example using simulated data Troubleshooting information when running the jar file : It may happen that you receive a parsing error for temperature and humidity values when running the jar file. Please make sure you have the settings shown in the picture below for your Language and Regions settings options. To access the Region and Language window on Windows OS, go to Start and type Region and Language in the search bar. The second window displayed in the picture below is opened by clicking on Additional settings. on Raspberry Pi PTC Inc. 14
Reference Naming conventions: 1) Names must begin with a letter, the dollar sign "$", or the underscore character "_" 2) No white space in the name i.e. Temperature Sensor is not allowed 3) After the first character, letters, digits, dollar signs and underscore characters are allowed 4) Good naming practices include: making one word names all lowercase letters for multi-word names, type the first word in lowercase and capitalize the first letter of each subsequent word temperaturesensorthingshape is an example of an appropriate name on Raspberry Pi PTC Inc. 15
2015 PTC Inc. The information contained herein is provided for informational use and is subject to change without notice. The only warranties for PTC products and services are set forth in the express warranty statements accompanying such products and services and nothing herein should be construed as constituting an additional warranty. PTC shall not be liable for technical or editorial errors or omissions contained herein. Important Copyright, Trademark, Patent, and Licensing Information: See the About Box, or copyright notice, of your PTC software. 01012015 on Raspberry Pi PTC Inc. 16