# i2c = I2C(scl=Pin(4), sda=Pin(5)) i2c = I2C(scl=Pin(22), sda=Pin(21)) builtinLedPin = 5 display = None builtinLed = None cfg = config.Config('config.eventhub.json') mySensor = cfg.sensor.Sensor(i2c) if cfg.hubType == 'eventhub': hub = eventhub.EventHub(cfg.host, cfg.eventHubName, cfg.eventHubPolicyName, cfg.key) elif cfg.hubType == 'iothub': hub = iothub.IotHub(cfg.host, cfg.deviceId, cfg.key) wlan = network.WLAN(network.STA_IF) lastUpdated = 0 updateSas = True def newSasToken(): global lastUpdated, updateSas, SAS if time.ticks_diff(time.time(), lastUpdated) > 60 * 15: lastUpdated = time.time() updateSas = True if updateSas: SAS = hub.generate_sas_token()
telemetry = msg_txt % ('Sydney, AU', humidity, pressure, temperature, id) print(telemetry) client.publish(iot.hubTopicPublish, telemetry) time.sleep(sampleRateInSeconds) except KeyboardInterrupt: print("IoTHubClient sample stopped") return except: print("Unexpected error") time.sleep(4) iot = iothub.IotHub(hubAddress, deviceId, sharedAccessKey) client = mqtt.Client(deviceId, mqtt.MQTTv311) client.on_connect = on_connect client.on_disconnect = on_disconnect client.on_message = on_message client.on_publish = on_publish client.username_pw_set(iot.hubUser, iot.generate_sas_token()) client.tls_set("/etc/ssl/certs/ca-certificates.crt") client.connect(hubAddress, 8883) client.loop_start() publish()
except KeyboardInterrupt: print("IoTHubClient sample stopped") return except: print("Unexpected error") time.sleep(4) if len(sys.argv) == 2: cfg = config.Config(sys.argv[1]) else: cfg = config.Config("config_default.json") mysensor = cfg.sensor.Sensor(cfg.owmApiKey, cfg.owmLocation) iot = iothub.IotHub(cfg.hubAddress, cfg.deviceId, cfg.sharedAccessKey) client = mqtt.Client(cfg.deviceId, mqtt.MQTTv311) client.on_connect = on_connect client.on_disconnect = on_disconnect client.on_message = on_message client.on_publish = on_publish if iotHubMode: client.username_pw_set(iot.hubUser, iot.generate_sas_token()) #client.tls_set("/etc/ssl/certs/ca-certificates.crt") # use builtin cert on Raspbian client.tls_set( "baltimorebase64.cer" ) # Baltimore Cybertrust Root exported from Windows 10 using certlm.msc in base64 format client.connect(cfg.hubAddress, 8883)
from iothub_client import * import time import sys sys.path[0:0] = [ '../common' ] ## path to shared owm.py (open weather map) and iothub.py (azure iot hub python) files import owm import iothub sensorLocation = "Sydney" owmLocation = 'Sydney,AU' iothubConnectionString = 'HostName=IoTCampAU.azure-devices.net;DeviceId=UbuntuPython34;SharedAccessKey=32JQMI0quRp69GEosVSUF2tkdq5r0nzi2lPAn1rZckY=' openWeather = owm.Weather('c204bb28a2f9dc23925f27b9e21296dd', owmLocation) iot = iothub.IotHub(iothubConnectionString) msg_txt = "{\"Geo\":\"%s\",\"Humidity\":%d,\"HPa\":%d,\"Celsius\": %.2f,\"Light\":%d,\"Id\":%d}" def callback(message, properties): print(message) print("Properties: %s" % properties) def iothub_client_sample_run(): id = 0 iot.initialise(callback) while True: try: