Introduction

I wanted to play with IoT a bit and got myself a bunch of Rpi pico w to start. After digging, and digging, and digging again the documentation, let’s face it, I won’t do high level shit for now, let’s start basic and grow overtime.

The raspberry, the python, and the serial communication

First thing first, you can develop either in C or using micropython to have a python interpreter to play with. We’ll go for that second option.

Installing the micropython firmware

Micropython builds firmwares for a lot of cards. You can ofcourse build it yourself but we want to start quickly for now. In my case, i’ll have to download the rp2-pico-w prebuilt image. Once you retrieved the proper uf2 file, connect your pico w using USB while keeping the BOOTSEL button pressed while you plug the pico in, it’ll force it to connect as a mass storage USB device.

Put the uf2 file in it, and after a while, you’ll see the USB storage disconnecting itself.

Congrats ! Micropython have been installed !

Connect to the serial port

This part depends on your OS, on FreeBSD ( and any linux i think ), you can use minicom to communicate with the serial port. Take a look in /dev or in dmesg to find the serial interface that have been created, for me, it’s /dev/cuaU0.

I can therefor connect with doas minicom -D /dev/cuaU0 ( doas is a sudo replacement, you can use sudo instead ) and voilà !

you are prompted with the caracteristic >>> python prompt.

If you ctrl+D, you’ll have

MicroPython v1.19.1 on 2022-08-19; Raspberry Pi Pico W with R0
Type "help()" for more information.
>>>

You can then start writing python code and see it being executed on the fly.

Blinking the led

The usual first thing to do : blinking the onboard LED, is done that way :

>>> from machine import Pin
>>> led = Pin('LED', Pin.OUT)
>>> led.toggle()
>>> led.toggle()

Writing a script and executing it

Upload any script

Ok, we can interact with the board with the prompt, but the goal isn’t to keep the board connected to your laptop. I want to upload a script and see it executed.

For that, you can rely on ampy-adafruit. ampy will allow you to browse, get, put, and execute script in your board.

You can therefor edit a script with your favorite editor ( aka : vim ), and then test it with doas ampy --port /dev/cuaU0 run servo.py

If it works properly, you can then ampy --port /dev/cuaU0 put servo.py and boom, it’s on your board, ready to use.

Script at boot

Ok, now how to determine what will be launched at boot when you power on your board ? Well, there are two specific script that micropython is looking for at boot : boot.py and main.py

boot.py

I won’t say too much about it for now, i did not checked deeper enough, but boot.py control the boot itself and is executed on power / reset, it’s meant to stay low level and i’ll discover it later

main.py

You guess it, main.py will be the one, the script where your code lives and which is executed when you power on. So if you want your servo.py script to be started at boot, use doas ampy --port /dev/cuau0 put servo.py /main.py

Read the doc, it’ll save your live.

To be able to properly use your piico W, you have to read. A LOT. OF. DOC. So here are the main entrypoints. Have a good evening, have a good night, or a good weekend, reading all of that. Seriously if you are new to it, take your time to assimilate everything.

Conclusion

With all that, we are now able to experiment with the serial port, create a script, test the script with ampy run, and when we are happy, upload it with ampy put script.py /main.py so it’s executed each time you power the board !

That was the first post of a long serie.

Later, I’ll post on how to connect sensors, get data from them, connect to the network, and send the data ( using MTQQ ? ) to make a home assistant dashboard.

And maybe other things i have in mind like reverse enginneering my garage door key signal and being able to send it again from your Pi, or how to create a laser moving randomly for your cat to play with.

I have a LOT of ideas in mind, and it’s just the beginning !