Asyncio and WebREPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Divergentti
Posts: 66
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Asyncio and WebREPL

Post by Divergentti » Mon Nov 02, 2020 1:17 pm

This is propably one of those stupid questions, but anyways ... is there way to check webrepl status?

I converted my motion sensor script to asyncio and for some reason webrepl, which is activated in the boot.py, is not responsive anymore.

Command: help (webrepl)

gives me:

object <module 'webrepl' from 'webrepl.py'> is of type module
start_foreground -- <function start_foreground at 0x3ffe5600>
__file__ -- webrepl.py
websocket_helper -- <module 'websocket_helper' from 'websocket_helper.py'>
accept_conn -- <function accept_conn at 0x3ffe5390>
setup_conn -- <function setup_conn at 0x3ffe5340>
client_s -- None
stop -- <function stop at 0x3ffe53a0>
uwebsocket -- <module 'uwebsocket'>
__name__ -- webrepl
socket -- <module 'usocket'>
_webrepl -- <module '_webrepl'>
listen_s -- <socket>
network -- <module 'network'>
start -- <function start at 0x3ffe5400>
uos -- <module 'uos'>

so, how do I implement check in the main.py so that I can check if the webrepl is already running? Start I can do with webrepl.start() if not started already.

User avatar
MostlyHarmless
Posts: 163
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Asyncio and WebREPL

Post by MostlyHarmless » Mon Nov 02, 2020 6:27 pm

It works here, and not only that. I have a picoweb application with 4 additional uasyncio tasks plus webrepl, utelnetserver and uftpd all running happily together on one ESP32.

Are you sure that webrepl is being started? The setup normally just tacks the import and start() at the end of boot.py. If your original boot.py imported main.py AND that main.py import never finishes (because it calls uasyncio.run() for a never ending task for example), then it isn't started. You should then get a "connection refused" if you try to connect to port 8266 via something like telnet or nc.


Regards, Jan

Divergentti
Posts: 66
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Re: Asyncio and WebREPL

Post by Divergentti » Wed Nov 04, 2020 11:01 am

MostlyHarmless wrote:
Mon Nov 02, 2020 6:27 pm
It works here, and not only that. I have a picoweb application with 4 additional uasyncio tasks plus webrepl, utelnetserver and uftpd all running happily together on one ESP32.

Are you sure that webrepl is being started? The setup normally just tacks the import and start() at the end of boot.py. If your original boot.py imported main.py AND that main.py import never finishes (because it calls uasyncio.run() for a never ending task for example), then it isn't started. You should then get a "connection refused" if you try to connect to port 8266 via something like telnet or nc.


Regards, Jan
I am sure that boot.py start the webrepl, and boot.py and the webrepl works with other async setups like async mqtt.

This problem device is different. From USB serial everything seems to boot up and operate normally, but when I reboot with machine.reset() and try with webrepl, no connection and indeed, connection is refused for telnet ip:8266. I will erase flash and try again or use another dev board.

My boot.py:

Code: Select all

import utime
import machine
import network
import time
import ntptime
import webrepl
from time import sleep

#  0 = power on, 6 = hard reset, 1 = WDT reset, 5 = DEEP_SLEEP reset, 4 soft reset
print("Edellisen bootin syy %s" % machine.reset_cause())

try:
    from parametrit import SSID1, SSID2, SALASANA1, SALASANA2, WEBREPL_SALASANA, NTPPALVELIN, DHCP_NIMI
except ImportError as e:
    print("Tuontivirhe %s", e)
    if (SSID1 is not None) and (SALASANA1 is not None):
        SSID2 = None
        SALASANA2 = None
        WEBREPL_SALASANA = None
        NTPPALVELIN = None
        DHCP_NIMI = None
    else:
        print("Vaaditaan minim SSID1 ja SALASANA!")
        raise


def ei_voida_yhdistaa():
    print("Yhteys ei onnistu. Bootataan 1 s. kuluttua")
    sleep(1)
    machine.reset()


