Before You Begin
This 15-minute tutorial shows you how to build a Python application that simulates a virtual device and sends a message to Oracle Internet of Things (IoT) Cloud Service.
Background
Oracle IoT Cloud Service provides communication services between devices and enterprise applications. In this tutorial, you build a simulated device in a Python application, and then transmit a Hello World message to Oracle IoT Cloud Service.
What Do You Need?
- Access to the Oracle IoT Cloud Service
- A computer running Windows 7 or higher with a web browser
- Download the 3.6.4 release software of Python (Windows x86-64 executable installer) and save in the
My Documents
directory - Download the Python code zip file:
helloworld.zip
to extract thehelloworld.py
file - Download the requirements zip file:
requirements.zip
to extract therequirements.txt
file - Download the latest version of the Python Client Software Library from Oracle Technologies Network in the
My Documents
directory - Firefox or Chrome web browser
- Complete the following tutorial: Configuring an Application in Oracle Internet of Things Cloud Service to obtain the provisioning file
Install Python
-
Browse to the directory where you downloaded Python's windows installer package, double click
python-3.6.4-amd64.exe
and then follow the steps in the wizard. - On the first page of the wizard, ensure that you select the Add Python 3.6 to PATH before you click Install Now.
- Wait until the installer finishes, then click Close.
-
Go to the installation location,
C:\Users\user\AppData\Local\Programs\Python\Python36
, and verify that the contents of the directory are similar to the following screenshot:
Create a Python Virtual Environment
Setup a Python Virtual Environment (PVE) for the software that you installed.
- Ensure that you have downloaded the latest Client Software Library for Python (binary, source code and samples) zip files,
iotcs-csl-python-bin-release.zip
,iotcs-csl-python-src-release.zip
, andiotcs-csl-python-samples-release.zip
, whererelease
is the release and version number. - Create the directory
iotcs
withinMy Documents
directory. - Open a command prompt and go to the
My Documents\iotcs
directory. -
To create the
iot-pcl-env
directory, run the following command in the command prompt:python3 -m venv iot-pcl-env
-
To setup the shell environment, run the activate script:
Once the Python Virtual Environment (PVE) has been established perform all command line options in the shell in which activate was invoked. Note the iot-pcl-env prior to the prompt.iot-pcl-env\Scripts\activate.bat
- Update pip setup tool with this command:
python3 -m pip install --upgrade pip
If you are behind a proxy, use this command:python3 -m pip install --proxy=https://proxy:port --upgrade pip
- After a few minutes, verify the installation of the pip package.
Successfully installed pip-version
-
Extract the
requirements.txt
file from the zip file that you downloaded in the pre-requisites to the current directory. -
Install the requirements in the PVE using the
requirements.txt
file by running this command:pip install -r requirements.txt
If you are behind a proxy, use this command:pip install --proxy=https://proxy:port -r requirements.txt
- After a few minutes, verify that the following packages have been installed:
Successfully installed Jinja-version MarkupSafe-version Pygments-version Sphinx-version alabaster-version babel-version certifi-version chardet-version colorama-version docutils-version idna-version imagesize-version netifaces-version packaging-version pycryptodome-version pycryptodomex-version pyparsing-version pypiserver-version pytz-version requests-version six-version snowballstemmer-version sphinxcontrib-websupport-version urllib3-version
Install the Python Client Software Library
- In the same command prompt, install the libraries by running these commands:
Note thepip install iotcs-csl-python-bin-release.zip
pip install iotcs-csl-python-samples-release.zip
Successfully installed iotcs-csl-python-...
result after each command.
Run the HelloWorld Sample
-
In your
My Documents\iotcs
directory, copy the sample Python code filehelloworld.py
. -
Copy the Provisioning file obtained from the Configuring an Application in Oracle Internet of Things Cloud Service tutorial listed in the What Do You Need section, to the same directory as the
helloworld.py
file. - Open the
helloworld.py
in a text editor. - Change the information about the provisioning file and the file protection password, used in that same tutorial, by editing the lines 15 and 17 respectively.
15 CONFIG_FILE = "provisioning.file" 17 PASSPHRASE = "file protection password"
- Run the following command in the same directory:
Note that after running, the program shows:python3 helloworld.py
- Deactivate the virtual environment, using this command:
iot-pcl-env\Scripts\deactivate.bat
- Sign in to your Oracle IoT Cloud instance.
- In the Oracle IoT Cloud Service Management Console, navigate to the Oracle IoT Asset Monitoring application.
- Select Data, and then in Filter By, select Device..
- Find your messages in the Messages table and review the value of Message Content.