"If something works for some one, it doesn't mean that it will work for you" -- this is the hard lesson I learned in past weeks. I was caught in between some personal matters for last two weeks and didn't gave much time for the challenge. I'm so ashamed that I tried to fix something here and there for two weeks and all I know now is a thousand ways how it will not work . So this is what basically happened. In my project, there are these 'room controllers' which will be managing all the devices in each room( areas ) in my home. These room controllers will maintaining a communication with the raspberry pi running openhab and whenever an event happens in openhab, a corresponding command will be send to these room controller. Say, I told openHAB to water my indoor plant in main hall, openhab will send a corresponding message to the room controller in main hall and that room controller will in turn open the valve for watering. While preparing the proposal, I decide to use arduinos and BLE shields for my room controllers, but later due to the budget constraints, I decide to use MSP430G2 launchpad and nRF204L01+ modules. While everything seems to work on all those blogs I referred before ordering the components, it was not the same when I got mine in my hands. My idea was to run an MQTT client program running in Pi, which will monitor for the events from openhab and put them on air using nRF module. I thought, in this way, I will be able to decouple my main controller running openHAB( Pi ) from directly accessing hardware and hence can run more smoothly. Also I thought this will give enough modularity to my project so that I can have plug and play room controllers which will be fun.
First thing I did was to hook up nRF modules to G2 launchpads and tried communication between them. I used energia and Enrf library. Everything went fine. They were able to talk each other.
Next thing I wanted was to hook a nRF to my Pi and make a wireless interface to my G2 launchpads. Sadly it didn't worked. I tried using the well known RF24 library and some of it's forks but none was able to even listen to what my launchpads are talking each other. My Pi is able to detect a channel activity in the channel my launchpads are talking, but not able to decode them. I opened a question here and it's still open .
So now I'm here, with almost no hope that I can make my Pi talk to launchpads over nRF before the challenge ends( Oh in 4 days!! ).
Okay, now what's next? Today I decide to scrap the idea of nRF modules and decided to use ethernet for communication to room controllers. I know, it's a truly uninspiring solution, that's the one option I see for now.
I decided to use my Galileo and connected launchpad as my room controllers for now. What I did today was to setup the linux "bigger linux" environment for Galileo, installed Paho client in it, and played with it a little. I chose to use the python library from Paho. I also tried two GPIO libraries for Galileo in python, but both of them are not good it seems.
By evening, this is what I achieved. I have my raspberry Pi running openhab, BBB with mosquitto and Galileo with Paho python client. Now my Galileo is able to listen to MQTT topics from openHAB via BBB. Next thing I need to do is to wire up things in Galileo to control the devices attached to it.
Below is the code I'm using in Galileo( probably messy, with little comments )
import paho.mqtt.client as mqtt # Subscription handlers def onMsg__lamp0( client, userData, msg ): print( "LAMP0 : " + str( msg.payload ) ); def onMsg__lamp1( client, userData, msg ): print( "LAMP1 : " + str( msg.payload ) ); def onMsg__socket0( client, userData, msg ): print( "SOCKET0 : " + str( msg.payload ) ); def onMsg__socket1( client, userData, msg ): print( "SOCKET1 : " + str( msg.payload ) ); def onMsg__fan( client, userData, msg ): print( "FAN : " + str( msg.payload ) ); def onMsg__tv( client, userData, msg ): print( "TV : " + str( msg.payload ) ); # node configuration basePath = "homenet/area" controllerArea = "mainhall" devices = { "lamp0" : onMsg__lamp0, "lamp1" : onMsg__lamp1, "socket0" : onMsg__socket0, "socket1" : onMsg__socket1, "fan" : onMsg__fan, "tv" : onMsg__tv }; # tracking for each subscriptions mid_lamp0 = 0; mid_lamp1 = 0; mid_fan = 0; mid_socket0 = 0; mid_socket1 = 0; mid_tv = 0; # the call back for when the client recieves a CONNACK response from server def onConnect( client, userData, retCode ): print( "Connected with return code " + str( retCode ) ); # publish a entry message client.publish( "homenet/devices/galileo", '{"role":"controller","area":' + controllerArea + '}' ); # calback when a publish message is recieved from broker def onMessage( client, userData, msg ): print( msg.topic + " " + str( msg.payload) ) def onSubscribe( client, userData, mid, grantedQos ): print( "Subscribed to " + str(mid) + " Qos " + str( grantedQos ) ); client = mqtt.Client( client_id = "galileo", clean_session = True, userdata = None, protocol = mqtt.MQTTv31 ) # add callbacks client.on_connect = onConnect client.on_message = onMessage client.on_subscribe = onSubscribe # Connect to broker client.connect( "192.168.1.79", 1883, 60 ); # start a thread for connection client.loop_start(); # make scubscritions and add callbacks for device in devices: topic = basePath + "/" + controllerArea + "/" + str(device); retCode, mid = client.subscribe( topic ); client.message_callback_add( topic, devices[device] ); try: while True: pass # forever except KeyboardInterrupt: client.loop_stop(); client.disconnect(); # client.loop_forever()
And my openHAB items file has :
// deivces main hall Switch mainhall_lamp0 (All) {mqtt=">[localBroker:homenet/area/mainhall/lamp0:command:ON:default],>[localBroker:homenet/area/mainhall/lamp0:command:OFF:default]"} Switch mainhall_lamp1 (All) {mqtt=">[localBroker:homenet/area/mainhall/lamp1:command:ON:default],>[localBroker:homenet/area/mainhall/lamp1:command:OFF:default]"} Switch mainhall_socket0 (All) {mqtt=">[localBroker:homenet/area/mainhall/socket0:command:ON:default],>[localBroker:homenet/area/mainhall/socket0:command:OFF:default]"} Switch mainhall_socket1 (All) {mqtt=">[localBroker:homenet/area/mainhall/socket1:command:ON:default],>[localBroker:homenet/area/mainhall/socket1:command:OFF:default]"} Switch mainhall_fan (All) {mqtt=">[localBroker:homenet/area/mainhall/fan:command:ON:default],>[localBroker:homenet/area/mainhall/fan:command:OFF:default]"} Switch mainhall_tv (All) {mqtt=">[localBroker:homenet/area/mainhall/tv:command:ON:default],>[localBroker:homenet/area/mainhall/tv:command:OFF:default]"}
and sitemap as with :
Frame label="Main Hall" { Switch item=mainhall_lamp0 label="Lamp 0" Switch item=mainhall_lamp1 label="Lamp 1" Switch item=mainhall_socket0 label="Socket 0" Switch item=mainhall_socket1 label="Socket 1" Switch item=mainhall_fan label="Fan" Switch item=mainhall_tv label="TV" }
And yayy...I have my room first room controller.
As I lost two weeks in between, I'm so sure that I will not be able to complete all the things I promised in my introduction in next four days, but I'll try my level best to finish most of them. And one day, all of them