Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Micropython primer

Hardware

  • Esp32 wroom dev kitc
  • Breadboard
  • One led
  • One 220Ω
  • One 10kΩ
  • Two jumper wires male/male
  • One button
  • One reed-switch

Flashing the firmware (Linux)

We can use esptool to flash the hardware. You can install this program in your project via pip install esptool or uv add esptool.

micropython firmware for esp32

The command to flash your esp32 is:

sudo uv run -m esptool -p /dev/ttyUSB0 erase-flash
sudo uv run -m esptool -p /dev/ttyUSB0 --baud 460800 write-flash 0x1000 firmware.bin

-p stands for port. On Linux, it will be the file used to interact with the usb port.

-baud 460800 defines the serial speed rate at which to communicate with the esp32. The computer communicates with the Esp32 via an Usb-To-Uart port. It is based on serial data communication with no shared clock. It means that both side has to define the speed rate at which they send bits. Here we set communication at 460800.

0x1000 is the address to which we ask esptool to copy the firware.

firmware.bin is the filepath where esptool can find the firmware on our machine.

There are two ways you can use to know that. You can do a ls /dev/ and look at the file that is most likely the usb. Or you can use dmesg and grep to see where the kernel has mounted the usb port with the command dmesg | grep USB after you plugged the device. Here is an example output:

$ sudo dmesg | grep USB

[29241.401573] usb 1-1: New USB device found, idVendor=10c4, idProduct=ea60, bcdDevice= 1.00
[29241.401591] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[29241.401594] usb 1-1: Product: CP2102N USB to UART Bridge Controller
[29241.406268] usb 1-1: cp210x converter now attached to ttyUSB0 <--------

According to this log, the usb port is in /dev/ttyUSB0.

How to not sudo all the time

The way to avoid sudoing a command to interact with the /dev/ttyUSB0 file is to add your user in the right group. It changes depending on your Linux distribution:

  • uucp on arch.
  • dialout on Debian based machine and RedHat/Fedora.

Then type the command:

 $ sudo usermod -a -G uucp $USER

Turn on and off a led

WHen you only flash the ESP32, the board will follow its boot sequence and Micropython will run a REPL. You can connect to the repl using an app like picocom or minicom.

$ picocom -b 115200 /dev/ttyUSB0

-b 115200 defines the Baud Per Second at which the computer must send data. It also tells us at which pace to read the incoming data. This is part of the serial data communication system. We need to know the rate at which to decode signals; otherwise, we might read the same symbol several times or miss others. I did not find where to know that number.

>> from machine import Pin
>> led = Pin(33, Pin.OUT)
>> led.on()
>> led.off()

To exit picocom, type C-aC-x

Circuit description

We use two cables, a 220Ω resistance and one led.

  1. connect GPIO 33 to the resistance
  2. Connect the resistance to one side of the led
  3. Connect the other side of the resistance to GND

Picture of the led breadboard circuit

Copying files

After we flashed the hardware, there is a filesystem that we can use to store files. There are two important files in the filesystem:

  • boot.py
  • main.py

The file boot.py will be the first to ran by the runtime. Then, it is some kind of initializer script that we can change to run things. Then, the run time will be main.py.

Copy a file

We can copy files to the file system using pyboard.py. You first will need to install the dependency pip install pyserial.

Then, if you want to copy a file, run the following command. I use uv, so it will look like:

$ sudo uv run pyboard.py -d /dev/ttyUSB0 -f cp ./main.py :main.py
# Or, you can type the shorthand
$ sudo uv run pyboard.py -d /dev/ttyUSB0 -f cp ./main.py :

Turn on a led

There is a very simple program in simple_led.py, let's use as a practice example.

$ sudo uv run pyboard.py -d /dev/ttyUSB0 -f cp ./simple_led.py :main.py

Then reboot the esp32 using the reboot button (the button on the red led side).

Debugging

In our code, we print debug information. The way to see it is the same as when we connected to the rpl. We run picocom -b 115200 /dev/ttyUSB0.

