notes by alifeee profile picture tagged microcontrollers (2) rss

return to notes / blog / website / weeknotes / linktree

here I may post some short, text-only notes, mostly about programming. source code.

tags: all (44) scripting (13) linux (5) bash (4) geojson (4) obsidian (4) android (3) github (3) html (3) jq (3) ............ see all (+63)

testing micropython on an ESP8266 D1 Mini # prev single next top

tags: micropython, microcontrollers • 544 'words', 163 secs @ 200wpm

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.

Until today!

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).

I'll share here my initial experiments! I'm running on a Linux computer, on Pop!_OS.

I read the:

…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:

# install files and virtual environment
mkdir -p /git/micropython
cd /git/micropython
python -m venv env
. env/bin/activate
pip install esptool

# download firmware
$ ls
ESP8266_GENERIC-20241129-v1.24.1.bin

# at this point I plugged in the ESP but it was not recognised
#   after looking at `tail -f /var/log/syslog`, I saw that `brltty`
#   was doing something spooky. I remembered having this issue before,
#   and that `brltty` was something to help Braille readers. As I don't
#   need that, I...
# disabled brltty
sudo systemctl stop brltty.service
sudo systemctl mask brltty.service
sudo systemctl disable brltty.service
sudo systemctl restart

# now I could see the ESP as a USB device
$ lsusb
$ ls /dev/ | grep "ttyUSB"
ttyUSB0

# flash ESP
esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py --port /dev/ttyUSB0 --baud 1000000 write_flash --flash_size=4MB -fm dio 0 ESP8266_GENERIC-20241129-v1.24.1.bin

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.

sudo apt install picocom
picocom /dev/ttyUSB0 -b115200
>>> from machine import Pin
>>> p = Pin(2, Pin.OUT)
>>> p.off()
>>> p.on()

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".

I also tried connecting to WiFi and using the Web-REPL, so you can execute Python commands over the air! With...

>>> import network
>>> wlan = network.WLAN(network.WLAN.IF_STA)
>>> wlan.active(True)
>>> wlan.scan()
>>> wlan.isconnected()
>>> wlan.connect("ssid", "key")
>>> # wait a bit
>>> wlan.isconnected()
>>> # or use function from https://docs.micropython.org/en/latest/esp8266/quickref.html#networking

Then you can configure webrepl with:

>>> import webrepl_setup

…and it will print out an IP that you can connect to and use the REPL from your browser! Very nice.

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.

I'll give that a go soon...

back to top

how easily can I get back into an old CO2 monitoring project # prev single next top

tags: documentation, microcontrollers, co2 • 661 'words', 198 secs @ 200wpm

About 10 months ago I acquired a CO2 sensor (Sensirion SCD40), I attached it to a D1 Mini ESP8266 microcontroller, and for a week I looked into CO2 monitoring. At the end, I set up an InfluxDB database, and flashed the microcontroller to get and upload the CO2 data every 30 seconds. Eventually, I set Influx to delete data over 7 days old (as I didn't want to make a weird historical "is anyone in the room" sensor), and from then I could visit my Influx to see how much CO2 was in the room the sensor was in.

Since then, I have:

  1. forgotten a lot of how I did it
  2. stopped using VSCode (started using VSCodium), which I used to flash the microcontroller using the PlatformIO extension, which doesn't work in VSCodium
  3. stopped using Windows (started using Linux)

I wanted to re-use the sensor at an environmental talk today, so I grabbed it and started a small task. But, I didn't know whether it would be easy to reprogram the device, or to get data from it, or how possible it would be, as I no longer use either the OS (Windows) or the tool (VSCode) I used. I knew that PlatformIO had a CLI (Command Line Interface), so I didn't need the VSCode plugin to use it, and I knew programming/serial communication was probably easy enough on Linux. To be honest, I would rather know how to use the CLI than the plugin, as I prefer this most of the time. Then, when I write instructions, I can write commands instead of writing "press this button in the GUI".

I'd written a lot in the documentation on the project, so hopefully I didn't have to remember much. This is what I had to remember...

After all this figuring, I was able to reflash the microcontroller to just send environment information over Serial (i.e., not push to InfluxDB), so then I could use some commands to display a big "604 ppm" on my terminal screen.

# log from monitor
pio device monitor --quiet | awk '{printf "%s\t%s\n", strftime("%H:%M:%S", systime()), $0}' | tee env.log
# display nicely
tail -n1 env.log | awk -F'\t' '{printf "%s ppm.", $2}' | figlet -t -c
# plot
# install eplot and chmox +x
cat env.log | awk -F'\t' '{print $2}' | eplot -d -t CO2
back to top