|
| 1 | +--- |
| 2 | +title: testing micropython on an ESP8266 D1 Mini |
| 3 | +date: 2025-03-13 |
| 4 | +tags: |
| 5 | +- MicroPython |
| 6 | +- microcontrollers |
| 7 | +--- |
| 8 | +I've toyed for a while with microcontrollers, and only really used Arduino/C/C++. Sometimes, I've heard talk of MicroPython, but I've never tried it out. |
| 9 | + |
| 10 | +Until today! |
| 11 | + |
| 12 | +I had a little experiment, and it seems promising. I might have a larger experiment soon (maybe try to retry some of my [hardware hacking](https://alifeee.co.uk/favourites/#hardware%20hacking)). |
| 13 | + |
| 14 | +I'll share here my initial experiments! I'm running on a Linux computer, on Pop!\_OS. |
| 15 | + |
| 16 | +I read the: |
| 17 | + |
| 18 | +- installation guide: <https://www.wemos.cc/en/latest/tutorials/d1/get_started_with_micropython_d1.html> |
| 19 | +- usage guide: <https://docs.micropython.org/en/latest/esp8266/quickref.html> |
| 20 | +- web-REPL help guide: <https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html> |
| 21 | +- D1 Mini pinout reference: <https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/05/ESP8266-WeMos-D1-Mini-pinout-gpio-pin.png> |
| 22 | + |
| 23 | +…and downloaded the firmware file for the ESP8266 D1 Mini (which I have a few of) from <https://micropython.org/download/ESP8266_GENERIC/>, and then ran: |
| 24 | + |
| 25 | +```bash |
| 26 | +# install files and virtual environment |
| 27 | +mkdir -p /git/micropython |
| 28 | +cd /git/micropython |
| 29 | +python -m venv env |
| 30 | +. env/bin/activate |
| 31 | +pip install esptool |
| 32 | + |
| 33 | +# download firmware |
| 34 | +$ ls |
| 35 | +ESP8266_GENERIC-20241129-v1.24.1.bin |
| 36 | + |
| 37 | +# at this point I plugged in the ESP but it was not recognised |
| 38 | +# after looking at `tail -f /var/log/syslog`, I saw that `brltty` |
| 39 | +# was doing something spooky. I remembered having this issue before, |
| 40 | +# and that `brltty` was something to help Braille readers. As I don't |
| 41 | +# need that, I... |
| 42 | +# disabled brltty |
| 43 | +sudo systemctl stop brltty.service |
| 44 | +sudo systemctl mask brltty.service |
| 45 | +sudo systemctl disable brltty.service |
| 46 | +sudo systemctl restart |
| 47 | + |
| 48 | +# now I could see the ESP as a USB device |
| 49 | +$ lsusb |
| 50 | +$ ls /dev/ | grep "ttyUSB" |
| 51 | +ttyUSB0 |
| 52 | + |
| 53 | +# flash ESP |
| 54 | +esptool.py --port /dev/ttyUSB0 erase_flash |
| 55 | +esptool.py --port /dev/ttyUSB0 --baud 1000000 write_flash --flash_size=4MB -fm dio 0 ESP8266_GENERIC-20241129-v1.24.1.bin |
| 56 | +``` |
| 57 | + |
| 58 | +That's it installed! Now, using the guides above I found I needed a terminal emulator, so I used `picocom`. And, to reach the pinnacles of complexity, tried turning the inbuilt LED on and off. |
| 59 | + |
| 60 | +```bash |
| 61 | +sudo apt install picocom |
| 62 | +picocom /dev/ttyUSB0 -b115200 |
| 63 | +>>> from machine import Pin |
| 64 | +>>> p = Pin(2, Pin.OUT) |
| 65 | +>>> p.off() |
| 66 | +>>> p.on() |
| 67 | +``` |
| 68 | + |
| 69 | +It works! Neat! The REPL (Read-Evaluate-Print-Loop) is really nice to quickly debug with. Perhaps nicer than the "waiting-for-20-seconds-for-your-C-code-to-flash-onto-the-device". |
| 70 | + |
| 71 | +I also tried connecting to WiFi and using the Web-REPL, so you can execute Python commands over the air! With... |
| 72 | + |
| 73 | +```bash |
| 74 | +import network |
| 75 | +wlan = network.WLAN(network.WLAN.IF_STA) |
| 76 | +wlan.active(True) |
| 77 | +wlan.scan() |
| 78 | +wlan.isconnected() |
| 79 | +wlan.connect("ssid", "key") |
| 80 | +# wait a bit |
| 81 | +wlan.isconnected() |
| 82 | +# or use function from https://docs.micropython.org/en/latest/esp8266/quickref.html#networking |
| 83 | +``` |
| 84 | + |
| 85 | +Then you can configure `webrepl` with: |
| 86 | + |
| 87 | +```bash |
| 88 | +import webrepl_setup |
| 89 | +``` |
| 90 | + |
| 91 | +…and it will print out an IP that you can connect to and use the REPL from your browser! Very nice. |
| 92 | + |
| 93 | +What I haven't tried yet is using `boot.py`. From what I know it will execute on every reset of the ESP, so basically is how you "program" it, but a lot quicker, since you just place a file on the filesystem. |
| 94 | + |
| 95 | +I'll give that a go soon... |
0 commit comments