Pages

Sunday, 16 February 2020

ESP32 WiFi Manager and mDNS


The appeal of the ESP32 for personal projects is the combination of a performant WiFi enabled microprocessor and low cost. I find myself creating various IoT devices on a local network and soon hit two common management issues with IoT devices:
  • How to avoid hard-coding WiFi credentials in my MicroPython scripts.
  • How to access the device if I don't know what IP address is assigned.

To solve the issue of hard-coding credentials when connecting to the wireless network I am using the MicroPython WiFi Manager by tayfunulu. A detailed tutorial on the use of this library can be found here. The key parameters to access the WiFi Manager are:
  • ap_ssid = 'WifiManager'
  • ap_password = 'tayfunulu'
  • IP address = 192.168.4.1

Once the ESP32 is connected to the local network the next issue you are likely to encounter is what is the IP address of the device? The recent MicroPython v1.12 release added multicast DNS (mDNS) support for the ESP32 so you can now assign a DHCP hostname for the device and access it by hostname instead of needing to know its IP address when on the local subnet.

I typically use a config.py file to store device specific configuration like pin assignments and the device name. Calling wlan.config(dhcp_hostname = 'mydevicename') sets the ESP32 hostname. It is important to set the hostname parameter before the network connection is established. You can check that the hostname has been set by pinging the ESP32 from another device on the local subnet.

ping -c 3 mydevicename.local


The below gist shows the typical boot.py file that I'm currently using on projects.
Loading https://gist.github.com/deanstheory/699cd3d8a083b7356e0df2fc49873ef5 ...


No comments:

Post a Comment