Skip to content

esp32: don't fully reset the wifi device #6600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2022

Conversation

jepler
Copy link

@jepler jepler commented Jul 14, 2022

.. this makes reconnecting without a full reset not work. The user initiated wifi flag is now used to track whether import wifi needs to re-enable the wifi interface.

Because this works on other generations of the esp32 (c2, c3, etc), apply this behavior only to esp32.

After this change, it's possible to connect multiple times to wifi in different runs of code.py or the repl after soft rebooting.

Testing procedure: Connect with Thonny. Put wifi deets in .env on device (to open this file when it exists already, use the hamburger menu to select to show hidden files on the target device). Run the following script with F5:

#raise SystemExit

import wifi
import socketpool
import ssl
import os
import adafruit_requests

print(dir())
print(f"connected with address {wifi.radio.ipv4_address=}?")


def print_networks():
    print("networks:")
    for n in wifi.radio.start_scanning_networks(): print(f"{n.ssid:24} {n.rssi: 3}")
    wifi.radio.stop_scanning_networks()

def connect():
    wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))

    print(f"connected with address {wifi.radio.ipv4_address}")

    pool = socketpool.SocketPool(wifi.radio)
    requests = adafruit_requests.Session(pool, ssl.create_default_context())
    print(requests.get("https://httpbin.org/get").json())

def go():
    print_networks()
    connect()
    
if __name__ == '__main__':
    go()

jepler added 2 commits July 14, 2022 15:01
.. this makes reconnecting without a full reset not work.

Because this works on other generations of the esp32 (c2, c3, etc),
apply this behavior only to esp32.

After this change, it's possible to connect multiple times to wifi in
different runs of code.py or the repl after soft rebooting.
.. otherwise, nothing set it enabled on the second soft reboot.
@jepler jepler requested a review from tannewt July 14, 2022 20:10
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit 02c804c into adafruit:main Jul 14, 2022
@Bruno-DaSilva
Copy link

Bruno-DaSilva commented Aug 12, 2022

Hey @jepler should this actually apply to the esp32-c3 as well?

I'm experiencing something similar on my Wemos C3 Mini. First run after reset - this script works:

import wifi

print(dir())
print(f"connected with address {wifi.radio.ipv4_address=}?")

print("networks:")
for n in wifi.radio.start_scanning_networks():
    print(f"{n.ssid:24} {n.rssi: 3}")

wifi.radio.stop_scanning_networks()

print("connecting...")
wifi.radio.tx_power=8.5
wifi.radio.connect('ssid_here', 'passwd_here')
print(f"connected with address {wifi.radio.ipv4_address}")

I see my local networks printed and the connection is successful. I can even re-run this script manually several times in the repl.

When I soft reset (ctrl-d in repl) and try to run this again, it fails. No networks are printed and I receive a ConnectionError: No network with that ssid error.

Is this what you saw with your esp32 chip? And if so, is it expected this same fix would apply to c3's?
If not, I'll keep digging.

@Bruno-DaSilva
Copy link

Can confirm, I went ahead and make a custom bin with this on 223:

--- #ifndef CONFIG_IDF_TARGET_ESP32
+++ #if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3)

And now wifi works between repl resets. How do y'all want to proceed? I can do some leg work on getting a PR up but dunno exactly what approach we want to take here (is this worth a re-think?).

@RetiredWizard
Copy link

RetiredWizard commented Aug 13, 2022

I am able to run @Bruno-DaSilva 's test script multiple times on my QT Py ESP32-C3 without a problem.

I have found that the my C3 doesn't have the network range as some of the other ESP chips and when I'm near it's range limit my network ssid will switch in and out of being listed in a wifi scan.

I did throw some Ctrl-D's between runs and the script still was able to connect.

@Bruno-DaSilva
Copy link

@RetiredWizard well that's no bueno as far as compatibility 😆

Different behaviour between different boards of the same chip, that's fun. Open to guidance on next steps for sure...

@tannewt
Copy link
Member

tannewt commented Aug 15, 2022

@Bruno-DaSilva Please open a new issue. They are easier to track than follow up on than a merged PR. Thanks for helping test on the C3!

@Bruno-DaSilva
Copy link

@tannewt you go it, will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants