This commit is contained in:
Oguz Bastemur 2019-04-17 14:42:12 -07:00
Родитель 152e904d80
Коммит 4246dfab0b
3 изменённых файлов: 156 добавлений и 0 удалений

Просмотреть файл

@ -29,6 +29,11 @@ args:
i.e. => dps-certgen --new-root-cert
```
### Sample apps
- [Azure IoT x509 C SDK client example](./samples/azure-iot-c-sdk/README.md)
- [Azure IoT x509 Python client example](./samples/python/README.md)
### Sample usage
#### Create a new X509 root certificate

95
samples/python/README.md Normal file
Просмотреть файл

@ -0,0 +1,95 @@
### Connect to Azure IoT with Python X509
Step by step x509 client authentication with Python
### Clone SDK and install dependencies
Install `dps-certgen`
```
npm i -g dps-certgen
```
Make sure you have Python 2.7+, or Python 3.4+ or Micropython 1.9+
Install `iotc`
```
pip install iotc
```
### Create and register an x509 certificate
We will need an environment to use this certificate and connect to iothub.
- Visit [apps.azureiotcentral.com](https://apps.azureiotcentral.com) for a free Azure iothub and Azure iot dps setup.
- Click to `New Application` button
- Select `Trial` and `Sample Devkits`... then click `Create`
- Browse `Administration` tab on the left
At this point, lets create the root certificate that we are going to use for the app.
```
dps-certgen --new-root-cert
```
Running the command above will generate the files below.
(in case you want to modify the specifics of this root cert, update the `certgen.config.json` file and re-run the command above)
```
./root-cert.pem
./root-public-key.pem
./root-private-key.pem
```
- Back to Azure IoT Central `Administration` Tab
- Click to `Device Connection` and under the `Credetentials` select `Certificates (X509)`
- Click to blue `folder` button and select `root-cert.pem` that you've created above
- Click to blue `cog` button and get the verification code. (you may need to click to blue recycle button to generate one)
Use the `verification code` as shown below to generate verification certificate;
```
dps-certgen --new-leaf-cert <put verification code here>
```
Running the command above will generate the files below
```
./subject-cert.pem
./subject-public-key.pem
./subject-private-key.pem
```
- Back to Azure IoT Central and click to blue `Verify` button.
- Select the `subject-cert.pem` that you've created above
- That's it! Azure IoT setup is complete.
Now it's time to create a certificate for our sample device.
Assuming the device id is `dev1`
```
dps-certgen --new-leaf-cert dev1
```
Running the command above will generate the files below for `dev1`
```
./subject-cert.pem
./subject-public-key.pem
./subject-private-key.pem
```
- edit `app.py` at your favorite editor and update `DEVICE_ID` to `dev1` (consistent with the leaf certificate above)
- Go back to Azure iot central and copy `Scope Id` from `Administration` / `Device Connection`
- on `app.py`, update `SCOPE_ID` to the one you've just grabbed from iot central
Let's try
```
python app.py
```
You will possibly see an error regarding to provisioning.
- Go back to `Azure iot central` and browse `Device Explorer` > `Unassociated device`
- Select `dev1`. From the top menu, select `associate` (second left icon on the top bar menu)
Now, it should run just fine!
```
python app.py
```
You may monitor the fake telemetry from Azure iot central's device explorer.

56
samples/python/app.py Normal file
Просмотреть файл

@ -0,0 +1,56 @@
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license.
import iotc
from iotc import IOTConnectType, IOTLogLevel
from random import randint
deviceId = "DEVICE_ID"
scopeId = "SCOPE_ID"
cert = {
"certfile": "./subject-cert.pem",
"keyfile": "./subject-private-key.pem",
}
iotc = iotc.Device(scopeId, cert, deviceId, IOTConnectType.IOTC_CONNECT_X509_CERT)
iotc.setLogLevel(IOTLogLevel.IOTC_LOGGING_API_ONLY)
gCanSend = False
gCounter = 0
def onconnect(info):
global gCanSend
print("- [onconnect] => status:" + str(info.getStatusCode()))
if info.getStatusCode() == 0:
if iotc.isConnected():
gCanSend = True
def onmessagesent(info):
print("\t- [onmessagesent] => " + str(info.getPayload()))
def oncommand(info):
print("- [oncommand] => " + info.getTag() + " => " + str(info.getPayload()))
def onsettingsupdated(info):
print("- [onsettingsupdated] => " + info.getTag() + " => " + info.getPayload())
iotc.on("ConnectionStatus", onconnect)
iotc.on("MessageSent", onmessagesent)
iotc.on("Command", oncommand)
iotc.on("SettingsUpdated", onsettingsupdated)
iotc.connect()
while iotc.isConnected():
iotc.doNext() # do the async work needed to be done for MQTT
if gCanSend == True:
if gCounter % 20 == 0:
gCounter = 0
print("Sending telemetry..")
iotc.sendTelemetry("{ \
\"temp\": " + str(randint(20, 45)) + ", \
\"accelerometerX\": " + str(randint(2, 15)) + ", \
\"accelerometerY\": " + str(randint(3, 9)) + ", \
\"accelerometerZ\": " + str(randint(1, 4)) + "}")
gCounter += 1