Categories
ESP32 Python

ESP32 MicroPython run script on boot

In a previous post we flashed a W32-WROOM-32 with MicroPython and successfully got to an interactive python prompt via serial. Now we will create a simple python script and upload it to the ESP32 and have it run on boot.

First, lets create a really simple python script -:

import time
print('Hello world! I can count:')
i = 1

while True:
    print(i)
    i += 1
    time.sleep(1.0)  # Delay for 1 second.

This script prints a message and initialises a variable to 1, then goes into a while loop printing the variable, incrementing the variable by 1, and waiting 1 second.

To upload this python file to the ESP32 use the ampy tool, again install using pip -:

pip install adafruit-ampy

I have saved my python file as demo.py, we will use ampy to upload to the ESP32 via serial. To make the script run on boot it must be uploaded as main.py -:

ampy --delay 1 --port /dev/cu.usbserial-0001 put demo.py /main.py

Note the delay is important, it will error out without it. Now if we reboot the ESP32 and connect to serial we should see the following output -:

MicroPython running a script
MicroPython running a script

In the next post we will look at connecting the ESP32 to our wireless network.

Leave a Reply

Your email address will not be published. Required fields are marked *