Beware that we cannot have two programs using /dev/ttyUSB0. So if you're listening to this tty, you won't be able to use pyboard.py to copy files on the esp32.

Try to run the previous program and you will see the debug information.

The most important command to know while running picocom is C-aC-x. It will exit the program. The C-a switches mode to Raw console and C-x is the command to exit raw terminal.

Activate the led remotely

In this example, we will setup a webserver on our esp32 and allow a remote user to turn on and off the led.

TODO: Check basic Wifi for ESP32

Files

  • secret.py: contains the WiFi-credentials. This files is not commit in Git, you need to create it.
  • webserver.py: contains a simple websever that handles http request.
  • async_webserver.py: contains an async websever that handles http request.
  • server_led.py: contains the actual logic that handles led and routing
  • main.py: uncommont what you need

Activating network

The esp32 allows two ways of using the wifi:

  • Connect to an existing wifi.
  • Run as a wifi point.

We will use the first one to connect to an existing Wifi point. See the code in webserver.init_network.

Setup the webserver

For this example, we will use two files:

The first file contains the logic to run the server and a dispatch function to give to the server.

The second file is a very simple webserver that listen to a socket and handle new client via the handle_client. Both functions take a handler function passed by the caller. This function must returns bytes when a path is handled of a falsy value to trigger a 404.

There is also an async version of the webserver in async_webserver.py.

Button: check button

TODO: Check what is important to know with Interrupt

This wiring is a pull-down type of wiring. This is because we connect one side of the button to the GND and the GPIO we will monitor. The other side of the button is connected to the 3.3V pin.

  1. Connect the 3.3V pin to the button.
  2. Connect the other side of the button to GPIO 32.
  3. Connect the same side of the button to a resistance.
  4. Connect the resistance to the GND.

Picture of the button breadboard circuit

Files

Notify system

Explanation

Notify(ntfy.sh) is an open source notification system. You can use the docker compose file to spin up a server. Before using it with our Esp32, let's send a notification via Curl.

$ curl -d "Hey" "http://localhost:8080/door-open"

{"id":"7HV6WuBWR1u2","time":1776107631,"expires":1776150831,"event":"message","topic":"door-open","message":"Hey"}

It will create a new feed named door-open to which you can subscribe.

You can type the following command to get your IP address on your local WIFI network:

$ ip addr | grep wlan0 -A 5 | grep "inet "

inet 192.168.1.143/24 brd 192.168.1.255 scope global dynamic noprefixroute wlan0

Then you can install ntfy on your phone and subscribe to a new topic. When doing so, click on Use another server and write http://192.168.1.143:8080. The IP addres is the one you get from the ip addr command the port is defined in the docker-compose file.

The Esp-32 code that will use is in notify_user.py. We only send a simple post request along with the host in the header. We also need to read the response as it can prevent ntfy from registering the notification.

Files

Reed switch

The code is in door_probe_interrupt.py. The only difference with the button_interrupt.py code is that we check the opposite signal. The reed switch, when the door is closed, is triggering a high signal to the GPIO pin. When the door is open, the signal falls low. If it lasts too long, we notify the user.

Watchdog

The example is in button_interrupt.py. A watchdog is some kind of a timer that is reset anytime we feed it. If the program fails to notify the watchdog, it will reset the system.

A way to simulate that is to add a firewall rule to the port 8080. Doing so will make the esp32 hang when trying to notify the user. As the OS silently drops the packet, the esp32 waits for the TCP handshake to happen. We can set a watchdog that will reset the system and avoid waiting.

Here is how to set the firewall:

sudo ufw deny 8080/tcp

# Then, when we want to drop the rule
sudo ufw allow 8080/tcp

TODO

  • Refactor README

    • Notify is in docker
    • For each part, list files to see
    • Add comment to explain the Micropython interface
  • Add notion we will cover in presentation: GPIO, WiFi, Interrupt, Watchdog

References

Esp32 devkitc datasheets Basic on UART esptool documentation micropython firmware for esp32 Esp32 boot process specificaiton MicroPython documention for esp32 Interrupt documentation Micropython

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages