std-training icon indicating copy to clipboard operation
std-training copied to clipboard

fix(bsc): wifi connection; need to wait until the status is right

Open amanjeev opened this issue 3 years ago • 8 comments
trafficstars

I was trying to flash my esp32-c3 board and while I was successful at that, my wifi did not connect.

It turns out that the code in the bsc vendoered lib did not lend way to check for the status in a loop until it is either connected of disconnected. This is unhelpful because there are three states of the connection status enum and one of them is Connecting which may take a few ms.

Caveat: I am new to this so I did what I thought was the right thing. Please help improve this if you think I made a mistake.

amanjeev avatar Jul 27 '22 19:07 amanjeev

This works now, thanks to you all - @Dajamante , @justahero and @SergioGasquez. I see wifi connecting and the output is

…dware-check  on  amanjeev/flashing-and-wifi-connection is 📦 v0.1.0 via 🦀 v1.61.0-nightly took 28s 
at 13:57:29  ❤  RUST_LOG=info cargo espflash --release --monitor /dev/ttyACM0
Serial port: /dev/ttyACM0
Connecting...

    Finished release [optimized] target(s) in 0.06s
Chip type:         ESP32-C3 (revision 3)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi
MAC address:       60:55:f9:c0:1f:c0
[00:00:00] ########################################      12/12      segment 0x0
[00:00:00] ########################################       1/1       segment 0x8000
[00:00:07] ########################################     412/412     segment 0x10000

Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit
......snip.......
I (7361) esp_idf_svc::wifi: STA IP event DhcpIpAssigned(DhcpIpAssignment { netif_handle: 0x3fca477c, ip_settings: ClientSettings { ip: 192.168.1.148, subnet: Subnet { gateway: 192.168.1.1, mask: Mask(24) }, dns: None, secondary_dns: None }, ip_changed: true }) handled, set status: Status(Started(Connected(Done(ClientSettings { ip: 192.168.1.148, subnet: Subnet { gateway: 192.168.1.1, mask: Mask(24) }, dns: None, secondary_dns: None }))), Stopped)
I (7401) esp_netif_handlers: sta ip: 192.168.1.148, mask: 255.255.255.0, gw: 192.168.1.1
I (7411) esp_idf_svc::wifi: Waiting for status done - success
I (7421) esp_idf_svc::wifi: Providing status: Status(Started(Connected(Done(ClientSettings { ip: 192.168.1.148, subnet: Subnet { gateway: 192.168.1.1, mask: Mask(24) }, dns: None, secondary_dns: None }))), Stopped)
I (7431) esp32_c3_dkc02_bsc::wifi: Wifi connected
I (8441) hardware_check: Hello, world!
I (10441) hardware_check: Hello, world!
I (12441) hardware_check: Hello, world!
I (14441) hardware_check: Hello, world!

@BriocheBerlin , @Mirabellensaft could you please try my branch too and maybe it works?

@Dajamante maybe flashing may work for you this time?

amanjeev avatar Jul 29 '22 18:07 amanjeev

I am not sure why there is a cargo deps conflict in the docker check test run. this does not happen to me on my local.

amanjeev avatar Jul 29 '22 19:07 amanjeev

The conflict doesn't show when running intro/hardware-check or intro/hello-world, but it does for me when trying to build intro/http-client/exercise

9names avatar Jul 30 '22 05:07 9names

I am not sure why there is a cargo deps conflict in the docker check test run. this does not happen to me on my local.

You may need to update the dependencies in all the projects, as common/lib/esp32-c3-dkc02-bsc crate gets used in all of them (or most of them, if I recall properly). I am currently ooo but looking at the modified files and with @9names comment it looks like that should be the issue.

SergioGasquez avatar Jul 30 '22 07:07 SergioGasquez

I made a PR against this PR branch, forgot that the branch was on the main repo - I'm more used to the fork and branch model

9names avatar Jul 30 '22 08:07 9names

with this branch, my wifi works! Can you rebase, so it's on the same state as main, minus your changes? Just to make sure it fixed the right issue

Mirabellensaft avatar Aug 02 '22 15:08 Mirabellensaft

@Mirabellensaft I rebased and pushed but now i see stuff :see_no_evil: i did not commit...

