Monday, September 7, 2015

Yes, I'm alive

I've been suffering from blog withdrawal, and folk have contacted to make sure I'm still alive. So, I thought I'd at least write a bit about what I've been doing recently.

I've been doing things that aren't of a technical nature. I've dug some trenches and holes with the new (to me) tractor, and put a lot of money into my other tractor to get it running again. Here's a picture of my agricultural fleet:

I got the little tractor when I first moved out here and have been fixing things on it over time. It died recently because the fuel pump wore out. If you're familiar with diesel engines, you just cringed a bit. Diesel engine fuel pumps are expensive, and some of them are a real pain to replace because they set flow with shims and timing with various techniques. Fortunately, this is a three piston pump with each piston supplying an injector so there's no timing needed. I did have to set the flow with shims, but I guessed right the first time.

The pump cost me $535 and then I had to replace other stuff as well since messing with the old hoses and things broke stuff. All in all I have another $650 or so invested in it, but I need the little tractor to fit in places that the yellow monster can't get to. When I was all done, it started when the first cylinder hit the compression cycle; perfect.

Now the little tractor is off the to-do list, the trench is good enough to keep the flood waters from getting to the house, so I'm moving the big tractor into the garage for some work. Someone before me decided to fix the dashboard on it and got in over their head. None of the gauges work, and I want to know the oil pressure and temperature at an absolute minimum. I couldn't care less about the total hours, but it would be nice to know the battery is charging. I guess I get to learn all about monitoring tractor vital statistics next.

And yes, i can fit the tractor into the garage. I have an empty RV garage that I built with the house, but then couldn't actually afford to keep the RV on the road because fuel prices went totally out of sight. I sold the RV and use the space to set up tools for various projects. It's really nice to be able to set a table saw beside a band saw and move from one to the other as I build something.

A man can't ever have a big enough garage.

I really haven't done squat on the technical aspects of the house. Yes, I keep experimenting with MQTT to understand more about it and I'm very slowly converting the various devices to use the toolset, but progress is slow because I keep thinking about hydraulic pressures and depth of culvert openings to handle water flow.

I'm easily distracted.

Speaking of culverts: The driveway to my place kept getting a deep rut dug into it by the monsoons here. See, we don't get much rain, but when it does rain, sometimes it comes down in a real torrent. We sometimes get three inches in twenty or so minutes and that causes flash flooding. I'm well away from the most dangerous areas for this, but as last year would attest, I'm not invulnerable to water coming down a hill and making a mess:

You should be able to see how high the water got on the house in the picture. When I realized I couldn't keep the water out of the house, I just opened the doors on the other side and let it out. That left me with about a half inch of water in the house and my wet vacuum was able to clean up the mess in a couple of days. I have tile floors, so all I had to do was mop up and everything was fine. It took about three weeks to clean out the pool though. I got seven wheel barrows of mud out of it before I was done.

Anyway, back to the culvert. When I did some digging around the rut in the road, I found caliche near the surface. That limited the size of culvert pipe I could put in, so I used three 10" pipes instead of a single 18" pipe. Metal culverts need some sand in the bottom and then a covering with something that didn't have rocks and could be compacted. The sand I had in abundance, but I had to get 30 ton of aggregate to fill it back in. It turned out nice and the 16" rut is gone from the drive. The driveway services four houses, so I did a good deed for the neighbors as well.

I want to admit to something though; I didn't use my yellow tractor to dig the culvert channel. The big yellow tractor was brand new to me and I didn't have a clue how to use it. I begged a neighbor for a favor and got him to bring his big ol' Cat backhoe over and help out.

Then another neighbor from down the road came up to watch and brought his skip loader with him. I had an entire crew of folk out there messing around with the road. I totally love my neighbors! It took all day because we dug up a telephone line (mine) and had to wait for them to show up to advise us. Yes, they were responsible for it because I called in advance and had the lines marked and they missed it. It really does pay to follow the rules when your digging with a machine; if I hadn't had it marked, they would have made me pay for fixing the line.

The before picture:

It's hard to tell from the picture, but the right hand side of the rut is over 16 inches deep. It made for an interesting drive for people with normal cars; they had to pull way over to the side and go very slow. The paint marks are where they marked the various lines for power and phone. They missed.

That gray thing in the hole is what's left of the conduit and MY phone line. It was a good eight feet from where they marked it should have been. The picture below is just before I was finally able to cover the pipe. It took them over a week to decide who was going to come out and fix it. I finally got them to do something when I told them it was just a matter of time before someone drove into the hole and decided to sue because of the damage to their vehicle. I think the idea of lawyers was enough for them to actually do something.

Just before I was finally able to cover it:

The folk among you that have done something like this are wondering what I did with the dirt and rock I dug out of the hole since I refilled it from other sources. I used that dirt to fill in the channels on my lot that were carved by the rain. I still need some more, but that will have to wait until I have some more money. With the new trench to divert the water, I don't mind buying some fill material to spruce thing up a bit and cover some of the rocks. Yes, I used my own tractor for this with a little help from the skip loader neighbor; I can work a front loader just fine, it was the backhoe that I needed to learn about.

Looks like a normal road now:

The rocks on the right are to keep people from driving into the intake hole, there will be more rocks moved in to define the road better when I get around to it. I didn't want someone trying to cut the corner driving into the hole there. Now, they'll slam into the rock and have only themselves to blame.

Folk in apartments in town wonder why people like me live out here and put up with this kind of thing. Frankly, from time to time I wonder myself. Me and Barkley, my dog, have killed three rattlesnakes so far this year; the desert toads have been going through their breeding season and the screams they make actually hurt your ears; there have been several weather alerts for sand storms; only one scorpion sting so far; and I had to clean the septic filter yesterday. But, sometimes you get something like this:



and think, "Maybe this isn't so bad after all."

Remember back in school when the teacher wanted an essay about "What I did last vacation?" Well this is mine.

Thursday, July 9, 2015

Unexpected Electronics

I have a 2002 Jeep Wrangler that is  my everyday car. Living where I live I occasionally actually need the 4x4 and the winch I put on it has helped several people out of mud holes; nice vehicle. The other day my dash check engine light came on and when I glanced at the gauges, the oil pressure was maxed out. 

I've seen this before on other vehicles, and wasn't too worried; if it had gone the other way, I would have pulled off the road and checked it out. So, when I got to the WalMart down the road to pick up some stuff, I killed the engine and then turned the key on without starting it. Yep, I had 80 pounds of oil pressure without starting the engine. Needed a new sensor.

Stopped off at O'Reilly's and got one, then went home. I decided to put it in later when the engine was cold, I had other things I wanted to do besides burn myself on the exhaust pipe replacing a sensor.

A couple of days later I went out with my morning coffee, a dog, and intentions of knocking this repair off in about thirty minutes. An hour later, I still hadn't managed to get the dog gone plug off the sensor.  Yes, I read the articles on the web, and looked a videos and such, but they didn't help a bit; the stupid plug was still on there. The sensor was near the passenger side firewall about a yard away and I could barely reach it cramming my arm down among all the wires and such with the release for the plug on the wrong side where I couldn't see it and get a screw driver in for leverage. Take a look:

Can't see it? Neither could I at first. It's that brass looking sliver thing in the middle of the picture way down inside. Click on the picture then enlarge and you'll see it. No, I couldn't get to it from the bottom; there was exhaust pipes and suspension in the way and I couldn't get my arm in there.

I was pretty disgusted by this time so I went Medieval on it. I grabbed a steel bar, a hammer and busted the plastic top off the sensor. Now, I pulled the wire up and disconnect the plug from the broken piece. I was on my way and had the job done in about fifteen minutes.

Moral, when all else fails, get the hammer.

This is all introduction to what I really wanted to show you; all folk like us have had problems like this where something is at the end of our reach and everything conspires to keep us from doing it. Just get a hammer.

What I wanted to show you was the circuitry I found:

Yes, inside the sensor sealed in brass and plastic is a circuit board. I couldn't find any numbers inside to tell me about the circuitry, but it was reasonably sophisticated. No wonder the thing cost me $48; this is a cool device.

It made me wonder about some of the other sensors the car has. The throttle position sensor is a simple potentiometer; I've already taken one of them apart. I'm going to dismantle the others as they fail and get replaced, it may make this kind of job more fun.

Yes, I know that I have other things to do. I'm getting to the conversion to mqtt as I have the time, but converting the various devices to use JSON strings and such is a drudge since I've done that before, but I'll get to it ... promise. But I've got that new tractor and some holes to dig before the end of August when the real rains come.

Have fun.

Saturday, June 27, 2015

Arduino, Raspberry Pi, Pool Controller and Another Guest Speaker

Let me introduce Mike He's another one of us folk that wanted automation, but didn't want to pay unreasonable prices or be locked into the control of some system 'out there.' He's been working on a project that I want to get into deeply at some point (when I get sorta caught up), and he's way ahead of me on it. Go ahead Mike:

