Pi Zero W to Pi Pico W

We’re finally able to get our hands on some sweet Pis after a long shortage. I got a Pi Pico W because it’s just that cheap, why not? It’s the W that really makes the difference; years ago I went from full Pis to Pi Zeros because the Wifi capabilities meant I didn’t have to worry about USB dongles. Everything I do tends to be Wifi connected so not having it makes a board irrelevant. And so with a W next to the Pico’s name, I’m definitely interested. It’s pretty smart of the Raspberry Pi foundation to put in this functionality into a microcontroller, it’s just enough to get me curious in what I could actually do with it.

Now I have to get over the barrier of not having a fully fledged OS (and the sweet package managers with all the Debian packages that comes with it), but with Wifi I can definitely do some tasks with a Pico I’m usually doing with a Zero. So I figured I’d try replacing the house temperature sensor and see how that goes. I initially tried to use the Pico’s built in temperature sensor, but it’s terribly inaccurate so I did rewire the DS18B20 from the Zero to the Pico.

Brain transfer, The size gain is nice but not particularly significant

Coding was a breeze, there is isn’t much to say about that, but if you’re interested, here the code I’m running for a web server I can ask for current temperature. One thing I learned is that you need to make sure you have a “lib” directory with libraries you might need for your code.

import machine
import time
import network
import socket
from machine import ADC
import machine, onewire, ds18x20

html = """{\"house_temperature\":<TEMPERATURE>}"""

led = machine.Pin( "LED", machine.Pin.OUT )

ds_pin = machine.Pin( 17 )
ds_sensor = ds18x20.DS18X20( onewire.OneWire(ds_pin) )
roms = ds_sensor.scan()
print( "Found DS devices: ", roms )

ssid = "<redacted>"
password = "<redacted>"
wlan = network.WLAN( network.STA_IF )
wlan.active( True )
wlan.connect( ssid, password )

# wait for connect or fail
max_wait = 20
while max_wait>0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print( "waiting for connection..." )
    time.sleep( 1 )

# handle connection error
if wlan.status()!=3:
    raise RuntimeError( "network connection failed" )
else:
    print( "connected" )
    status = wlan.ifconfig()
    print( "ip = " + status[0] )

# open socket
addr = socket.getaddrinfo( "0.0.0.0", 80)[0][-1]
s = socket.socket()
s.bind( addr )
s.listen( 1 )
print( "web server listening on", addr )

# listen for connections
while True:
    try:
        cl, addr = s.accept()
        print( "client connected from", addr)

        request = cl.recv( 1024 )
        print( request )
        request = str( request )
        
        ds_sensor.convert_temp()
        time.sleep_ms(750)
        response = html.replace( "<TEMPERATURE>", str(ds_sensor.read_temp(roms[0])) ) 
        
        led.value( 1 )
        
        cl.send('HTTP/1.0 200 OK\r\nContent-type: application/json\r\n\r\n')
        led.value( 0 )
        cl.send(response)
        cl.close()
 
    except OSError as e:
        cl.close()
        print('connection closed')

I got the libraries online, but here they are in case it’s useful.

# 1-Wire driver for MicroPython
# MIT license; Copyright (c) 2016 Damien P. George

import _onewire as _ow


class OneWireError(Exception):
    pass


class OneWire:
    SEARCH_ROM = 0xF0
    MATCH_ROM = 0x55
    SKIP_ROM = 0xCC

    def __init__(self, pin):
        self.pin = pin
        self.pin.init(pin.OPEN_DRAIN, pin.PULL_UP)

    def reset(self, required=False):
        reset = _ow.reset(self.pin)
        if required and not reset:
            raise OneWireError
        return reset

    def readbit(self):
        return _ow.readbit(self.pin)

    def readbyte(self):
        return _ow.readbyte(self.pin)

    def readinto(self, buf):
        for i in range(len(buf)):
            buf[i] = _ow.readbyte(self.pin)

    def writebit(self, value):
        return _ow.writebit(self.pin, value)

    def writebyte(self, value):
        return _ow.writebyte(self.pin, value)

    def write(self, buf):
        for b in buf:
            _ow.writebyte(self.pin, b)

    def select_rom(self, rom):
        self.reset()
        self.writebyte(self.MATCH_ROM)
        self.write(rom)

    def scan(self):
        devices = []
        diff = 65
        rom = False
        for i in range(0xFF):
            rom, diff = self._search_rom(rom, diff)
            if rom:
                devices += [rom]
            if diff == 0:
                break
        return devices

    def _search_rom(self, l_rom, diff):
        if not self.reset():
            return None, 0
        self.writebyte(self.SEARCH_ROM)
        if not l_rom:
            l_rom = bytearray(8)
        rom = bytearray(8)
        next_diff = 0
        i = 64
        for byte in range(8):
            r_b = 0
            for bit in range(8):
                b = self.readbit()
                if self.readbit():
                    if b:  # there are no devices or there is an error on the bus
                        return None, 0
                else:
                    if not b:  # collision, two devices with different bit meaning
                        if diff > i or ((l_rom[byte] & (1 << bit)) and diff != i):
                            b = 1
                            next_diff = i
                self.writebit(b)
                if b:
                    r_b |= 1 << bit
                i -= 1
            rom[byte] = r_b
        return rom, next_diff

    def crc8(self, data):
        return _ow.crc8(data)
