Serial Gateway connection to Openhab



  • I read on the comments for the Openhab Controller page (http://www.mysensors.org/build/openhab) that Tim is working on a serial gateway connection to Openhab. Is anyone else working on something like this?



  • @CARSTEN I'm running MySensors with OpenHab via Serial Gateway. You migth have a look at http://forum.mysensors.org/topic/301/the-best-way-to-connect-to-openhab/13. and/or http://forum.mysensors.org/topic/302/openhab-mqtt-tips-hints/6
    Until now (20 Nodes) the functions are very reliable. The backdraw is - you have to manage all replies in the codesection by yourself. It's an noticeable amount of (repetitive) coding, but offers also great flexibility.


  • Hero Member

    @CARSTEN I've compiled a modified version of the OpenHab Serial binding with a baud rate of 115200 as this is standard in mysensors.

    Download: org.openhab.binding.serial_1.6.0.201411271703.jar

    As @tboha suggested you currently have to manage all commands by yourself.

    I've attached my modified serial binding here. Please be aware, that all other configurations that depend on serial binding won't work with the modified version, because of the changed baud rate.

    I'm currently testing with this Serial Gateway:

    Here is my OpenHab configuration for simple testing:

    demo.items: demo.items
    demo.rules: demo.rules
    demo.sitemap: demo.sitemap



  • @TimO Thanks a lot. I will definitely have a look at this. However I realized that I still have to go through quite a bit of a learning curve first. So I decided to first get the MQTT gateway woring and connecting to openhab.
    I will definitely come back to your solution later as it would give me a more minimalistic hardware approach.
    Thanks again.



  • @TimO Thank you Tim!

    I think i am on the right way but i couldnt change relay state. Please help me.

    Orginal sketch didnt worked for me (i dont have any temp-hum sensor and 2 relays yet) so i changed it to 1 relay without temp-hum

    here is my code;

    include <MySensor.h>
    include <SPI.h>

    define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    define NUMBER_OF_RELAYS 1 // Total number of attached relays
    define RELAY_ON 1 // GPIO value to write to turn on attached relay
    define RELAY_OFF 0 // GPIO value to write to turn off attached relay

    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)

    MySensor gw(100);

    unsigned long lastCheckTime = millis();

    void setup()
    {
    // Initialize library and add callback for incoming messages
    gw.begin(incomingMessage, AUTO, true);
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Relay", "1.0");

    // Fetch relay status
    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Register all sensors to gw (they will be created as child devices)
    gw.present(sensor, S_LIGHT);
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    //digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
    digitalWrite(pin, RELAY_OFF);
    }

    //metric = gw.getConfig().isMetric;
    }

    void loop()
    {
    // Alway process incoming messages whenever possible
    gw.process();

    //gw.sleep(SLEEP_TIME); //sleep a bit
    }

    void incomingMessage(const MyMessage &message) {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_LIGHT) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    gw.saveState(message.sensor, message.getBool());
    // Write some debug info
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
    }
    }

    and openhab;

    Launching the openHAB runtime...
    osgi> 2014-12-06 21:00:22.323 [INFO ] [.o.core.internal.CoreActivator] - openHAB runtime has been started (v1.6.1).
    2014-12-06 21:00:23.266 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - mDNS service has been started
    2014-12-06 21:00:23.301 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - Service Discovery initialization completed.
    2014-12-06 21:00:25.171 [INFO ] [penhab.io.rest.RESTApplication] - Started REST API at /rest
    2014-12-06 21:00:32.963 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model '1.sitemap'
    2014-12-06 21:00:33.121 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'db4o.persist'
    2014-12-06 21:00:33.144 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'exec.persist'
    2014-12-06 21:00:33.150 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'logging.persist'
    2014-12-06 21:00:33.161 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'rrd4j.persist'
    2014-12-06 21:00:33.187 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model '1.items'
    2014-12-06 21:00:33.809 [INFO ] [.o.u.w.i.servlet.WebAppServlet] - Started Classic UI at /openhab.app
    2014-12-06 21:00:37.412 [INFO ] [.service.AbstractActiveService] - HTTP Refresh Service has been started
    2014-12-06 21:00:37.466 [INFO ] [.service.AbstractActiveService] - NTP Refresh Service has been started
    2014-12-06 21:00:38.892 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;14;Gateway startup complete.

    2014-12-06 21:00:43.265 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model '1.rules'
    2014-12-06 21:01:02.305 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;read: 0-0-0 s=255,c=0,t=18,pt=0,l=5:1.4.1
    0;255;0;0;18;1.4.1
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=6,pt=1,l=1:0

    ArduinoUpdate -> 0;0;3;0;9;read: 0-0-0 s=255,c=0,t=18,pt=0,l=5:1.4.1
    0;255;0;0;18;1.4.1
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=6,pt=1,l=1:0
    2014-12-06 21:01:04.304 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;read: 0-0-0 s=255,c=3,t=11,pt=0,l=5:Relay
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=12,pt=0,l=3:1.0
    0;0;3;0;9;read: 0-0-0 s=1,c=0,t=3,pt=0,l=5:1.4.1
    0;1;0;0;3;1.4.1

    ArduinoUpdate -> 0;0;3;0;9;read: 0-0-0 s=255,c=3,t=11,pt=0,l=5:Relay
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=12,pt=0,l=3:1.0
    0;0;3;0;9;read: 0-0-0 s=1,c=0,t=3,pt=0,l=5:1.4.1
    0;1;0;0;3;1.4.1
    2014-12-06 21:01:23.048 [INFO ] [runtime.busevents ] - RED_LED received command ON
    2014-12-06 21:01:23.130 [INFO ] [runtime.busevents ] - Arduino received command 100;1;1;0;2;1

    ArduinoUpdate -> 100;1;1;0;2;1
    2014-12-06 21:01:35.421 [INFO ] [runtime.busevents ] - RED_LED received command OFF
    2014-12-06 21:01:35.429 [INFO ] [runtime.busevents ] - Arduino received command 100;1;1;0;2;0

    ArduinoUpdate -> 100;1;1;0;2;0
    2014-12-06 21:02:02.885 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;read: 0-0-0 s=255,c=0,t=18,pt=0,l=5:1.4.1
    0;255;0;0;18;1.4.1
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=6,pt=1,l=1:0

    ArduinoUpdate -> 0;0;3;0;9;read: 0-0-0 s=255,c=0,t=18,pt=0,l=5:1.4.1
    0;255;0;0;18;1.4.1
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=6,pt=1,l=1:0
    2014-12-06 21:02:04.991 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;read: 0-0-0 s=255,c=3,t=11,pt=0,l=5:Relay
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=12,pt=0,l=3:1.0
    0;0;3;0;9;read: 0-0-0 s=1,c=0,t=3,pt=0,l=5:1.4.1
    0;1;0;0;3;1.4.1

    ArduinoUpdate -> 0;0;3;0;9;read: 0-0-0 s=255,c=3,t=11,pt=0,l=5:Relay
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=12,pt=0,l=3:1.0
    0;0;3;0;9;read: 0-0-0 s=1,c=0,t=3,pt=0,l=5:1.4.1
    0;1;0;0;3;1.4.1
    2014-12-06 21:03:31.097 [INFO ] [runtime.busevents ] - RED_LED received command ON
    2014-12-06 21:03:31.103 [INFO ] [runtime.busevents ] - Arduino received command 100;1;1;0;2;1

    ArduinoUpdate -> 100;1;1;0;2;1



  • @C.r.a.z.y. , maybe the Node-Id Delaration might be ineffektive. The output of OpenHab says your node identifies itself as Node 0 (Node 0 should be the Gateway itself). You should consider to give Static ID´s like:
    #define NODE_ID 100
    gw.begin(incomingMessage, NODE_ID, true);

    So far I understand your sketch, yout try to assign a Node-id in the declaration statement

    MySensor gw(100);
    

    I´m not sure if this works. It will be safe to change it to

     MySensor gw();
    

    unless you got some very special hardware.

    If the Node-Id is assigned properly, your code example should work fine.



  • @tboha thank you!

    I tried motion sensor sketch (without any change) and it works. I also added motion to ui and i can see "motion" -"no motion" notifications.
    I will try relay again and give info.
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    **How can i add new devices automatically to UI OR add new device button in UI? - No touch to sitemap-items-rules etc files..

    More deeper : i have a "sensors" frame (like first floor in demo) and all sensors will be added to under this location OR how can i select which device will be added under x frame in UI? **



  • @C.r.a.z.y. OpenHab doesn't offer such methods like "inclusion" or search for unknown devices at the moment. But this is not due to MySensors properties, this is common to all bindings.
    Prerequisite for working with OpenHab seems to be the knowledge of your hardware to a very low level (e.g. programming the sensors yourself = MySensors). Accessing the configuration files results in great possibilities but also in much work.

    There is another User-Interface to this functions - https://github.com/cdjackson/HABmin - you might give this a try to get rid of the Eclipse-part of the OpenHab designer. I haven't tried it, but the slideshows are really impressive. Habmin may result in easier working -- but probably not in less work.

    You haven't told us what project you are aiming for - so it's a bit difficult to give a hint what may fit your needs.

    Regarding your last question, consider the following example:

    String HomeMaticS1 "Status1-1 [%s]"	<shield> (HomeMatic,kitchen, FirstFloor) {homematic="address=JEQ0459986, channel=1, parameter=LED_STATUS" }
    

    It is a String definition for my Homematic controlling one of 16 dual-color LEDs
    String denotes the type of this object
    HomematicS1 ist the name of this object.
    "Status1-1" ist the label displayed in the browser.,** [%s]** is a formating directive.
    **<shield> **is the name of the icon (which has to reside in \OpenHab1.6.1\webapps\images\shield.png --with exact this name)
    The round parenthesis **(Homematic, kitchen, FirstFloor) **tells Openhab to which group this object belongs --yes, one object may belong to many groups (= frames).

    The rest of this declaration implements the homematic binding with parameters you should know before - as mentioned above.

    The other 15 LEDControls are simply constructed by copy&paste and changing the numbers accordingly. Inconsistent numbering is a nice pitfall here and provides you with many happy hours.

    With the following Frame in the current .sitemap

     Frame label="MyFrame" {
    	Group item=HomeMatic icon="homematic"
    	Group item=kitchen icon="kitchen"
    	}
    

    Item **HomematicS1 ** will show up in both subframes labeled as "Status1-1".

    This applies to switches and other widgets in the same way, so you can operate your actors from within different frames.



  • @tboha Thank you for your detailed answer. :+1:



  • @C.r.a.z.y. Maybe the VERA controller is suitable for you. Have a look at http://forum.micasaverde.com/index.php, here especially http://forum.micasaverde.com/index.php/board,27.0.html. VERA has an inclusion mode and obviously supports Raspberry pi with Camera module (I didn't know before). Very probably you need additional additional hardware - I don'know if this is your intention.



  • @TimO said:

    @CARSTEN I've compiled a modified version of the OpenHab Serial binding with a baud rate of 115200 as this is standard in mysensors.

    Download: org.openhab.binding.serial_1.6.0.201411271703.jar

    As @tboha suggested you currently have to manage all commands by yourself.

    I've attached my modified serial binding here. Please be aware, that all other configurations that depend on serial binding won't work with the modified version, because of the changed baud rate.

    I'm currently testing with this Serial Gateway:

    And a litte sensor/relay combination (switch LED on/off and read DHT22):

    Here is my OpenHab configuration for simple testing:

    demo.items: demo.items
    demo.rules: demo.rules
    demo.sitemap: demo.sitemap

    Tim did you modify this jar file? Where can i follow updates?
    1.6.1.jar version have arduino update problems


  • Hero Member

    @C.r.a.z.y. said:

    Tim did you modify this jar file? Where can i follow updates?
    1.6.1.jar version have arduino update problems

    Yes, I've modified the serial binding to work with mysensors.

    I've invested some work to solder a little serial mysensors gateway to test it's stability together with OpenHab. I've soldered two sockets (for an arduino nano and the nrf24l01+) on a perfboard. Currently there are only two sensors with dht22 connected, but everything works stable and smooth.

    Next step is to add two battery driven Atmega328p with dht22. I will build them from the scratch.

    I'm searching for a solution for the updates of the modified serial binding for OpenHab. My current idea is to commit a change to OpenHab, that allows to set the baudrate as a parameter, so I don't need to maintain updates by myself. Another solution could be a special mysensors binding. That would allow a few additional features, but needs to be maintained.

    Little Update:

    I've changed the Serial binding from OpenHab and commited a pull request. Let's see what the OpenHab developers think:

    https://github.com/openhab/openhab/pull/1897


  • Hero Member

    Problem solved, My pull request has been merged in the 1.7.0 branch.

    With the next version OpenHab supports the MySensors serial gateway with the serial binding out of the box.

    Baud rate parameter for example in demo.items:

    String Arduino "XXXXTemperature" { serial="/dev/ttyACM0@115200" }



  • Thank you for this great information. I have been setting up Openhab for the past month, and I've been trying different types of nodes using Moteino (Arduino clone) and RFM69 radios. I have also started building some scripts for Openhab to help streamline the receipt of serial messages. All of this is due to the great information I have found here and in other places on the forums.

    The one issue that has me frustrated is when I reboot OpenHab (I am currently using a headless raspberry pi). In order to get the sensors to work I have to unplug and re-plug in my gateway, and then run around the house and cycle power off and on to all my other nodes. Sometimes this works, and sometimes this doesn't work. Is there a reliable way to restart the serial gateway via software?

    I just did some more troubleshooting and noticed that when I fully reboot the entire raspberry pi, that it comes up correctly. However, sometimes I just want to exit and reboot openhab while I'm doing development.

    Once I find out how to make this part more reliable, the whole system will be working great! Thank you again for all of your very knowledgeable advice!


  • Hero Member

    @Matt-Perry
    I don't need to restart the serial gateway when I restart OpenHab.

    Do you use a static or auto node ids? I'm using static node ids. Maybe that is the reason you have to restart everything?

    When restarting OpenHab it outputs something like: "Removing stale Lockfile on "/dev/ttyACM0 .." and that's it.



  • @TimO

    I am giving the nodes static IDs. Here is an example of how I initialize an IR receiver/transmitter that I'm testing...

    #include <MySensor.h>
    #include <SPI.h>
    #include <IRLib.h>
    
    int RECV_PIN = 8;
    
    #define CHILD_1  3  // childId
    #define NODE_ID 101
    
    IRsend irsend;
    IRrecv irrecv(RECV_PIN);
    IRdecode decoder;
    //decode_results results;
    unsigned int Buffer[RAWBUF];
    MySensor gw;
    MyMessage msg(CHILD_1, V_VAR1);
     
    void setup()  
    {   
      irrecv.enableIRIn(); // Start the ir receiver
      decoder.UseExtnBuf(Buffer);
      //gw.begin(incomingMessage);
      gw.begin(incomingMessage, NODE_ID, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("IR Sensor", "1.0");
     
     // Register a sensors to gw. Use binary light for test purposes.
     gw.present(CHILD_1, S_LIGHT);
    }
    

    Again, if I fully reboot my raspberry pi, it works (I am running Openhab as a startup script). But, if I want to troubleshoot a little bit, and "kill" the Openhab process and then call "start.sh" on the command line, the serial gateway does't reinitialize. Maybe I'm missing a step somewhere?

    If this problem only relates to me, I don't want to waste too much forum space. I could very possibly have something set up wrong on my end and I'll figure it out!

    Thanks!


  • Hero Member

    What did the OpenHab debug messages say?

    Did you check the permissions? Which user starts OpenHab with the startup script and which user do you use when you call "start.sh". Maybe there is a block because of the lock file? Kill the lock file (something like: "/var/lock/LCK..ttyUSB0") after killing OpenHab and before restarting with "start.sh".

    I'm currently not using a startup script, I'm using "start.sh" within a screen session, which I'm able to resume for debugging or to kill OpenHab.

    My network of MySensors nodes keeps working and reporting (temperature for example) and as soon as there is a controller running, the information is received.



  • @TimO

    Thanks again for the reply. Everything is working now. I believe the issue was with the serial connection moving to ttyUSB1. I'm not sure of why, or how, but I did create a SYMLINK and everything works just fine now, no matter what USB port the device is plugged into.

    Thanks again!



  • @TimO I tested your openHAB configuration and it is working fine most of the time. There is still one problem I was not able to fix. Some payloads have a dynamic length. Your example rules base on fixed length of the transmitted values, right?
    I tried to split the incoming strings with string.split("\n"), but the \n seems to be removed by openHAB or the serial gateway. Any ide how we can resolve this? Without this issue openHAB would work perfectly together with the serial gateway.



  • Hi

    I have the same problem with Matt and tried USB01 but didn't work.
    When i unplug the usb, server error : Write action failed! Input/output error2015-03-03 19:25:23.682 [ERROR] [b.serial.internal.SerialDevice] - Error writing '100;3;1;0;0;1
    ' to serial port /dev/ttyUSB0: Input/output error in writeArray

    Only solution for me stop server, unplug serial usb and plug it. I think ---Kill the lock file (something like: "/var/lock/LCK..ttyUSB0")--- will give the same solution like unplug the usb?


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.