The initial project...

   Several years ago, I was trying to think of a way to extend my swimming season without breaking the bank. I have a heat pump on the pool but they aren't very cost effective to operate. As I live in Florida,  we have plenty of sun so solar was an obvious choice.  Problem is that I didn't want my roof covered with plastic pool panels as I intend to install PV electric panels there and since they tend to develop leaks, i didn't want salt water on the roof. I already heat my hot water with solar so I started looking into using the same concept for the pool.

   I located a pair of glazed DHW panels and set them up with a titanium heat exchanger so that I wasn't running corrosive salt water through the panels. Here's a picture of the panel...

   I used a circulation pump to pump the water in the closed solar loop and quickly discovered I could gain 5 to 6 degrees a day in pool temperature in March. I was losing 2 to 3 degrees at night but the idea was successful as I was still gaining heat. Since I didn't want to heat the pool past a certain point and I didn't want to have to manually control it, I needed a contoller. I was already  toying with my first Arduino (a Uno) so it was an obvious choice.

   I began to research temperature sensors and ran across the "Differduino" project. All I had to do was modify the hell out of it and I had my basic controller! I added an ethernet shield so that I could send data to an external site now known as Xively.

   The controller monitors the pool temperature and the panel temperature and turns the circulation pump on and off on a differential provided the pool temperature is below the setpoint. This part of the project has not changed, much....

The evolution begins...

   Then one day, I stumbled across the "Desert Home" page. Don't remember what I was looking for but this crazy fool was doing things I had thought about but hadn't figured out how to do practically. As he had put up all of his code and wanted people to see it and benefit from it, I decided to do so. Dave was (and still is) very helpful in getting things up and running and I soon had a Raspberry Pi to play with.

   The Ethernet interface on the pool controller was replaced with an XBEE radio and the data was being magically sent through the air to the RPI where it was being stored to do with as I chose. This led to a Web interface that can be seen at that allowed me to see the data in real time. I added a temperature and humidity sensor so I knew what was going on outside. Here's a picture of the controller about this time..

The house controller...

   The heart of the project has actually shifted from the pool controller to the house controller. This creation started out as Dave's, 100%! As it was written in Python, it was a learning curve. My programming experience was mostly C++. As I came to figure out more of Dave's code, it became more natural to read and much easier to learn from. Once again, Dave would prod me along rather than hand me the answers. Kudos to Dave for that...

   As I was collecting more data from the pool controller and have built a few other devices as well, data was getting tedious to process. I ran across a library called ArduinoJSON and spent a weekend in the woods figuring out how it best suited my needs. I'd like to think I turned Dave on to that little treasure but he may have been exploring JSON already. If you haven't looked at JSON, I suggest doing so. I learned quickly that only so much will fit on an Uno...


   I got hold of a Mega 2560 and loaded the code to it. I had a ton of memory now (and 3 extra hardware serial ports). The SoftwareSerial library I was using could now go away but I didn't do that until I realized that it didn't work well with two way communication on the Mega. I added and GPS module I scavenged from Streets and Trips. Now I had Dave's house clock as well so I could set up timers for the pool. A Timezone library even correctly handled the time change twice a year. This has come in handy on a few other devices. I intend to add a pH probe to monitor the pool and an acid pump to keep the pH at a certain point but I haven't done it yet. I have added two way communication with the pool controller and several things are now adjustable from the Web interface.

   The pool controller and all other devices I've built so far operate entirely on their own and continue to do their thing even if the house controller is offline. I recently had to replace my pool pump motor and chose to get a Hayward variable speed unit instead. Best decision possible in my opinion. Much quieter and the energy efficiency is a big benefit.  Electric bill went down almost $60! I'm now using the timer that I programmed to run the pool pump to run the chlorine generator and the timers on the pump to vary the pump speed. I let it run at a low speed overnight instead of turning it off.

   The house controller also now incorporates ZWAVE and controls several lights and doors. It's function grows almost every time I look at it. The power of these inexpensive machines is impressive.

What's next?

   The pH monitor and acid pump are still on the list. I need to get my panels finished up, right now I'm operating on just one, and I need to get them permanently mounted. I'm working on replacing my DHW controller with one that integrates into this system and I've got an LED sign that displays weather data and pool information all over the XBEE network.  I'm also looking into adding an FPH system from Hotspot. I'll do my own controller rather than using theirs but between it and the solar, I should be able to swim almost year round. We don't get much of a winter....

This is Dave again. Nice job Mike, and thanks for chiming in. See people, you're not the only one that does this kind of thing.

Have fun.

Friday, June 26, 2015

My Newest Old Toy

I've gotten some flack, not a lot, but there have been a few people that seem to think all I do is huddle over a laptop and play with tiny devices ignoring the world around me. Most of that is true, but there is another side I don't post much about.

After the flooding last year, and the possibility of the same when the rains come this year, I decided to dig some drainage ditches. My little Yannar landscape tractor can't do that in this soil. The dirt here has settled into a rock strewn landscape left over by glaciers and is almost impossible to dig in with a pick and shovel. I posted about this after a really annoying experience <link>, and I've had several more problems like that; I live on rock with a little sand thrown in.

Time for a bigger tractor because renting one at the pace I work (slowly) would be horribly expensive, and hiring a contractor has all the usual problems of getting someone to actually show up out here and do the job. There's about a hundred blog posts possible on that one subject alone.

Let me show off my cool new old toy:

This is the industrial model of the Ford 3550 tractor. These tractors were designed for daily use on large farms back in the '60s and '70s, so it's not a spring chicken, and has seen some use. However, it's supported by a huge collection of parts and books as well as a large number of people like me that have one to tinker with. This guy was made in April of 1974 by the afternoon shift; I decoded the date sticker under the hood.

This is one of the old-school tractors that doesn't use sheet metal, it uses 3/16 and 1/8 inch plate steel. Opening the hood is an interesting experience after playing with a small Japanese landscape tractor, the engine is massive.

Frankly, it scares me. Sitting 5 feet off the ground on a big machine is a thrill and the noise is exciting (remember, I ride a Harley too), but digging and moving hundreds of pounds of rock at a time is intimidating to the inexperienced. I guess I'll start by digging several holes and filling them back up to get some experience with it.

No, I'm not changing the direction of this blog, it's still about automating the house in a practical fashion with inexpensive technology that is fun to mess with, but I'm like a kid at Christmas with this big old thing and had to show it off. Besides, I may need to run wires underground or move a big device of some kind.

I think I'm going to love it.

Tuesday, June 23, 2015

Hacking Into The Iris Door Sensor, Part 4, Resolution

The previous post on this project is here <link>.

Well, my partner in questionable activity in hacking the Iris Contact Switch and Key Fob has gotten his devices to work as well as mine, so it's time to close this project off for a while.

The reason my setup worked well and his didn't was because he didn't have a router on his network of Iris devices and I did. The Iris Smart Switch is a router and I have a handful of them scattered around the house. When he plugged one in and tried the devices, his started working and away he went. I just got word that he is switching his network over to completely local control.

Why is a router necessary? Frankly, I'm not sure at this point and I'll check into it more over time, but I have a couple of door switches working quite well, and the key fob controlling one of my smart switches. The system works nicely.

Between the two of us we managed to decode the various timers and such so you folk can pick up where we left off and implement these little devices in your own home for whatever you want. Nicely made (physically) product that was hampered only by the special code Iris put in them to force you to use their hub. I don't know why manufacturers insist on doing that, especially since there are folk like me that will take it as a challenge.

Here's the latest code with the various items. It will support the Iris smart switch, Key Fob and Door Switch. The code doesn't save status to a data base, or forward it to anything else, it just joins the devices and watches them; you'll need to adapt it to what you want to do.

#! /usr/bin/python
Hacking into the iris door sensor
Have fun
from xbee import ZigBee 
import datetime
import time
import serial
import sys, traceback
import shlex
import Queue
from struct import *
import binascii
import inspect

# line number for debugging
def getLineNumber():
    return inspect.stack()[1][2]
# show data formatted so I can read it
def showData(data):
    print "********** Message Contents"
    for key, value in data.iteritems():
        if key == "id":
            print key, value
            print key, "".join("%02x " % ord(b) for b in data[key])
    print "**********"
def showClusterData(lAddr,sAddr, clusterId, data):
    print int(time.time()),
    print "".join("%02x" % ord(b) for b in lAddr) + \
        " " + \
        "".join("%02x" % ord(b) for b in sAddr) + \
        " clid "+"%04x" % clusterId + "-" + \
        "".join("%02x " % ord(b) for b in data)

# this is a call back function for XBee receive. 
# When a message comes in this function will 
# get the data.
# I had to use a queue to make sure there was enough time to 
# decode the incoming messages. Otherwise, in heavy traffic
# periods, I'd get a new message while I was still working on 
# the last one.
def messageReceived(data):
    #print "queueing message"

