Patriot 4: Swift Programming IoT Home Automation

I’ve turned a corner in my thinking about how best to automate my home. For years now I’ve been trying to integrate and generalize all of the various technologies (MQTT, SmartThings, Particle.io, Alexa, iOS, Raspberry Pi, etc) to provide a simplified interface for controlling everything. The tough part is really describing what I want the system to do; how it behaves. Using an any sort of text or table driven, simplified, abstract programming model always comes up short. By this I mean the sort of approach where I am allowed to specify things such as “if device A is on, and the time is after HH:MM PM, then set device B to XX”. This works great for doing fairly simple things, but doesn’t really handle the more complex types of scenarios I always run into, or if it does handle it, it does so in a manner that gets very complicated and unmaintainable very quickly. As a professional programmer, this situation is very familiar, and describes what happens with poorly written code. So I started thinking about how to solve this problem in a way that would be greatly extensible and very maintainable.

And I got to thinking about how Patriot automatically detects new devices. Well, sort of automatic. If I program a Photon to expose a device, and write the Photon code to implement that device, then Alexa can detect it and control it with voice. That’s very cool, but I’m getting tired of having to tell Alexa which lights to turn on and off every morning and night. I want my home automation system to know when to do that based on the plethora of sensors that I’ve installed.

So then the big ah-ha moment for me. Automatically detecting and installing devices doesn’t provide any automation until I write some sort of specific code to control it. This could be generalized code such as IFTTT, or bare bones custom code like Photon C++ or Python code on a Raspberry Pi.

One of the issues that frustrates me is the large number of different programming languages and platforms I’ve had to work with. So recently I decided to replace my Raspberry Pi with a used Mac Mini in order to reduce the number of different programming languages, environments, and platforms I have to remember and work with. The 2010 Mac Mini is quite powerful and flexible, and I found one on craigslist for $150. It’s now my media center as well as my MQTT broker and home automation controller. And I can program it in Swift, which is my daytime job programming language.

So if I have to write code no matter what, why not do it using the best tools, environment, and clean programming practices? To my mind that means Swift programming on the Mac Mini.

So that’s where I’m going with Patriot 4. I’m writing a Mac app in Swift using clean, TDD code. I’ll be sharing the Swift framework that is evolving out of this, but it’s going to require Swift programming skills in order to use it. I’m excited to see just how far this can go.

Home Assistant to SmartThings and Back Again

How many programmers does it take to control a Z-wave light bulb?

I consider myself fairly technical, but I’ve got to say that trying to understand and control Z-Wave and Zigbee devices sure is confusing. And don’t even get me started with how complicated trying to program them has been. It all began when I decided that I needed to control a regular 120 vac light bulb in a desk lamp on my computer desk. As you can tell from all my earlier posts, I’ve done all my automation so far working with 12v DC, and prefer it that way. So it seemed like just using a Z-wave or Zigbee light bulb would be the easiest approach; no wiring required.

I did some early investigation, and saw that Home Assistant and SmartThings were a couple viable options. So I started putting together a Home Assistant system, while looking at ways for it to control either Zigbee or Z-Wave.

It just so happened that there was a Z-Wave sponsored contest starting, and applicants could receive a free Raspberry Pi + Sigma USB Z-Wave controller + choice of any Z-Wave device. So I applied, was accepted, and chose the Zipato RGBW2 light bulb. Off to a great start!

Home Assistant is an amazing open source project! I like it. I tried using it to control the light bulb via the Sigma controller, but could never quite get it working fully. Oh sure, I could turn the light on or off, but not control the RGB or 2 types of white light. Plus the darn Raspberry Pi 3 kept hanging overnight.

So, in typical programmer fashion, I decided I’d just cut out the middle man and write my own code to control the light bulb. Well, the scarcity of examples and lack of any tutorials made this a very frustrating effort. I never did get the Z-Wave bulb to respond to any of my own code. So when my maximum frustration threshold was exceeded, I decided to punt on writing my own code and give SmartThings a try.

Samsung has put together some pretty extensive, well written developer documentation. So I was initially very optimistic. I ordered a kit that included the hub plus a bunch of sensors, and quickly had the light bulb turning on and off in response to motion and doors opening. However, going from there to integrating with all my other automated lights and Alexa skills was again very challenging. I eventually was able to get everything integrated together by writing a SmartThings Device Handler and Service Manager that could together automatically detect and control my Patriot devices, but then what? In order to automate stuff, I had to write a bunch code, and organizing all this code quickly got out of hand.

In my googling about all this, I came across several posts that shared my experiences; SmartThings can control a lot of stuff, but using it to automate things required writing lots of code. Some of these posts described that Home Assistant was really good at solving that problem. I also read where the Home Assistant guy (folks?) are looking into using machine learning to establish automation rules. That’s the direction I’ve been trying to go. So maybe I’ll throw my hat into that ring.

So I’m back to working with Home Assistant. And I was delighted to find that in the several months since I looked at it before, installing Home Assistant has become simpler and easier.

There are options for connecting SmartThings sensors to Home Assistant using an MQTT broker, and I may take a look at that later. I’ll eventually want to be able to interface with Zigbee devices, and using the SmartThings hub is one option. But we’ll see.

Oh, and the Raspberry Pi hang? I finally figured out that it was caused by leaving an open SSH connection overnight. So now I exit SSH when I’m done using it, and haven’t had any more hangs.

Backup Internet for my Alexa and Particle.io Devices

I’m really enjoying using Alexa to control the lights in my home. One down side to this however is the requirement to be connected to the internet. Without an internet connection Alexa becomes a lazy idiot.

SmartThings, TPLink Nano, and iPhone

Since I live in an RV, I won’t always have a fast internet connection. So what I’ve done to deal with that is to have two separate hot spots, only one of which is ever active at any given time.

When my fast internet (was Roadrunner, will soon be Google fiber) is available and working, it transmits to all my devices.

When I’m on the road, or the fast internet breaks (like for the past 5 days!), I plug in a TPLink Nano Wireless N router. This little $35 box is very versatile, and can handle receiving and/or transmitting on Wifi and/or ethernet.

For example, as shown in the image, the SmartThings hub needs an ethernet connection. So the Nano is configured as a “Client”, to receive WiFi from the iPhone and convert it to an Ethernet connection for the hub.

Likewise, with the Nano connected to my normal WiFi router, I can configure the Nano to receive WiFi at whatever RV Park I happen to be staying, and distribute it to all my WiFi and IoT devices without having to reconfigure anything except the Nano.

SmartThings Control of Photon Devices Using Patriot

SmartThings

So having finally received my SmartThings hub and a few devices, I’ve spent the past week learning how to program it to interface with my existing Particle.io Photon controllers running Patriot. It turns out that SmartThings has a fairly nice architecture that made automatically discovering my existing devices fairly easy. I needed to write a Service Manager SmartApp and a child Device Handler. Altogether this was about 200 lines of Groovy code (basically Java). The service manage interacts with Particle.io to locate each Photon controller on my account, and then ask it what devices it supports. It then creates a child device for each using the name exposed by the Photon. Voila!

This is essentially the same approach used by the Patriot Alexa Smart Home controller that I published for Alexa.

So yesterday when I finally got the kinks worked out, I reinstalled the Patriot Service Manager SmartApp that I’d written, and it automatically discovered my two dozen Patriot lights and added them to SmartThings. Woohoo!

So now I just need to explore the Alexa implementation on SmartThings and see if my Alexa Smart Home skill is still needed. At this point it looks like it can be completely replaced by the SmartThings Alexa support.

Switching to Samsung SmartThings

After a frustrating few weeks of unsuccessfully trying to get Home Assistant working with my Patriot Particle.io devices and Z-Wave bulbs, I’ve decided to switch directions and won’t be investing any more time on Home Assistant. While I really appreciate all the hard work that folks have put into Home Assistant, it just isn’t a good fit me.

So I am switching gears and am investigating integrating Patriot with the Samsung SmartThings hub instead. This will give me both Z-Wave and Zigbee control, and some level of operation when disconnected from the internet.

I’ve read through much of the SmartThings developer documentation, which is very good. I like the architecture, and clear descriptions of how to integrate 3rd party hardware.

I ordered a SmartThings Monitoring Kit from Amazon that should be here tomorrow in time to have some of my Holiday time off to play with it. This kit includes the SmartThings hub, a couple door sensors (with vibration and temperature sensing), an a/c switched outlet, and a motion sensor.

In searching the developer forums I’ve located some posts by others that have been successful in integrating SmartThings with the Particle.io cloud, so I’m confident that in no time I’ll have it controlling the plethora of lights, fans, and awnings that I currently control using Particle.io Photons and Patriot.