def aseta_aika():
    try:
        ntptime.settime()
    except OSError as e:
        print("NTP-palvelimelta %s ei saatu aikaa! Virhe %s" % (NTPPALVELIN, e))
        ei_voida_yhdistaa()
    print("Aika: %s " % str(utime.localtime(utime.time())))


def kaynnista_webrepl():
    if WEBREPL_SALASANA is not None:
        try:
            webrepl.start(password=WEBREPL_SALASANA)
        except OSError:
            pass
    else:
        try:
            webrepl.start()
        except OSError as e:
            print("WebREPL ei kaynnisty. Virhe %s" % e)
            raise Exception("WebREPL ei ole asenettu! Suorita import webrepl_setup")


wificlient_if = network.WLAN(network.STA_IF)


#  Ollaan jo yhteydessä 1, 2 tai 4 resetin vuoksi
if wificlient_if.config('essid') != '':
    print("Yhteydessä verkkoon %s" % network.WLAN(network.STA_IF).config('essid'))
    print('Laitteen IP-osoite:', network.WLAN(network.STA_IF).ifconfig()[0])
    print("WiFi-verkon signaalitaso %s" % (network.WLAN(network.STA_IF).status('rssi')))
    aseta_aika()
    kaynnista_webrepl()
else:
    etsi_lista = []
    ssid_lista = []
    kaytettava_salasana = ""
    kaytettava_ssid = ""
    wificlient_if = network.WLAN(network.STA_IF)
    wificlient_if.active(False)
    time.sleep(1)
    wificlient_if.active(True)
    time.sleep(2)
    if DHCP_NIMI is not None:
        wificlient_if.config(dhcp_hostname=DHCP_NIMI)
    if NTPPALVELIN is not None:
        ntptime.host = NTPPALVELIN
    try:
        ssid_lista = wificlient_if.scan()
        time.sleep(3)
    except KeyboardInterrupt:
        raise
    except ssid_lista == []:
        print("WiFi-verkkoja ei löydy!")
        ei_voida_yhdistaa()
    except OSError:
        ei_voida_yhdistaa()
        #  Katsotaan löytyvätkö SSID1 ja SSID2 listalta
    try:
        etsi_lista = [item for item in ssid_lista if item[0].decode() == SSID1 or item[0].decode() == SSID2]
    except ValueError:
        # SSDI ei löydy
        print("Etsittyä WiFi-verkkoja ei löydy!")
        ei_voida_yhdistaa()
    # Mikäli listan pituus on 2, silloin löytyi molemmat ja valitaan voimakkain, muuten valitaan vain se joka löytyi
    if len(etsi_lista) == 2:
        #  kolmas lopusta on signaalinvoimakkuus rssi
        if etsi_lista[0][-3] > etsi_lista[1][-3]:
            kaytettava_ssid = etsi_lista[0][0].decode()
            kaytettava_salasana = SALASANA1
        else:
            kaytettava_ssid = etsi_lista[1][0].decode()
            kaytettava_salasana = SALASANA2
    else:
        # vain yksi listalla
        kaytettava_ssid = etsi_lista[0][0].decode()
        kaytettava_salasana = SALASANA1
    # machine.freq(240000000)

    print("Yhdistetään verkkoon %s" % kaytettava_ssid)
    try:
        wificlient_if.connect(kaytettava_ssid, kaytettava_salasana)
        time.sleep(5)
    except wificlient_if.ifconfig()[0] == '0.0.0.0':
        print("Ei saada ip-osoitetta!")
        ei_voida_yhdistaa()
    except OSError:
        ei_voida_yhdistaa()
    finally:
        if wificlient_if.ifconfig()[0] != '0.0.0.0':
            aseta_aika()
            kaynnista_webrepl()
            print('Laitteen IP-osoite:', wificlient_if.ifconfig()[0])
            print("WiFi-verkon signaalitaso %s" % (wificlient_if.status('rssi')))
        else:
            ei_voida_yhdistaa()

Post Reply