Friday, May 30, 2014

(#7) Nerd Out Time 

Let's pause for a minute and talk about some nerd specific topics. I've covered off a little bit on the hardware and sensors involved. And I've mentioned that they broadcast events. Let's dig a bit deeper into the nerdosity behind the event mechanism.



Mosquitto


I have zero desire to talk about the Internet of Things here, other than to say I believe it's coming soon and will be big.  Really big. Al-Gore-Internet big.

One of the technologies collecting interest in this space is the publish/subscribe message broker called MQTT.   (Go nuts, read more about it here.)

There are others. I've used a few of them.  But I've decided to invest some energy into using MQTT.  Why?  Because, that's why.  I'm using two implementations of MQTT. My broker, 'C' and Python applications are using the Mosquitto code.

It's really cool and has been working really well.  

On the Java side, I've been using the Paho MQTT Libraries.   Also cool, also working well.


Picture Time!
MQTT is pretty simple.  Run the broker code, someplace on your network. Mine's on a server, in a closet.  But you already knew about the closet part.



The publishers are applications on the network.

My Eaton Home Heartbeat controller code is one publisher.
My LaCrosse Technology WS-2308 AL Weather Station is another publisher.

Let's put off the subscriber for a paragraph or three and dig into those messages.


Events are Represented by MQTT Messages

That's right.  Events that occur in my house end up as published MQTT Messages. When the front door opens, this message is broadcasted:

HHB/STATUS | 2014-05-30 14:10:07 -0600 | 03 | OPEN-CLOSE SENSOR | Front Door | OPEN | 18000 | ALARM ON OPEN | NO ALARM ON CLOSE | DO NOT CALL ON OPEN | DO NOT CALL ON CLOSE | ONLINE | BATTERY OK | CLEARED | 0000010228 |


It's long, but let's bust it up and look at the parts of the message. The first thing to notice is that I use a vertical bar '|' to separate the fields. It makes parsing the string easier.

HHB/STATUS |                The MQTT Topic!
2014-05-30 14:10:07 -0600 | The date / time of the event
03 |                        The sensor type (Open / Close are type '3')
OPEN-CLOSE SENSOR |         The sensor type in English
Front Door |                Where the sensor is
OPEN |                      Open or Closed
10 |                        Number of seconds it's been open
ALARM ON OPEN |             Some Home Heartbeat Specific Stuff
NO ALARM ON CLOSE |         Some more
DO NOT CALL ON OPEN |       And some more
DO NOT CALL ON CLOSE |      And even some more
ONLINE |                    Online of Offline if the sensor is alive
BATTERY OK |                Battery state of the sensor
CLEARED |                   Alarm cleared or 'triggered'
0000010228 |                Unique MAC Address of the sensor

Could it get any more nerdy?
Yes, just a bit more.
Recall I'm looking for three events, in order.  These are the three events, as MQTT messages that I'm waiting for (some fields removed to save space!)


HHB/STATUS | 14:10:01  | 03 | OPEN-CLOSE SENSOR | Door to the Garage | OPEN |
HHB/STATUS | 14:11:01  | 23 | TILT SENSOR | Garage Door | OPEN |
HHB/STATUS | 14:13:01  | 23 | TILT SENSOR | Garage Door | CLOSED |


How hard can this be?
In the onslaught of network events, just write some code that looks for these three events in this sequence.

Said another way:
- write some easily maintainable modifiable code, since I may change my mind about the rules
- that subscribes to all these events
- events coming across as MQTT messages
- hammering the network at about four messages per second

And code that can recognize this pattern of events: open, open close.


This pattern of events.
Recognize this pattern of events.
After I code up this one I'll probably have other patterns to watch for.
Patterns.
Gang of Four - no - wrong pattern.

Patterns of events.
Why does that ring a bell?



Code that watches an event stream for patterns.
And reacts accordingly.


Ding!  This is why Patterns of Events rings a bell!

But first, a commercial for a slick Home Automation product that's sure to be a hit!.

No comments :

Post a Comment