Sensors are sporadically showing up



  • Unfortunately when I started playing around with Mysensors a week or so ago, life was a little quieter than what it is right now. After playing around with my Gateway I have to readd all my sensors to it, and I was silly today and have tried to combine two sensors that I have had running flawlessly, into one sensor. Big mistake.... I've had a couple issues happen in my family and now I've stuffed up my garage door operation... I have been working on the sketch now for a couple hours and I just can't seem to work out the problem.

    I have built a sensor which will operate 2 relays, and will also report the state of my two garage doors. My controller is Vera 3. What is happening is something weird; the repeater part always shows up after inclusion, but sometimes it shows the 2 door sensors, then other times it shows the 2 relays. So my guess is that the issue is around Presentation, but it all looks fine to me, and when the relay and the door sensor sketches are uploaded individually, each sensor shows up as they should. I have even tried different power supplies in case it was a power issue, but the issue is still there.

    Here is my sketch:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_RF24_PA_LEVEL RF24_PA_HIGH
    
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // 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
    
    #define SSR_A_ID 1   // Id of the sensor child
    #define SSR_B_ID 2   // Id of the sensor child
    
    const int buttonPinA = 6;
    const int buttonPinB = 7;
    
    int oldValueA = 0;
    int oldValueB = 0;
    
    bool stateA = false;
    bool stateB = false;
    
    Bounce debouncerA = Bounce();
    Bounce debouncerB = Bounce();
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgA(SSR_A_ID, V_TRIPPED);
    MyMessage msgB(SSR_B_ID, V_TRIPPED);
    
    void before()
    {
      for (int sensor = 1, pin = RELAY_PIN; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, loadState(sensor) ? RELAY_ON : RELAY_OFF);
      }
    }
    
    void setup()
    {
      // Setup the button
      pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
      pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
    
    
      // After setting up the buttons, setup debouncer
      debouncerA.attach(buttonPinA);
      debouncerA.interval(5);
      debouncerB.attach(buttonPinB);
      debouncerB.interval(5);
    
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Garage door Operate/State", "1.0");
    
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(SSR_A_ID, S_DOOR);
      present(SSR_B_ID, S_DOOR);
    
      for (int sensor = 1, pin = RELAY_PIN; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_BINARY);
      }
    }
    
    
    
    //  Check if digital input has changed and send in new value
    void loop()
    {
      debouncerA.update();
      // Get the update value
      int valueA = debouncerA.read();
    
      if (valueA != oldValueA) {
        // Send in the new value
        send(msgA.set(valueA == HIGH ? 0 : 1));
        oldValueA = valueA;
    
      }
    
      debouncerB.update();
      // Get the update value
      int valueB = debouncerB.read();
    
      if (valueB != oldValueB) {
        // Send in the new value
        send(msgB.set(valueB == HIGH ? 0 : 1));
        oldValueB = valueB;
      }
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {
        // Change relay state
        digitalWrite(message.sensor - 1 + RELAY_PIN, message.getBool() ? RELAY_ON : RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      }
    }
    

    I am sorry to be asking yet again for help. I really have tried for so long today to get this to work on my own, but I can't see the issue. I do have the flu, so maybe that isn't helping!



  • You are using the same ChildID's for relays and tripped/door states. Imo this is messing things up, so e.g. use 11+12 for tripped values.
    Remark: If you are looking for a good example on using arrays for that kind of tasks, see korttomas work here: https://forum.mysensors.org/post/51488



  • Thank you so much for your help, that fixed it!!!

    My wife thanks you too! 🙂


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts