I have begun to learn how to program my NodeMCU.

Granted, all it’s doing here is running the example “Blink” program that comes with the Arduino IDE, but the important point is that the little light is turning on and off because I uploaded some code that tells it to.

It’s not connected to a computer here. The USB cable is simply plugged into a power outlet.

Smart Home Adventures

Ever since I bought my own home nearly a year ago, I’ve
become increasingly interested in making it smart.

Right off the bat, I feel like I should clarify what that
means to me. The ability to turn some lights on or off with an app is not
smart, in my opinion – the smart way of controlling lights is by flicking a
switch conveniently located in the room you wish to illuminate.

A smart home needs to be much more intelligent. It’s about
automation. It’s about the home being able to notify me if something is
happening that I need to know about. It’s about being able to accomplish things
with minimal difficulty, not adding complexity and more steps.

That’s where off the shelf “smart home” solutions really
started to fall down for me. I could spend hundreds or maybe even thousands of
dollars, for what? The ability to turn on my living room lights while I’m still
at the office? Why would I ever need to do that?

Nevertheless, the lack (in my opinion) of a pre-packaged,
useful, holistic solution that accomplished my vision of what a “smart home”
should be didn’t deter me from tackling things bit by bit. It started with our
burglar alarm. It has internet connectivity which sends me alerts in the event
that something unexpected is happening, and lets me arm or disarm the system
from my phone – which I actually do find useful.

Next up was our thermostat. The one that was installed when
we bought the house was an old-fashioned one with a simple mercury switch
inside. You set the temperature, and that was it. We replaced that about a
month ago with something programmable (it doesn’t need to be as warm in here at
night as it does during the day; it doesn’t need to be as warm if nobody’s
home), and I took the opportunity to get one with WiFi so I can set the
temperature remotely. That’s not useful in and of itself, but if you take that
functionality and look at it in the context of my wider vision then the
thermostat is certainly something I’d like to be able to programmatically
control.

It was around this same time that I discovered home assistant, and now my dream is
starting to come alive.

image

Home Assistant is an open-source project that runs on a
variety of hardware (I was originally running it on a Raspberry Pi, and I’ve
since switched to running it in a Docker
container on our home
server
). It has a ton of plugins
(“components”) that enable it to support a variety of products – including our
existing alarm, thermostat, streaming media players, and others (including,
somewhat ironically, the colour-changing lightbulbs we have in our family
room). It includes the ability to create scripts and automations, it uses our
cellphones to know our locations, and can send us push notifications.

My initial setup was all about notifications. If we both
leave the house but the burglar alarm isn’t set then it tells us (and provides
an easy way to fix the issue). If we leave one of the exterior doors open for
more than five minutes, it notifies us (or just one of us, if the other is
out). I also created a dashboard (that you may have seen in my last post) to display some of this stuff on a monitor in my office.

Since installing the thermostat I’ve added more automation.
The time we go to bed isn’t always predictable, but when we do go to bed we set
the alarm. So, if it’s after 7pm and the alarm goes from disarmed to armed, the
thermostat gets put into night mode. If nobody is home then the temperature
gets gradually turned down based on how far away we are.

If nobody is home at dusk then it turns on some lights and
streams talk radio through the family room speakers to give the impression that
someone is.

This stuff meets my definition of smart, and I’m barely
scratching the surface. The open nature of the platform not only means that I’m
not tied to a particular vendor or technology, but also means that I can add on
to the system in a DIY way.

Which is exactly what I’m going to do. I’ve bought some NodeMCU microcontrollers which are
WiFi enabled, Arduino IDE-compatible
development boards designed to the basis for DIY electronics projects.

Watch this space, because over the coming months I’ll be
connecting our doorbell, garage door and laundry appliances to Home Assistant.
I’ll be learning as I go, and I’ll share the hardware and software.

Smart Home Dashboard

Recently, I have been all about turning our home into a smart home. That’s mostly because I’ve discovered Home Assistant: a little piece of software that runs (in my case) on a Raspberry Pi and pulls together data from all the sensors and smart devices you own.

Apart from a new WiFi thermostat that I was going to buy anyway I haven’t yet spent any money at all in my quest for this – I’ve merely connected together the devices we already owned (Chromecasts; family room lights; our burglar alarm), added in some data available through various APIs (weather; internet speed; server status) and pulled location information from our phones using an app that publishes the information to my own server.

Anyway, now that I have all this information in one place I wanted an attractive way to display it in my office. Happily I’d already bought a monitor with a built-in powered USB hub from a surplus equipment sale a while back, with this kind of project in mind.

So, my Friday afternoon project today became hooking up another Raspberry Pi to this monitor and crafting a HTML dashboard for it to display. Not everything works yet, but I think it’s pretty good for an afternoon’s work.

(Since I didn’t remove the clock from the frame when I shot the video, I can’t pretend this thing is quick to boot. I may have manipulated the video speed at a couple of points. But anyway).

Oh, and watch this space for some home-brew smart home devices, coming soon!

Using JavaScript to Identify Whether a Server Exists

Recently, for reasons I’m sure I’ll write about in the
future, I needed to find a way to use JavaScript to test if either of two
web-locations are accessible – my home intranet (which would mean the user is on
my network), or the corporate intranet of the company for which I work (which
would mean the user is on my organization’s network). The page doing this test
is on the public web.

My solution for doing this test was simple. Since neither
resource is accessible publicly I put a small JavaScript file on each, then I
use AJAX and jQuery to try and fetch it. If
that’s successful, I know the user has access to whichever intranet site served
the request and my page can react accordingly.

If neither request is successful I don’t have to do
anything, but the user doesn’t see any errors unless they choose to take a look
in the browser console.

This all worked wonderfully until I enabled SSL on the page
that needs to run these tests, then it immediately fell apart.

Both requests fail, because a page served over HTTPS is
blocked from asynchronously fetching content over an insecure connection. Which
makes sense, but really throws a spanner into the works for me: neither my home
nor corporate intranet sites are available outside the confines of their safe
networks, so neither support HTTPS.

My first attempt at getting around this was to simply change
the URL prefix for each from http:// to https:// and see what happened. Neither
site supports that protocol, but is the error that comes back different for a
site which exists but can’t respond, vs. a site which doesn’t exist? It appears
so!

Sadly, my joy at having solved the problem was extremely
short lived. The browser can tell the difference and reports as much in the
console, but JavaScript doesn’t have access to the error reported in the
console. As far as my code was concerned, both scenario was still identical
with a HTTP response code of 0 and the status description worryingly generic “error.”

We are getting closer to the solution I landed on, however.
The next thing I tried was specifying the port in the URL. I used the https://
prefix to avoid the “mixed content” error, but appended :80 after the hostname
to specify a port that the server was actually listening on.

This was what I was looking for. Neither server is capable
of responding to a HTTPS request on port 80, but the server that doesn’t exist
immediately returns an error (with a status code of 0 and the generic “error”
as the descriptive text), but the server that is accessible simply doesn’t
respond. Eventually the request times out with a status code of 0 but a status
description, crucially, of “timeout.”

From that, I built my imperfect but somewhat workable
solution. I fire a request off to each address, both of which are going to
fail. One fails immediately which indicates the server doesn’t exist, and the
other times-out (which I can check for in my JavaScript), indicating that the
server exists and I can react accordingly.

It’s not a perfect solution. I set the timeout limit in my
code to five seconds, which means a “successful” result can’t possibly come
back in less time than that. I’d like to reduce that time, but when I
originally had it set at 2.5 seconds I was occasionally getting a
false-positive on my corporate network caused by, y’know, an actual timeout
from a request that took longer than that to return in an error state.

Nevertheless if you have a use-case like mine and you need
to test whether a server exists from the client perspective (i.e. the response
from doing the check server-side is irrelevant), I know of no other way. As for
me, I’m still on the lookout for a more elegant design. I’m next going to try
and figure out a reliable way to identify if the user is connected to my home
or corporate network based on their IP address. That way I can do a quick
server-side check and return an immediate result.

It’s good to have this to fall back on, though, and for now
at least it appears to be working.

Being Smarter by Not Thinking

There’s a popular
myth that says we only use 10% of our brains
.

It’s simply not true. Studies (including the source of all scientific truth: an episode of
MythBusters) have proven that all areas of the brain have a function, and while
the percentage that we’re “using” at any given time varies by task it can
certainly exceed 10%.

image

One thing that seems very obvious to me without needing to
cite a study about it, however, is that I certainly have unused brain capacity,
and it can do amazing things when you leave it to its own devices.

As an example of what I’m talking about, I refer you to a
link I posted on this very blog some time ago: Why
Great Ideas Always Come in the Shower (and How to Harness Them)
.

In the brief commentary I added, I mentioned that never in
my life have I had a good idea in a meeting. Great ideas come to me while I’m
doing other things. Specifically, other things that do not take much in the way
of thought and offer little in the way of distraction: things where my brain
gets left to it’s own devices and has an opportunity to wander – showering,
certainly, but also commuting, trying to get to sleep at the very end of the
day (infuriatingly), and when I’m at the gym.

Talking of the latter one, I haven’t been to the gym for
quite some time.

When we lived in our apartment there was a gym in the
building, and that was great. I could easily fit in a solid 45 minutes there at
lunch. Any spare 30 minute window in my schedule could be turned into 20
minutes on the stationary bike.

I want to go back, but now that we’ve bought the house there
is obviously not an on-site gym. There’s a gym at the office (20 minutes away)
and a Goodlife Fitness close by (10 minutes away) where I’d get a discounted
rate, but small though it is even that travel time is putting me off. I will
most likely join Goodlife, since I rarely go to the office these days and
installing a home gym just isn’t in the budget right now, but I’ve been missing
the ability to easily take 30 minutes and get some exercise, and I’m sad that
none of the solutions will offer me that. In the absence of a perfect solution,
I haven’t done anything at all… until yesterday.

Since the weather here in Calgary is distinctly spring-like
these days, I went for a walk before I started my work day. I didn’t go far – a
little less than 2km, according to the Google Fit data from my phone and watch
– just down the road a bit and then back along the pathways that run through
our neighbourhood.

I liked it so much I did it again at lunch time, and then
for a third time this morning.

The physical benefits of this, though I’m sure not huge by any
means, are probably much needed at this point. Really though what I like about
it so much are the mental benefits. I’ve never been much of a morning person
and I would never consider going to the gym before work, but rolling out of bed
and attempting to be productive more or less immediately is not a recipe for
success either. Feeling like my day has already started by the time I sit down
to get some work done definitely gives me a mental boost that I’ve been able to
capitalize on. More significantly though, there’s a lot to be said for the kind
of problem solving that can only come from not thinking about something too
much and letting my subconscious guide me in ways that I’d never have come up
with if I were sitting at my desk consciously trying to focus on something.

It’s amazing what you can do when you’re not trying to do
anything.