Wednesday, September 21, 2016

I'm Still Hung Up On Power Failures, but Suppose

OK, OK, maybe I'm a little OCD at times, but that can be a good thing. Folk have been making suggestions, and there have been some interesting pointers given to me, but none of them pass the test. But one suggestion caused a brain storm a little while ago.

A tiny bit of background. It's easy to control a relay or a light, what's hard is the smarts to know when to turn it on, or off. Sometimes this gets pretty complicated. Turn the sprinklers on at 10PM but only if the ground is dry and rain isn't predicted tonight. Turn the porch light on when someone approaches but not during the day. Turn the bathroom light on when someone enters the room, but not if it's after bedtime. We all have problems like that, and the problem is that every time we want to add a little more smarts to the device, we have to do some code and get it on the machine. That often means taking the device out of the box or whatever it's in and carrying it somewhere to reprogram it. Suppose I'm reprogramming something in the attic? It would totally suck having to crawl up there and modify it. Raspberry Pi's are great for this, just use a terminal program like Putty and connect to it over wireless. You can change things, try them, generally mess around until you get what you want and never have to lug the laptop out to the barn to plug it into the chicken feeder.

Suppose I split the work between two computers? One would be fully protected by a UPS that can do all the stuff I need, and another that can survive power failures, but only does the controlling. Let's take a specific but reasonably simple example: sprinklers. I don't need them, but other folk do, so let's take a case of a yard with 4 stations that need to be controlled.

I go out and get one of those relay boards and an Arduino to run it. I can put that in a weather proof box and stick it out close to the sprinkler valves. I will have a power supply wall wart running to it and an XBee to communicate. Arduino's handle power failures well since they have their software in ROM. I only need four command sets:

Relay one on-off
Relay two on-off
Relay three on-off
Relay four on-off

That code would be really easy to put together and implement. Then, I get a Raspberry Pi to send the commands and handle timing and such. I could easily put together menu screens to set the various on-off times, keep track of when they were run, etc. The Pi could be the ultimate sprinkler controller. Of course the Pi would be in a safe place with its power under the monitoring of a UPS that has the correct features.

Let's get a little more complex and consider a swimming pool controller. That would need more relays and possibly some really special features. The basic things I would need in my case would be:

Motor on-off
Fountain on-off
Waterfall on-off
Light on-off

Darn, doesn't that look exactly like the sprinkler control?  Now let's get a little bit more sophisticated and add control to the motor speed and the solar heater. Thinking about it a bit, I only need to add temperature readings for the water and the sun light on the roof to control the solar. Then for the motor, I only need to send motor commands and read their response. I've done all those things with my temperature sensor and pool control projects.

Take the sprinkler idea a bit further by adding some moisture sensors in strategic places with an XBee to transmit the data back to a Pi that figures out what needs to be done and sends back commands. Once again, the brains are in a protected spot with the actual worker computer somewhere else. The Pi could have elaborate software that looks pretty to control the pool and all the features could be made available to a web interface. That would be a fun project, but we might have to resort to battery powered devices to get to some of the hard to reach places.

One of my dreams is to monitor the temperature in each room, gather it in a central place, then control the heat pumps and air handlers intelligently to move heated or cooled air around the house. Little devices that use ROM and can send data would need to be placed in each room, then another simple computer would be placed in each air handler to actually control the fans, compressors and changeover switch. A Pi would be used to look at the data and do something appropriate. I would be able to use any program language or piece of software to develop the smart part from anywhere I feel comfortable working.

So, the various Arduinos would be worker computers that run software that doesn't need to be changed unless I want to add some hardware feature; they do just fine if the power is pulled. The Pi's would be more sophisticated and would handle all the real decisions. They would be backed up automatically and protected from nasty power crap. It would expand my XBee network, but I've got lots of bandwidth there. The other really great thing is that the Pi's can be programmed and reprogrammed any time I want without lugging a laptop out to the sprinkler junction or up into the attic. The PI's could send mail, sms, whatever is needed, heck they could send a signal to an alarm bell just like the big systems. This idea has been sitting right in front of me forever.

I just needed a nudge or two from my readers. Thanks.

