MQTT Gateway: Add a confirmation/acknowledgement to the publish topic upon setting via subscribe topic.



  • Essentially right now, if i turn on a light/fan/binary/etc. over MQTT by using my in topic, it does what it is supposed to. But there is no output on the out topic to confirm to the Home Automation controller, (Home Assistant in this case), that the state was changed. I have to use optimistic mode, which does not help me if i want to know if the equipment is on/off. So after receive of a change via MQTT, send a update of the current status on the out topic.


    Log in to reply
     

  • Mod

    Welcome to the MySensors community @Brian-Morris. Can't you just send a reply after switching the output? See https://www.mysensors.org/download/sensor_api_20#sending-data



  • @mfalkvidd I did not think of that That should definitely work. I will give it a shot. Thanks!

    Also for the record, I was thinking of a feature request globally. MySensors already does the acknowledgements in its own protocol, could be cool to do it with MQTT as well.


  • Mod

    @Brian-Morris a global feature could be nice. To my knowledge, the current protocol does not support sending ack to a controller but maybe it can be added.


  • Mod

    btw, it looks like the dimmable led example already does what you're requesting: https://www.mysensors.org/build/dimmer



  • @mfalkvidd that did not seem to work. It causes a loop where it sends the state back and forth, changing the value and switch state. This topic could be moved to troubleshooting if someone can do so as well.

    void receive(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();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(CHILD_ID, state);
         send(msg.set(state?false:true), true);
    
         if (state == true){
           colorSet(strip.Color(0, 255, 0));  // Green
         } else {
           colorSet(strip.Color(255, 255, 255));  // White
         }
    
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       }
    }
    
    

  • Mod

    @Brian-Morris you can probably fix the looping by changing the second if in an else if.
    Now the ack message can/will trigger a new message...



  • Changed to the following. Still no change. Still loops.

    if (message.isAck()) {
         Serial.println("This is an ack from gateway");
         send(msg.set(state?false:true), true);
      } else if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(CHILD_ID, state);
    
    
         if (state == true){
           colorSet(strip.Color(0, 255, 0));  // Green
         } else {
           colorSet(strip.Color(255, 255, 255));  // White
         }
    
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       }
    }```

  • Mod

    @Brian-Morris I think

    send(msg.set(state?false:true), true);
    

    should be moved to inside the else if.
    If that doesn't help, could you please post a debug log from the node and the gateway, with your complete sketch? Easier to get all the cards on the table 🙂



  • with it here, I attempt to turn on the switch by sending a payload of "1" to turn on the switch via MQTT. The gateway receives it, sends it to the node, where the node reverses it, sends it back 0. I can press the button many times over, stays 0/off.

    void receive(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");
      } else if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(CHILD_ID, state);
         send(msg.set(state?false:true));
    
    
         if (state == true){
           colorSet(strip.Color(0, 255, 0));  // Green
         } else {
           colorSet(strip.Color(255, 255, 255));  // White
         }
    

    Node DEBUG

    0 MCO:BGN:INIT REPEATER,CP=RNNRA--,VER=2.1.1
    4 TSM:INIT
    4 TSF:WUR:MS=0
    12 TSM:INIT:TSP OK
    14 TSM:INIT:STATID=80
    16 TSF:SID:OK,ID=80
    18 TSM:FPAR
    55 TSF:MSG:SEND,80-80-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    557 TSF:MSG:READ,0-0-80,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    563 TSF:MSG:FPAR OK,ID=0,D=1
    2062 TSM:FPAR:OK
    2062 TSM:ID
    2064 TSM:ID:OK
    2066 TSM:UPL
    2070 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2076 TSF:MSG:READ,0-0-80,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2082 TSF:MSG:PONG RECV,HP=1
    2086 TSM:UPL:OK
    2088 TSM:READY:ID=80,PAR=0,DIS=1
    2127 !TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=NACK:0100
    4136 TSF:MSG:SEND,80-80-0-0,s=255,c=0,t=18,pt=0,l=5,sg=0,ft=1,st=OK:2.1.1
    4147 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    6158 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=11,pt=0,l=22,sg=0,ft=0,st=OK:Living Room Fan Switch
    6170 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.5
    6178 TSF:MSG:SEND,80-80-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    6187 MCO:REG:REQ
    6191 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    6197 TSF:MSG:READ,0-0-80,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    6203 MCO:PIM:NODE REG=1
    6207 MCO:BGN:STP
    8660 MCO:BGN:INIT OK,TSP=1
    170743 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:1
    170752 TSF:MSG:SEND,80-80-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    Incoming change for sensor:1, New status: 1
    173297 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:1
    173305 TSF:MSG:SEND,80-80-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    Incoming change for sensor:1, New status: 1
    294385 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    294391 TSF:MSG:ACK
    This is an ack from gateway
    

    GW

    0;255;3;0;9;MCO:BGN:STP
    0;255;3;0;9;MCO:BGN:INIT OK,TSP=1
    IP: 10.200.62.10
    0;255;3;0;9;Attempting MQTT connection...
    0;255;3;0;9;MQTT connected
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/255/0/0/18
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/255/3/0/11
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/255/3/0/12
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/10/0/0/3
    pm open,type:2 0
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    

    moving it to here, the physical switch stays at 1 no matter how many times you click. Controller to MQTT works and the switch works, but still no response back to the controller.

    void receive(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");
         send(msg.set(state?false:true));
      } else if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(CHILD_ID, state);
    
         if (state == true){
           colorSet(strip.Color(0, 255, 0));  // Green
         } else {
           colorSet(strip.Color(255, 255, 255));  // White
         }	 
    

    Node

    0 MCO:BGN:INIT REPEATER,CP=RNNRA--,VER=2.1.1
    4 TSM:INIT
    4 TSF:WUR:MS=0
    12 TSM:INIT:TSP OK
    14 TSM:INIT:STATID=80
    16 TSF:SID:OK,ID=80
    18 TSM:FPAR
    55 TSF:MSG:SEND,80-80-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    704 TSF:MSG:READ,0-0-80,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    708 TSF:MSG:FPAR OK,ID=0,D=1
    2062 TSM:FPAR:OK
    2062 TSM:ID
    2064 TSM:ID:OK
    2066 TSM:UPL
    2070 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2084 TSF:MSG:READ,0-0-80,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2091 TSF:MSG:PONG RECV,HP=1
    2093 TSM:UPL:OK
    2095 TSM:READY:ID=80,PAR=0,DIS=1
    2101 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2109 TSF:MSG:READ,0-0-80,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2117 TSF:MSG:SEND,80-80-0-0,s=255,c=0,t=18,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    2129 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    4141 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=11,pt=0,l=22,sg=0,ft=0,st=OK:Living Room Fan Switch
    4153 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.5
    4161 TSF:MSG:SEND,80-80-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    4169 MCO:REG:REQ
    4173 TSF:MSG:SEND,80-80-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    4239 TSF:MSG:READ,0-0-80,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    4245 MCO:PIM:NODE REG=1
    4247 MCO:BGN:STP
    6701 MCO:BGN:INIT OK,TSP=1
    ▒&L▒TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:1
    Incoming change for sensor:1, New status: 1
    M▒TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:▒2▒Ɂ▒▒▒ͽ▒▒brU▒▒.]X▒us: 1
    54794 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    54800 TSF:MSG:ACK
    This is an ack from gateway
    54806 TSF:MSG:SEND,80-80-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    96909 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:0
    Incoming change for sensor:1, New status: 0
    99350 TSF:MSG:SEND,80-80-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    99360 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    99366 TSF:MSG:ACK
    This is an ack from gateway
    99371 TSF:MSG:SEND,80-80-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    101445 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    101451 TSF:MSG:ACK
    This is an ack from gateway
    101455 TSF:MSG:SEND,80-80-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    113451 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:0
    Incoming change for sensor:1, New status: 0
    118966 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:1
    Incoming change for sensor:1, New status: 1
    121210 TSF:MSG:READ,0-0-80,s=1,c=1,t=2,pt=0,l=1,sg=0:0
    Incoming change for sensor:1, New status: 0
    

    GW

    0;255;3;0;9;MCO:BGN:STP
    0;255;3;0;9;MCO:BGN:INIT OK,TSP=1
    IP: 10.200.62.10
    0;255;3;0;9;Attempting MQTT connection...
    0;255;3;0;9;MQTT connected
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/255/0/0/18
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/255/3/0/11
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/255/3/0/12
    0;255;3;0;9;Sending message on topic: /mysgw1-out/0/10/0/0/3
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    pm open,type:2 0
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:0
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 0
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;TSF:MSG:READ,80-80-0,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    0;255;3;0;9;Sending message on topic: /mysgw1-out/80/1/1/0/2
    Incoming change for sensor:1, New status: 1
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:0
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    0;255;3;0;9;Message arrived on topic: /mysgw1-in/80/1/1/0/2
    0;255;3;0;9;TSF:MSG:SEND,0-0-80-80,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:0
    

    Whole code

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 80
    #define MY_REPEATER_FEATURE
    
    #include <Arduino.h>
    #include <Adafruit_NeoPixel.h>
    #include <Ticker.h>
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN  2  // Arduino Digital I/O pin number for relay
    #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button
    #define CHILD_ID 1   // Id of the sensor child
    #define RGB_CHILD_ID 2   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    #define NUMPIXELS 1
    #define PIXELPIN 4
    #define BRIGHTNESS 10
    
    Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800);
    long RGB_values[3] = {0,0,0};
    
    Bounce debouncer = Bounce();
    int oldValue=0;
    bool state;
    
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    void setup()
    {
      // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
    
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);
    
      // Set relay to last known state (using eeprom storage)
      state = loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    
      strip.begin();
      strip.setBrightness(BRIGHTNESS);
      strip.show(); // Update the strip, to start they are all 'off'
    
      startupSequence();
    
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Living Room Fan Switch", "1.5");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_LIGHT);
      //present(RGB_CHILD_ID, S_RGB_LIGHT);
    }
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop()
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
      if (value != oldValue && value==0) {
          send(msg.set(state?false:true), true); // Send new state and request ack back
          if (value == true){
            colorSet(strip.Color(0, 255, 0));  // Green
          } else {
            colorSet(strip.Color(255, 255, 255));  // White
          }
      }
      oldValue = value;
    }
    
    void receive(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");
         send(msg.set(state?false:true));
      } else if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(CHILD_ID, state);
    
         if (state == true){
           colorSet(strip.Color(0, 255, 0));  // Green
         } else {
           colorSet(strip.Color(255, 255, 255));  // White
         }
    
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       }
    }
    
    
    void startupSequence(){
      colorSet(strip.Color(255, 0, 0));  // Red
      delay(350);
      colorSet(strip.Color(255, 255, 0));  // Yellow
      delay(350);
      colorSet(strip.Color(0, 255, 0));  // Green
      delay(350);
      colorSet(strip.Color(0, 0, 0));  // Black
      delay(350);
      colorSet(strip.Color(0, 255, 0));  // Green
      delay(350);
      colorSet(strip.Color(255, 255, 255));  // White
      delay(350);
      colorSet(strip.Color(0, 0, 0));  // Black
      delay(350);
      colorSet(strip.Color(255, 255, 255));  // White
    }
    
    
    void colorSet(uint32_t color)
    {
        for (int i = 0; i < strip.numPixels(); i++)
        {
            strip.setPixelColor(i, color);
        }
        strip.show();
    }
    

  • Plugin Developer

    @Brian-Morris

    Don't invert the state before you send it back to the controller . Ie this part:

    state?false:true
    

    should be:

    state?true:false
    

    in the receive function.


Log in to reply
 

3 out of 11

Suggested Topics


  • Announcements   24 Mar 2014, 20:12

    3
  • 5

  • Troubleshooting   3 Sept 2024, 08:58

    2
  • 6

  • Troubleshooting   28 Jun 2024, 22:42

    1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts