Getting ssid and password using web server - AP mode

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
tiwanacote
Posts: 1
Joined: Sun Jan 10, 2021 12:57 am

Getting ssid and password using web server - AP mode

Post by tiwanacote » Sun Jan 10, 2021 1:55 am

Hi, I'm new in micropython and in programming. Maybe somebody can help me with this topic.

I'm using an ESP32 CAM for an aplication which must connect to wifi. The main idea is to create a friendly user interface to enter the ssid and password using the ESP in AP_mode in its first use, where the user have to connect to it, and finally entry data in a exposed web served page using a web browser.

I am asking for:
1) How to solve the following not working propossed code
2) An alternative or example to implement what I am looking for.


1)At the moment I'm using picoweb with the following code. (ESP in AP mode) I can run it calling app.run(debug=1, host = "192.168.4.1" ) and print into REPL the ssid and pasword entered but I can not exit the running app to continue with the main program. I have thougth to launch the app in a threading and kill it at the end as an alternative, but seems to be a dirty way. Any suggestion to finish the app execution?

Thank you!

Code: Select all

app = picoweb.WebApp(__name__)

@app.route("/")
def index(req, resp):
    global settings
    if req.method == "POST":
        print("request is post")
        yield from req.read_form_data()
        
        settings["ssid"] = req.form.get('ssid')
        settings["password"] = req.form.get('password')
        flag_params = True
        yield from resp.awrite("Connect Form received")
        print(settings)        
    else:
        yield from picoweb.start_response(resp)
        f = open('NetworkForm.html')
        http_form= f.read()
        yield from resp.awrite(http_form)


@app.route("/squares")
def squares(req, resp):
    yield from picoweb.start_response(resp)
    yield from app.render_template(resp, "squares.tpl", (req,))

uraich
Posts: 41
Joined: Tue Mar 26, 2019 12:24 pm

Re: Getting ssid and password using web server - AP mode

Post by uraich » Sun Jan 10, 2021 9:43 am

This is what I do:

station = network.WLAN(network.STA_IF)
station.connect(ssid, password)
ipaddr = station.ifconfig()[0]

When I start picoweb:
app = picoweb.WebApp("__main__")
app.run(debug=2, host = ipaddr,port=80)

Hope this helps

Post Reply