def handleMessage(data):
#        if data['source_addr_long'] not in \
#            ['\x00\x0d\x6f\x00\x04\x51\x07\x82',]:
#            return
        #print 'gotta packet' 
        if (data['id'] == 'rx_explicit'):
            #print "RX Explicit"
            clusterId = (ord(data['cluster'][0])*256) + ord(data['cluster'][1])
            #print 'Cluster ID:', hex(clusterId),

            if (data['profile']=='\x00\x00'): # The General Profile
                print 'Cluster ID:', hex(clusterId),
                print "profile id:", repr(data['profile'])
                if (clusterId == 0x0000):
                    print ("Network (16-bit) Address Request")
                elif (clusterId == 0x0004):
                    # Simple Descriptor Request, 
                    print("Simple Descriptor Request")
                elif (clusterId == 0x0005):
                    # Active Endpoint Request, 
                    print("Active Endpoint Request")
                elif (clusterId == 0x0006):
                    print "Match Descriptor Request"
                    the switch looks for clusters under profile
                    c216, and I respond with only 1 cluster 02
                    print "Sending match descriptor response"
                        dest_addr_long = data['source_addr_long'],
                        dest_addr = data['source_addr'],
                        src_endpoint = '\x00',
                        dest_endpoint = '\x00',
                        cluster = '\x80\x06',
                        profile = '\x00\x00',
                        options = '\x01',
                        data = data['rf_data'][0:1] + '\x00\x00\x00\x01\x02'
                    # The contact switch is a bit slow, give it 
                    # some time to digest the messages.
                        dest_addr_long = data['source_addr_long'],
                        dest_addr = data['source_addr'],
                        src_endpoint = '\x02',
                        dest_endpoint = '\x02',
                        cluster = '\x00\xf6',
                        profile = '\xc2\x16',
                        data = '\x11\x01\xfc'
                elif (clusterId == 0x0008):
                    # I couldn't find a definition for this 
                    print("This was probably sent to the wrong profile")
                elif (clusterId == 0x13):
                    # This is the device announce message.
                    print 'Device Announce Message'
                    # this will tell me the address of the new thing
                    # so I'm going to send an active endpoint request
                    print 'Sending active endpoint request'
                    epc = '\xaa'+data['source_addr'][1]+data['source_addr'][0]
                    print "".join("%02x " % ord(b) for b in epc)
                        dest_addr_long = data['source_addr_long'],
                        dest_addr = data['source_addr'],
                        src_endpoint = '\x00',
                        dest_endpoint = '\x00',
                        cluster = '\x00\x05',
                        profile = '\x00\x00',
                        options = '\x01',
                        data = epc
                elif (clusterId == 0x8000):
                    print("Network (16-bit) Address Response")
                elif (clusterId == 0x8005):
                    # this is the Active Endpoint Response This message tells you
                    # what the device can do, but it isn't constructed correctly to match 
                    # what the switch can do according to the spec.  This is another 
                    # message that gets it's response after I receive the Match Descriptor
                    print 'Active Endpoint Response'
                # elif (clusterId == 0x0006):
                elif (clusterId == 0x8038):
                    print("Management Network Update Request");
                    print ("Unimplemented Cluster ID", hex(clusterId))
            elif (data['profile']=='\xc2\x16'): # Alertme Specific
                if (clusterId == 0xee):
                    clusterCmd = ord(data['rf_data'][2])
                    status = ''
                    if (clusterCmd == 0x80):
                        if (ord(data['rf_data'][3]) & 0x01):
                            status = "ON"
                            status = "OFF"
                elif (clusterId == 0xef):
                    clusterCmd = ord(data['rf_data'][2])
                    status = data['rf_data'] # cut down on typing
                    if (clusterCmd == 0x81):
                        usage = unpack('<H', status[3:5])[0]
                    elif (clusterCmd == 0x82):
                        usage = unpack('<L', status[3:7])[0] / 3600
                        upTime = unpack('<L', status[7:11])[0]
                        #print ("%s Minute Stats: Usage, %d Watt Hours; Uptime, %d Seconds" %(name, usage/3600, upTime))
                elif (clusterId == 0xf0):
                    # If the cluster cmd byte is 'xfb', it's a status
                    if data['rf_data'][2] == '\xfb':
                        status = data['rf_data'] # just to make typing easier
                        if status[3] == '\x1f':
                            print " Door Sensor",
                            print str(float(unpack("<h", status[8:10])[0])\
                                / 100.0 * 1.8 + 32) + "F",
                            if ord(status[-1]) & 0x01 == 1:
                                print "reed switch open",
                                print "reed switch closed",
                            if ord(status[-1]) & 0x02 == 0:
                                print "tamper switch open",
                                print "tamper switch closed",
                        elif status[3] == '\x1c':
                            #  Never found anything useful in this
                            print "Power Switch",
                        elif status[3] == '\x1d':
                            print " Key Fob",
                            print str(float(unpack("<h", status[8:10])[0])\
                                / 100.0 * 1.8 + 32) + "F",
                            print 'Counter', unpack('<I',status[4:8])[0],
                        elif status[3] == '\x1e':
                            # This indicates a door sensor
                            # with an invalid temperature reading
                            # the other items are OK 
                            print " Door Sensor",
                            print "Temperature invalid",
                            if ord(status[-1]) & 0x01 == 1:
                                print "reed switch open",
                                print "reed switch closed",
                            if ord(status[-1]) & 0x02 == 0:
                                print "tamper switch open",
                                print "tamper switch closed",
                            #This may be the missing link to this thing
                            print 'sending missing link',
                               dest_addr_long = data['source_addr_long'],
                               dest_addr = data['source_addr'],
                               src_endpoint = data['dest_endpoint'],
                               dest_endpoint = data['source_endpoint'],
                               cluster = '\x00\xf0',
                               profile = '\xc2\x16',
                               data = '\x11\x39\xfd'
                            print " Don't know this device yet",
                        print ''
                        print " Unknow cluster command"
                        print ''
                elif (clusterId == 0x00f2):
                    print 'Tamper Switch Changed State to',
                    status = data['rf_data'] 
                    if ord(status[3]) == 0x02:
                        print "Open",
                        print "Closed",
                    print ''
                elif (clusterId == 0x00f3):
                    print ' Key Fob Button',
                    status = data['rf_data'] 
                    print ord(status[3]),
                    if status[2] == '\x01':
                        print 'Closed',
                    elif status[2] == '\x00':
                        print 'Open',
                        print 'Unknown',
                    print 'Counter', unpack('<H',status[5:7])[0],
                    print ''
                elif (clusterId == 0xf6):
                    print ''
                    print "Identify Message"
                    #extract vendor strings
                    v = data['rf_data']
                    vendorstr = " - Vendor:"
                    start = 21
                    while(start < datalen):
                        vendorstr = vendorstr + " " + v[start+1:start+1+slen]
                        start = start+slen+1
                    print vendorstr
                    print "Sending init message"
                       dest_addr_long = data['source_addr_long'],
                       dest_addr = data['source_addr'],
                       src_endpoint = '\x00',
                       dest_endpoint = '\x02',
                       cluster = '\x00\xf0',
                       profile = '\xc2\x16',
                       data = '\x19\x41\xfa\x00\x01'
                elif (clusterId == 0x0500): # This is the security cluster
                    # When the switch first connects, it come up in a state that needs
                    # initialization, this command seems to take care of that.
                    # So, look at the value of the data and send the command.
                    if data['rf_data'][3:7] == '\x15\x00\x39\x10':
                        print "sending initialization"
                            dest_addr_long = data['source_addr_long'],
                            dest_addr = data['source_addr'],
                            src_endpoint = data['dest_endpoint'],
                            dest_endpoint = data['source_endpoint'],
                            cluster = '\x05\x00',
                            profile = '\xc2\x16',
                            data = '\x11\x80\x00\x00\x05'
                    # The switch state is in byte [3] and is a bitfield
                    # bit 0 is the magnetic reed switch state
                    # bit 3 is the tamper switch state
                    switchState = ord(data['rf_data'][3])
                    if switchState & 0x04:
                        print 'Tamper Switch Closed',
                        print 'Tamper Switch Open',
                    if switchState & 0x01:
                        print 'Reed Switch Opened',
                        print 'Reed Switch Closed',
                    print ''
                    print ("Unimplemented Cluster ID", hex(clusterId))
                print ("Unimplemented Profile ID")
        elif(data['id'] == 'route_record_indicator'):
            print("Route Record Indicator")
            print("some other type of packet")
        print "I didn't expect this error:", sys.exc_info()[0]
