Skip to content
  • MySensors
  • 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. My Project
  3. A very beginner needs help - MySensors and OpenHab !

A very beginner needs help - MySensors and OpenHab !

Scheduled Pinned Locked Moved My Project
43 Posts 7 Posters 28.0k Views 6 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 TimO

    Hi @ewgor ,

    could you make your whole configuration available for download? It is easier for us to help you to find the error.

    E Offline
    E Offline
    ewgor
    wrote on last edited by
    #20

    @TimO what do you mean by " configuration available for download"? i'm combining different Arduino sketches found here on the site and i'm trying to find a way to match them with the Openhab configuration found here too! All my files are above but right now i'm trying to combine the Arduino sketch from above with this one for relay with button actuator from here http://www.mysensors.org/build/relay and its very difficult. couldn't do it today, maybe tomorrow :(
    As a conclusion because i don't know if i was very clear: Could anyone help me combine Arduino sketch from above with the one for relay with button actuator for 2 (two relays and two buttons) devices and to check the Openhab config which are above too? As i already said, today o tried all day long to combine them but cant lose the errors, it cant compile :(

    Hoping for an answer ...

    1 Reply Last reply
    0
    • E Offline
      E Offline
      ewgor
      wrote on last edited by
      #21

      People, still struggling to get the relay with button actuator working with openhab!
      This time im using the Arduino example from here http://www.mysensors.org/build/relay (the 2nd one) with this Openhab configuration:
      Rules (forget the temp and hum 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",    // looks good
          "livingHum01"       -> "101;0;",
         
      	"101;3;"            -> "livingTemp01", 
          "livingTemp01"      -> "101;3;"
      
         "101;1;"            -> "lightBar01", 
          "lightBar01"      -> "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 )
                          }
                      if (subType == V_WATT){
                          postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                          println ("Watt item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " watt: " + msg )
                          }
      				if (subType == V_KWH){
                          postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                          println ("Kwh item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " kwh: " + 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 "Light"
              when
                      Item lightBar01 received update
              then
                     if(receivedCommand == ON) {
                  sendCommand(Arduino, "101;1;1;0;2;1\n")
              }
              if(receivedCommand == OFF) {
                 sendCommand(Arduino, "101;1;1;0;2;0\n")
              }
      end
      
      

      items:

      String Arduino "Arduino" { serial="COM4@115200" }
      Number          livingTemp01                            "Temperatura [%.1f °C]" <temperature> 
      Number          livingHum01                             "Umiditate [%.1f %%]" <water>
      Number          livingWatt01                             "Consum Electric Watt [%.1f Watt]" <energy>
      Number          livingKwh01                             "Consum Electric Kilowattora [%.1f kWh]" <energy>
      Switch           lightBar01                          "Bar Lights"
      Switch           lightKitchenCabinet01               "Cabinet Lights"
      

      sitemap:

      sitemap demo label="Acasa"
      {
        Frame label="Dormitor" {
          Text item=livingTemp01
          Text item=livingHum01
          Text item=livingWatt01
      	Text item=livingKwh01
        }
         Frame label="Lights" {
              Group label="Lights" {
                  Switch item=lightBar01 label="Bar Lights"
                  Switch item=lightKitchenCabinet01 label="Cabinet Lights"
              }
          }
      

      The physical button is connected to digital pin 3 and a LED to digital pin 4.
      If the LED is switched on/ off from physical button the server shows this:

      2015-07-31 11:36:02.067 [INFO ] [runtime.busevents             ] - Arduino state
       updated to 0;0;3;0;9;read: 101-101-0 s=1,c=1,t=2,pt=2,l=2:1
      0;0;3;0;9;send: 0-0-101-101 s=1,c=1,t=2,pt=2,l=2,st=ok:1
      101;1;1;0;2;1
      

      If the Openhab switch is activated on/off the server shows this:

      2015-07-31 11:38:13.435 [INFO ] [runtime.busevents             ] - lightBar01 re
      ceived command ON
      

      but the LED is not switching on or off.
      On the other hand when pressing the physical button on / off the LED is working but the switch in Openhab is not moving or refreshing.

      i'm trying to take it easy! please help masters :)

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ewgor
        wrote on last edited by
        #22

        Update:
        i noticed that if i leave only the light Openhab config as bellow, when pressing the physical button on my breadboard it turns the led on or off and it changes the Openhab switch state from on to off and vice-versa but it doesn't work from Openhab to Arduino.
        Openhab configuration (sitemap, items, rules):

        // items
        String Arduino "Arduino" { serial="COM4@115200" }
        Switch           lightBar01                          "Bar Lights"
        =====================================================
        //sitemap
        sitemap demo label="Acasa"
        {
           Frame label="Lights" {
                    Switch item=lightBar01 label="Bar Lights"
            }
        ======================================================
        //rules
        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;1;"            -> "lightBar01",    // looks good
            "lightBar01"       -> "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_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 "Light"
                when
                        Item lightBar01 received update
                then
                       if(receivedCommand == ON) {
                    sendCommand(Arduino, "101;1;1;0;2;1\n")
                }
                if(receivedCommand == OFF) {
                   sendCommand(Arduino, "101;1;1;0;2;0\n")
                }
        end
        

        The server returns this error but its still working:

        2015-07-31 17:52:01.787 [INFO ] [runtime.busevents             ] - lightBar01 st
        ate updated to ON
        Light Item: lightBar01 Light: ON
        2015-07-31 17:52:01.795 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during
        the execution of rule 'Light': The name 'receivedCommand' cannot be resolved to
        an item or type.
        

        If i delete the rule "light" (the last part of rule file) the server doesn't show the error and the server still works so pressing the physical button it turns on or off the LED and it change the Openhab switch from on to off but from Openhab to Arduino, the same, still not communicating, not turning any LED!
        This is how the server looks if i delete the rule "light" and i press the physical button on my breadboard:

        2015-07-31 17:56:03.448 [INFO ] [runtime.busevents             ] - lightBar01 st
        ate updated to OFF
        Light Item: lightBar01 Light: OFF
        

        and switching from Openhab but without changing the LED real state:

        2015-07-31 17:57:43.719 [INFO ] [runtime.busevents             ] - lightBar01 re
        ceived command ON
        2015-07-31 17:57:45.004 [INFO ] [runtime.busevents             ] - lightBar01 re
        ceived command OFF
        

        This makes sens and the problem i know is on the rule file. Now is there anyone who can help me make this work from Openhab to Arduino too? thanks all for your kindness :)

        PS: using the Arduino relay with button actuator sketch on which i added the node id 101 and the pins 48,49 for my Arduino mega.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          ewgor
          wrote on last edited by
          #23

          No body? Any idea?

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

            Found it!

            "received command" not "received update"!

            rule "Light"
                    when
                            Item lightBar01 received command
                    then
                           if(receivedCommand == ON) {
                        sendCommand(Arduino, "101;1;1;0;2;1\n")
                    }
                    if(receivedCommand == OFF) {
                       sendCommand(Arduino, "101;1;1;0;2;0\n")
                    }
            end
            
            
            E 1 Reply Last reply
            1
            • T TimO

              Found it!

              "received command" not "received update"!

              rule "Light"
                      when
                              Item lightBar01 received command
                      then
                             if(receivedCommand == ON) {
                          sendCommand(Arduino, "101;1;1;0;2;1\n")
                      }
                      if(receivedCommand == OFF) {
                         sendCommand(Arduino, "101;1;1;0;2;0\n")
                      }
              end
              
              
              E Offline
              E Offline
              ewgor
              wrote on last edited by
              #25

              @TimO yuhuuuuuuu super! finally the damn led works ! thanks @TimO one small step for mankind but a huge step for me :)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ewgor
                wrote on last edited by
                #26

                Hi,
                @TimO finally I managed to add the dht 11 too and it works fine. Can you please tell me what to change in the rule file to add more buttons (same as this one that works)? I tried to change some parts of the light address but it doesn't work.
                I would like to add a normal button too (not like a switch) with a door symbol like door open or close for my exit door and to change its status from blue to red when you push the button.

                Thank you for the help you gave me by now!
                All the best!

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  ewgor
                  wrote on last edited by ewgor
                  #27

                  Hi All,

                  Can anyone please tell me if I want to add a few more buttons (same as this one with relay with button actuator) what should i change in the Openhab rules and in the Arduino sketch too? can it be done as the relay actuator example ?

                  #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                  

                  i mean first relay is connected to pin 3 and the next relay to pin 3+1=4, pin 4+1=5 and so on and the buttons pins to be alocated in the same way? is there any way to do it like this but for the relay with button actuator example?

                  In the Openhab rules i tried to modify as i said above some of the addresses of the commands on or off from here:

                  sendCommand(Arduino, "101;1;1;0;2;1\n")
                  

                  except the last numbers which i understand are like 1 for ON and 0 for OFF but it didn't work.

                  The last thing that i would like to add is related only to Openhab if ill get an answer or a suggestion to my ask here above is a normal button (not like a slider) to control an electrical strike through a relay so when pressed once door open for 5 seconds or more and than close again. I know that here are more to discuss because maybe the door its not gonna be psychically closed but the Openhab status could show that the door is closed but this can be solved with a magnetic contact (i think) that can be added so the Openhab rules should be made like so: if the magnetic contact is connected than the door is closed and locked, else the door is open / unlocked even the electrical strike is closed until the magnetic contact will be connected! or to close the electrical strike only after the magnetic contact is closed.
                  OH too much talking, I know this is maybe too much for me but theoretical speaking I know how to make it and I think i know how to set up the connection but physically is impossible because of missing knowledge in programming Arduino or Openhab!

                  Lets take it one by one: Can anyone help me with first part of my request, how to add a few more buttons same as the only one working now?

                  Thanks all

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    ewgor
                    wrote on last edited by
                    #28

                    People i see that this post was seen by more than 2k times but nobody is answering! How is this possible? Nobody knows how to do it or everybody enjoy watching a noob asking for help? Common people at least off topic let's say hi each other!

                    hekH 1 Reply Last reply
                    0
                    • E ewgor

                      People i see that this post was seen by more than 2k times but nobody is answering! How is this possible? Nobody knows how to do it or everybody enjoy watching a noob asking for help? Common people at least off topic let's say hi each other!

                      hekH Offline
                      hekH Offline
                      hek
                      Admin
                      wrote on last edited by
                      #29

                      Hey @ewgor, tone it down a bit. You won't get any answers by acting aggressive.

                      It could be as simple as no-one has the the answer and you got to find a solution yourself.

                      E 1 Reply Last reply
                      1
                      • hekH hek

                        Hey @ewgor, tone it down a bit. You won't get any answers by acting aggressive.

                        It could be as simple as no-one has the the answer and you got to find a solution yourself.

                        E Offline
                        E Offline
                        ewgor
                        wrote on last edited by
                        #30

                        @hek thanks for the answer. At least now i know i'm not alone :). Anyhow as far as i've seen by now only @TimO was the only one who answered me and gave me some info. It is possible that non of the heros, admins or other kind of this forum members to have a suggestion for me?
                        Sorry if i was aggressive again!

                        Good help :)

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

                          @ewgor said:

                          #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                          

                          i mean first relay is connected to pin 3 and the next relay to pin 3+1=4, pin 4+1=5 and so on and the buttons pins to be alocated in the same way? is there any way to do it like this but for the relay with button actuator example?

                          Well, the short answer for this is yes. It depends on your sketch. The loop with pin+1 is just a short notation.

                          I would suggest to start with the simple sketch with one button here: http://www.mysensors.org/build/binary

                          Give the node a static id, change items, rules and sitemap in OH.

                          You have to change V_TRIPPED to V_LIGHT:

                          // Change to V_LIGHT if you use S_LIGHT in presentation below
                          MyMessage msg(CHILD_ID,V_TRIPPED);
                          

                          So the OH rule with V_LIGHT will match and the item state is changed, when the button is pressed.

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            ewgor
                            wrote on last edited by
                            #32

                            Oh TimO i thought i'll never see you again here :) thanks for the answer.
                            I'm using the RelayWithButtonActuator example on which i added the humidity and temperature part and its working well. I choose this example because in this way i can get in real time the light status in Openhab even the light is turned ON / OFF from physical button on the breadboard or from the Openhab button.
                            The Openhab rules are already made with the V_LIGHT command not the V_TRIPPED.

                            I cant figure out what to add in the Arduino sketch to add more relays and more buttons and to make it work in the setup or loop functions. Than, on the Openhab rules i tried to copy the only light rule i have right now and to change some of the numbers in the

                            sendCommand(Arduino, "101;1;1;0;2;1\n") 
                            

                            but its not working :(

                            If i'll get at least 5 buttons and 5 relays working in this way it would be perfect.

                            Looking for any answer.

                            1 Reply Last reply
                            0
                            • E Offline
                              E Offline
                              ewgor
                              wrote on last edited by
                              #33

                              Hello? TimO? :) are you there?

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

                                Well, there are many approaches and I would go the easiest (but not the most elegant) way.

                                Double everything for every additional button / relay. If that works for two/three buttons/relays use arrays (like in the relay example sketch).

                                #include <MySensor.h>
                                #include <SPI.h>
                                #include <Bounce2.h>
                                
                                #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
                                #define RELAY_PIN2 5 ######## ADD THAT ######
                                #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
                                #define BUTTON_PIN2 6 ######## ADD THAT ######
                                #define CHILD_ID 1   // Id of the sensor child
                                #define CHILD_ID2 2 ######## ADD THAT ######
                                #define RELAY_ON 1
                                #define RELAY_OFF 0
                                
                                Bounce debouncer = Bounce(); 
                                Bounce debouncer2 = Bounce(); ######## ADD THAT ######
                                int oldValue=0;
                                int oldValue2=0; ######## ADD THAT ######
                                bool state;
                                bool state2; ######## ADD THAT ######
                                MySensor gw;
                                MyMessage msg(CHILD_ID,V_LIGHT);
                                MyMessage msg2(CHILD_ID2,V_LIGHT); ######## ADD THAT ######
                                
                                void setup()  
                                {  
                                  gw.begin(incomingMessage, AUTO, true);
                                
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Relay & Button", "1.0");
                                
                                 // Setup the button
                                  pinMode(BUTTON_PIN,INPUT);
                                  pinMode(BUTTON_PIN2,INPUT); ######## ADD THAT ######
                                  // Activate internal pull-up
                                  digitalWrite(BUTTON_PIN,HIGH);
                                  digitalWrite(BUTTON_PIN2,HIGH); ######## ADD THAT ######
                                  
                                  // After setting up the button, setup debouncer
                                  debouncer.attach(BUTTON_PIN);
                                  debouncer2.attach(BUTTON_PIN2); ######## ADD THAT ######
                                  debouncer.interval(5);
                                  debounder2.interval(5);######## ADD THAT ######
                                
                                  // Register all sensors to gw (they will be created as child devices)
                                  gw.present(CHILD_ID, S_LIGHT);
                                  gw.present(CHILD_ID2,S_LIGHT);######## ADD THAT ######
                                
                                  // Make sure relays are off when starting up
                                  digitalWrite(RELAY_PIN, RELAY_OFF);
                                  digitalWrite(RELAY_PIN2,RELAY_OFF); ######## ADD THAT ######
                                  // Then set relay pins in output mode
                                  pinMode(RELAY_PIN, OUTPUT);
                                  pinMode(RELAY_PIN2, OUTPUT); ######## ADD THAT ######   
                                      
                                  // Set relay to last known state (using eeprom storage) 
                                  state = gw.loadState(CHILD_ID);
                                  state2 = gw.loadState(CHILD_ID2); ######## ADD THAT ######
                                  digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                  digitalWrite(RELAY_PIN2, state2?RELAY_ON:RELAY_OFF); ######## ADD THAT ######
                                }
                                
                                
                                /*
                                *  Example on how to asynchronously check for new messages from gw
                                */
                                void loop() 
                                {
                                  gw.process();
                                  debouncer.update();
                                  debounder2.update(); ######## ADD THAT ######
                                
                                  // Get the update value
                                  int value = debouncer.read();
                                  if (value != oldValue && value==0) {
                                      gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                                  }
                                  oldValue = value;
                                } 
                                
                                value = debouncer2.read(); ######## ADD THAT ######
                                  if (value != oldValue2 && value==0) { ######## ADD THAT ######
                                      gw.send(msg2.set(state?false:true), true); // Send new state and request ack back ######## ADD THAT ######
                                  } ######## ADD THAT ######
                                  oldValue2 = value; ######## ADD THAT ######
                                } ######## ADD THAT ######
                                 
                                void incomingMessage(const MyMessage &message) {
                                  // We only expect one type of message from controller. But we better check anyway.
                                  if (message.isAck()) {
                                     Serial.println("This is an ack from gateway");
                                  }
                                
                                  if (message.type == V_LIGHT) {
                                     // Change relay state
                                     state = message.getBool();
                                     if(message.sensor == 1) { ######## ADD THAT ######
                                       digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                       // Store state in eeprom
                                       gw.saveState(CHILD_ID, state);
                                     } else if(message.sensor == 2) { ######## ADD THAT ######
                                       digitalWrite(RELAY_PIN2, state?RELAY_ON:RELAY_OFF); ######## ADD THAT ######
                                       // Store state in eeprom ######## ADD THAT ######
                                       gw.saveState(CHILD_ID2, state); ######## ADD THAT ######
                                    }
                                    
                                     // Write some debug info
                                     Serial.print("Incoming change for sensor:");
                                     Serial.print(message.sensor);
                                     Serial.print(", New status: ");
                                     Serial.println(message.getBool());
                                   } 
                                }
                                

                                The code is not tested nor compiled, but I hope you'll get the idea of how to add a button and a relay.

                                Of course this could be done much more elegant with arrays, that could be your next step.

                                1 Reply Last reply
                                0
                                • E Offline
                                  E Offline
                                  ewgor
                                  wrote on last edited by ewgor
                                  #35

                                  Wow! thanks for your time to fill this sketch! appreciate that.

                                  I tried your sketch and after cleaning some small mistakes i got these errors and i don't know how to lose them.
                                  this is how the sketch looks:

                                  #include <MySensor.h>
                                  #include <SPI.h>
                                  #include <Bounce2.h>
                                  
                                  #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
                                  #define RELAY_PIN2 5 //######## ADD THAT ######
                                  #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
                                  #define BUTTON_PIN2 6 //######## ADD THAT ######
                                  #define CHILD_ID 1   // Id of the sensor child
                                  #define CHILD_ID2 2 //######## ADD THAT ######
                                  #define RELAY_ON 1
                                  #define RELAY_OFF 0
                                  
                                  Bounce debouncer = Bounce(); 
                                  Bounce debouncer2 = Bounce(); //######## ADD THAT ######
                                  int oldValue=0;
                                  int oldValue2=0; //######## ADD THAT ######
                                  bool state;
                                  bool state2; //######## ADD THAT ######
                                  MySensor gw;
                                  MyMessage msg(CHILD_ID,V_LIGHT);
                                  MyMessage msg2(CHILD_ID2,V_LIGHT); //######## ADD THAT ######
                                  
                                  void setup()  
                                  {  
                                    gw.begin(incomingMessage, AUTO, true);
                                  
                                    // Send the sketch version information to the gateway and Controller
                                    gw.sendSketchInfo("Relay & Button", "1.0");
                                  
                                   // Setup the button
                                    pinMode(BUTTON_PIN,INPUT);
                                    pinMode(BUTTON_PIN2,INPUT); //######## ADD THAT ######
                                    // Activate internal pull-up
                                    digitalWrite(BUTTON_PIN,HIGH);
                                    digitalWrite(BUTTON_PIN2,HIGH); //######## ADD THAT ######
                                    
                                    // After setting up the button, setup debouncer
                                    debouncer.attach(BUTTON_PIN);
                                    debouncer2.attach(BUTTON_PIN2); //######## ADD THAT ######
                                    debouncer.interval(5);
                                    debouncer2.interval(5);//######## ADD THAT ######
                                  
                                    // Register all sensors to gw (they will be created as child devices)
                                    gw.present(CHILD_ID, S_LIGHT);
                                    gw.present(CHILD_ID2, S_LIGHT);//######## ADD THAT ######
                                  
                                    // Make sure relays are off when starting up
                                    digitalWrite(RELAY_PIN, RELAY_OFF);
                                    digitalWrite(RELAY_PIN2, RELAY_OFF); //######## ADD THAT ######
                                    // Then set relay pins in output mode
                                    pinMode(RELAY_PIN, OUTPUT);
                                    pinMode(RELAY_PIN2, OUTPUT); //######## ADD THAT ######   
                                        
                                    // Set relay to last known state (using eeprom storage) 
                                    state = gw.loadState(CHILD_ID);
                                    state2 = gw.loadState(CHILD_ID2); //######## ADD THAT ######
                                    digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                    digitalWrite(RELAY_PIN2, state2?RELAY_ON:RELAY_OFF); //######## ADD THAT ######
                                  }
                                  
                                  
                                  /*
                                  *  Example on how to asynchronously check for new messages from gw
                                  */
                                  void loop() 
                                  {
                                    gw.process();
                                    debouncer.update();
                                    debouncer2.update(); //######## ADD THAT ######
                                  
                                    // Get the update value
                                    int value = debouncer.read();
                                    if (value != oldValue && value==0) {
                                        gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                                    }
                                    oldValue = value;
                                  } 
                                  
                                    int value2 = debouncer2.read(); 
                                    if (value2 != oldValue && value==0) { 
                                        gw.send(msg2.set(state?false:true), true); 
                                    }
                                    oldValue2 = value;
                                  }
                                   
                                  void incomingMessage(const MyMessage &message) {
                                    // We only expect one type of message from controller. But we better check anyway.
                                    if (message.isAck()) {
                                       Serial.println("This is an ack from gateway");
                                    }
                                  
                                    if (message.type == V_LIGHT) {
                                       // Change relay state
                                       state = message.getBool();
                                       if(message.sensor == 1) { //######## ADD THAT ######
                                         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                         // Store state in eeprom
                                         gw.saveState(CHILD_ID, state);
                                       } else if(message.sensor == 2) { //######## ADD THAT ######
                                         digitalWrite(RELAY_PIN2, state?RELAY_ON:RELAY_OFF); //######## ADD THAT ######
                                         // Store state in eeprom ######## ADD THAT ######
                                         gw.saveState(CHILD_ID2, state); //######## ADD THAT ######
                                      }
                                      
                                       // Write some debug info
                                       Serial.print("Incoming change for sensor:");
                                       Serial.print(message.sensor);
                                       Serial.print(", New status: ");
                                       Serial.println(message.getBool());
                                     } 
                                  }
                                  

                                  and those are the errors:

                                  
                                  sketch_sep09a.ino:81:3: error: expected unqualified-id before 'if'
                                  sketch_sep09a.ino:84:3: error: 'oldValue2' does not name a type
                                  sketch_sep09a.ino:85:1: error: expected declaration before '}' token
                                  Error compiling.
                                  
                                  

                                  About the OH rules i made them like this:

                                  rule "Light"
                                          when
                                                  Item lightBar02 received command
                                          then
                                                 if(receivedCommand == ON) {
                                              sendCommand(Arduino, "101;1;1;0;2;1\n")
                                          }
                                          if(receivedCommand == OFF) {
                                             sendCommand(Arduino, "101;1;1;0;2;0\n")
                                          }
                                  end
                                  

                                  what do you think? do i still need to modify something?

                                  Thank you a lot!:pray:

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

                                    There is a copy & paste error. Remove the bracket after "oldValue = value;" :)

                                    The OH rule looks good.

                                    E 1 Reply Last reply
                                    0
                                    • T TimO

                                      There is a copy & paste error. Remove the bracket after "oldValue = value;" :)

                                      The OH rule looks good.

                                      E Offline
                                      E Offline
                                      ewgor
                                      wrote on last edited by
                                      #37

                                      @TimO still doesnt want to compile :(
                                      this is the last error:

                                      sketch_sep10b.ino:81:3: error: expected unqualified-id before 'if'
                                      Error compiling.
                                      
                                      

                                      Thanks!

                                      1 Reply Last reply
                                      0
                                      • TheoLT Offline
                                        TheoLT Offline
                                        TheoL
                                        Contest Winner
                                        wrote on last edited by TheoL
                                        #38

                                        You have a closing bracelet ({) too much that's ending your loop() method. I corrected it and added a comment so you can see what's been causing the problem.

                                        #include <MySensor.h>
                                        #include <SPI.h>
                                        #include <Bounce2.h>
                                        
                                        #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
                                        #define RELAY_PIN2 5 //######## ADD THAT ######
                                        #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
                                        #define BUTTON_PIN2 6 //######## ADD THAT ######
                                        #define CHILD_ID 1   // Id of the sensor child
                                        #define CHILD_ID2 2 //######## ADD THAT ######
                                        #define RELAY_ON 1
                                        #define RELAY_OFF 0
                                        
                                        Bounce debouncer = Bounce(); 
                                        Bounce debouncer2 = Bounce(); //######## ADD THAT ######
                                        int oldValue=0;
                                        int oldValue2=0; //######## ADD THAT ######
                                        bool state;
                                        bool state2; //######## ADD THAT ######
                                        MySensor gw;
                                        MyMessage msg(CHILD_ID,V_LIGHT);
                                        MyMessage msg2(CHILD_ID2,V_LIGHT); //######## ADD THAT ######
                                        
                                        void setup()  
                                        {  
                                          gw.begin(incomingMessage, AUTO, true);
                                        
                                          // Send the sketch version information to the gateway and Controller
                                          gw.sendSketchInfo("Relay & Button", "1.0");
                                        
                                         // Setup the button
                                          pinMode(BUTTON_PIN,INPUT);
                                          pinMode(BUTTON_PIN2,INPUT); //######## ADD THAT ######
                                          // Activate internal pull-up
                                          digitalWrite(BUTTON_PIN,HIGH);
                                          digitalWrite(BUTTON_PIN2,HIGH); //######## ADD THAT ######
                                          
                                          // After setting up the button, setup debouncer
                                          debouncer.attach(BUTTON_PIN);
                                          debouncer2.attach(BUTTON_PIN2); //######## ADD THAT ######
                                          debouncer.interval(5);
                                          debouncer2.interval(5);//######## ADD THAT ######
                                        
                                          // Register all sensors to gw (they will be created as child devices)
                                          gw.present(CHILD_ID, S_LIGHT);
                                          gw.present(CHILD_ID2, S_LIGHT);//######## ADD THAT ######
                                        
                                          // Make sure relays are off when starting up
                                          digitalWrite(RELAY_PIN, RELAY_OFF);
                                          digitalWrite(RELAY_PIN2, RELAY_OFF); //######## ADD THAT ######
                                          // Then set relay pins in output mode
                                          pinMode(RELAY_PIN, OUTPUT);
                                          pinMode(RELAY_PIN2, OUTPUT); //######## ADD THAT ######   
                                              
                                          // Set relay to last known state (using eeprom storage) 
                                          state = gw.loadState(CHILD_ID);
                                          state2 = gw.loadState(CHILD_ID2); //######## ADD THAT ######
                                          digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                          digitalWrite(RELAY_PIN2, state2?RELAY_ON:RELAY_OFF); //######## ADD THAT ######
                                        }
                                        
                                        
                                        /*
                                        *  Example on how to asynchronously check for new messages from gw
                                        */
                                        void loop() 
                                        {
                                          gw.process();
                                          debouncer.update();
                                          debouncer2.update(); //######## ADD THAT ######
                                        
                                          // Get the update value
                                          int value = debouncer.read();
                                          if (value != oldValue && value==0) {
                                              gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                                          }
                                          oldValue = value;
                                        // }  <---- remove this bracelet it's closing the loop() method. Then your compiler sees an if statement that's not in a method, which is not allowed
                                        
                                          int value2 = debouncer2.read(); 
                                          if (value2 != oldValue && value==0) { 
                                              gw.send(msg2.set(state?false:true), true); 
                                          }
                                          oldValue2 = value;
                                        }
                                         
                                        void incomingMessage(const MyMessage &message) {
                                          // We only expect one type of message from controller. But we better check anyway.
                                          if (message.isAck()) {
                                             Serial.println("This is an ack from gateway");
                                          }
                                        
                                          if (message.type == V_LIGHT) {
                                             // Change relay state
                                             state = message.getBool();
                                             if(message.sensor == 1) { //######## ADD THAT ######
                                               digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                               // Store state in eeprom
                                               gw.saveState(CHILD_ID, state);
                                             } else if(message.sensor == 2) { //######## ADD THAT ######
                                               digitalWrite(RELAY_PIN2, state?RELAY_ON:RELAY_OFF); //######## ADD THAT ######
                                               // Store state in eeprom ######## ADD THAT ######
                                               gw.saveState(CHILD_ID2, state); //######## ADD THAT ######
                                            }
                                            
                                             // Write some debug info
                                             Serial.print("Incoming change for sensor:");
                                             Serial.print(message.sensor);
                                             Serial.print(", New status: ");
                                             Serial.println(message.getBool());
                                           } 
                                        }
                                        

                                        Good luck with it.

                                        b.t.w. I have no knowledge of openHab.

                                        E 1 Reply Last reply
                                        0
                                        • TheoLT TheoL

                                          You have a closing bracelet ({) too much that's ending your loop() method. I corrected it and added a comment so you can see what's been causing the problem.

                                          #include <MySensor.h>
                                          #include <SPI.h>
                                          #include <Bounce2.h>
                                          
                                          #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
                                          #define RELAY_PIN2 5 //######## ADD THAT ######
                                          #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
                                          #define BUTTON_PIN2 6 //######## ADD THAT ######
                                          #define CHILD_ID 1   // Id of the sensor child
                                          #define CHILD_ID2 2 //######## ADD THAT ######
                                          #define RELAY_ON 1
                                          #define RELAY_OFF 0
                                          
                                          Bounce debouncer = Bounce(); 
                                          Bounce debouncer2 = Bounce(); //######## ADD THAT ######
                                          int oldValue=0;
                                          int oldValue2=0; //######## ADD THAT ######
                                          bool state;
                                          bool state2; //######## ADD THAT ######
                                          MySensor gw;
                                          MyMessage msg(CHILD_ID,V_LIGHT);
                                          MyMessage msg2(CHILD_ID2,V_LIGHT); //######## ADD THAT ######
                                          
                                          void setup()  
                                          {  
                                            gw.begin(incomingMessage, AUTO, true);
                                          
                                            // Send the sketch version information to the gateway and Controller
                                            gw.sendSketchInfo("Relay & Button", "1.0");
                                          
                                           // Setup the button
                                            pinMode(BUTTON_PIN,INPUT);
                                            pinMode(BUTTON_PIN2,INPUT); //######## ADD THAT ######
                                            // Activate internal pull-up
                                            digitalWrite(BUTTON_PIN,HIGH);
                                            digitalWrite(BUTTON_PIN2,HIGH); //######## ADD THAT ######
                                            
                                            // After setting up the button, setup debouncer
                                            debouncer.attach(BUTTON_PIN);
                                            debouncer2.attach(BUTTON_PIN2); //######## ADD THAT ######
                                            debouncer.interval(5);
                                            debouncer2.interval(5);//######## ADD THAT ######
                                          
                                            // Register all sensors to gw (they will be created as child devices)
                                            gw.present(CHILD_ID, S_LIGHT);
                                            gw.present(CHILD_ID2, S_LIGHT);//######## ADD THAT ######
                                          
                                            // Make sure relays are off when starting up
                                            digitalWrite(RELAY_PIN, RELAY_OFF);
                                            digitalWrite(RELAY_PIN2, RELAY_OFF); //######## ADD THAT ######
                                            // Then set relay pins in output mode
                                            pinMode(RELAY_PIN, OUTPUT);
                                            pinMode(RELAY_PIN2, OUTPUT); //######## ADD THAT ######   
                                                
                                            // Set relay to last known state (using eeprom storage) 
                                            state = gw.loadState(CHILD_ID);
                                            state2 = gw.loadState(CHILD_ID2); //######## ADD THAT ######
                                            digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                            digitalWrite(RELAY_PIN2, state2?RELAY_ON:RELAY_OFF); //######## ADD THAT ######
                                          }
                                          
                                          
                                          /*
                                          *  Example on how to asynchronously check for new messages from gw
                                          */
                                          void loop() 
                                          {
                                            gw.process();
                                            debouncer.update();
                                            debouncer2.update(); //######## ADD THAT ######
                                          
                                            // Get the update value
                                            int value = debouncer.read();
                                            if (value != oldValue && value==0) {
                                                gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                                            }
                                            oldValue = value;
                                          // }  <---- remove this bracelet it's closing the loop() method. Then your compiler sees an if statement that's not in a method, which is not allowed
                                          
                                            int value2 = debouncer2.read(); 
                                            if (value2 != oldValue && value==0) { 
                                                gw.send(msg2.set(state?false:true), true); 
                                            }
                                            oldValue2 = value;
                                          }
                                           
                                          void incomingMessage(const MyMessage &message) {
                                            // We only expect one type of message from controller. But we better check anyway.
                                            if (message.isAck()) {
                                               Serial.println("This is an ack from gateway");
                                            }
                                          
                                            if (message.type == V_LIGHT) {
                                               // Change relay state
                                               state = message.getBool();
                                               if(message.sensor == 1) { //######## ADD THAT ######
                                                 digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                                 // Store state in eeprom
                                                 gw.saveState(CHILD_ID, state);
                                               } else if(message.sensor == 2) { //######## ADD THAT ######
                                                 digitalWrite(RELAY_PIN2, state?RELAY_ON:RELAY_OFF); //######## ADD THAT ######
                                                 // Store state in eeprom ######## ADD THAT ######
                                                 gw.saveState(CHILD_ID2, state); //######## ADD THAT ######
                                              }
                                              
                                               // Write some debug info
                                               Serial.print("Incoming change for sensor:");
                                               Serial.print(message.sensor);
                                               Serial.print(", New status: ");
                                               Serial.println(message.getBool());
                                             } 
                                          }
                                          

                                          Good luck with it.

                                          b.t.w. I have no knowledge of openHab.

                                          E Offline
                                          E Offline
                                          ewgor
                                          wrote on last edited by
                                          #39

                                          @TheoL thanks man, I'll try it right now, thanks again.

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


                                          15

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 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
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular