Konubinix' opinionated web of thoughts

Communications Using Micropython

Fleeting

wifi

The documentation suggest to use that common code to ensure a reproducible configuration.

import network, time

def wifi_reset():   # Reset wifi to AP_IF off, STA_IF on and disconnected
  sta = network.WLAN(network.WLAN.IF_STA); sta.active(False)
  ap = network.WLAN(network.WLAN.IF_AP); ap.active(False)
  sta.active(True)
  while not sta.active():
    time.sleep(0.1)

  sta.disconnect()   # For ESP8266
  while sta.isconnected():
    time.sleep(0.1)

  return sta, ap

espnow

use a device as a gateway to send messages to servers

A common scenario is where one ESPNow device is connected to a wifi router and acts as a proxy for messages from a group of sensors connected via ESPNow:

https://docs.micropython.org/en/latest/library/espnow.html#espnow-and-wifi-operation ([2025-09-19 Fri])

Then, the gateway can connect to the access point like

import network, time, espnow
from wifi.common import wifi_reset
from wifi.stack import connect

def setup():
    sta, ap = wifi_reset()
    connect(sta)
    while not sta.isconnected():
        time.sleep(0.5)
        print("Disable power saving to prevent missing paquets")
        sta.config(pm=sta.PM_NONE)

def conn():
    e = espnow.ESPNow()
    e.active(True)
    return e

And the device sending data could setup like this

from wifi.common import wifi_reset

def setup(channel=None):
    sta, ap = wifi_reset()
    if channel is not None:
        sta.config(channel=6)

def conn(peers=None)
e = espnow.ESPNow()
e.active(True);
    if peers is not None:
        for peer in peers:
            e.add_peer(peer)
    return e

Notes linking here