amanjeev avatar Aug 02 '22 22:08 amanjeev

Hello! I was ooo for a few days, can someone update me on the state of this PR? Why it was changed to draft?

Thanks in advance!

SergioGasquez avatar Aug 09 '22 09:08 SergioGasquez

@SergioGasquez it was changed to draft primarily because this PR/branch is being used to test the material internally. Until our checks are all green, it was suggested this be draft. Fear not, this will be merged once all the folks at FS have finished their checks.

cc/ @Mirabellensaft @justahero

amanjeev avatar Aug 11 '22 13:08 amanjeev

@amanjeev Thanks for the update! I think merging https://github.com/ferrous-systems/espressif-trainings/pull/85 would make the CI tests pass.

SergioGasquez avatar Aug 11 '22 13:08 SergioGasquez

@SergioGasquez we have pinged on the #85 already but not sure why #85 is a draft as well?

amanjeev avatar Aug 11 '22 16:08 amanjeev

FYI, I have a related issue, which this PR seems to PARTIALY fix.

Currently, when flashing/monitoring via the USB/JTAG port, the Wifi dosn't connect.

  • Stopping the monitor and resetting board, WIFI connects fine
  • using the normal USB port with monitor, WIFI connects fine
  • Using USB/JTAG, Wifi won't connect with either monitor running, or OpenOCD Debug running.

Testing with this PR, USB/JTAG with monitor, Wifi now connects about 75% of the time, (three out of four tries it works, one it fails.) So much more reliable, but not always.

Can actually debug now with this PR, with Wifi connecting. (generally).

Trace from a fail in case it is usefull: I (3572) esp_idf_svc::wifi: About to wait 2100s for status I (3572) wifi:state: assoc -> run (10) I (3592) esp_idf_svc::wifi: Timeout while waiting for status I (3592) wifi:connected with Gumtrees, aid = 2, channel 1, BW20, bssid = 78:8a:20:da:6f:97 I (3602) wifi:security: WPA2-PSK, phy: bgn, rssi: -51 I (3602) wifi:pm start, type: 1 I (3602) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 0, mt_pti: 25000, mt_time: 10000 I (3612) esp_idf_svc::wifi: Got wifi event: StaConnected I (3622) esp_idf_svc::wifi: STA event StaConnected handled, set status: Status(Started(Connected(Waiting)), Stopped) I (3632) esp_idf_svc::wifi: Stopping I (3632) wifi:state: run -> init (0) I (3642) wifi:pm stop, total sleep time: 0 us / 33289 us I (3642) wifi:new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1 I (3652) esp_idf_svc::wifi: Disconnect requested I (3662) wifi:flush txq I (3662) wifi:stop sw txq I (3662) wifi:lmac stop hw txq I (3662) esp_idf_svc::wifi: Stop requested I (3672) esp_idf_svc::wifi: About to wait for status I (3672) esp_idf_svc::wifi: Got wifi event: StaDisconnected I (3682) esp_idf_svc::wifi: STA event StaDisconnected handled, set status: Status(Started(Disconnected), Stopped) I (3692) esp_idf_svc::wifi: Got wifi event: StaStopped I (3702) esp_idf_svc::wifi: STA event StaStopped handled, set status: Status(Stopped, Stopped) I (3702) esp_idf_svc::wifi: Waiting for status done - success I (3712) esp_idf_svc::wifi: Stopped I (3722) esp_idf_svc::wifi: Event handlers deregistered I (3722) wifi:Deinit lldesc rx mblock:10 I (3732) esp_idf_svc::wifi: Driver deinitialized I (3732) esp_idf_svc::wifi: Deinitialization complete I (3742) esp_idf_svc::wifi: Dropped I (3742) esp_idf_svc::nvs: Dropped I (3742) esp_idf_svc::eventloop: Dropped I (3752) esp_idf_svc::netif: Dropped Error: could not connect to Wi-Fi network: Unexpected Wifi status (Transitional state): Status(Started(Connecting), Stopped)

Note: There is no 'delay' when it is waiting for status, it seems to blow right through that line. Certainly dosn't wait 2100s !

markfarnan avatar Aug 11 '22 20:08 markfarnan

Solved by #99

Dajamante avatar Sep 06 '22 09:09 Dajamante