Trouble sending message to Relay from Openhab



  • Simply put I am trying to send a message from Openhab to a MySensors sensor via a Serial Gateway.

    My environment is Openhab 1.7 running on RPi 3. I have a stock Serial Gateway running on Nano using RF24. I have two sensors DHT and micro switch sending data to the gateway and values are appearing in Openhab fine. For about week now I have been trying to turn the communication around. In Openhab I have configured an Switch Item called Relay. When it changes from Off to On I have a rule to send a command to the Arduino gateway item. However the message never shows up on the sensor. Any advice would be appreciated.

    Here is some of the code, config, and logs:

    Relay Sensor:

    #include <EEPROM.h>
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    void setup()  
    {   
      //Wipe eprom
      for (int i = 0 ; i < EEPROM.length() ; i++) {
        EEPROM.write(i, 0);
      }
      
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, 105, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
    
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      Serial.println("message received");
    
    }
    
    

    arduino items

    String Arduino "Arduino" { serial="/dev/ttyUSB0@115200" }
    
    Contact Garage_Door             "Garage Door [MAP(en.map):%s]"          (gGarage, Windows)
    Number Temperature_Basement     "Temperature [%.1f F]"  <temperature>   (Temperature, gBasement)
    Number Humidity_Basement        "Humidity [%.1f %%]"    <temperature>   (gBasement)
    
    Switch Relay          "Relay"  (gBasement)
    

    test.rules

    import org.openhab.core.library.types.*
    
    rule "Send Relay Arduino Command"
    when
            Item Relay changed from OFF to ON
    then
            logInfo("FILE","Entering Rule: Send Relay Arduino Command")
            sendCommand(Arduino, "105;2;1;0;2;1\n")
    
    end
    

    openhab.log during registration

    2016-04-24 11:51:57.728 [INFO ] [org.openhab.model.script.FILE ] - nodeId: 105 childId: 255 msgType: 3 subType: 6 msg: 0
    2016-04-24 11:51:57.841 [INFO ] [org.openhab.model.script.FILE ] - Entering rule Arduino sends to Openhab
    2016-04-24 11:51:58.229 [INFO ] [org.openhab.model.script.FILE ] - Entering rule Arduino sends to Openhab
    2016-04-24 11:51:59.161 [INFO ] [org.openhab.model.script.FILE ] - nodeId: 0 childId: 0 msgType: 3 subType: 9 msg: read: 105-105-0 s=255,c=3,t=12,pt=0,l=3,sg=0:1.0
    2016-04-24 11:51:59.454 [INFO ] [org.openhab.model.script.FILE ] - nodeId: 0 childId: 0 msgType: 3 subType: 9 msg: send: 0-0-105-105 s=255,c=3,t=8,pt=1,l=1,sg=0,st=fail:0
    2016-04-24 11:52:00.462 [INFO ] [org.openhab.model.script.FILE ] - nodeId: 105 childId: 255 msgType: 3 subType: 12 msg: 1.0
    

    openhab.log after turning on switch

    2016-04-24 11:49:30 - Relay received command ON
    2016-04-24 11:49:31 - Arduino received command 105;2;1;0;2;1
    2016-04-24 11:49:31 - Arduino state updated to 0;0;3;0;9;send: 0-0-105-105 s=2,c=1,t=2,pt=0,l=1,sg=0,st=fail:1
    

    openhab events.log

    2016-04-24 11:49:31.056 [INFO ] [org.openhab.model.script.FILE ] - Entering Rule: Send Relay Arduino Command
    2016-04-24 11:49:31.522 [INFO ] [org.openhab.model.script.FILE ] - Entering rule Arduino sends to Openhab
    2016-04-24 11:49:31.943 [INFO ] [org.openhab.model.script.FILE ] - Entering rule Arduino sends to Openhab
    2016-04-24 11:49:32.651 [INFO ] [org.openhab.model.script.FILE ] - nodeId: 105 childId: 2 msgType: 1 subType: 2 msg: 1
    


  • Is there a Serial.begin in your Arduino Code?



  • @Ericb2745

    I think you are missing in your arduino code the process for sending the relay information to OpenHAB. Try using this:

    send(msg.set(state?false:true), true); // Send new state and request ack back
    

    Also I can't see the entire item file but you may be missing or incorrectly settting your MQTT binding. This is mine for reference:

    Switch  OpenWindow 			"Extend Actuator"							<rollershutter> 	(gH)								{mqtt=">[mymosquitto:mysensors-in/5/0/1/0/2:command:ON:1],>[mymosquitto:mysensors-in/5/0/1/0/2:command:OFF:0]"}
    


  • @CJ-Cassarino Thanks for the reply. I will try your send syntax in my Arduino rules. Also, the reason you don't see the mqtt tag is that I am using a Serial Gateway not a MQTT gateway.



  • @Ericb2745

    Well, in that case, it seems you are missing the receiving part of the code. This is directly from the example for the actuator and it works for me... It goes at the end of the code after the void loop.

    // process incoming message
    void receive(const MyMessage &message) {
    
      if (message.type == V_LIGHT) {
        if (message.sensor < noRelays) {          // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
          Serial.print("Incoming change for sensor:");
          Serial.print(message.sensor);
          Serial.print(", New status: ");
          Serial.print(message.getBool());
          if(message.getBool() == 0)
            Serial.println(" = OFF");
          else(Serial.println(" = ON"));
        }
      }
    }
    

 

466
Online

7.9k
Users

8.8k
Topics

94.3k
Posts