def stopXBee():
    print("XBee stop handler")

####################### Actually Starts Here ################################    
#------------ XBee Stuff -------------------------
# this is the /dev/serial/by-id device for the USB card that holds the XBee
ZIGBEEPORT = "/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A901QL3F-if00-port0"
# Open serial port for use by the XBee
# The XBee addresses I'm dealing with
BROADCAST = '\x00\x00\x00\x00\x00\x00\xff\xff'
UNKNOWN = '\xff\xfe' # This is the 'I don't know' 16 bit address

# create a queue to put the messages into so they can
# be handled in turn without one interrupting the next.
messageQueue = Queue.Queue(0)

# Create XBee library API object, which spawns a new thread
zb = ZigBee(ser, callback=messageReceived)
print "started"
while True:
        if messageQueue.qsize() > 0:
            #print "getting message"
            message = messageQueue.get()
            sys.stdout.flush() # if you're running non interactive, do this
    except KeyboardInterrupt:
        print "Keyboard interrupt"
        print "Unexpected error:", sys.exc_info()[0] 

print ("After the while")
# just in case

Remember, for this to work properly, you'll need one of the Smart Switches in the network, but the Smart Switch that can measure power usage as well as control it was what got me into looking at these devices in the first place. Remember to look at the previous posts on this project as well, I may have forgotten to mention something.

