hackzone / cz20-badge

13 stars 4 forks source link

App's failing to connect to WiFi #12

Closed ijsklont closed 1 year ago

ijsklont commented 4 years ago

Badge allows me to download and update firmware trough WiFi (confirmed working on both home WiFi and phone AP), however apps that require WiFi connection fail to connect to WiFi.

davidweterings commented 4 years ago

I have similiar issues, but mine won't ever connect to my wifi (which is using an emoji for the SSID), but it also won't connect to my phone's hotspot. I tried messing around a bit with the python terminal + wifi module, but no luck so far :)...edit; I found the python api reference https://docs.badge.team/esp32-app-development/api-reference/wifi/

This example (https://docs.badge.team/esp32-app-development/api-reference/wifi/#scanning-for-networks) gives me an error:

>>> sta_if.scan() 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: Wifi Invalid Argument
therealprocyon commented 4 years ago

I figured how it appears to happen: there are 2 possible issues: opening settings again after having set WiFi, or on reboot by using the touch home button.

tjclement commented 4 years ago

@renzenicolai @annejan we might want to look into the sta_if.scan() bug mentioned by @davidweterings.

@therealprocyon can you elaborate what you mean? I'm not sure I follow.

@ijsklont I'm not able to reproduce this, and have no idea what might be causing your problems. Sorry!

therealprocyon commented 4 years ago

@tjclement if you use the home button it may reset wifi, not sure. Could also be opening settings in the browser after having set Wifi, as it blanks out when you reopen it in browser that is.

tjclement commented 4 years ago

Yeah the home button does a full reboot, and then the launcher starts again. This is to start apps from a fresh state always. A consequence is that wifi is not connected then.

The wifi setting being cleared when you open the page again is just on the web side. We don't save the credentials, so they disappear.

therealprocyon commented 4 years ago

that explains it.

therealprocyon commented 4 years ago

might want to add it to the docs...

therealprocyon commented 4 years ago

such as that people will add other ways to exit apps...

therealprocyon commented 4 years ago

that may be the issue. What also could be nifty is having a method of storing wifi stuff on the SD, so it could be read from there.

tjclement commented 4 years ago

I don't think that's what @ijsklont and @davidweterings are reporting: their wifi isn't working at all. Every app that uses wifi initialises it themselves (as they should). For @ijsklont, however, connecting in python apps fails, whereas the OTA update written in C works. That's odd.

therealprocyon commented 4 years ago

ah that's odd... in my case 9/10 times it fails too

annejan commented 4 years ago

that may be the issue. What also could be nifty is having a method of storing wifi stuff on the SD, so it could be read from there.

The WIFI settings are stored in NVS on the badge, the webUSB site doesn't store them . .

I only have badge WiFI issues in places with terrible 2.4 GHz pollution . . where my other (5GHz) devices don't have issues . . Wondering how I can replicate the issues you are having.

ijsklont commented 4 years ago

After updating to 20072205 this morning, i was able to have an app connect to wifi (538 Radio). However after pressing home and test another radio app, the wifi isnt connecting anymore for all the apps.

therealprocyon commented 4 years ago

if I init the app on the launcher via the dashboard, it works. otherwise it doesn't.

stitch commented 4 years ago

My experiences with wifi are solid, with only one workaround: connecting to a personal hotspot from my phone required the hotspot to be restarted when running the app for a second time.

What i did notice was that the wifi routine in many radio apps is flaky at best. It doesn't really try, and the voice sample 'waiting for wifi' isn't always available. So i rewrote it to make a graphical wifi waiting screen that has fewer dependencies. It also tries more times, and has some loading indication (flashing led every attempt). The code for that is shown below.

Hope this helps you:

def animation_wifi_searching():
    # waiting for wifi is a thing. The 'wifi' samples are probably not available. So visualize without deps.

    # draw wifi signal thingy, the top left corner is a status color, that changes color every connection attempt.
    display.drawFill(0xFFFFFF)

    display.drawPixel(0, 2, 0x333333)
    display.drawPixel(1, 1, 0x333333)
    display.drawPixel(2, 1, 0x333333)
    display.drawPixel(3, 2, 0x333333)

    display.flush()

def animation_wifi_searching_progress():
    # used as a progress bar indicator, also helps with feedback when crashing during (WIFI) setup.
    display.drawPixel(0, 0, random.choice([0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF]))
    display.flush()

def connect_wifi():
    print("Creating wifi connection")
    animation_wifi_searching()
    while not wifi.status():
        # show that wifi is expected. The home button still works to exit.
        print("- Attempting to connect to wifi...")
        animation_wifi_searching_progress()
        wifi.connect()
        wifi.wait()
    print("Connected to wifi")

just run:

connect_wifi()

at startup

and at the moment you need wifi:

if not wifi.status():
    connect_wifi()

See original code: https://gitlab.com/stitch1/mod-music-player-cz2020-badge/-/blob/master/__init__.py https://gitlab.com/stitch1/mod-music-player-cz2020-badge/-/blob/master/graphics.py

No13 commented 4 years ago

I was checking newer esp-idf versions, these lib's will do the wifi handling of the ESP chip. Apparently the 'logic' of the ESP will prefer the lowest channel over a better RSSI :-/ You can supply a channel on the sta connect() function but it didn't do anything for me.

You can check which AP uses the lowest channel for your SSID and check if that works. To be continued