Please help



  • Hi everyone, can you help me with my new node.

    0_1467128596459_node.jpg
    i have problems with sending a signal to my garage door to get it open/close. the node doesn't sent anything. here is my code:

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <RCSwitch.h>

    #define CHILD_ID_GDOOR 0
    #define CHILD_ID_VDOOR 1
    #define CHILD_ID_RELAY1 2
    #define CHILD_ID_RELAY2 3
    #define CHILD_ID_RELAY3 4

    #define GDOOR_PIN 2
    #define VDOOR_PIN 3
    #define RELAY1_PIN 5
    #define RELAY2_PIN 6

    #define RELAY_ON 0
    #define RELAY_OFF 1

    #define DOOR_CLOSED 0
    #define DOOR_OPEN 1

    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();

    boolean oldValueA = 0;
    boolean oldValueB = 0;
    boolean trippedA = 0;
    boolean trippedB = 0;

    bool state;

    MySensor gw;
    MyMessage msggdoor(CHILD_ID_GDOOR,V_TRIPPED);
    MyMessage msgvdoor(CHILD_ID_VDOOR,V_TRIPPED);

    RCSwitch mySwitch = RCSwitch();

    void setup()
    {

    mySwitch.enableTransmit(4);

    // Optional set pulse length.
    // mySwitch.setPulseLength(320);

    // Optional set protocol (default is 1, will work for most outlets)
    // mySwitch.setProtocol(2);
    mySwitch.setRepeatTransmit(3);

    gw.begin(incomingMessage, AUTO, true);

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Relay & Button", "1.0");

    pinMode(GDOOR_PIN,INPUT);
    pinMode(VDOOR_PIN,INPUT);
    digitalWrite(GDOOR_PIN,HIGH);
    digitalWrite(VDOOR_PIN,HIGH);

    pinMode(RELAY1_PIN, OUTPUT);
    pinMode(RELAY2_PIN, OUTPUT);

    digitalWrite(RELAY1_PIN, RELAY_OFF);
    digitalWrite(RELAY2_PIN, RELAY_OFF);

    // Set relay to last known state (using eeprom storage)
    state = gw.loadState(CHILD_ID_RELAY1);
    digitalWrite(RELAY1_PIN, state?RELAY_ON:RELAY_OFF);

    gw.present(CHILD_ID_GDOOR, S_DOOR);
    gw.present(CHILD_ID_VDOOR, S_DOOR);
    gw.present(CHILD_ID_RELAY1, S_LIGHT);
    gw.present(CHILD_ID_RELAY2, S_LIGHT);
    gw.present(CHILD_ID_RELAY3, S_LIGHT);
    }

    void loop()
    {
    // Read digital reed switch value
    trippedA = digitalRead(GDOOR_PIN);
    trippedB = digitalRead(VDOOR_PIN);

    // Send value only if changed
    if(trippedA != oldValueA)
    {
    //Serial.print("Door A status: ");
    //Serial.println(trippedA);
    gw.send(msggdoor.set(trippedA ? DOOR_CLOSED : DOOR_OPEN));
    //gw.sendVariable(CHILD_ID_DOORA, V_LOCK_STATUS, trippedA?DOOR_CLOSED:DOOR_OPEN); // Send value change to gw
    delay(1000);
    oldValueA = trippedA;
    }

    // Send value only if changed
    if(trippedB != oldValueB)
    {
    //Serial.print("Door B status: ");
    //Serial.println(trippedB);
    gw.send(msgvdoor.set(trippedB ? DOOR_CLOSED : DOOR_OPEN));
    //gw.sendVariable(CHILD_ID_DOORB, V_LOCK_STATUS, trippedB?DOOR_CLOSED:DOOR_OPEN); // Send value change to gw
    delay(1000);
    oldValueB = trippedB;
    }

    gw.process();

    }

    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) {
    int actPin;
    // Change relay state
    if (message.sensor == 2){
    state = message.getBool();
    digitalWrite(RELAY1_PIN, state?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    gw.saveState(message.sensor, state);
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());

      }
    
      if (message.sensor == 3){ 
        state = message.getBool();
        digitalWrite(RELAY2_PIN,LOW);
        delay(1000);
        digitalWrite(RELAY2_PIN,HIGH);
        // Store state in eeprom
        gw.saveState(message.sensor, state);
     Serial.print("Incoming change for sensor:");
     Serial.print(message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
      }
      
      if (message.sensor == 4){ 
        state = message.getBool();
        mySwitch.send(739041, 24);
     Serial.print("Incoming change for sensor:");
     Serial.print(message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
      }
    

    }
    }


  • Contest Winner

    Hi @pesh000, couple of remarks at your sketch

    1. Did you try the RCSwitch code with out any MySensor stuff so for example the SendDemo sketch with for your garage door the correct ID and pin settings? And did the garage door open/close?

    2. Why declaring 2 debouncer variables but still using digitalRead? Look at the MySensors RelayWithButtonActuator example for debouncer examples

    3. Why using hard coded numbers in the incomingMessage switch to determine which sensor did broadcast the V_LIGHT message while you have CHILD_ID_RELAY1, CHILD_ID_RELAY2 and CHILD_ID_RELAY3 declared?

    4. better use gw.wait(1000); in stead g delay(1000); during gw.wait() MySensors messages keep being processed. Even better do not use wait() functions or very short ones to keep your sketch responsive to triggers

    And last but not least it will help if you can post some debug logging from your sketch. I hope these comments will help you to debug , good luck


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 1
  • 1
  • 2
  • 5

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts