## Adding temperature sensors to a hot water heater

I’ve got one of those hydronic home heating systems where hot water from the hot water heater gets pumped to radiators around the house in addition to heating up water for faucets. A few days ago it died on me and threw an error code indicating something was wrong with ignition. I took a look at the igniter and found that it was full of an oxide layer.

After sandpapering it, it worked great, but I ordered a spare for when this inevitably happens again. Along the way, it occurred to me that it’d be kind of fun to have instrumentation on my hot water heater. I just got it up and running.

Way back in my first post about hot tubs, I used OneWire sensors called Dallas 18B20s (datasheet) with an Arduino 2009. They worked great at hot water temperature, so I decided to try them out again. This time, rather than using an Arduino, I’m using a ESP8266 microcontroller. These are cheap and have Wi-Fi, so I can easily get the data into my home assistant setup, just like I did with my mom’s furnace, my doorbell, and other stuff.

Step one is to solder a bunch of sensors together. I wanted to get readings on all the different pipes going into and out of my hot water heater. I went down there and measured how much space I’d need between each sensor. Then I soldered them up. Notably 18B20s can work in “parasite mode” with just two wires, but there are problems with parasite mode on ESP8266’s, and in prototype testing I was unable to get that mode to work. So I just wired them up to 3 wires. This tutorial is a good one for wiring up these sensors.

For software, I already have a MQTT server up and running from my home automation setups, so the easiest thing is to just transmit the data via Wi-Fi/MQTT to my server and then receive/process it with Home Assistant.

The ESP8266 code is pretty straightforward. It connects to Wi-Fi and MQTT and then just sits around for a pre-determine delay (60 seconds) after which it reads the sensors and transmits them. Here it is:

Then the setup in Home Assistant configuration is just a few MQTT sensors. To figure out which sensor was which I had to put my finger on them individually to see which one heated up first.

With that configured, I went to the front end and added a new History Graph card from Lovelace and choose the new sensors by name. An hour later, the data looked like this:

I love it. And of course, I added an e-mail alert when the faucet outlet temperature drops below 30 °C so I’ll be able to swap out the igniters before the thing cools all the way down.

## Making my analog doorbell smart by simply attaching a $7 sensor to it I live in tall and skinny house with a loft on the upper floor. I can’t hear the doorbell going off when I’m up there, especially if I have music playing. This post is about how I extended the range of my doorbell by hooking a sensor up to it that communicates over Wifi to my smart-home, which then plays a doorbell tone over my speakers throughout the house. I already have a reasonably capable smart home based on Home Assistant, so I challenged myself to do this in the cheapest, least intrusive way possible. In the end, I did this with a$7 part and without changing any of the wiring in my existing doorbell (I just had to connect 2 extra wires to the existing transformer).

Here’s a preview of it working:

I used my favorite chip for this, the ESP8266 WiFi microcontroller. When it senses that the button was pressed, the ESP sends a signal to my smart home (via a MQTT message over the WiFi). Then, Home Assistant senses this and plays a recording of the doorbell noise. It can also e-mail me or something if I’m not home.

Doorbells are usually wired like this. The transformer is usually kicking out a good 16-24 VAC when nothing is happening. When the doorbell is pressed, electromagnets slap some metal to cause a chime. At this time, the voltage drops at the transformer because the circuit is temporarily interrupted. That’s a good place to put the ESP8266 to watch for action.

My transformer reads 18.75 VAC on my voltmeter under normal conditions. To experiment before installation, I’ll be using a 6 VAC test transformer, which reads 7.23 VAC on my meter, and which corresponds nicely with the 20.6 Vpp I see measured on the oscilloscope.

The ESP8266 chip has a 10-bit analog-to-digital (ADC) converter on it that functions between 0-1 V, returning digital values between 0-1024. Many development boards, like my Wemos D1, have a voltage divider already in place that takes the max input up to 3.3 V. So I needed to convert my doorbell signal down to 3.3 Vpp. This is the job of a simple voltage divider, plus a diode to chop off the negative side of the AC wave.

We solve: $$V_{peak} = V_{RMS} \cdot \sqrt{2} \cdot x = 3.3V$$ for x

For fun, we can try simulating the circuit with Qucs.

On my test transformer, I tried cutting the voltage by a factor of 10. A voltage divider with a top resistor 9x the bottom one will work. So 1 kΩ and 10 kΩ would be close. Measuring reality with the oscilloscope agrees:

On the production system, I need to drop 18.7 Vrms down to 3.3 V peak, so my voltage divider must cut voltage down to 0.124 the peak, so a ratio close to 1:8 is needed. Honestly, 1 kΩ and 10 kΩ, with a 1:11 ratio should work fine. Let’s simulate it. Theory says it should give 2.4 V. Simulation agrees. Reality will also.

2.4V on a 10-bit 3.3 ADC will be $$1024 \cdot \frac{2.4}{3.3} = 744$$. So let’s set the ESP ADC to consider anything above 720 to indicate BUTTON NOT PRESSED, and anything below 720 to be BUTTON PRESSED.

## Signal Processing and Software

Originally I thought it’d be really easy to read the peaks and detect the drop in voltage. I got it going and hooked into MQTT and Home Assistant really quickly with some simple software.

However, seeing the break in the circuit is not so easy. With some test runs on the real doorbell, I wasn’t able to reliably detect a quick/normal doorbell press. If I really held down on the doorbell button I could detect it, but it wasn’t good enough. So I had to fiddle around in software to find a good read of the ADC with these kinds of pulses.

Sidenote: If you were using MQTT over TLS before and now you have connection troubles, it’s probably because ESP switched to a more robust TLS library and you either need to explicitly allow insecure connections over TLS or load a certificate chain. This took me a while to figure out. More here.

The ESP8266’s ADC isn’t all that fast. In the ESP’s API guide, you can find a really nice sample program around the description of system_adc_read_fast. This is also discussed here with a very similar waveform to what I’m dealing with. I did some tests in the Appendix below.

In the USA, a 60 Hz sine wave will cycle once every 16.6 milliseconds. If I sample enough to capture at least 1 full cycle and save the max value from that window, I can watch for a reduction in max values and trigger off of that. From the appendix, one cycle looks like it will require about 2300 samples with clk_div = 16 to capture. Then, we’ll want to keep the past 20 cycles or so to check/respond in about a 5th of a second. That ought to be fast enough.

I set up two interrupts with two timers: A fast one to read nearly every peak from every AC cycle and a slower one to take a look at the past 20 peaks and see if any of those have dipped below the trigger threshold.

Here’s the fast interrupt:

And here’s the sentinel one looking for button-presses in the data:

Both interrupts re-schedule themselves with their respective timers unless a button-press has been detected, at which point they suspend themselves until something else turns them back on. That something lives in my loop function:

That’s pretty much it. The WiFi and MQTT setups are all pretty traditional. I reset ADC results to HIGH on the ADC 1024 to avoid echo-triggers after one gets sensed.

This arrangement of the dual timers and windows and stuff is the result of a bit of trial and error. I’m pretty happy with its ability to sense short variations in the peak AC signal. I cannot press it fast enough to make it miss the read. Yay.

## Automation setup in Home Assistant

Now that the doorbell status is just a MQTT topic that we will monitor, the configuration in Home Assistant is pretty straightforward. First we need a binary sensor based on the MQTT topic:

And of course the automation:

That’s it! It works great and plays throughout the house thanks to my Snapcast setup. Total incremental parts cost for a smart doorbell: \$7.

## Appendix A: How fast is the ESP8266 ADC?

I figured I’d just do some experiments with the ADC to help me understand how fast it really is. In summary, I was able to read 16384 samples that captured 8 peaks of a 60 Hz signal, so the fastest sample rate is roughly 123,000 samples/second. Not great, not terrible. Totally sufficient for this kind of work though, for sure.

The code used to do these demos is below. I just copy/pasted the data from the serial monitor into a text file and wrote a quick plotter in Python to graph the data. (Interestingly I couldn’t copy/paste all from the console output b/c one of the garbage characters halted the paste, so I shift-clicked to copy the large block quickly).

The code uses a timer to call the system_adc_read_fast function once and then prints it out and then just sits forever. I triggered the software watchdog a few times so I put that wdtFeed call in there to prevent it from resetting me all the time.

## My book showcasing some open-source tools is now available

A teaching fellow at the University of Michigan once asked me if I could provide some career advice to her nuclear engineering students. I started off with basic industry knowledge but soon came upon computers. When hiring people onto a computational physics team, I always expect a certain level of computer-savvy. But it occurred to me that it’s probably challenging for a young student to catch up on all the classic computer goodies. So I wrote a book about them.

Digital Superpowers hit the shelves of Amazon as an e-book tonight. It covers a bunch of open-source tools in an informal and broad tour, starting simple and building from there. It covers a few computer basics and introduces you to package managers before delving into short and sweet hands-on follow-alongs (and sometimes just drive-bys) with virtual machines, regular expressions, pdf toolkit, TOR, GnuPG encryption, LaTeX, Sphinx, pandoc, graphviz, Imagemagick, GIMP, Inkscape, darktable, Blender, Openshot, Audacity, LMMS, Hydrogen, Mixxx, git, Python, Django, Home Assistant, ESP8266s, Raspberry Pis, and self-hosting (among others).

Try it out and let me know what you think.

## Reading “The Rise and Fall of the Third Reich” by William L. Shirer

While traveling this year I ended up visiting a Gestapo museum in Cologne as well as the Holocaust museum in DC. Wanting to learn more, I recalled my college years when my history-major friend Kris was lugging around this immense book with a swastika on the cover. I procured the (much lighter) Kindle version of it and went to work. Well, months later, I have finished it. It was an incredible experience to read: artfully written and absolutely dense with fascinating information. Terrifying but exciting. Gruesome but essential.

Hitler and the Nazis are quasi-taboo today in that while they’re brought up in every internet argument, such comparisons are dismissed as unattainable. They were so evil, people say, that it’s offensive to even begin comparing anything modern to them. But 1933 wasn’t all that long ago from civilization’s perspective, not to mention evolution’s. Human brains today are nearly identical to the brains of that time.

I totally reject the idea that Germans are some how pre-dispositioned as a people to have been the spawning ground of Nazism (aka Sonderweg). This book goes there a little and has been criticized for doing so. It’s clear that what happened there could have happened anywhere given the circumstances, and so we must be wary of it happening again in the future given any similar circumstances. Desperate, beaten, embarrassed people were conned by their own cognitive dissonance and one truly charismatic (yet crazed) leader into thinking it was some outsider’s fault, and that eliminating the outsiders would solve all the painful problems. The results were catastrophic.

But anyway let’s get to the book. Here are some notable things that I really had no idea about. I’m obviously no student of history so excuse me if this is all common knowledge.

Continue reading Reading “The Rise and Fall of the Third Reich” by William L. Shirer

## Some fun or spooky Halloween automations with Home Assistant

It’s that spooky time of year again, but this time it can be extra spooky with the help of home automation.

## Motion-activated jumpscare on TV

One classic spooky thing to do is have a TV do something scary when people walk by. This is easier than ever on any TV now that everyone has Raspberry Pi with HDMI output and z-wave (or other) motion sensors everywhere. Check this out:

## Authenticating and populating users in Django using a Windows Active Directory and SASL

I’ve been trying to get some Django stuff running that can securely authenticate users against Windows Active Directory and also populate some info (first/last name, email address, maybe groups etc.). There are lots of resources out there but nothing was fully complete or modern and it took me some figuring/hacking to get it done.

Resources I found include:

• django-auth-ldap — the normal LDAP plugin. Problem: It does not natively support SASL and simple binds would send clear-text passwords. I think normal people would just activate TLS in this case but I didn’t want to do that
• A relevant SO post — with an answer linking to a useful snippet that no longer works on recent django versions.
• django-auth-ldap-ad — Someone’s entire different ldap plugin made specifically for this purpose. But it isn’t being maintained, is GPL-2, and doesn’t work directly in Python 3 or recent django.

## I think my new motorcycle gloves “melted” my Oxford Heater grips

This may be a coincidence but about a week after I got new motorcycle gloves, my Oxford Heater grips started like, deteriorating away. Check this out:

It has been hot recently, but this is still very odd. I got these Street & Steel V-74 gloves from RevZilla. Maybe it’s the Gel Palm leaking out and reacting with the grips? Who knows. Anyway it’s sticky and gross and annoying. Poor Oxford Heaters, I love those things.

Has this happened to anyone else!?

## Multi-room audio over Wi-Fi with PulseAudio and Raspberry Pi(s)

NOTE/UPDATE: After an update this kind of stopped working and I struggled with it a lot. Now I actually recommend using snapcast instead of this solution. It works better!

I moved to a new place and it has more than one room. Naturally, I hooked up the stereo in the living room and tested it like my dad taught me: by playing “Money For Nothing” really loudly. It worked. But wait a minute, there’s an upstairs now… how will I get it playing up there? I could always use the wifi network and raspberry pis to beam audio around. Yeah, let’s do that!

## The Hero Jr. personal robot from HeathKit: a 1984 product way ahead of its time

One of my first memories is a vision of lying near my dad in the basement in the mid-1980s while he endlessly soldered away at some big project. Later, I spent a lot of my childhood messing around with the product he was assembling: a Hero Jr. robot. This was a educational personal robot, intended to be your “friend, companion, and security guard.” Here he is:

Hero Jr. has a sonar, infrared motion sensor, light sensor, sound sensor, radio-frequency remote, drive motor, obstruction sensor, and a RS-232 serial port. His out-of-the-box features included a security guard mode, alarm clock, poetry, singing, and (my favorite) the ability to explore around the house, often while singing America, Daisy Bell, or Little Miss Muffet.

## Idea: Communal voice assistant that chimes in during fact-based discussions

With all the AI voice assistants around today there are lots of interesting applications people are dreaming up. Here’s another one.

You could set your voice assistant on the table and start having a discussion or debate that inevitably involves bringing up facts about news or history or how something works. A lot of times when someone doubts what was said a phone will come out to do some fast wikipediaing or other searching. If a AI could somehow either know or be triggered to check something, that’d be an interesting new dynamic to the conversation. It could do things like:

• Correct misquotes and other slight error in the discussion, e.g. “Actually, the NOAA temperature data were corrected in 1950 because the volunteer network switched from morning readings to afternoon readings.”
• Look up details and say them when they’d help

It’d have to be a really smart AI to know when its utterances would be useful in a dynamic conversation. It could start by just lighting up when it thinks it has something to contribute and people could allow it to chime in, rather than having it chime in only when someone wakes it. Then eventually once it’s smart enough it could chime in on its own. The future is fun.

## Helping an artist with a Morse code protest chant installation in Denmark

A few months ago, I got an email from a Sweden-based artist and teacher named Mary Coble asking if my Morse code laser post could be helpful for an art installation she was working on. She’s used Morse code in previous installations like one in Toronto where she sent coded light from a giant dome to people below who then relayed signals with flashlights. As a part of a new project, she wanted people to be able to enter protest chants into a website and then have a system convert the message to code and flash a light in a gallery window. Did I want to help? You bet I did!

I got super excited about the prospect of helping with this and knew that with a combination of things I’ve used before it would be really doable. The plan was to have a webserver accept messages from a form and transmit them to a Raspberry Pi (cheap mini-computer), which would then flip pins on a relay to blink the light, like this:

