Skip to content

Latest commit

 

History

History
76 lines (54 loc) · 2.62 KB

howto-connectionstrings.md

File metadata and controls

76 lines (54 loc) · 2.62 KB

How to Find and Set Your Connection Strings

On your Raspberry Pi

  1. Install decouple, see https://pypi.org/project/python-decouple/ for details.

    pip install python-decouple
  2. Create an '.env' file and save it to GitHub root (e.g. c:/repos/IoT) directory from your GitHub forked clone, then add your entries as follows:

    IOTHUB_CONNECTION_STRING="{IoT hub Primary Connection String}"
    IOTHUB_DEVICE_CONNECTION_STRING="{Your Device Connection String}"
  3. Obtain the environment variable by opening a python session and run the following script

    cd {github forked clone root}
    python
    from decouple import config
    config('IOTHUB_DEVICE_CONNECTION_STRING')

    for example,

    cd c:/repos/IoT 
    c:/repos/IoT> python
    >>> from decouple import config
    >>> config('IOTHUB_DEVICE_CONNECTION_STRING')
    >>> 'HostName=HubMsg********p2qwy.azure-devices.net;DeviceId=myDevice;SharedAccessKey=8IrO********ZUkg='

On your Windows Cloud Machine

  1. Add system environment variables by opening a PowerShell command prompt in Administrative mode and run the following script.

    [Environment]::SetEnvironmentVariable('IOTHUB_CONNECTION_STRING','{connection string from previous step}','Machine')
  2. Obtain the environment variable by opening a python session in the same directory as the '.env' file where you'll run your scripts.

    import os
    os.getenv('IOTHUB_DEVICE_CONNECTION_STRING')

    for example,

    c:/repos/IoT python
    >>> import os
    >>> os.getenv('IOTHUB_DEVICE_CONNECTION_STRING')
    >>> 'HostName=HubMsg********p2qwy.azure-devices.net;DeviceId=myDevice;SharedAccessKey=8IrO********ZUkg='
  3. Delete your system variable from a PowerShell command prompt in Administrative mode by changing the {your environment variable} name and running following script:

    [Environment]::SetEnvironmentVariable('{your environment variable}',$Null,'Machine')

    For example

    [Environment]::SetEnvironmentVariable('IOTHUB_CONNECTION_STRING',$Null,'Machine')

Resources