ESP-32 config wifi in micropython

Make a file with the name ConnectWifi.py with the following code:

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect('essd', 'password')
        while not sta_if.isconnected():
            pass
        print('network config:', sta_if.ifconfig())

Edit boot.py to:

import ConnectWifi
ConnectWifi.do_connect()

Upload both files with ampy to the esp-32

ampy -p /dev/ttyUSB0 put boot.py
ampy -p /dev/ttyUSB0 put ConnectWifi.py

Now when your micropython device boots up it will connect immediately connect to the configured wifi-network.

Micropython on the ESP-32

Some time ago i bought an esp-32 and now is the time to play with it. I like python so micropython will be flashed on it. I downloaded the latest firmware from micropython.org and installed the esptool to flash the firmware.

sudo pip3 install esptool

I attached the esp-32 to my computer with a micro-usbcable and looked for the port it got attached to.

 sudo dmesg

I this case it was /dev/ttyUSB0.
Because it is the first time I flash micropython on the chip it is recommended to erase the flash first

 esptool.py --port /dev/ttyUSB0 erase_flash

Then flashed micropython on the chip. (your version of the .bin file may differ)

esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-20180403-v1.9.3-503-gbcfff4fc.bin

Did it work? Well, let’s connect with it to get a repl.

screen /dev/ttyUSB0 115200

Yeah, we got a repl: >>>

print("Hello World"

My version of the esp-32 has a led on pin 16 which is on if the pin is low and off if the pin is high.

import machine
led = machine.Pin(16, machine.Pin.OUT)
led.value(0) # led is on
led.value(1) # led is off

You can exit screen with Ctrl-a \ y. On the computer create a file called main.py with the following code

import machine
import time
led = machine.Pin(16, machine.Pin.OUT)
while True:
    led.value(0)
    time.sleep(0.5)
    led.value(1)
    time.sleep(0.5)

To ‘upload’ a file we need a tool called ampy.

sudo pip3 install ampy

Now ‘upload’ main.py to the esp-32 with ampy.

ampy -p /dev/ttyUSB0 put main.py

Powercycle the esp-32 and it will start main.py automaticly.
Yeah, we have blinky running!