Tuesday, February 18, 2014

Logic Layer....

As I posted in the last blog, I discovered the wonderful world of databases. Actually I had used them in the past, but not programmed anything such as queries or even created them. I had to laugh, cause back in the OLD OLD days, we didnt have databases. In fact if you wanted to store stuff say when running a BASIC program, you had to open a file, write to the file and close it.....sound familiar.....yeah that what we do now with databases.....history repeats itself...

As I mentioned in the last blog as well it became obvious that adding a logic layer just made sense. It gave me one place to modify the system without creating much in the way of downtime, actually none really and I could add or remove just about anything from the system as I wanted.

The Logic Layer is nothing more than a python script. I really do love python, its a powerful language and even just using IDLE, easy to program. I have not come across anything I thought of that I could not do in python, short of creating a GUI, which I would love to do, but thats more learning for later. The Logic layer script you can think of is a "front end" to the controller. Basically the logic script subscribes to ALL the topics on the MQTT server.....all of them. The output of this script is MANY publication to ONE topic. As of right now there are nearly two dozen different topics on the MQTT server, of which about 17 are house related. This will of course grow as the system does. The logic script then listens to all 17 topics, acts on payloads received, and issues payloads to ONE topic in return. This one topic is the ONLY topic the main controller arduino listens to. As I explained, this allows me a simple way to add and remove nodes from the system without ever touching the controller firmware.

The Script..........since Ill assume that you know how to use python, youll need to add some modules to python. I prefer to use pip, with my linux box. Its simply just less headache and easy to use, I like less head aches.....

The top of the script shows the modules youll need....

import mosquitto
import datetime

from colorama import init
from colorama import Fore, Back, Style
import sys
init()

print chr(27) + "[2J"
print
print
print (Fore.CYAN + Back.BLACK+  "    | MySQL DataBase Update Script |")
           
print


mosquitto module at least for ubuntu, can be gotten in the software center, I prefer synaptic, I even installed it back to my 12.10Ubuntu box after I installed ubuntu, its omitted in versions after 11.10 I think. I like synaptic, always have, and I dont have to deal with a bloated GUI of the software center. Since my production box doesnt have 40 cores, and 50GB of RAM, using synaptic is easier and faster. You can add it back to your ubuntu box by:

sudo apt-get install synaptic . Once you install it just pin it to the unity desktop bar

For Windows users, you can install all the python modules as you normally do. I work on both systems, however all my final work is done on a linux box.....


The module colorama, is totally unnecessary. Its  a fun module that allows you to change the fore ground, back ground and make the font bold, or dim in a terminal window. Since I like to, in production, use terminal window outputs to monitor the stability of a script when testing, adding color to represent certain conditions is cool. For instance, if I get an alarm, I can turn the output test to a bold white font, with a red background....catches your eye. Again, totally unnecessary, and you can ignore the code lines referring to it.

Datetime youll need to add...cool module that allows you to format time and date stamps. It totally worth reading or at least glancing over the documentation for datatime. This module came in real handy for the trigger script. Also it will match exactly the DATETIME setting for the time stamps in MySQL.....cool..

The rest of the top of this script is easy. print chr(27) + "[2J" does nothing more than clear the terminal window when the script is started, and then a script header. This just prints to th screen what script I am running, nothing more.

So now you know what modules to grab........next blog we will get into the nuts and bolts of the code...

Happy coding!

No comments:

Post a Comment