Oracle by Example brandingQuick Start Tutorial Using a Python Application

section 0Before 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 the helloworld.py file
  • Download the requirements zip file: requirements.zip to extract the requirements.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

section 1Install Python

  1. 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.
  2. On the first page of the wizard, ensure that you select the Add Python 3.6 to PATH before you click Install Now.
    python installer
    Description of the illustration python-installer2.png
  3. Wait until the installer finishes, then click Close.
  4. 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:
    installed files of python installer
    Description of the illustration python_install2.png

section 2Create a Python Virtual Environment

Setup a Python Virtual Environment (PVE) for the software that you installed.

  1. 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, and iotcs-csl-python-samples-release.zip, where release is the release and version number.
  2. Create the directory iotcs within My Documents directory.
  3. Open a command prompt and go to the My Documents\iotcs directory.
  4. To create the iot-pcl-env directory, run the following command in the command prompt:
    python3 -m venv iot-pcl-env
  5. To setup the shell environment, run the activate script:
    iot-pcl-env\Scripts\activate.bat 
    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.
    command prompt showing the activation of the Python virtual environment
    Description of the illustration activate_venv.png
  6. 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
  7. After a few minutes, verify the installation of the pip package.
    Successfully installed pip-version
  8. Extract the requirements.txt file from the zip file that you downloaded in the pre-requisites to the current directory.
  9. 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
  10. 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

section 3Install the Python Client Software Library

  1. In the same command prompt, install the libraries by running these commands:
    pip install iotcs-csl-python-bin-release.zip
    pip install iotcs-csl-python-samples-release.zip
    Note the Successfully installed iotcs-csl-python-... result after each command.

section 4Run the HelloWorld Sample

  1. In your My Documents\iotcs directory, copy the sample Python code file helloworld.py.
  2. 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.
  3. Open the helloworld.py in a text editor.
  4. 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"
  5. Run the following command in the same directory:
    python3 helloworld.py
    Note that after running, the program shows:
    Messages table
    Description of the illustration python_helloworld.png
  6. Deactivate the virtual environment, using this command:
    iot-pcl-env\Scripts\deactivate.bat 
  7. Sign in to your Oracle IoT Cloud instance.
  8. In the Oracle IoT Cloud Service Management Console, navigate to the Oracle IoT Asset Monitoring application.
  9. Select Data, and then in Filter By, select Device..
  10. Find your messages in the Messages table and review the value of Message Content.
    Messages table
    Description of the illustration messages_table.png

more informationWant to Learn More?