After many emails and some ups and downs, everything worked! This really feels like how the internet is supposed to work.

## A (medium-length) primer on energy, greenhouse gas, intermittency, and nuclear

Thanks entirely to the efforts of local climate-related organizations in Seattle, I’ve now spoken at a handful of book stores, breweries, universities, and even Town Hall on climate and energy. Last week, I was honored to be on one such panel at a brewery in Ballard alongside Univ. of Washington oceanographer LuAnne Thompson and Governor Inslee’s senior climate policy advisor, Reed Schuler. My role was to provide background information on the human relationship with energy: what we’ve used in the past, what we’re using today, and what our low-carbon options are moving forward.  I touched on progress and challenges with intermittency, hydro, and nuclear. This post summarizes and expands upon these topics.

### Energy is a replacement for the labor of human beings

The first part of my talk was easy. I threw up my favorite slides demonstrating how energy improves quality of life by replacing human labor. Between construction, farming, heating, water, laundry, and travel it’s a pretty easy case to make.

## A fancy Home Assistant automation that checks the weather and figures out when to turn on your heater

So in the continuing saga with my mom’s home-automated furnace, it got extra cold recently and I noticed it wasn’t getting up to temperature in time for her to wake up. I figured I could come up with a formula to compute the time needed to come to temperature and turn on the furnace at a dynamic time in the morning so it’d be just right.

## Getting your Ubuntu 17.04 computer set up for the fast.ai deep learning courses

I’m becoming convinced that Jeremy Howard is right to predict that deep learning is going to be “more important and more transformational than the internet.” If you don’t know who Jeremy Howard is, he’s part of the duo behind fast.ai free and high-quality deep learning course series, which is dedicated to making deep learning accessible to everyone.

Deep learning takes advantage of certain graphics processors (GPUs) to be efficient. If you take the course, it’s recommended that you sign up for an Amazon Web Services machine with an appropriate GPU so you can just run the provided setup scripts and be on your way learning deep learning. But you may want to try to get everything set up on your own machine if you happen to have one. I just built a small server and added a modest GPU just for this purpose so I figured I’d give it a whirl. This is how I did it.

## Mobile gas station to fill-up self-driving vehicles while they are in motion

You know how some airplanes can get their gas filled up while in the air by tankers (aerial refueling)? And how ships at sea do this too (underway replenishment)? And you know how self-driving cars and trucks are taking over everything soon? Well there’s going to be a need for mobile gas stations on the road.

Think of it! Long-haul trucking will want to go non-stop, and to do so there can be little sections of road where a tanker truck drives alongside the main truck, hooks up a hose, and refuels it for 10 minutes while everyone’s moving. Then the self-driving truck carries on and the tanker truck crosses the road, services a truck going in the other direction, and repeats until it eventually has to fill up from a bigger tank nearby.

Another manifestation is a thing on a long rail that refuels you as you drive alongside it. The hose could be on a sliding coupler that maintains a hermetic seal.

This could happen with passenger vehicles too. Presumably people will hop in their cars at night and expect to wake up in Florida the next day so they’re going to need automated gas refilling as well. Ideally this would be underway but I guess if gas stations could just fill up cars that roll in that’d be acceptable too. It will be more comfortable and less disturbing if this happens while on the road though.

That’ll be a billion dollar industry soon. If they’re electric cars, these will be charging stations instead of refueling stations.

## Building a NAS server/home server in 2017

I decided I wanted a network-attached storage (NAS) server because I needed some central and safe place to put all my big files. I’ve been using more and more hard drive space because I’ve been taking photos in RAW and collecting more digital video (camera, dashcam, digitized home videos from the 1990s, and drone). I also just enjoy fiddling with servers and stuff and thought I could use a home server for a variety of other things. My raspberry pi has been doing well for my home automation but a bigger server might make it faster. I’m trying to learn Blender and have been eyeing a Machine Learning course. Both of those require a nice modern GPU. Finally, I just enjoy learning things about computers.