WifiManager Script Template

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
bitninja
Posts: 134
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

WifiManager Script Template

Post by bitninja » Tue Oct 11, 2016 8:32 pm

Hello.
I'm new to MicroPython (and Python in general) and was hoping to find some guidance and resources for my current project.

What I would like to do is build a simple mechanism (script) where the ESP8266 device can be configured for local network wifi (Internet) access, by acting as a standalone AP and serving web pages that would allow the user to set the credentials for the local wifi. Once the credentials were saved, the device would log into the local wifi and disable the standalone AP. If the device is moved, or the original wifi network is not available... it should fall back into the stand-alone configuration mode.

Something like this https://github.com/tzapu/WiFiManager

I have found that the httpserver examples with MicroPython do not show enough for me to complete the task. On the other hand I have seen httpserver implementations with TOO MUCH functionality.

I'm looking for an httpserver example that is lightweight, but can handle more than a static response.

Thanks in advance.

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: WifiManager Script Template

Post by chrisgp » Wed Oct 12, 2016 5:05 am

Here's a basic http server I wrote that demonstrates listing the available access points to the user and allowing them to select one and enter a password. After clicking Submit it brings them to a page that configures the ESP8266 with the supplied SSID and password. There are probably some cases where it fails to do appropriate error checking and it's not the most memory efficient, but it might be helpful for you.

https://github.com/cpopp/MicroPythonSam ... kconfig.py
listing.png
listing.png (24.77 KiB) Viewed 6489 times
configured.png
configured.png (16.64 KiB) Viewed 6489 times

bitninja
Posts: 134
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: WifiManager Script Template

Post by bitninja » Wed Oct 12, 2016 2:42 pm

This is excellent! Thanks!
I've been looking for a simple, clean example of how to do this and yours fits the bill.

gregsvo
Posts: 10
Joined: Fri Oct 21, 2016 6:12 pm

Re: WifiManager Script Template

Post by gregsvo » Wed Oct 26, 2016 3:05 am

I'm confused about how to fire this up. I created 2 files, main.py, and networkconfig.py.

Inside of main.py:
import networkconfig
networkconfig.start()

Inside of networkconfig.py:
The code contained in your repo.

When restarted, my micropython access point appears, but I'm not sure what to do next. I couldn't find the right ip address to go to, or if I did something else wrong.

Any help you can give is appreciated. Thanks!

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: WifiManager Script Template

Post by chrisgp » Wed Oct 26, 2016 4:53 pm

When you connect to the ESP8266's access point your machine will get some IP like w.x.y.z. The IP of the ESP8266 will likely be w.x.y.1 (just replacing the last section with 1).

You can set the IP address of the ESP8266 for its access point interface to ensure it is always something like 192.168.1.1 using network.ifconfig (see viewtopic.php?t=2378). This ensures that you'll always know what the IP is when connecting to the access point.

Things could be improved as well to do interesting things with DNS to cause a user connected to the access point to navigate to the ESP8266 whenever they open their browser, but that's beyond the intent of this simple example. This is called a captive portal and I've done it before with the Arduino codebase on ESP8266 (here's an example) but I haven't seen anything like this for MicroPython.

bitninja
Posts: 134
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: WifiManager Script Template

Post by bitninja » Wed Oct 26, 2016 10:13 pm

I was fortunate, in that my design has an OLED display for user directions.
20161026_165342.jpg
20161026_165342.jpg (108.45 KiB) Viewed 6314 times
Otherwise a captive portal using a rigged DNS server would be great. It does not need to be a full blown DNS server although it would add some overhead to the application. IDK...I'm new to Python in general so there may be some code already out there...

BTW: Thanks for the head start again chrisgp! I've uploaded my work here... https://github.com/joewez/ESP-Widget

Primesty
Posts: 38
Joined: Sun Jun 28, 2020 11:06 pm

Re: WifiManager Script Template

Post by Primesty » Sat Jul 11, 2020 3:57 pm

At the risk of nobody reading this ---

@chrisgp I was able to implement your

Code: Select all

networkconfig.py
script (https://github.com/cpopp/MicroPythonSam ... honSamples).

In my main.py file I import all the libraries and then call networkconfig.start()

I'm using an ESP 8266 Nodemcu with micropython 1.12

Code: Select all

import machine
from machine import Pin
import networkconfig
import time

led = machine.Pin(2, machine.Pin.OUT)

networkconfig.start()

for i in range(3):
	led.off()
	time.sleep(0.5)
	led.on()
	time.sleep(0.5)

I'm also able to access the webpage to select SSID and password, and get a successful message "Wi-Fi configured for SSID XYZ". In the IDE it shows me that the client is connected.

However, then, the main.py script does not move on to the next action of blinking the led.

How can I establish the wifi connection to the local network AND then do other stuff like send webhook requests, blink leds, define other functions. It seems the networkconfig.start() function does not stop because of the while loop maybe?

Any help would be greatly appreciated!!

Thanks!

Post Reply