Skip to content
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Controllers
  3. OpenHAB
  4. [Tutorial] openHAB with serial gateway
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

[Tutorial] openHAB with serial gateway

Scheduled Pinned Locked Moved OpenHAB
78 Posts 25 Posters 76.3k Views 16 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TimO
    Hero Member
    wrote on last edited by
    #1

    As it is now possible to use a serial gateway with openHAB I've written a simple (as simple as possible) entry tutorial.

    What do we need?

    • a MySensors serial gateway
    • a linux system (like a RPi, example based on Ubuntu)
    • a MySensors sensor (for example with a DHT22 and a fixed NODE_ID of 101)

    Where to start?

    Start with the serial gateway.

    Build it: http://www.mysensors.org/build/serial_gateway

    If you see something like this in the serial monitor for arduino you're done:

     0;0;3;0;14;Gateway startup complete.
    

    Connect the serial gateway to the linux system and execute "dmesg". You should see something like:

    [  186.047748] usb 2-1.5: new full-speed USB device number 7 using ehci-pci
    [  186.147030] usb 2-1.5: New USB device found, idVendor=0403, idProduct=6001
    [  186.147036] usb 2-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    [  186.147040] usb 2-1.5: Product: FT232R USB UART
    [  186.147044] usb 2-1.5: Manufacturer: FTDI
    [  186.147047] usb 2-1.5: SerialNumber: A40360T2
    [  186.149618] ftdi_sio 2-1.5:1.0: FTDI USB Serial Device converter detected
    [  186.149659] usb 2-1.5: Detected FT232RL
    [  186.149663] usb 2-1.5: Number of endpoints 2
    [  186.149667] usb 2-1.5: Endpoint 1 MaxPacketSize 64
    [  186.149670] usb 2-1.5: Endpoint 2 MaxPacketSize 64
    [  186.149673] usb 2-1.5: Setting MaxPacketSize 64
    [  186.150205] usb 2-1.5: FTDI USB Serial Device converter now attached to ttyUSB0
    

    As we see the serial gateway is now accessible through "ttyUSB0" like: /dev/ttyUSB0. But the Number may change. It depends on the FTDI Chip and could also be something like "/dev/ttyACM0".

    But we don't want to look for the correct name after every reboot or after we plugged out and in the serial gateway, so we add the following in "/etc/udev/rules.d/99-usb-serial.rules"

    SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="A40360T2", SYMLINK+="ttyUSB99"
    

    Look at the output from "dmesg" above and change "0403", "6001" and "A40360T2" according to your hardware.

    Now plug out and plug in the serial gateway.

    The serial gateway will now be accessible through "/dev/ttyUSB99".

    Install openHAB

    It is important to install a version like or above 1.7.0. This version is not stable yet so download a snapshot from cloudbees:

    https://openhab.ci.cloudbees.com/job/openHAB/

    You need:

    distribution-1.7.0-SNAPSHOT-addons.zip
    distribution-1.7.0-SNAPSHOT-runtime.zip

    Move to the "/opt" directory and extract the files:

    cd /opt
    mkdir openhab
    cd openhab
    unzip /tmp/distribution-1.7.0-SNAPSHOT-runtime.zip
    cd addons
    unzip /tmp/distribution-1.7.0-SNAPSHOT-addons.zip org.openhab.binding.serial-1.7.0-SNAPSHOT.jar
    

    We will keep it simple and just use the serial binding.

    Configure openHAB

    At first we need to configure openHAB to use the serial binding to connect with the gateway. So we switch to the "configurations" directory:

    cd /opt/openhab/configurations
    

    There we create a file named "demo.items" in the "items" directory:

    vi items/demo.items
    

    Insert the following content:

    String Arduino "Arduino" { serial="/dev/ttyUSB99@115200" }
    Number          livingTemp01                            "Temperatur [%.1f °C]" <temperature> 
    Number          livingHum01                             "Feuchtigkeit [%.1f %%]" <temperature>
    

    We keep it simple and just assume we just want to read out a DHT22.

    Start up openHAB with "/opt/openhab/start.sh". You should see something like:

    2015-04-02 20:05:30.731 [INFO ] [runtime.busevents             ] - Arduino state updated to 0;0;3;0;14;Gateway startup complete.
    

    If that is the case: very good!!

    Now we need rules that reads the information that is received by openHAB via /dev/ttyUSB99 and show it.

    vi rules/demo.rules
    

    Content (that is the hardest part):

    import org.openhab.core.library.types.*
    import org.openhab.core.persistence.*
    import org.openhab.model.script.actions.*
    import org.joda.time.*
    import java.util.*
    import org.eclipse.xtext.xbase.lib.*
    import org.openhab.core.items.*
    
    
    var String ArduinoUpdate = ""
    var String sketchName = ""
    
    var int V_TEMP = 0
    var int V_HUM = 1
    var int V_LIGHT = 2
    var int V_DIMMER = 3
    var int V_PRESSURE = 4
    var int V_FORECAST = 5
    var int V_RAIN = 6
    var int V_RAINRATE = 7
    var int V_WIND = 8
    var int V_GUST = 9
    var int V_DIRECTION = 10
    var int V_UV = 11
    var int V_WEIGHT = 12
    var int V_DISTANCE = 13
    var int V_IMPEDANCE = 14
    var int V_ARMED = 15
    var int V_TRIPPED = 16
    var int V_WATT = 17
    var int V_KWH = 18
    var int V_SCENE_ON = 19
    var int V_SCENE_OFF = 20
    var int V_HEATER = 21
    var int V_HEATER_SW = 22
    var int V_LIGHT_LEVEL = 23
    var int V_VAR1 = 24
    var int V_VAR2 = 25
    var int V_VAR3 = 26
    var int V_VAR4 = 27
    var int V_VAR5 = 28
    var int V_UP = 29
    var int V_DOWN = 30
    var int V_STOP = 31
    var int V_IR_SEND = 32
    var int V_IR_RECEIVE = 33
    var int V_FLOW = 34
    var int V_VOLUME = 35
    var int V_LOCK_STATUS = 36
    var int V_DUST_LEVEL = 37
    var int V_VOLTAGE = 38
    var int V_CURRENT = 39
    var int msgPresentation = 0
    var int msgSet = 1
    var int msgReq = 2
    var int msgInternal = 3
    var int msgStream = 4
    var int alarmArmor = 1
    
    // Internal Commands
    
    var int I_BATTERY_LEVEL = 0
    var int I_TIME = 1
    var int I_VERSION = 2
    var int I_ID_REQUEST = 3
    var int I_ID_RESPONSE = 4
    var int I_INCLUSION_MODE = 5
    var int I_CONFIG = 6
    var int I_FIND_PARENT = 7
    var int I_FIND_PARENT_RESPONSE = 8
    var int I_LOG_MESSAGE = 9
    var int I_CHILDREN = 10
    var int I_SKETCH_NAME = 11
    var int I_SKETCH_VERSION = 12
    var int I_REBOOT = 13
    var int I_GATEWAY_READY = 14
    
    // Mappings
    var HashMap<String, String> sensorToItemsMap = newLinkedHashMap(
        "101;0;"            -> "livingHum01",
        "livingHum01"       -> "101;0;",
        "101;1;"            -> "livingTemp01",
        "livingTemp01"      -> "101;1;"
    )
        
    
    //receiving msg from mysensors gateway
    rule "Arduino sends to Openhab"
        when
            Item Arduino received update
        then
            var String lineBuffer =  Arduino.state.toString.split("\n")
            for (String line : lineBuffer) {
                var String[] message = line.split(";")
                var Integer nodeId = new Integer(message.get(0))
                var Integer childId = new Integer(message.get(1))
                var Integer msgType = new Integer(message.get(2))
                var Integer ack = new Integer(message.get(3))
                var Integer subType = new Integer(message.get(4))
                var String msg = message.get(5)
                if(msgType == 1 ){
                    if (subType == V_TEMP){
                        postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                        println ("Temp item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " temp: " + msg )
                        }
                    if (subType == V_HUM){
                        postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                        println ("Hum item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " hum: " + msg )
                        }
                    }    
                // Internal Command
                if(msgType == 3){
                    if(subType == I_SKETCH_NAME){
                            println("Sketch name: " + msg )
                            sketchName=msg
                        }
                    if(subType == I_SKETCH_VERSION){
                            println("Sketch version: " + msg )
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), sketchName+" " +msg )
                            sketchName=""
                        }
                    }
                }   
            }
    end
    

    Wow, that is hardcore! Are you still with me? Luckily there is not much to change for start.

    There is a mapping, that you may change or want to extent:

    // Mappings
    var HashMap<String, String> sensorToItemsMap = newLinkedHashMap(
        "101;0;"            -> "livingHum01",
        "livingHum01"       -> "101;0;",
        "101;1;"            -> "livingTemp01",
        "livingTemp01"      -> "101;1;"
    )
    

    Where "101" is the NODE_ID of my DHT22 Sensor. I'm using hardcoded NODE_IDs, so if your sensor has another NODE_ID already, just change the mappings in the configuration. The "0" and "1" after the "101" is the childId. Change that too if you must. Just look at the Arduino code of your sensor.

    Start openHAB if it is not already running and look for the output.

         2015-04-02 20:19:47.057 [INFO ] [runtime.busevents             ] - Arduino state updated to 0;0;3;0;9;read:101-101-0s=0,c=1,t=1,pt=7,l=5:44.7 
         101;0;1;0;1;44.7
    
    2015-04-02 20:19:47.114 [INFO ] [runtime.busevents             ] - livingHum01 state updated to 44.7
    Hum item: livingHum01 hum: 44.7
    

    If you see something like the above it is still working! ;-)

    *Nearly done! *

    Know we want to show the information in a sitemap. We just create one with:

    vi sitemaps/demo.sitemap
    

    Content:

    sitemap demo label="Hauptmenü"
    {
      Frame label="Wohnzimmer" {
        Text item=livingTemp01
        Text item=livingHum01
      }
    }
    

    We keep it simple here!

    If OpenHAB is not running start it and fire up your favorite browser and open

    http://localhost:8080/openhab.app?sitemap=demo
    

    For further information have a look at this thread: http://forum.mysensors.org/topic/655/serial-gateway-connection-to-openhab/2

    1 Reply Last reply
    7
    • raditvR Offline
      raditvR Offline
      raditv
      wrote on last edited by
      #2

      Great Post Tim :+1:

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jan Gatzke
        wrote on last edited by Jan Gatzke
        #3

        Nice tutorial. I discarded my own incoming rules and am now using this solution. I have extended the rule by a few additional sub types:

        if (subType == V_LIGHT_LEVEL){
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                            println ("Light item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " light: " + msg )
                            }
        if (subType == V_VOLTAGE){
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                            println ("Voltage item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " voltage: " + msg )
                            }
        if (subType == V_LOCK_STATUS){
                            var String lockstatus="OPEN"
                            if (msg=="1"){
                                lockstatus="CLOSED"
                            }
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), lockstatus)
                            println ("Door/Window item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " Status: " + lockstatus )
                            }
        if (subType == V_TRIPPED){
                            var String tripped="OFF"
                            if (msg=="1"){
                                tripped="ON"
                            }
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), tripped)
                            println ("Button/Contact item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " Status: " + tripped )
                            }
        if (subType == V_LIGHT){
                            var String light="OFF"
                            if (msg=="1"){
                                light="ON"
                            }
                            postUpdat(sensorToItemsMap.get( nodeId + ";" + childId + ";"), light)
                            println ("Light item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " Light: " + light )
                            }
        
        
        
        1 Reply Last reply
        2
        • R Offline
          R Offline
          reddy11
          wrote on last edited by
          #4

          Great post. Love the hashmap idea, much better then how I was doing it.
          Just wanted to point out that on my beaglebone I had add the serial port parameter to the /etc/init.d/openhab file or to the /usr/share/openhab/start.sh depending on your setup.

          -Dgnu.io.rxtx.SerialPorts=/dev/ttyO4 
          

          Im sure it has been covered elsewhere, but to enable other serial ports on the beaglebone you need to edit the /boot/uEnv.txt file and add the following to the optargs line:

          capemgr.enable_partno=BB-UART4
          
          

          This allows you to hook your arduino to P9_11(RX) and P9_13(TX) pins.

          1 Reply Last reply
          0
          • ChaoticC Offline
            ChaoticC Offline
            Chaotic
            wrote on last edited by Chaotic
            #5

            I think I'm going to try setting this up soon. I've been using the MQTT gateway but this would be simpler I think.

            Any advice for switching from MQTT to serial in openhab.

            I think I need to update openhab to use the 1.70 version but I don't think that would be too hard.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TimO
              Hero Member
              wrote on last edited by
              #6

              Can't give you an advice according the switch from MQTT to serial gateway.

              Upgrading to 1.7.0 is no big deal, just save the configurations folder. I suppose it is possible to use the 1.7.0 snapshot serial binding, with an older version of the openHAB runtime.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jan Gatzke
                wrote on last edited by
                #7

                @Chaotic said:

                oon. I've been using the MQTT gateway but this would be simpler I think.

                Any advice for switching from MQTT to serial in openhab.

                I think I need to update openhab to use the 1.70 version but I don't think that would be too hard.

                I am using openHAB 1.6.1 with this rule. Works without problems. You need to compile the gateway with 9600 baud in config.h because openHAB 1.6 has a fixed baud value for the serial communication.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  schudoel
                  wrote on last edited by
                  #8

                  @TimO Thank you very much! I could not get the Symlink to work at first, but then I fould this, maybe it can help other Raspberry Pi owners.

                  @Jan-Gatzke I tried to connect a different sensor (RelayActuator) to my Gateway, added it to my .items, .sitemap and added your V_LIGHT routine in my .rules, but no success. Did I miss anything?

                  Thanks to all of you
                  Flo

                  J 1 Reply Last reply
                  0
                  • S schudoel

                    @TimO Thank you very much! I could not get the Symlink to work at first, but then I fould this, maybe it can help other Raspberry Pi owners.

                    @Jan-Gatzke I tried to connect a different sensor (RelayActuator) to my Gateway, added it to my .items, .sitemap and added your V_LIGHT routine in my .rules, but no success. Did I miss anything?

                    Thanks to all of you
                    Flo

                    J Offline
                    J Offline
                    Jan Gatzke
                    wrote on last edited by
                    #9

                    @schudoel These rules do only handle incoming messages from the sensor nodes. If you want to switch a relay you need an additional rule which sends the command to the the serial device.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      schudoel
                      wrote on last edited by
                      #10

                      @Jan-Gatzke Thank you! I had a rule for the command, but it did not work out as I wanted. I think I had a typo in the code, but I could fix it. Are there any log files I could use for debugging the rules?

                      This is the code I use now:

                      import org.openhab.core.library.types.*
                      import org.openhab.core.persistence.*
                      import org.openhab.model.script.actions.*
                      import org.joda.time.*
                      import java.util.*
                      import org.eclipse.xtext.xbase.lib.*
                      import org.openhab.core.items.*
                      
                      
                      var String ArduinoUpdate = ""
                      var String sketchName = ""
                      
                      var int V_TEMP = 0
                      var int V_HUM = 1
                      var int V_LIGHT = 2
                      var int V_DIMMER = 3
                      var int V_PRESSURE = 4
                      var int V_FORECAST = 5
                      var int V_RAIN = 6
                      var int V_RAINRATE = 7
                      var int V_WIND = 8
                      var int V_GUST = 9
                      var int V_DIRECTION = 10
                      var int V_UV = 11
                      var int V_WEIGHT = 12
                      var int V_DISTANCE = 13
                      var int V_IMPEDANCE = 14
                      var int V_ARMED = 15
                      var int V_TRIPPED = 16
                      var int V_WATT = 17
                      var int V_KWH = 18
                      var int V_SCENE_ON = 19
                      var int V_SCENE_OFF = 20
                      var int V_HEATER = 21
                      var int V_HEATER_SW = 22
                      var int V_LIGHT_LEVEL = 23
                      var int V_VAR1 = 24
                      var int V_VAR2 = 25
                      var int V_VAR3 = 26
                      var int V_VAR4 = 27
                      var int V_VAR5 = 28
                      var int V_UP = 29
                      var int V_DOWN = 30
                      var int V_STOP = 31
                      var int V_IR_SEND = 32
                      var int V_IR_RECEIVE = 33
                      var int V_FLOW = 34
                      var int V_VOLUME = 35
                      var int V_LOCK_STATUS = 36
                      var int V_DUST_LEVEL = 37
                      var int V_VOLTAGE = 38
                      var int V_CURRENT = 39
                      var int msgPresentation = 0
                      var int msgSet = 1
                      var int msgReq = 2
                      var int msgInternal = 3
                      var int msgStream = 4
                      var int alarmArmor = 1
                      
                      // Internal Commands
                      
                      var int I_BATTERY_LEVEL = 0
                      var int I_TIME = 1
                      var int I_VERSION = 2
                      var int I_ID_REQUEST = 3
                      var int I_ID_RESPONSE = 4
                      var int I_INCLUSION_MODE = 5
                      var int I_CONFIG = 6
                      var int I_FIND_PARENT = 7
                      var int I_FIND_PARENT_RESPONSE = 8
                      var int I_LOG_MESSAGE = 9
                      var int I_CHILDREN = 10
                      var int I_SKETCH_NAME = 11
                      var int I_SKETCH_VERSION = 12
                      var int I_REBOOT = 13
                      var int I_GATEWAY_READY = 14
                      
                      // Mappings
                      var HashMap<String, String> sensorToItemsMap = newLinkedHashMap(
                          "100;0;"            -> "livingHum01",
                          "livingHum01"       -> "100;0;",
                          "100;1;"            -> "livingTemp01",
                          "livingTemp01"      -> "100;1;",
                          "101;1;"            -> "livingRelay",
                          "livingRelay"       -> "101;1;"
                      )
                      
                      //switch function
                      val org.eclipse.xtext.xbase.lib.Functions$Function4 switchOperation = [
                          org.openhab.core.library.items.SwitchItem relayItem, 
                          org.openhab.core.library.items.StringItem arduinoItem, 
                          String arduinoDevMap,
                          Integer subType|
                          var Integer state = 0
                              if (relayItem.state == OFF) {
                              state = 0 
                          }
                          else {
                              state = 1
                          }
                          println ("Function: switchOperation >> "+arduinoDevMap + "1;1;" + subType + ";" + state )
                          arduinoItem.sendCommand(arduinoDevMap + "1;0;" + subType + ";" + state + "\n")
                      ]
                      
                      
                      
                      //receiving msg from mysensors gateway
                      rule "Arduino sends to Openhab"
                          when
                              Item Arduino received update
                          then
                              var String lineBuffer =  Arduino.state.toString.split("\n")
                              for (String line : lineBuffer) {
                                  var String[] message = line.split(";")
                                  var Integer nodeId = new Integer(message.get(0))
                                  var Integer childId = new Integer(message.get(1))
                                  var Integer msgType = new Integer(message.get(2))
                                  var Integer ack = new Integer(message.get(3))
                                  var Integer subType = new Integer(message.get(4))
                                  var String msg = message.get(5)
                                  if(msgType == 1 ){
                                      if (subType == V_TEMP){
                                          postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                                          println ("Temp item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " temp: " + msg )
                                          }
                                      if (subType == V_HUM){
                                          postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                                          println ("Hum item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " hum: " + msg )
                                          }
                      		if (subType == V_LIGHT){
                                          var String state
                                          var Integer statusInt = new Integer(message.get(5))
                                          if(statusInt == 1) { 
                                              state = "ON"
                                              } 
                                          else { 
                                              state = "OFF" 
                                              }
                                          postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), state)
                                          println ("Light Item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " Light: " + state )
                                          }
                      
                      
                                  // Internal Command
                                  if(msgType == 3){
                                      if(subType == I_SKETCH_NAME){
                                              println("Sketch name: " + msg )
                                              sketchName=msg
                                          }
                                      if(subType == I_SKETCH_VERSION){
                                              println("Sketch version: " + msg )
                                              postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), sketchName+" " +msg )
                                              sketchName=""
                                          }
                      		
                                      }
                                  }   
                              }
                      end
                      
                      rule "livingRelay Switch Rule"
                          when
                              Item livingRelay changed
                          then
                              switchOperation.apply(livingRelay, Arduino, sensorToItemsMap.get("livingRelay"), V_LIGHT)         
                      end 
                      

                      and it's working like charm.

                      J 1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        schudoel
                        wrote on last edited by
                        #11

                        Was working ... I do not know why, but after I changed the power cables of the sensor, it stopped working correctly. Serial output:

                        repeater started, id 101
                        send: 101-101-0-0 s=255,c=0,t=18,pt=0,l=5,st=fail:1.4.1
                        send: 101-101-0-0 s=255,c=3,t=6,pt=1,l=1,st=fail:0
                        send: 101-101-0-0 s=255,c=3,t=11,pt=0,l=5,st=fail:Relay
                        send: 101-101-0-0 s=255,c=3,t=12,pt=0,l=3,st=fail:1.0
                        send: 101-101-0-0 s=1,c=0,t=3,pt=0,l=5,st=fail:1.4.1
                        

                        I do not have a decoupling capacitor to test whether it could solve my issues, but my hum/temp sensor and gateway do not have one either and are still working perfectly. I have tried to find any information on those fail messages, but wasn't successful. Do you have any suggestions?

                        1 Reply Last reply
                        0
                        • S schudoel

                          @Jan-Gatzke Thank you! I had a rule for the command, but it did not work out as I wanted. I think I had a typo in the code, but I could fix it. Are there any log files I could use for debugging the rules?

                          This is the code I use now:

                          import org.openhab.core.library.types.*
                          import org.openhab.core.persistence.*
                          import org.openhab.model.script.actions.*
                          import org.joda.time.*
                          import java.util.*
                          import org.eclipse.xtext.xbase.lib.*
                          import org.openhab.core.items.*
                          
                          
                          var String ArduinoUpdate = ""
                          var String sketchName = ""
                          
                          var int V_TEMP = 0
                          var int V_HUM = 1
                          var int V_LIGHT = 2
                          var int V_DIMMER = 3
                          var int V_PRESSURE = 4
                          var int V_FORECAST = 5
                          var int V_RAIN = 6
                          var int V_RAINRATE = 7
                          var int V_WIND = 8
                          var int V_GUST = 9
                          var int V_DIRECTION = 10
                          var int V_UV = 11
                          var int V_WEIGHT = 12
                          var int V_DISTANCE = 13
                          var int V_IMPEDANCE = 14
                          var int V_ARMED = 15
                          var int V_TRIPPED = 16
                          var int V_WATT = 17
                          var int V_KWH = 18
                          var int V_SCENE_ON = 19
                          var int V_SCENE_OFF = 20
                          var int V_HEATER = 21
                          var int V_HEATER_SW = 22
                          var int V_LIGHT_LEVEL = 23
                          var int V_VAR1 = 24
                          var int V_VAR2 = 25
                          var int V_VAR3 = 26
                          var int V_VAR4 = 27
                          var int V_VAR5 = 28
                          var int V_UP = 29
                          var int V_DOWN = 30
                          var int V_STOP = 31
                          var int V_IR_SEND = 32
                          var int V_IR_RECEIVE = 33
                          var int V_FLOW = 34
                          var int V_VOLUME = 35
                          var int V_LOCK_STATUS = 36
                          var int V_DUST_LEVEL = 37
                          var int V_VOLTAGE = 38
                          var int V_CURRENT = 39
                          var int msgPresentation = 0
                          var int msgSet = 1
                          var int msgReq = 2
                          var int msgInternal = 3
                          var int msgStream = 4
                          var int alarmArmor = 1
                          
                          // Internal Commands
                          
                          var int I_BATTERY_LEVEL = 0
                          var int I_TIME = 1
                          var int I_VERSION = 2
                          var int I_ID_REQUEST = 3
                          var int I_ID_RESPONSE = 4
                          var int I_INCLUSION_MODE = 5
                          var int I_CONFIG = 6
                          var int I_FIND_PARENT = 7
                          var int I_FIND_PARENT_RESPONSE = 8
                          var int I_LOG_MESSAGE = 9
                          var int I_CHILDREN = 10
                          var int I_SKETCH_NAME = 11
                          var int I_SKETCH_VERSION = 12
                          var int I_REBOOT = 13
                          var int I_GATEWAY_READY = 14
                          
                          // Mappings
                          var HashMap<String, String> sensorToItemsMap = newLinkedHashMap(
                              "100;0;"            -> "livingHum01",
                              "livingHum01"       -> "100;0;",
                              "100;1;"            -> "livingTemp01",
                              "livingTemp01"      -> "100;1;",
                              "101;1;"            -> "livingRelay",
                              "livingRelay"       -> "101;1;"
                          )
                          
                          //switch function
                          val org.eclipse.xtext.xbase.lib.Functions$Function4 switchOperation = [
                              org.openhab.core.library.items.SwitchItem relayItem, 
                              org.openhab.core.library.items.StringItem arduinoItem, 
                              String arduinoDevMap,
                              Integer subType|
                              var Integer state = 0
                                  if (relayItem.state == OFF) {
                                  state = 0 
                              }
                              else {
                                  state = 1
                              }
                              println ("Function: switchOperation >> "+arduinoDevMap + "1;1;" + subType + ";" + state )
                              arduinoItem.sendCommand(arduinoDevMap + "1;0;" + subType + ";" + state + "\n")
                          ]
                          
                          
                          
                          //receiving msg from mysensors gateway
                          rule "Arduino sends to Openhab"
                              when
                                  Item Arduino received update
                              then
                                  var String lineBuffer =  Arduino.state.toString.split("\n")
                                  for (String line : lineBuffer) {
                                      var String[] message = line.split(";")
                                      var Integer nodeId = new Integer(message.get(0))
                                      var Integer childId = new Integer(message.get(1))
                                      var Integer msgType = new Integer(message.get(2))
                                      var Integer ack = new Integer(message.get(3))
                                      var Integer subType = new Integer(message.get(4))
                                      var String msg = message.get(5)
                                      if(msgType == 1 ){
                                          if (subType == V_TEMP){
                                              postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                                              println ("Temp item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " temp: " + msg )
                                              }
                                          if (subType == V_HUM){
                                              postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                                              println ("Hum item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " hum: " + msg )
                                              }
                          		if (subType == V_LIGHT){
                                              var String state
                                              var Integer statusInt = new Integer(message.get(5))
                                              if(statusInt == 1) { 
                                                  state = "ON"
                                                  } 
                                              else { 
                                                  state = "OFF" 
                                                  }
                                              postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), state)
                                              println ("Light Item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " Light: " + state )
                                              }
                          
                          
                                      // Internal Command
                                      if(msgType == 3){
                                          if(subType == I_SKETCH_NAME){
                                                  println("Sketch name: " + msg )
                                                  sketchName=msg
                                              }
                                          if(subType == I_SKETCH_VERSION){
                                                  println("Sketch version: " + msg )
                                                  postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), sketchName+" " +msg )
                                                  sketchName=""
                                              }
                          		
                                          }
                                      }   
                                  }
                          end
                          
                          rule "livingRelay Switch Rule"
                              when
                                  Item livingRelay changed
                              then
                                  switchOperation.apply(livingRelay, Arduino, sensorToItemsMap.get("livingRelay"), V_LIGHT)         
                          end 
                          

                          and it's working like charm.

                          J Offline
                          J Offline
                          Jan Gatzke
                          wrote on last edited by
                          #12

                          @schudoel You can have a look at the command line output of openHAB. Just start it via start.sh instead of the init script.

                          Are you sure your cabling is 100% ok?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            schudoel
                            wrote on last edited by
                            #13

                            @Jan-Gatzke Yes, the command line of openHAB is always open. Wiring was okay. I bought some capacitors yesterday and added one 4.7uF to every nrf24l01+, now it is working again :)

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              Tango2
                              wrote on last edited by
                              #14

                              Not having much linux experience, I'm struggling with getting my serial gateway communicating with openHAB. If I install 1.7.0 in the /opt directory (entirely) this works, however I don't know how to get 1.7.0 installed after having installed 1.6.2 via apt-get - I tried replacing the files in each directory with the updated 1.7.0 files but 1.6.2 runtime is still starting automatically on boot in the openhab.log file.

                              I'm also reading that 1.7.0 may not be necessary for this to work, however with 1.6.2 running my USB port is not recognized in openhab - is that possibly due to the port speed being set at 115200 like @Jan-Gatzke mentioned above?

                              @Jan-Gatzke

                              I am using openHAB 1.6.1 with this rule. Works without problems. You need to compile the gateway with 9600 baud in config.h because openHAB 1.6 has a fixed baud value for the serial communication.

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                TimO
                                Hero Member
                                wrote on last edited by
                                #15

                                The support for a baudrate of 115.200, which is used by the MySensor serial gateway, is available since 1.7.0 in the OpenHab serial binding.

                                You're able to remove the old version with "apt-get remove ...", no need to replace the files. Just use the installation in /opt.

                                C 1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  Tango2
                                  wrote on last edited by
                                  #16

                                  Thanks @TimO. I got everything running in /opt (@9600, but I can change that later), and am receiving humidity/temp stats from my one sensor module running on a nano with DHT22 connected. I need to go through and do some major cleanup now, and also figure out how to convert to from F to C for the temp. It seems like the stock code has provisions for that, however I haven't quite figured out how to enable the change yet.

                                  Thanks for the help - I look forward to implementing more of these sensors.

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    guzzler
                                    wrote on last edited by guzzler
                                    #17

                                    I am having trouble getting Arduino Nano detected by Raspberry Pi via USB.

                                    I could see this in the monitor when I selected the correct baudrate:

                                     0;0;3;0;14;Gateway startup complete.
                                    
                                    [151199.509040] usb 1-1.2: new full-speed USB device number 5 using dwc_otg
                                    [151199.612259] usb 1-1.2: New USB device found, idVendor=1a86, idProduct=7523
                                    [151199.612296] usb 1-1.2: New USB device strings: Mfr=0, Product=2,
                                    SerialNumber=0
                                    [151199.612314] usb 1-1.2: Product: USB2.0-Serial
                                    [153464.158413] usb 1-1.2: USB disconnect, device number 5
                                    [153715.311307] usb 1-1.3: new full-speed USB device number 6 using dwc_otg
                                    [153715.414679] usb 1-1.3: New USB device found, idVendor=1a86, idProduct=7523
                                    [153715.414717] usb 1-1.3: New USB device strings: Mfr=0, Product=2,
                                    SerialNumber=0
                                    [153715.414733] usb 1-1.3: Product: USB2.0-Serial
                                    

                                    Initially while flashing serial gateway sketch to nano from Mac I had some trouble to get the right drivers. After following this post it worked fine. Is this to do with chinese knock offs?
                                    http://kiguino.moos.io/2014/12/31/how-to-use-arduino-nano-mini-pro-with-CH340G-on-mac-osx-yosemite.html

                                    Also I installed Arduino sketch on Raspberry pi with apt-get install arduino: When I launch Arduino and go to Tools>Port is disabled.

                                    Is there anything I need to do on RPi so it can detect Arduino Nano knock pff?

                                    P 1 Reply Last reply
                                    0
                                    • T Offline
                                      T Offline
                                      TimO
                                      Hero Member
                                      wrote on last edited by
                                      #18

                                      I followed the link to the page you mentioned and from there to the chinese page.
                                      There is a zip-file for linux too, maybe you need to install the driver on the RPi too?

                                      http://www.wch.cn/download/CH341SER_LINUX_ZIP.html

                                      Just a guess. :-)

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        guzzler
                                        wrote on last edited by
                                        #19

                                        Thanks @TimO .

                                        I have been trying to make and load that driver but cannot get hands on 3.12.28+ kernel source. It wont compile without it.

                                        I am wondering how come no body has come accross this issue, I bought my Arduino Nano from one of the links on mysensors store.

                                        http://www.aliexpress.com/item/1-pcs-for-Nano-V3-0-ATmega328P-Module-Board-Free-Mini-USB-Cable-for-Arduino-Compatible/1397847851.html?aff_platform=aaf&sk=3jYZr3nAE%3A&aff_trace_key=23901740799811215&cpt=1431974332161

                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          Jan Gatzke
                                          wrote on last edited by
                                          #20

                                          I don't understand why everybody is using an USB connection for the serial gateway when openHAB is running on a raspberry pi. The pi has a rock stable uart with 3.3V logic. There is no need to convert the arduinos uart to USB. Just connect RX to TX and TX to RX. Connect ground to ground and VCC of the arduino to the 3.3V pin of the pi and you are done. The official openHAB wiki contains instructions on how to use the onboard uart of the pi: https://github.com/openhab/openhab/wiki/Serial-Binding

                                          1 Reply Last reply
                                          1
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          18

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular