python: add masterkey sample
This commit is contained in:
Родитель
fc488523b7
Коммит
78793cace9
|
@ -1,11 +1,10 @@
|
|||
## Azure IoT Central Reference Firmware for Raspberry Pi 2/3
|
||||
## Azure IoT reference application for Raspberry Pi 2/3 (using python)
|
||||
|
||||
### Description
|
||||
|
||||
An example of writing a firmware solution to send data to Azure IoT Central and
|
||||
to receive events back from Azure IoT Central to be processed by the device.
|
||||
You are free to take this code and the concepts used, and use them as a basis
|
||||
for your own firmware for Azure IoT Central.
|
||||
An example python app to..
|
||||
- send data to Azure IoT Central
|
||||
- receive events back from Azure IoT Central to be processed by the device.
|
||||
|
||||
### How to run
|
||||
|
||||
|
@ -20,12 +19,11 @@ Install [azure iot central python client](https://pypi.org/project/iotc/) `iotc`
|
|||
pip install iotc
|
||||
```
|
||||
|
||||
Go create an application
|
||||
Fill the information below (under app.py).
|
||||
```
|
||||
deviceId = "DEVICE_ID"
|
||||
scopeId = "SCOPE_ID"
|
||||
mkey = "DEVICE_KEY"
|
||||
deviceKey = "DEVICE_KEY"
|
||||
```
|
||||
|
||||
Run!
|
||||
|
@ -35,3 +33,8 @@ python app.py
|
|||
```
|
||||
|
||||
Python 2/3 and micropython(*) are supported.
|
||||
|
||||
|
||||
#### Using master key for producing deviceKey on runtime
|
||||
|
||||
Take a look at the `appMasterKey.py` sample
|
||||
|
|
|
@ -7,9 +7,9 @@ from random import randint
|
|||
|
||||
deviceId = "DEVICE_ID"
|
||||
scopeId = "SCOPE_ID"
|
||||
mkey = "PRIMARY/SECONDARY device KEY"
|
||||
deviceKey = "PRIMARY/SECONDARY device KEY"
|
||||
|
||||
iotc = iotc.Device(scopeId, mkey, deviceId, IOTConnectType.IOTC_CONNECT_SYMM_KEY)
|
||||
iotc = iotc.Device(scopeId, deviceKey, deviceId, IOTConnectType.IOTC_CONNECT_SYMM_KEY)
|
||||
iotc.setLogLevel(IOTLogLevel.IOTC_LOGGING_API_ONLY)
|
||||
|
||||
gCanSend = False
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
# Copyright (c) Microsoft. All rights reserved.
|
||||
# Licensed under the MIT license.
|
||||
|
||||
import sys
|
||||
import iotc
|
||||
from iotc import IOTConnectType, IOTLogLevel
|
||||
from random import randint
|
||||
import base64
|
||||
import hmac
|
||||
import hashlib
|
||||
|
||||
gIsMicroPython = ('implementation' in dir(sys)) and ('name' in dir(sys.implementation)) and (sys.implementation.name == 'micropython')
|
||||
|
||||
def computeKey(secret, regId):
|
||||
global gIsMicroPython
|
||||
try:
|
||||
secret = base64.b64decode(secret)
|
||||
except:
|
||||
print("ERROR: broken base64 secret => `" + secret + "`")
|
||||
sys.exit()
|
||||
|
||||
if gIsMicroPython == False:
|
||||
return base64.b64encode(hmac.new(secret, msg=regId.encode('utf8'), digestmod=hashlib.sha256).digest())
|
||||
else:
|
||||
return base64.b64encode(hmac.new(secret, msg=regId.encode('utf8'), digestmod=hashlib._sha256.sha256).digest())
|
||||
|
||||
deviceId = "DEVICE_ID"
|
||||
scopeId = "SCOPE_ID"
|
||||
masterKey = "PRIMARY/SECONDARY master Key"
|
||||
|
||||
deviceKey = computeKey(masterKey, deviceId)
|
||||
|
||||
iotc = iotc.Device(scopeId, deviceKey, deviceId, IOTConnectType.IOTC_CONNECT_SYMM_KEY)
|
||||
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
|
Загрузка…
Ссылка в новой задаче