There’s a lamp on my wall that knows how much internet my house is using. When nobody is doing anything, a blob of orange wax floats up and down, very slowly. When I start a big download, the wax boils.
(open full size)I’m a home-labber, which means I’m a bit crazy about my network and I guard it very jealously. But all that traffic is invisible: it goes through a cable, the router counts it, and to see it I have to go and open a graph somewhere.
I wanted to give it a shape, something I could see from the couch without opening anything.
The hardware
Nothing exotic, all off the shelf:
- an Adafruit 64x32 RGB LED matrix, mounted vertically;
- an Adafruit Matrix Portal M4, the little board that plugs into the back of the panel. It has a microcontroller for the graphics and an ESP32 that does the WiFi;
- a 3D-printed frame. I didn’t design this one, I printed this enclosure from Printables;
- my MikroTik router, which is where the numbers come from.
The Matrix Portal runs CircuitPython, which is Python for microcontrollers. Easy to write, very slow to run. Keep that in mind, it comes back later.
Asking the router
MikroTik routers have a REST API (RouterOS 7.12 or newer), and one of the things you can ask is “how much traffic is passing through this interface right now”. The router does the math itself and answers in bits per second, so the lamp doesn’t have to count anything.
Once per second the lamp sends this to the router, with a dedicated lamp user:
1body = json.dumps({
2 "interface": "ether1", # the WAN port, where the internet comes in
3 "once": "",
4 ".proplist": "name,rx-bits-per-second,tx-bits-per-second,"
5 "rx-packets-per-second,tx-packets-per-second",
6})
7# POST /rest/interface/monitor-traffic
rx is what comes into the house (download), tx is what goes out (upload).
Pause! The board is so small that I wrote the HTTP client by hand, base64 for the password included. HTTP/1.0, one request, the router closes the connection, done. The first version went through a Prometheus exporter instead. It still works as an option, but asking the router directly is one hop less.
This also means the lamp knows how much, never what. It gets a few numbers per second and has no idea which sites or which devices they belong to. Given how jealous I am of my network, that was a requirement.
The network part runs as its own async task, so the animation keeps going while the lamp waits for the router. Otherwise every request would freeze the lava for a moment, which kind of ruins the vibe of a lava lamp. The WiFi chip is a separate ESP32 that the main chip talks to over SPI, so “async” here means opening a socket on the ESP32, then asking it every 10–20 ms “anything for me?” and rendering frames in between.
When the router doesn’t answer, the lamp doesn’t panic. It halves the last rates it knew, so the effect slowly calms down, and doubles the wait before trying again, up to 30 seconds. The animation never stops.
From bits to blobs
My traffic goes from a few kilobits per second (the house idling) to hundreds of megabits during a big download. On a linear scale, the quiet times would all look exactly the same, a flat zero.
So the lamp uses a logarithmic scale from 10 kbit/s up to the speed of my line:
1def level(self, bps):
2 if bps <= self.min_bps:
3 return 0.0
4 return min(1.0, (math.log(bps) - self.log_min) / self.span)
10 kbit/s is 0, my full line is 1, and every ×10 in between moves the level by the same step. The log scale squeezes everything above ~300 Mbit/s into the top 10%, so on top of it there is a second, linear value just for the “line is almost full” look.
Making it glide
The router answers once per second, and real traffic is jumpy: 200 Mbit/s, then 40, then 180. Fed directly to the lava, that’s a lamp having a seizure.
So every value goes through two stages. First an envelope: it follows the traffic up instantly, but comes down slowly, over about 3 seconds. Then a critically damped spring, the same math as Unity’s SmoothDamp, which eases into every change and never overshoots:
1def damp(x, v, target, smooth, dt):
2 w = 2.0 / smooth
3 k = w * dt
4 e = 1.0 / (1.0 + k + 0.48 * k * k + 0.235 * k * k * k)
5 c = x - target
6 tmp = (v + w * c) * dt
7 v = (v - w * tmp) * e
8 return target + (c + tmp) * e, v
(The real one also checks for overshoot and snaps to the target.) Going up takes ~0.6 seconds, going down ~2.5, so the lamp reacts fast and calms down slowly, like a real lamp cooling off.
Surges
Then there’s the surge, the moment the lamp visibly reacts. The lamp keeps a slow baseline of the traffic, a moving average over about 30 seconds. When a new sample jumps well above it (+0.08 on the log scale, or a big jump as a share of the line, because 100 → 600 Mbit/s barely moves the log), it fires a kick:
- the kick is between 0.55 and 1, bigger for bigger jumps;
- it holds for 0.6 seconds, then fades over ~2;
- after a surge there’s a 2.5 second cooldown, so a noisy download doesn’t turn into a strobe light.
The lava flares, the rain becomes a downpour, the fire throws a shower of sparks.
LEDs don’t see like you do
Colours are a rabbit hole of their own. LEDs are linear: 50% power looks much brighter than 50% grey on a monitor. So in the code I write every colour as it should look (like a CSS colour), and a lookup table converts it with a gamma of 2.2.
There’s a second catch: the panel runs in RGB565, which means only 5 bits for red and blue, 32 levels each. The lookup table aims for the centre of the 5-bit bucket the panel keeps, so the truncation can’t push a colour one level down.
Then the palettes have to move with the traffic. The lava has four of them (calm, hot, upload, hot + upload) plus a “flare” one, and the colour on screen is a weighted mix of all five. Mixing 256 colours × 3 channels × 5 palettes entry by entry in CircuitPython takes ~100 ms. Way too slow.
The trick: Python integers have no size limit, and the math on them runs in C. So each palette is packed into one giant integer, with 768 slots of 16 bits each, one per colour channel:
1def rep(value, n):
2 # value repeated in each of n 16-bit lanes
3 return value * (((1 << (16 * n)) - 1) // 0xFFFF)
4
5acc = 0
6for lanes, k in zip(self.lanes, weights): # weights sum to 256
7 acc += lanes * k # one multiply = 768 channels
8mixed = ((acc >> 8) & self.m255).to_bytes(1536, "little")
One multiplication scales all 768 channels at once, a homemade SIMD on a microcontroller. ~10 ms instead of ~100.
Three lamps in one
The UP and DOWN buttons on the board change the effect, and every 5 minutes it moves to the next one by itself. On top there’s always a small clock and the live download and upload rates. The clock digits are a 4x7 pixel font written straight into the code, digit by digit, as strings of # and .. The arrows blink every time a sample arrives from the router, a heartbeat that says the lamp is still listening.
To test them I ran a speedtest from my PC: first a quiet house, then the download, then the upload. Every GIF below is the same sequence.
Lava lamp
(open full size)The wax is a metaball simulation: blobs that come close merge, stretch a neck and pinch off, like the real thing. Download is heat, so the wax moves faster and glows hotter and yellower. Upload pushes the colour from orange towards magenta. A full line makes the lamp boil, almost white.
Matrix
(open full size)Yes, I know. I’m aware it’s a cliché, but streams of characters falling down is literally what data looks like in my head, so it had to be there. Download makes the rain denser, brighter and faster. Upload is cyan streams of digits going up, against the rain, because they’re leaving the house.
Campfire
(open full size)It’s based on Stefan Petrick’s Fire2018, a well-known noise fire for LED matrices. Download makes the flames taller and faster, upload throws sparks out of the fire, and a full line is a roaring blaze up to the top of the panel.
Cheating, a little
Remember that CircuitPython is slow? That board can’t run a metaball simulation at 32x64 in real time. On my PC, Python is roughly 50 times faster.
So I cheat. A script on my PC (bake.py, with numpy) runs every effect at full quality and saves it as a seamless loop: 50 seconds of lava, 12 seconds of fire, 30 seconds of rain. Each frame stores indices into a palette, not colours: “this pixel is wax at this density”, “this pixel is fire at this heat”. To save space, every frame stores only the pixels that changed since the previous one.
The file format is mine, LMP3:
1"LMP3", width, height, frames, fps x10, number of palettes
2palettes 256 colours each
3offsets table where each chunk starts
4chunk 0 the first frame
5chunk 1..n patches: the new index where a pixel changed,
6 255 ("skip") where it didn't, zlib-compressed
The last patch turns the last frame back into the first one, so the loop has no seam. Making the content seamless is a different trick for each effect: the lava blends its last 4 seconds into the beginning, every movement in the fire is periodic over the loop, and in the rain every stream’s speed is rounded so it does a whole number of laps in 30 seconds.
The results: lava is 750 frames at 15 fps in ~240 KB, rain is 600 frames at 20 fps in ~145 KB, fire is 240 frames at 20 fps in ~410 KB. The fire is the shortest loop and the biggest file, because flames change almost every pixel in every frame and there’s not much to skip.
The rain has one more trick: each pixel stores two brightness levels in one byte, index = a * 16 + b, one for the base rain and one for the extra streams. The palette decides live how much of the extra rain you see, so more download, more rain, without touching a single pixel.
The board just plays the loop back, and everything that has to react to traffic happens live:
- the palette decides the colours, so the lava can go from orange to magenta without re-rendering anything;
- the playback speed follows the download;
- the flame height is a threshold on the stored heat, set live;
- sparks and upload digits are drawn on top in real time.
The lamp is basically a video player whose colours and speed are controlled by my router. Applying a frame is one zlib.decompress and one bitmaptools.arrayblit, both written in C, so the board has time left for everything else.
I also compile the Python files to .mpy on my PC before copying them to the board, because compiling them on the board itself eats RAM that the effects need. And if something crashes anyway, the lamp writes down why and restarts itself instead of showing a black screen.
Pause! There’s also an
attic/folder with the effects that didn’t make it: a wormhole, an hourglass of data, an aurora, rain, warp speed, caustics. RIP.
Living with it
Now when the fire suddenly roars, I know what’s going on: a docker build on my PC, or my NAS pushing its off-site backup. I always knew those things were happening somewhere in the cable. Now they’re on the wall.
The code isn’t public yet, I’ll publish it later.