0
votes

I am creating an IoT application with Azure IoT Central. The client is written in python and I am using the iotc library (https://pypi.org/project/iotc/) to send / receive messages. The firewall here at my workplace is probably blocking MQTT traffic which is why the python iotc solution is not working.

My setup works over a hotspot, but not over the wired network here at the company I work. So this is why I think our firewall is blocking MQTT traffic over port 8883. According to https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support I should also be able to use MQTT over WebSockets and port 443. I am trying to find a way to use the sample python application from https://github.com/Azure/iot-central-firmware/blob/master/RaspberryPi/app.py that uses Websockets instead of MQTT on port 8883.

This is the demo app from Microsoft. I guess somewhere around the "iotc.connect()" line is where I should try to change from mqtt to websockets

# 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"
deviceKey = "PRIMARY/SECONDARY device KEY"

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

I expect some configuration where I can select the protocol that is used. Unfortunately so far I was not able to find one.

1
for test purpose: Use the MQTTBox client workswithweb.com/html/mqttbox/downloads.html configured for Secure Websockets(WSS) protocol to test connectivity to the Azure IoT Hub (IoT Central) in your workplace.Roman Kiss
Michael , Just checking back!, please let us know if you need further help.SatishBoddu-MSFT

1 Answers

0
votes

Looking at the doc I would assume you need to use setServiceHost(URL)

setServiceHost

set the service endpoint URL

device.setServiceHost(url)
  • url : URL for service endpoint. (default value is global.azure-devices-provisioning.net)

call this before connect

Since this takes a URL you should be able to include a port number e.g.

wss://foo.microsoft.com:443/

(Though 443 should be the default port for wss://)