asynchronous wlan scans

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
sjdh
Posts: 4
Joined: Fri Apr 17, 2020 7:25 am

asynchronous wlan scans

Post by sjdh » Tue Jun 09, 2020 3:10 pm

Hi

I am working on a a WiFi localization project.
With the excellent uasyncio module, I log data from several sensors including gps.
In addition I want to log wireless network scan's. Is it possible make scan's asynchronously? Now it is blocking the rest of the functions.

Also I'd like to have a look at the code for wlan scanning to get a better understanding.
Where is the source located? I am using the ESP32.

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: asynchronous wlan scans

Post by jcw » Tue Jun 09, 2020 3:57 pm

I don't know how it works, but it looks like the code you're after can be found here:

https://github.com/micropython/micropyt ... #L439-L473

sjdh
Posts: 4
Joined: Fri Apr 17, 2020 7:25 am

Re: asynchronous wlan scans

Post by sjdh » Tue Jun 09, 2020 9:44 pm

Thanks jcw. That looks like the part where the scanning happens.

For the esp8266, this ardruino example from shows asynchronous scanning for networks.
https://arduino-esp8266.readthedocs.io/ ... mples.html

Code: Select all

  
  
  // trigger Wi-Fi network scan
  if (currentMillis - lastScanMillis > SCAN_PERIOD)
  {
    WiFi.scanNetworks(true);
    Serial.print("\nScan start ... ");
    lastScanMillis = currentMillis;
  }

  // print out Wi-Fi network scan result uppon completion
  int n = WiFi.scanComplete();
  if(n >= 0)
  {
    Serial.printf("%d network(s) found\n", n);
    for (int i = 0; i < n; i++)
    {
      Serial.printf("%d: %s, Ch:%d (%ddBm) %s\n", i+1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "");
    }
    WiFi.scanDelete();
   
How would you write equivalents for WiFi.scanNetworks() and WiFi.scanComplete() for micropython?

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: asynchronous wlan scans

Post by jcw » Tue Jun 09, 2020 10:22 pm

Dunno, sorry.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: asynchronous wlan scans

Post by jimmo » Wed Jun 10, 2020 6:43 am

sjdh wrote:
Tue Jun 09, 2020 9:44 pm
How would you write equivalents for WiFi.scanNetworks() and WiFi.scanComplete() for micropython?
There's a couple of options here --

1. Make modnetwork "asyncio-aware". i.e. scan() would be something you could use with "async for"

Code: Select all

async def do_scan():
  async for result in sta_if.scan():
    handle(result)
I don't think there are any examples of this currently in the firmware. Although it would be cool to see more stuff starting to work that way, and I'd love to see asyncio become a more fundemental part of most of the core APIs (e.g. machine). e.g. "await pin" to wait for a pin edge.

2. Make modnetwork irq/callback-based. This is what we did in modbluetooth.

Code: Select all

def handler(event, data):
  if event == IRQ_SCAN_RESULT:
    handle(data)
  elif event == IRQ_SCAN_COMPLETE:
    handle(None)

ble.irq(handler)
ble.gap_scan(...)
However there's currently no way to then make this into something you can use from asyncio (but it's being actively worked on right now, see https://github.com/micropython/micropython/pull/6125/ and https://github.com/micropython/micropython/pull/6106/ ).

sjdh
Posts: 4
Joined: Fri Apr 17, 2020 7:25 am

Re: asynchronous wlan scans

Post by sjdh » Wed Jun 10, 2020 4:17 pm

Thanks
jimmo wrote:
Wed Jun 10, 2020 6:43 am

Code: Select all

async def do_scan():
  async for result in sta_if.scan():
    handle(result)
This is what I would love to use. It is not there, but how about your toy example

[/quote]
jimmo wrote:
Wed Jun 10, 2020 6:43 am
t would be cool to see more stuff starting to work that way, and I'd love to see asyncio become a more fundemental part of most of the core APIs (e.g. machine). e.g. "await pin" to wait for a pin edge.
how would you design this? are there any obstacles?

More general, if you want to get started with development of c code for micropython, where can you start? Are there perhaps some warm-up examples to get something very basic done? Or some other resources?

Post Reply