The piece that is still missing is support for an accelerometer that is inside the key fob. I don't have a clue how to initialize and use it. I don't need it, it would just be nice to understand. Maybe after I get some of the other things I'm working on done I'll come back and take another look.

Have fun.

Friday, June 19, 2015

MQTT, Early Lessons Learned, and a Concern

So, I'm stepping through code trying to work out the best way of switching to mqtt for handling data from various devices and commands to control them and it DIED.

Yep, I couldn't publish anything even though everything was connected. Without anything being published, I naturally couldn't update the weather data base, and everything got lost. Losing data is no big deal, I'll get more; it's not having a clue what went wrong that sucks. I went and looked at the logging for mqtt and enabled it to syslog so I could at least see something, and that's when it started working again. Probably just a coincidence, but I'm leaving the broker logging enabled for a while. During this process I found a nice tool for interacting with mqtt from my Windows laptop mqtt-spy, it is a java application and allows me to stuff things into mqtt and monitor any of the 'topics', but all it told me was that nothing was getting published.

I'll just wait for it to happen again and see if I can get more information.

Otherwise, I decided to organize the topics differently. This is the first of what will probably be a large number of decisions like this ... mostly wrong. Instead of having the weather devices under the topic Desert-Home/Weather, they're now under Desert-Home/Device. I did this because I realized that weather is a collection of readings, not a device. So, the AcuRite 5n1 and the barometer are now Desert-Home/Device/5n1 and Desert-Home/Device/Barometer.

Yes, a comment on my last post by Grant got me to thinking.

As I was moving and changing code, I thought of something else; wouldn't it be cool to create a log of all the activity of the various things in one place on one machine so I could prowl through the day to day operation easily? Thus was invented This new process subscribes to two topics: Desert-Home/Log and Desert-Home/Attention and simply writes what it receives to stdout. I redirect stdout to a file in the init configuration file and ta da, a log of whatever activity I want in one place. The topic Desert-Home/Attention is for stuff like the batteries going dead in the weather head or one of my battery operated devices. I'll eventually hook email to the Attention topic so I can be notified more easily.

The possible plan is to create a topic called Weather that has levels for the various readings that matter. There would be Desert-Home/Weather/CurrentTemp, HighestDaily, CurrentBarometer, RainfallToday, etc. Then I could pick and choose from the various values to present when I get that far. Frankly though, that's a lot of work and it may get modified to a JSON string that represents the weather reading right now. We'll see when I get there.

The weather station software has been converted to use mqtt now and is working reasonably well with the caveat that it may stop at any time because of the problem I mentioned up top. I can monitor the various things using mqtt-spy to see each device and the items involved including the log I created.

I'll post the code for the logger soon, I want to get a little time on it before I go and embarrass myself, and I want to give it a couple of days before cramming it into GitHub.

If it wasn't for that nagging possibility of it failing again I'd be really pleased.

Thursday, June 18, 2015

MQTT Conversion, First Steps

Last post I talked about trying out mqtt and being impressed with the ease of using it. It really went well and I decided to convert my house monitoring system to using this tool. So I took the very first steps. This is going to be a complicated conversion because I already have a system running and I don't want to break it while I'm moving to something different, so I decided on a few things to get me started.

I'm going to divide the sensors, saving data, and presenting data into different things. For example, I'm starting with the weather station because it's the simplest, but still has multiple sensing devices. The entire house is published under the 'topic' Desert-Home, so the weather station will be Desert-home/Weather, and the two current sensors are the Acurite 5n1 and the barometer out on the fence. The interesting thing here is that they are radically different.

