Home/Support/Support Forum/azure iot hub connection problems
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

azure iot hub connection problems

0 votes
Hello!

I want to connect to Microsoft Azure IoT hub with the python example, but have no success:

I am not able to connect to microsoft azure server. I just get this output:
Waiting for network connection...
Network connected
Connecting to Azure...
[MY_HUB].azure-devices.net
8883
Traceback (most recent call last):
File "/flash/main.py", line 206, in <module>
File "/flash/main.py", line 187, in main
File "/flash/main.py", line 58, in setup
File "/flash/main.py", line 86, in _connect
File "/flash/main.py", line 72, in _connect
File "/flash/lib/umqtt/simple.py", line 74, in connect
OSError: [Errno 7006] ENXIO

MicroPython v1.11-1417-g4092c08 on 2020-02-12; XBC LTE-M/NB-IoT Global with EFR32MG
Type "help()" for more information.
>>>

When changing the server in self.mqtt_client in AzureMQTT class to the IP Address, it exits to this:
Loading /flash/main.mpy...
Running bytecode...
Network connected
Connecting to Azure...
[IP-ADDRESS]
8883
0x9 b'04:4d:51:54:54:04:c2:00:00'
Traceback (most recent call last):
File "/flash/main.py", line 206, in <module>
File "/flash/main.py", line 187, in main
File "/flash/main.py", line 58, in setup
File "/flash/main.py", line 86, in _connect
File "/flash/main.py", line 84, in _connect
MQTTException: 5: The client is not authorized to connect.

MicroPython v1.11-1417-g4092c08 on 2020-02-12; XBC LTE-M/NB-IoT Global with EFR32MG
Type "help()" for more information.
>>>

I have tried different providers (SIM cards) without success.
Connecting to the echo server example is working.

Do you have any ideas what is wrong?

What is going wrong here?

Thank you,
asked May 19 in Digi Connect Cellular by installman New to the Community (1 point)

Please log in or register to answer this question.

2 Answers

0 votes
This is most likely do to you using NBiOT which does not support TLS connections.
answered May 19 by mvut Veteran of the Digi Community (13,083 points)
0 votes
After longer research I found a solution for the second problem (MQTTException 5). It is not a problem with TLS connection, it is the generate_sas_token function:
You need to add "/devices/"+self.params["DeviceId"] as uri parameter so that the function call looks like this:
self.sas_token = generate_sas_token(self.params["HostName"]+"/devices/"+self.params["DeviceId"], self.params["SharedAccessKey"], policy_name=policy_name, expiry=expiry)

Also in the function I changed the line
ttl = time() + expiry + 946684800
to
ttl = time() + expiry + 1590094978

Now it connects to Azure IoT hub.

Only the first problem which seems to be a DNS lookup problem is open. I wanted to hardcode the DNS IP to e.g. 8.8.8.8 (google DNS) but have not found a way to do that.
Maybe someone has an idea?
answered May 22 by installman New to the Community (1 point)
...