First though, I've been away from my house control system for a while because of other projects, and I decided to do some major updates to portions of it. Over the months, I've learned a lot and some of that new knowledge needs to be put into controlling the house. So, yes, I'm posting more often.
Often times, I want to post a new update to one of the processes, but I have keys and other stuff in the modules that have to be removed before I can put it on the web. I'm also going to move the code into GitHub at some point so folk don't have to copy and paste things for their own use. They can just grab what they want from there. That means I have to get the keys out of the code.
Unix has long had a customary way of doing things like this, an rc file. The 'rc' stands for run command and has a long history, look it up. There's a lot of files out that go into the user's home directory and have a name like .bashrc, .cshrc, etc. These are configuration commands and aliases. This is just the tool I need to use. I can put an rc file in the home directory and all the keys, database names, etc can go in there to be read at runtime by the applications. Cool, that way I can make something public without having to worry about somebody updating my data feeds by mistake.
But, I hate parsing data out of a file. I was looking at parsers and ways of storing data when I got an email about decoding a JSON file. While I was answering the mail I realized how easy it is in python to parse through JSON, so you guessed it, my rc file is JSON. That means that it's a tiny bit harder to create and incredibly easy to use in a program. Here's my .houserc file (with the secrets removed):
It's just a simple JSON string that can be easily taken apart. By using meaningful (to me anyway) names, it's pretty easy to read and edit as well. To get the data back out in a python program, all you have to do is:
And, since I don't want to type the directory name over and over again, I put the getHouseValues() in a python file and imported it.
No parsing, no silly obscure things to learn, just things I already had to work with doing this project. Admittedly, it was a pain going to each module and modifying it to use the rc file, but it was worth it. Notice I included the weird USB ports I use for the two XBees I have attached? I can do that with other things that may get changed from time to time. Just add them to the JSON string and I'll be able to get them back at run-time without changing code in any module.
Heck, it's not even hard to read.
Often times, I want to post a new update to one of the processes, but I have keys and other stuff in the modules that have to be removed before I can put it on the web. I'm also going to move the code into GitHub at some point so folk don't have to copy and paste things for their own use. They can just grab what they want from there. That means I have to get the keys out of the code.
Unix has long had a customary way of doing things like this, an rc file. The 'rc' stands for run command and has a long history, look it up. There's a lot of files out that go into the user's home directory and have a name like .bashrc, .cshrc, etc. These are configuration commands and aliases. This is just the tool I need to use. I can put an rc file in the home directory and all the keys, database names, etc can go in there to be read at runtime by the applications. Cool, that way I can make something public without having to worry about somebody updating my data feeds by mistake.
But, I hate parsing data out of a file. I was looking at parsers and ways of storing data when I got an email about decoding a JSON file. While I was answering the mail I realized how easy it is in python to parse through JSON, so you guessed it, my rc file is JSON. That means that it's a tiny bit harder to create and incredibly easy to use in a program. Here's my .houserc file (with the secrets removed):
{
"database":"/home/pi/database/database",
"xbeeport": "/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A901QLZ1-if00-port0",
"zigbeeport":"/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A901QQ2F-if00-port0",
"oldxively":{
"key":"secretkeythingieforlegacyxivelygoeshere",
"feed":"1234"},
"xively":{
"key":"secretkeythingieforthenewinterfacehere",
"feed":"1234567890"},
"emoncms":{
"key":"theyuseashorterkey"},
"grovestreams":{
"org":"grovestream-has-even-stranger-one",
"apiKey":"with-a-strange-key-as-well"},
"thingspeak":{
"key":"ASIMPLEKEYEASYTOUSE"},
"oreo":"double stuff"
}
It's just a simple JSON string that can be easily taken apart. By using meaningful (to me anyway) names, it's pretty easy to read and edit as well. To get the data back out in a python program, all you have to do is:
def getHouseValues():
json_data=open("/home/pi/.houserc").read()
return json.loads(json_data)
hv = getHouseValues()
FEED_ID = hv["oldxively"]["feed"]
API_KEY = hv["oldxively"]["key"]
# the database where I'm storing stuff
cookie = hv["oreo"]
And, since I don't want to type the directory name over and over again, I put the getHouseValues() in a python file and imported it.
No parsing, no silly obscure things to learn, just things I already had to work with doing this project. Admittedly, it was a pain going to each module and modifying it to use the rc file, but it was worth it. Notice I included the weird USB ports I use for the two XBees I have attached? I can do that with other things that may get changed from time to time. Just add them to the JSON string and I'll be able to get them back at run-time without changing code in any module.
Heck, it's not even hard to read.