Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Adafruit io Arduino WIFI reconnect issue
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Re: Adafruit io Arduino WIFI reconnect issue

by Systembolaget on Wed Jun 17, 2020 4:25 pm

Your new post came in as I typed the below. Too bad one can't edit one's prior posts : (

Directly after WiFi.disconnect(); on line 111, insert a new line with WiFi.end(); and try first with your smartphone, then your router. You should get a serial output like this:

Connected battery and the program begins to run

22:07:25.232 -> Trying to connect to WLAN router
22:07:31.105 -> 4
22:07:31.105 -> Connection to WLAN router successful
22:07:31.138 -> MAC address: A4:CF:12:12:78:7C
22:07:31.172 -> SSID:
22:07:31.172 -> IP Address: 0.0.0.0
22:07:31.206 -> Subnet mask: 0.0.0.0
22:07:31.242 -> Gateway IP: 0.0.0.0
22:07:31.242 -> Signal strength (RSSI): 0 dBm
22:07:31.276 -> Trying to connect to WLAN router
22:07:42.122 -> 3
22:07:42.122 -> Connection to WLAN router successful
22:07:42.156 -> MAC address: A4:CF:12:12:78:7C
22:07:42.189 -> SSID: Smartphone
22:07:42.223 -> IP Address: #
22:07:42.223 -> Subnet mask: 255.255.255.0
22:07:42.256 -> Gateway IP: #
22:07:42.291 -> Signal strength (RSSI): -53 dBm

WLAN and Internet connection established

22:07:42.325 -> Trying to connect to Adafruit IO
22:07:42.358 -> Connecting to: io.adafruit.com
22:07:45.741 -> Connect result: 1
22:07:45.774 -> MQTT connect packet:
22:07:45.808 -> [0x10], = [0x3D], [0x00], [0x04], M [0x4D], Q [0x51], T [0x54], T [0x54],
22:07:45.875 -> Had to delete rest of packet to obfuscate my user name
22:07:46.489 -> Client sendPacket returned: 63
22:07:46.798 -> Read data: [0x20],
22:07:46.798 -> Packet Type: [0x20],
22:07:46.832 -> Read data: [0x02],
22:07:46.865 -> Packet Length: 2
22:07:46.899 -> Read data: [0x00], [0x00],
22:07:48.870 -> Connection to Adafruit IO successful

AIO connection established

22:07:48.903 -> MQTT publish packet:
22:07:48.903 -> 0 [0x30], # [0x23], [0x00], [0x1E], Had to delete rest of packet to obfuscate my user name
22:07:49.309 -> Client sendPacket returned: 37
22:07:49.343 -> Data published to AIO

First dummy data published

22:07:59.307 -> MQTT publish packet:
22:07:59.307 -> 0 [0x30], # [0x23], [0x00], [0x1E], Had to delete rest of packet to obfuscate my user name
22:07:59.698 -> Client sendPacket returned: 37
22:07:59.734 -> Data published to AIO
22:08:09.708 -> MQTT publish packet:
22:08:09.708 -> 0 [0x30], # [0x23], [0x00], [0x1E], Had to delete rest of packet to obfuscate my user name
22:08:10.114 -> Client sendPacket returned: 37
22:08:10.147 -> Data published to AIO

Disabled WLAN router (smartphone) and re-enabled 10 seconds later

22:08:20.102 -> Trying to connect to WLAN router
22:08:25.974 -> 4
22:08:25.974 -> Connection to WLAN router successful
22:08:26.007 -> MAC address: A4:CF:12:12:78:7C
22:08:26.041 -> SSID:
22:08:26.041 -> IP Address: 0.0.0.0
22:08:26.074 -> Subnet mask: 0.0.0.0
22:08:26.108 -> Gateway IP: 0.0.0.0
22:08:26.108 -> Signal strength (RSSI): 0 dBm
22:08:26.142 -> MQTT publish packet:
22:08:26.176 -> 0 [0x30], " [0x22], [0x00], [0x1E], Had to delete rest of packet to obfuscate my user name

Publishing fails in MQTT function as it should

22:08:26.552 -> Connection failed!
22:08:26.590 -> Data published to AIO
22:08:36.537 -> Trying to connect to WLAN router
22:08:47.388 -> 3
22:08:47.388 -> Connection to WLAN router successful
22:08:47.422 -> MAC address: A4:CF:12:12:78:7C
22:08:47.458 -> SSID: Smartphone
22:08:47.491 -> IP Address: #
22:08:47.524 -> Subnet mask: 255.255.255.0
22:08:47.557 -> Gateway IP: #
22:08:47.557 -> Signal strength (RSSI): -51 dBm

WLAN and Internet connection re-established

22:08:47.591 -> Trying to connect to Adafruit IO
22:08:47.625 -> Connecting to: io.adafruit.com
22:08:51.070 -> Connect result: 1
22:08:51.104 -> MQTT connect packet:
22:08:51.137 -> [0x10], = [0x3D], [0x00], [0x04], M [0x4D], Q [0x51], T [0x54], T [0x54],
22:08:51.207 -> Had to delete rest of packet to obfuscate my user name
22:08:51.814 -> Client sendPacket returned: 63
22:08:51.886 -> Read data: [0x20],
22:08:51.919 -> Packet Type: [0x20],
22:08:51.953 -> Read data: [0x02],
22:08:51.986 -> Packet Length: 2
22:08:51.986 -> Read data: [0x00], [0x00],
22:08:53.963 -> Connection to Adafruit IO successful

AIO connection re-established

22:08:53.996 -> MQTT publish packet:
22:08:54.029 -> 0 [0x30], # [0x23], [0x00], [0x1E], Had to delete rest of packet to obfuscate my user name
22:08:54.410 -> Client sendPacket returned: 37
22:08:54.444 -> Data published to AIO

And AIO publishing of dummy data is resumed...

Systembolaget
 
Posts: 251
Joined: Wed Mar 08, 2017 1:01 pm

Re: Adafruit io Arduino WIFI reconnect issue

by jdamelio on Wed Jun 17, 2020 4:42 pm

Oooooh you are onto something there. Adding WiFi.end() gets me reliable re-connections in that sketch. I will test more with this tonight. I don't know what was going wrong that wifi.end is cleaning up, but I like it!

jdamelio
 
Posts: 15
Joined: Mon Mar 30, 2020 10:18 pm

Re: Adafruit io Arduino WIFI reconnect issue

by jdamelio on Wed Jun 17, 2020 10:29 pm

Yep, Wifi.end() before Wifi.begin() seems to fix this for all cases I have tested so far. I don't know what was wrong to begin with, but for a workaround this is simple and reliable! Thanks so much for the help!

jdamelio
 
Posts: 15
Joined: Mon Mar 30, 2020 10:18 pm

Re: Adafruit io Arduino WIFI reconnect issue

by Systembolaget on Thu Jun 18, 2020 2:34 am

What was wrong is that I forgot WiFi.end(), lol. The sequence of what happens when is important. Glad to hear it works for you. Happy AIO publishing.

WiFiNINA's library has the WiFi.end() method in WiFi.cpp WiFiDrv::wifiDriverDeinit(); which points to wifi_drv.cpp wifi.drv SpiDrv::end(); which in the end points to spi_drv.cpp spi.drv initialized = false;

Adafruit should maybe offer the "two functions" and "state machine" reconnection approaches on their AIO/WiFi tutorials, as the examples only connect in setup() and that is not very practical; automatic WiFi and MQTT/AIO reconnection is important. Maybe I make a tutorial they can link to, with explanations why, what and when.

Systembolaget
 
Posts: 251
Joined: Wed Mar 08, 2017 1:01 pm

Re: Adafruit io Arduino WIFI reconnect issue

by Systembolaget on Thu Jun 18, 2020 10:19 am

Oops, these lines

Code: Select all | TOGGLE FULL SIZE
// Status must be set to idle for when WiFi.begin() is called for
// the first time; it remains so until the WLAN connection attempt
// fails, succeeds or is re-established
int status = WL_IDLE_STATUS;

...

status = WiFi.begin(WLAN_SSID, WLAN_PASS);
make no more sense with this code and can be deleted, as there is no status variable necessary the way this works.

Systembolaget
 
Posts: 251
Joined: Wed Mar 08, 2017 1:01 pm

Re: Adafruit io Arduino WIFI reconnect issue

by brubell on Fri Jun 19, 2020 10:07 am

The second wifi connection succeeding while the second mqtt connection failing (13:04:33.837 -> Second MQTT connecting:-1) is a bit odd to me.

Could you print if the connection status of MQTTClient returns True prior to reconnecting ("second mqtt connecting")? (https://github.com/adafruit/Adafruit_MQ ... nt.cpp#L45).

It's possible that you may need to perform mqtt. disconnectServer() after disconnecting, and prior to reconnecting to first stop the client.

brubell
 
Posts: 946
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruit io Arduino WIFI reconnect issue

by jdamelio on Fri Jun 19, 2020 12:46 pm

Will check tonight what mqtt.connected() returns after the second wifi.begin(), but before the following mqtt.connect().

jdamelio
 
Posts: 15
Joined: Mon Mar 30, 2020 10:18 pm

Re: Adafruit io Arduino WIFI reconnect issue

by jdamelio on Fri Jun 19, 2020 9:22 pm

Here we check mqtt.connected before the second mqtt.connect.

You can see it actually makes the wifi connection fail one cycle earlier.

18:12:03.026 ->
18:12:05.043 -> Wifi connecting:3
18:12:20.931 -> MAC address: 3C:71:BF:77:9D:08
18:12:20.931 -> SSID: AndroidAP_1929
18:12:20.931 -> IP Address: 192.168.43.136
18:12:20.931 -> Subnet mask: 255.255.255.0
18:12:20.931 -> Gateway IP: 192.168.43.189
18:12:20.931 -> Signal strength (RSSI): -43 dBm
18:12:20.931 -> MQTT connecting:0
18:12:32.808 -> Data published to AIO
18:12:37.824 -> test operator disable wifi
18:12:42.841 -> test operator enable wifi
18:12:52.850 -> Second Wifi connecting:3
18:13:02.872 -> MAC address: 3C:71:BF:77:9D:08
18:13:02.872 -> SSID: AndroidAP_1929
18:13:02.872 -> IP Address: 192.168.43.136
18:13:02.872 -> Subnet mask: 255.255.255.0
18:13:02.872 -> Gateway IP: 192.168.43.189
18:13:02.872 -> Signal strength (RSSI): -40 dBm
18:13:02.872 -> MQTT status prior to second mqtt.connect():-1
18:13:31.642 -> Second MQTT connecting:-1
18:13:46.771 -> Data published to AIO
18:13:51.745 -> Third Wifi connecting:4
18:13:57.302 -> MAC address: 3C:71:BF:77:9D:08
18:13:57.302 -> SSID:
18:13:57.302 -> IP Address: 192.168.43.136
18:13:57.302 -> Subnet mask: 255.255.255.0
18:13:57.302 -> Gateway IP: 192.168.43.189
18:13:57.302 -> Signal strength (RSSI): 0 dBm
18:13:57.302 -> Third MQTT connecting:No Socket available
18:13:57.337 -> -1
18:14:02.325 -> Data published to AIO
18:14:07.331 -> Fourth Wifi connecting:4
18:14:12.336 -> MAC address: 3C:71:BF:77:9D:08
18:14:12.336 -> SSID:
18:14:12.336 -> IP Address: 192.168.43.136
18:14:12.336 -> Subnet mask: 255.255.255.0
18:14:12.336 -> Gateway IP: 192.168.43.189
18:14:12.336 -> Signal strength (RSSI): 0 dBm
18:14:12.336 -> Fourth MQTT connecting:No Socket available
18:14:12.336 -> -1
18:14:17.341 -> Data published to AIO
18:14:22.348 -> Test complete
18:14:23.351 -> Test complete
18:14:24.357 -> Test complete
18:14:25.342 -> Test complete

jdamelio
 
Posts: 15
Joined: Mon Mar 30, 2020 10:18 pm

Re: Adafruit io Arduino WIFI reconnect issue

by jdamelio on Sat Jun 20, 2020 12:55 am

Oops, in the previous post I made a copy paste error and added a call to connect, not connected. Here is the actual output for what you asked for:

21:45:41.947 ->
21:45:43.942 -> Wifi connecting:3
21:45:59.847 -> MAC address: 3C:71:BF:77:9D:08
21:45:59.847 -> SSID: AndroidAP_1929
21:45:59.847 -> IP Address: 192.168.43.136
21:45:59.847 -> Subnet mask: 255.255.255.0
21:45:59.847 -> Gateway IP: 192.168.43.208
21:45:59.847 -> Signal strength (RSSI): -57 dBm
21:45:59.847 -> MQTT connecting:0
21:46:09.359 -> Data published to AIO
21:46:14.366 -> test operator disable wifi
21:46:19.351 -> test operator enable wifi
21:46:29.397 -> Second Wifi connecting:3
21:46:39.409 -> MAC address: 3C:71:BF:77:9D:08
21:46:39.409 -> SSID: AndroidAP_1929
21:46:39.409 -> IP Address: 192.168.43.136
21:46:39.409 -> Subnet mask: 255.255.255.0
21:46:39.409 -> Gateway IP: 192.168.43.208
21:46:39.446 -> Signal strength (RSSI): -53 dBm
21:46:39.446 -> MQTT status prior to second mqtt.connect():1
21:46:39.446 -> Second MQTT connecting:-1
21:47:12.899 -> Data published to AIO
21:47:17.916 -> Third Wifi connecting:3
21:47:27.974 -> MAC address: 3C:71:BF:77:9D:08
21:47:27.974 -> SSID: AndroidAP_1929
21:47:27.974 -> IP Address: 192.168.43.136
21:47:27.974 -> Subnet mask: 255.255.255.0
21:47:27.974 -> Gateway IP: 192.168.43.208
21:47:27.974 -> Signal strength (RSSI): -53 dBm
21:47:27.974 -> Third MQTT connecting:-1
21:47:43.111 -> Data published to AIO
21:47:48.109 -> Fourth Wifi connecting:4
21:47:53.670 -> MAC address: 3C:71:BF:77:9D:08
21:47:53.670 -> SSID:
21:47:53.670 -> IP Address: 192.168.43.136
21:47:53.670 -> Subnet mask: 255.255.255.0
21:47:53.670 -> Gateway IP: 192.168.43.208
21:47:53.670 -> Signal strength (RSSI): 0 dBm
21:47:53.670 -> Fourth MQTT connecting:No Socket available
21:47:53.670 -> -1
21:47:58.674 -> Data published to AIO
21:48:03.691 -> Test complete
21:48:04.675 -> Test complete
21:48:05.696 -> Test complete

jdamelio
 
Posts: 15
Joined: Mon Mar 30, 2020 10:18 pm

Please be positive and constructive with your questions and comments.