The 5n1 is detailed on about a hundred posts on this blog, but finally wound up being a sensor on the roof that transmits in the 933MHz range. I catch and decode the signal with an SDR (software defined radio) and some code I stole and heavily modified. The readings from this sensor will be published under Desert-Home/Weather/5n1.

The barometer is a device I built up myself and put in a Stevenson Screen out on a fence post in the yard. It has an XBee that sends a JSON string to a Pi. This will be published under Desert-Home/Weather/Barometer.

I can see each of these devices by simply subscribing to 'Desert-Home/Weather/#'. The pound sign means to get everything that comes in under 'Desert-Home/Weather' and that will include every sensor I come up with related to weather.

I also decided to use JSON strings as much as possible for a few reasons. Most important to me is the ease of reading a JSON string when you're monitoring the network to see why something is messing up. The JSON string has key:value pairs that tell you what it is and what the value is. This helps a lot when I'm looking for a problem. This will be painful since most of my sensors use another technique entirely and they will have to be converted. Another reason important to me is that I don't have to write parsers to get the data back into variables, There are libraries that will do that for me and all I have to do is use them. Maybe that will make up for the pain I go through converting the devices.

So, since the XBee network I have is on a different Pi than the weather station RF software, I get to try out a multi-machine solution as the very first thing I try. Step one is done; I brought up a mqtt server (mosquitto, see previous post) and added publish lines to the XBee receive code and the weather station decoding. It worked on the second try; I had a couple of syntax errors to fix.

Mosquitto caught the data and all I have to do to see it is use the tool mosquitto_sub to watch the data as it is generated. The command line:

 mosquitto_sub -h "" -t "Desert-Home/Weather/#" -v

shows me what is coming in as it happens:

Desert-Home/Weather/Barometer {"Barometer":{"temperature":"114.4","pressure":"1011.4","utime":"1434639446"}}
Desert-Home/Weather/5n1 {"sensorId":{"SID":"92","t":"1434664650"},"channel":{"CH":"A","t":"1434664650"},"messageCaught":{"MC":"0","t":"1434664650"},"battLevel":{"BAT":"7","t":"1434664650"},"windSpeed":{"WS":"6.0","t":"1434664650"},"windDirection":{"WD":"W","t":"1434664650"},"temperature":{"T":"114.6","t":"1434664632"},"humidity":{"H":"9","t":"1434664632"},"rainCounter":{"RC":"445","t":"1434664650"}}
Desert-Home/Weather/5n1 {"sensorId":{"SID":"92","t":"1434664650"},"channel":{"CH":"A","t":"1434664650"},"messageCaught":{"MC":"0","t":"1434664650"},"battLevel":{"BAT":"7","t":"1434664650"},"windSpeed":{"WS":"6.0","t":"1434664650"},"windDirection":{"WD":"W","t":"1434664650"},"temperature":{"T":"114.6","t":"1434664632"},"humidity":{"H":"9","t":"1434664632"},"rainCounter":{"RC":"445","t":"1434664650"}}
Desert-Home/Weather/5n1 {"sensorId":{"SID":"92","t":"1434664650"},"channel":{"CH":"A","t":"1434664650"},"messageCaught":{"MC":"0","t":"1434664650"},"battLevel":{"BAT":"7","t":"1434664650"},"windSpeed":{"WS":"6.0","t":"1434664650"},"windDirection":{"WD":"W","t":"1434664650"},"temperature":{"T":"114.6","t":"1434664632"},"humidity":{"H":"9","t":"1434664632"},"rainCounter":{"RC":"445","t":"1434664650"}}

Yes, the outside temperature is REALLY 114.4, remember I live in Arizona and this is the middle of June, but it's a dry heat, the humidity reading is 9%. As if that matters when the temperature is this high ...

But, also notice that I have both strings coming to the subscription even though they are very different devices, on different Pi's. Heck, they could be half way around the world and it wouldn't matter, it would still work. I tested that with a conspirator just this morning.

Now to follow my plans, I have to catch this data in some other process and save it to my database. I'll get to that soon, maybe even today, but I'm going to hold off on changing the presentation to the web for later because I have many different presentations to consider when I get to that point. See, currently I take everything presented from my data bases. If I'm using my Android app to look at the house, the data is coming out of the data base. When I send a command to close the garage doors, the command is executed and the app doesn't update until it is recorded in the data base. I may change that to reflect the latest reading from the mqtt server. We'll see, and then I'll probably change it.

The code is not in GitHub, I'm going to hold off until I get enough in there to make a significant difference. But, the example from the last post is exactly what I did with the difference above. This is a lot simpler and more versatile than I expected.