Saturday, August 31, 2013

Finally got a Raspberry Pi

OK, I broke down and took the plunge to a different kind of little computer; I went out and bought myself a Raspberry Pi <link>.  Nice little machine.  It wasn't an easy decision on my part since I can finally make the little Arduinos do most anything I want to with hardware, but I got a bug up my butt.

I was corresponding with a person working on garage door control and he was pushing the door state out to his cell phone.  So, when the door opened, it sent a message to his phone and told him.  I decided that would be a really great way to keep track of things around the house.  Besides the garage door, it would be nice to know that the power was too high while I was away so I can log into the house and turn something off, that kind of thing.  The problem is that that stuff can get hard to do on an Arduino.  When you get into the internet realm, the Arduino doesn't shine.  What was needed was a machine that didn't use much power, and had plenty of support for ethernet and internet.  A laptop would do the job, but that costs way too much and relies on something that can take too much time to boot up and run.  The Raspberry Pi sounded like the perfect solution.  It has a serial port and an ethernet port built right in and runs Linux; how cool is that?

It came in, and I started looking at how to get started with the thing.  Let me tell you , there are hundreds of tutorials out there and they all leave something out that is necessary.  This is because you actually need a console for the little device to get it going.  I stepped through the process of creating an SD card with the operating system, then plugged it into my home ethernet network.  I downloaded a terminal program and  used ssh to talk to it, did the various configuration items and finally got it to work in a reasonable fashion.  So, I actually got it working without hooking it to my TV and going out to buy a bluetooth keyboard and mouse.

Yes, the inventors said they wanted people to be able to use the board without having to buy anything, but how many of us have a bluetooth keyboard around the house?  Sure we have a TV, it's mounted in an entertainment center and turning it around to get to the video ports is a pain, but doable.  They have a nice little port on there that can be a serial console, but who has an old fashion terminal anymore?  Besides, it 3.3 volts, not 12 like the old rs232 terminals; you have to buy a special adapter to use it.  And, I'm here to tell you the power supply you use matters ... a lot.  These things use a lot of power for a tiny little computer, and many of the wall warts out there can't do it.  See, not only does it need several hundred milliamps, it needs it highly regulated.  I had one wall wart that would supply 1.5 amps, but the regulation wasn't good enough and when the ethernet came up, it would fail because the voltage dropped a bit before the regulator kicked in and brought it back up.  There was another one that just didn't live up to its rating, a third that wasn't filtered enough.  But, like I said, I have a bunch of them and a couple of them worked just fine; they had enough capacity and were properly regulated and filtered.  See, wall warts are mostly made to charge batteries, not run little power hungry computers.  I'll probably be looking into power supplies for these little things in the future.

But I got it to work.  When you try it, look for 'Raspberry Pi headless' on bing or google and there are a few thousand examples of how to proceed.  None of the examples I found were perfect, but bouncing from one to the other I was able to get it going.

The little device came up, set the time, started all of the processes and just worked.  Now I had to do something with it.  I chose to use python as the language instead of C because I'm getting tired of having to do every little thing necessary and wanted some more sophisticated abilities.  Sure, each of the languages has it's strong point, but might as well start somewhere.  Loaded the libraries I thought I needed and wrote a little code to talk to my two air conditioner thermostats.  IT WORKED FIRST TRY.  Well, not exactly first try, I had some syntax errors and such to fix, but it worked.  Then I wanted to be able to schedule things like polling the thermostats every minute, so I loaded another library and it worked.

The Raspberry Pi Script
import time
from apscheduler.scheduler import Scheduler
import urllib2

NThermoUrl = "HTTP://192.168.0.202"
SThermoUrl = "HTTP://192.168.0.203"

NthermoStatus = []
SthermoStatus = []

def openSite(Url):
        try:
                webHandle = urllib2.urlopen(Url)
        except urllib2.HTTPError, e:
                errorDesc = BaseHTTPServer.BaseHTTPRequestHandler.responses[e.code][0]
                print "Cannot retrieve URL: " + str(e.code) + ": " + errorDesc
                sys.exit(1);
        except urllib2.URLError, e:
                print "cannot retrieve URL: " + e.reason[1]
        except:
                print "Cannot retrieve URL: Unknown error"
                sys.exit (1)
        return webHandle

def getThermoStatus(whichOne):
        if whichOne == "North":
                website = openSite(NThermoUrl + "/status")
        else:
                website = openSite(SThermoUrl + "/status")
        # now read the status that came back from it
        websiteHtml = website.read()
        # After getting the status from the little web server on
        # the arduino thermostat, strip off the trailing cr,lf
        # and separate the values into a list that can
        # be used to tell what is going on
        return  websiteHtml.rstrip().split(",")

def ThermostatStatus():
        print(time.strftime("%A, %B %d at %H:%M"))
        NThermoStatus = getThermoStatus("North")
        print "North reports: " + str(NThermoStatus)

        SThermoStatus = getThermoStatus("South")
        print "South reports: " + str(SThermoStatus)

        print

sched = Scheduler()
sched.start()
sched.add_interval_job(ThermostatStatus, minutes=1)

ThermostatStatus()
while 1:
        time.sleep(1)


Frankly, I'm impressed.  Here's a little bitty board that can be made to do the things I need to do around the house.  Sure, it needs the support of intelligent devices out there to do the actual work, but this little guy can coordinate activity and report things to me over various devices.

I still have a lot of work to do before it can take over the house, I have to get an XBee working on it, experiment with a web server, figure out where to put it and what to put it in, that kind of thing.  I've gotten used to my House controller having flashing lights to tell me things are happening and a special light to let me know something is wrong, so those things have to be considered.  I'll have to experiment with my cloud data store at Xively, I like having my data available there.  It took a long time and a lot of experimentation to get the current house controller working, this will be roughly the same.

However, this little guy has a real operating system on it and can truly multitask.  This could be fun.

No comments:

Post a Comment