12 comments:

  1. Nice write up.
    The gateway from lowpowerlab seems to do what you are looking for. I don't use it as I have preferred to code everything myself and although my system is much more limited I have learned a lot doing it.

    Btw, can anyone explain how the architecture or logic must be coded when doing schedules? I mean, I have all my nodes reporting to a central raspberry which receives everything via python, I want to implement schedules or logics based on data of mysql and schedules. How would I need to code it? Have a code checking every 1 minute? How to confirm is a schedule has been activated or its running or has been missed? Is there anywhere to read good practices on how to code such a system?
    Thank you

    ReplyDelete
    Replies
    1. I haven't seen any 'best practices' on this kind of thing yet, but there are lots of tricks that I've learned. What you do depends on how you want things to work. For example, it's easy to code an event trigger based on time, but what happens when the power goes off during the event?

      I use several methods in different devices. My favorite is to turn something on and in the code for turning it on, set a timer to turn it off, but a week ago, this left the pool motor running because the power flickered. At some point I'm going to dig really deeply into this because it can get annoying.

      Delete
    2. I recently did exactly this with my sprinkler controller and then handed it off to my Z-Wave controller for the integration with weather data and stuff like that. Very basic device now that all it's doing is looking for commands to turn relays on and off... I did build in a fail safe timer so that if it doesn't receive a command in a certain time frame, it turns off all the relays. That way, I never have to worry about my yard flooding from the sprinklers....

      Delete
    3. Yep, failsafes are an absolute necessity. I try to never leave the possibility of something staying on. But the idea of a relatively stupid device outside is really appealing.

      Delete
  2. The system design you are describing is exactly how I have my set up working. I have a raspberry Pi as the hub, running node-red, controlling and capturing events centrally from xbees and 433mhz transceiver etc.
    http://www.freakent.co.uk/2013/09/26/node-red-and-xbee/
    http://www.freakent.co.uk/2014/02/03/pretty-as-a-pi-cture-raspberry-pi-server-in-a-frame/

    ReplyDelete
    Replies
    1. I am mostly doing that around the house, but I also have completely autonomous devices. Those are the ones that I'm going to dig into because they constantly mess up if there's a power glitch.

      Delete
  3. I'm doing what you describe with openHab. It's easy to create "rules" and to add items and it speeks mqtt so it's easily implemented with home built "things". From reading your blogg for some years i might also point out that it's open source and can be run offline.

    I run it on a pi2 (a bit slow sometimes) together with some arduinos and esp8266. I hardly use any of their "bindings" except mqtt and astro (to approximate when it's dark outside). I use one arduino as 433MHz -> mqtt and one vice versa. Then i can buy cheap RF thermometers and other battery powered sensors. I also controll lights and such with what i guess is a european x10 equivalent, Nexa. Then for more critical stuff i use esp8266's with mqtt to get 2way communication.

    As always it's really interesting and fun to read your posts. I might have to get me an UPS...

    ReplyDelete
  4. openHAB seems a really nice project. However, to keep learning I would like to code all the scheduling system myself. That is the reason for asking if anyone could give me some guideline on how the system should be structured or how other systems like openhab work.
    Does the checking script execute every minute? When it does, does it go through a table in some database to read all the rules? How does it know if a rule has already been executed, or has been missed? How to make all this modular?
    If anyone can give me some idea or any starter article to read about it.
    Thank you very much

    ReplyDelete
    Replies
    1. Almost every system out there works on a minute basis. They look through a table of 'jobs', 'events', 'timers', whatever they happen to call it every minute.

      If you want to get an idea of the things involved, take a long look at the "cron" process on linux. If you read a number of articles on it, you'll get a feel for how most systems handle this kind of thing.

      The granularity of the checking is based on your needs. I use a minute, other people use 5 minutes. That totally depends on how finely you want to control things.

      Delete
    2. With openHab "rules" can be eventtriggered, on state update, cron and more. States can also be reloaded on startup. Check their wiki. It's a bit messy but there's lots of examples.

      Delete
  5. Here is another vote for NODE-RED. Someone has even made it easy. thethingbox.io has a pre-made install of Linux for the Raspberry Pi with NODE-RED, the mosquito MQTT broker and a couple other apps. You just download the image file and burn it to micro sd card.

    ReplyDelete