# DS18x20 temperature sensor driver for MicroPython.
# MIT license; Copyright (c) 2016 Damien P. George

from micropython import const

_CONVERT = const(0x44)
_RD_SCRATCH = const(0xBE)
_WR_SCRATCH = const(0x4E)


class DS18X20:
    def __init__(self, onewire):
        self.ow = onewire
        self.buf = bytearray(9)

    def scan(self):
        return [rom for rom in self.ow.scan() if rom[0] in (0x10, 0x22, 0x28)]

    def convert_temp(self):
        self.ow.reset(True)
        self.ow.writebyte(self.ow.SKIP_ROM)
        self.ow.writebyte(_CONVERT)

    def read_scratch(self, rom):
        self.ow.reset(True)
        self.ow.select_rom(rom)
        self.ow.writebyte(_RD_SCRATCH)
        self.ow.readinto(self.buf)
        if self.ow.crc8(self.buf):
            raise Exception("CRC error")
        return self.buf

    def write_scratch(self, rom, buf):
        self.ow.reset(True)
        self.ow.select_rom(rom)
        self.ow.writebyte(_WR_SCRATCH)
        self.ow.write(buf)

    def read_temp(self, rom):
        buf = self.read_scratch(rom)
        if rom[0] == 0x10:
            if buf[1]:
                t = buf[0] >> 1 | 0x80
                t = -((~t + 1) & 0xFF)
            else:
                t = buf[0] >> 1
            return t - 0.25 + (buf[7] - buf[6]) / buf[7]
        else:
            t = buf[1] << 8 | buf[0]
            if t & 0x8000:  # sign bit set
                t = -((t ^ 0xFFFF) + 1)
            return t / 16

Half the amp draw from a Zero

That’s it!

Stable Diffusion

I’ve played with Stable Diffusion a few months back, but the learning curve is steep, and easy access to DallE & MidJourney made it not worth it to press on. Until… I got excited about a super cool extension:

https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster

I just love the idea of having the same image have one analog meaning to a human and discrete meaning to a machine. And the same extension can be used for general masking other than just QR codes.

Without further ado, here’s the eye candy:

A benign QR code

General masking for an almost subliminal effect

There’s much to be refined in my incantation, but I really like how the mask isn’t just overlayed on top of a drawing in a subtle way, rather it’s the basis for growth of AI hallucinations.

Mindstorm Gondola Plotter

So out of nowhere Robin designed a gondola plotter with his Lego Mindstorms. There are many crazy pants projects I hear about every day. A lot aren’t attempted, most scratch an itch and finish half baked, few make it to top notch level. When Robin talked to me about his plotter, I was excited but skeptical it would turn into much. I also expressed some of the issues he might have with the early model. Lo and behold he finished the day with a very well made machine he threw together in 1 shot. We both got very excited when it moved for the first time.

I was on a mission to try and explain to him the double pythagorean theorem that gives these machine a cartesian coordinate system. We launched into it the next day so that he could translate it into Scratch, it was a good back and forth where we both managed to bridge each other’s understanding. I was very impressed by his ability to understand what was going on and problem solve bugs, he totally got it.

We ended up with this ugly code block, I have no idea if there’s a better way to code this in Scratch:

The machine became perfectly addressable on a cartesian system. Seeing this many first try successes, we got all the more excited and I told him I’d find a way to import SVGs in there. He didn’t believe that I could, and I tooted my own horn a bit saying I used to be a very good hacker. I had to deliver now… I couldn’t find a way to import anything in the neutered version of Scratch that Mindstorms run so I’d have to figure out a way to programmatically edit the *.lms file that is the project to inject the code blocks. Turns out, it’s just a bunch of files zipped up, including one called scratch.sb3, itself a bunch of files zipped up, including project.json. Browsing around at the data structure in that file, I found the doubly linked lists of objects which represent sequences of commands.

Trying to inject my own

I write some code to create these JSON blocks so that I can inject as many go_to function calls as there are instructions in my GCode converted SVG. Zip everything back up and we are in business. Robin totally thinks I can hack the FBI now.

So I convert an SVG into 32,000+ commands, and well, the Mindstorm app crapped its pants. I went a little easier with about 1800 and it had to think a while but it did it. Resulting in a hardly manageable project with a large go_to vomit in the middle of it. But it worked!

If you stream the commands… There isn’t enough room on the brick otherwise.

Finally, again with few obstacles, the Mindstorm Gondola Plotter gets to work on a simple Millenium Falcon.

It doesn’t make the calls to pen_up and pen_down yet, that’ll be a formality. It also has the very classic issue with moving on motor after another. Robin understands that we can simply break down the paths into more coordinates, but he may try other approaches.

For posterity, the first actual drawing (it looks a lot better than my first plotter drawings)

I absolutely love the design where all the logic is contained in the drawing apparatus, and the plotter is hanging on 2 fixed point. This was a bit forced by Lego in that you can’t have your motors too far away, but it’s really slick.

tel père tel fils

He was very proud of himself and I was too. I always prime him for the engineering process where a merciless series of failures precede any progress, but this project was very friendly at every step. Robin did spend a LOT of time working on various lego guns. I thought it was educational because of how long he had to spend on the fine mechanisms for moving, spring loading and releasing missiles. Clearly he learned from it given how easily he threw a complex machine together.