Problems with usocket implementations in micropython?

Hello,

I noticed that when we try to connect to the wrong IP with a socket, a strange and incorrect behavior occurs. If we immediately try a connection to a correct IP, we fail to connect, only the third or fourth time we manage to connect.

I use XBXC3 module, Cellular modem, XBC LTE-M / NB-IoT Global, with firmware 11415.
I used the micropython language to send text messages to an MQTT server.
For this I used the umqtt.simple library.

In the attached source code, there are 2 functions: wait_modem_to_connect waiting to connect to the NB-IoT network and the publish_to_mqtt function sending a text message to the server.

In the main program (while loop) the message publishing function is called 4 times.
I used Mosquitto as a test server. Intentionally a second time the server’s IP is wrong.

It is noticed that the timeout error (wrong IP) appears a second time, that is correct.
BUT, this happens for the third time for the next connection, and that is not correct, because here is a correct IP.
This is always the same and does not depend on the state of the server.

I didn’t notice anything in the library code that could fix this behavior.

The question is: do you think it is from the umqtt.simple class or is it a malfunction / possible error in the implementation of the socket / usocket class in the micropython language of the Cellular Xbee module?

The source code and capture is bellow:

MicroPython v1.11-1417-g4092c08 on 2020-02-12; XBC LTE-M/NB-IoT Global with EFR32MG
Type “help()” for more information.
>>>
flash compile mode; Ctrl-C to cancel, Ctrl-D to finish
1^^^ # import modules
2^^^ from umqtt.simple import MQTTClient
3^^^ import network
4^^^ import time
5^^^ import xbee
6^^^
7^^^ # here we start the main program
8^^^ print(“Start program …”)
9^^^ x = xbee.XBee()
10^^^ x.atcmd(‘SM’, 0)
11^^^
12^^^ def wait_modem_to_connect():
13^^^ c = network.Cellular()
14^^^ while not c.isconnected():
15^^^ print(“wait to connect …”)
16^^^ time.sleep(2)
17^^^ print(str(c.ifconfig()))
18^^^
19^^^ def publish_to_mqtt(server, port, topic, message):
20^^^ try:
21^^^ print(“Connect to the MQTT server (” + str(server) + “) …”)
22^^^ mqttClient = MQTTClient(“CLIENT_TEST”, server, port)
23^^^ mqttClient.connect()
24^^^ print(“MQTT server connected”)
25^^^ mqttClient.publish(topic, message, True, 1)
26^^^ mqttClient.disconnect()
27^^^ print(str(message) + " is publish")
28^^^ except Exception as err:
29^^^ print(str(err))
30^^^
31^^^ while True:
32^^^ # wait to connect to NB-IoT
33^^^ wait_modem_to_connect()
34^^^ # publish 4 messages for four times
35^^^ publish_to_mqtt(“5.196.95.208”, 1883, “TEST_MQTT_DIGI 1”, “Message 1”)
36^^^ publish_to_mqtt(“5.196.95.207”, 1883, “TEST_MQTT_DIGI 2”, “Message 2”)
37^^^ publish_to_mqtt(“5.196.95.208”, 1883, “TEST_MQTT_DIGI 3”, “Message 3”)
38^^^ publish_to_mqtt(“5.196.95.208”, 1883, “TEST_MQTT_DIGI 4”, “Message 4”)
39^^^
40^^^ time.sleep(120)
Erasing /flash/main.mpy…
Compiling 1240 bytes of code…
Saved compiled code to /flash/main.mpy (868 bytes).
Automatically run this code at startup [Y/n]? Y
Stored code will run at startup.
Press CTRL-R in the REPL to run the code at any time.

MicroPython v1.11-1417-g4092c08 on 2020-02-12; XBC LTE-M/NB-IoT Global with EFR32MG
Type “help()” for more information.
>>>
Loading /flash/main.mpy…
Running bytecode…
Start program …
wait to connect …
wait to connect …
wait to connect …
wait to connect …
wait to connect …
wait to connect …
wait to connect …
wait to connect …
wait to connect …
(‘10.21.0.141’, ‘255.255.255.255’, ‘0.0.0.0’, ‘0.0.0.0’)
Connect to the MQTT server (5.196.95.208) …
MQTT server connected
Message 1 is publish
Connect to the MQTT server (5.196.95.207) …
[Errno 7110] ETIMEDOUT
Connect to the MQTT server (5.196.95.208) …
[Errno 7110] ETIMEDOUT
Connect to the MQTT server (5.196.95.208) …
MQTT server connected
Message 4 is publish
(‘10.21.0.141’, ‘255.255.255.255’, ‘0.0.0.0’, ‘0.0.0.0’)
Connect to the MQTT server (5.196.95.208) …
MQTT server connected
Message 1 is publish
Connect to the MQTT server (5.196.95.207) …
[Errno 7110] ETIMEDOUT
Connect to the MQTT server (5.196.95.208) …
[Errno 7110] ETIMEDOUT
Connect to the MQTT server (5.196.95.208) …
MQTT server connected
Message 4 is publish
Traceback (most recent call last):
File “”, line 40, in
KeyboardInterrupt:

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

Thank you,
Ionel

Dragomi,

I would suggest using the socket close function

https://www.digi.com/resources/documentation/Digidocs/90002258/#reference/r_frame_socket_0x43.htm%3FTocPath%3DAPI%2520frames|_____21

https://www.digi.com/resources/documentation/digidocs/90002219/default.htm#tasks/t_basic_socket.htm?Highlight=closing%20sockets

This way it will close the undesired socket before you move on to the next one.

Hello, mvut,

I’m trying to force the socket.close () function.
But the behavior is the same.
In fact, I think, because the IP is wrong, the socket fails to connect, so socket.close () does not help.

If you have any other idea I would appreciate it.
Thanks,
Ionel

20^^^ try:
21^^^ print(“Connect to the MQTT server (” + str(server) + “) …”)
22^^^ mqttClient = MQTTClient(“CLIENT_TEST”, server, port)
23^^^ mqttClient.connect()
24^^^ print(“MQTT server connected”)
25^^^ mqttClient.publish(topic, message, True, 1)
26^^^ print(str(message) + " is publish")
27^^^ except Exception as err:
28^^^ print(str(err))
29^^^
30^^^ try:
31^^^ mqttClient.disconnect()
32^^^ except Exception as err:
33^^^ print(str(err))