The Little Bird

/images/_fe569f31-060c-4008-90fe-800aa73ad792.jpeg

If you know who Varys is, you’ll know he’s nothing without his “little birds.” They’re the ones who listen to the whispers of power and then tell him all about it.

Well, my varys needs some listeners too.

◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇

When I wrote earlier about this system, I thought I could forego any special-purpose devices. In principle, that remains true. If you have all-modern peripherals that can be polled for their power status, or that at least show up as absent when the power fails, then varys doesn’t need anything more. But my monitors are old enough that they don’t provide a power status I can query, and I’ve moved my 3D printer to a different room.

Long story short, I no longer have anything directly connected to my server that I can probe, other than my network router. Unfortunately, if my ethernet cable ever gets unplugged, that would look indistinguishable from a power failure, so I can’t rely on that alone. I need at least one more device to check.

So I built one specifically for the purpose. I’m calling it…

The Little Bird

It’s a simple device. Just a $5 microcontroller. All it needs to do is respond to occasional USB queries, and then stop responding if the power goes out, which coincidentally, is one of the side-effects caused by the power going out. So the code is actually pretty trivial.

The Microcode

In fact, I can show you the entire program right here:


#include <Arduino.h>

const String PROMPT = "Where does power reside?";

const String RESPONSE = "Where men believe it resides.";



void setup() {

  Serial.begin(9600);

  // wait for serial connection

  while (!Serial) {

    delay(10);

  }

  Serial.println("Little Bird is ready.");

}



void loop() {

  if (Serial.available()) {

    String incoming = Serial.readStringUntil('\n');

    incoming.trim();



    if (incoming == PROMPT) {

      Serial.println(RESPONSE);

    }

  }

}

All it does is listen for a challenge phrase and then provide the expected response, which it will do forever and ever, so long as the power stays on.

The Wiring

This part is pretty simple too. Nothing even needs to be soldered to the microcontroller. As long as it has a USB jack, you can just wire up a custom “power sniffer” cable and plug it in. On one end, the cable is just a standard USB cable coming from the computer. The data lines will go straight through into microcontroller, but instead of powering the microcontroller with the 5V+ line coming from the computer, we splice in the 5V+ coming from a second cable that is plugged into a wall-wart on mains power. (We also tie the two ground lines together to keep the ground reference balanced.)

The whole thing looks like this:

Notice: The connection to the server (via the USB cable) has no V wire connected at all. This is important, because we don’t want any of that mains voltage backfeeding into the computer.

The Microcontroller

In my case, I’m using a NodeMCU board with an ESP8266, because I had a few lying around, but anything with a USB serial port will do.

Server

On the server side, varys needs to know which USB device to talk to, but that’s a bit trickier. I can’t just look for the NodeMCU’s VENDOR:DEVICE code, because I work with a lot of those, any number of which could be plugged in at any time. I can’t hard-code a specific /dev/ttyUSB port for varys to check, because those are assigned randomly at connection time. I could modify the Little Bird’s EEPROM to add custom information to the device descriptor that gets reported to udev, but there’s not much difference between querying each device for a description string and simply asking each one if it’s a Little Bird. So to keep configuration simple, that’s exactly what I’ve done.

The varys service probes every device connected to any /dev/ttyUSB¹ port by sending the challenge phrase. If one of them responds correctly, then we know it’s a Little Bird and that it’s still receiving power. All must still be right in the realm. But if we get no response from anyone, something has gone wrong and the king might be in trouble.

If the routers are also non-responsive, it’s time to move the king into hiding.

¹ Not all microcontrollers use /dev/ttyUSB as the mount point pattern. Yours might be ttyAMA or ttyACM or even something else. Check it, and if it’s not ttyUSB, change the code in varys to match.

Optional Case

I’ve included the STL file for a case that fits my specific NodeMCU and USB connectors, but before you use it, double check the dimensions of all your parts. Especially the NodeMCU, which notoriously comes in a variety of board sizes, depending on manufacturer.

If yours is different, I’ve also included the OpenSCAD source, so you can customize whatever you need.


Read More


/images/_4fe98e2b-fbb3-434a-a1c3-dd80dd391f61.jpeg

Varys - The Power Spy

For years I’ve protected my office server from the harsh indignities of sudden power failures by shielding it behind a traditional UPS battery backup. For more than a decade, this arrangement has kept my pixels glowing through half a dozen actual power outages, and any number of brown-outs and glitches. Over that same period, the internal battery has eventually died and been replaced twice. I don’t begrudge the battery for failing - it’s an ancient battery tech that has more in common with the battery in your car than the one in your cell phone - but the replacements are expensive.

So when the battery failed again this week, I decided it was time to re-evaluate.

/images/PF-Logo-simple.png

PuppetFist

An Arduino-based animation input device that captures your arm movements to control a virtual Muppet.
/images/SunPuckLogo.png

SunPucks

An Arduino-based sunrise alarm light that acts in a coordinated "choir" to fill your room with gentle sunrise wakeup lighting.