Automated garage door



  • One use of my current automation system is that I have sensors on my garage door to tell it's position, as well as an X10 relay module connected to a door remote to open and close the door. So I currently have a reed switch on the door rail and a magnet on the garage door to tell if the door is up or down. This is located at the upper end of the door travel. The problem I have is that occasionally the door will be in a position slightly off of where the reed switch is so the automation system thinks that the door is closed when actually it is open. I have been looking at a way to get more control and better accuracy with this setup, so I thought about doing my sensing at the mechanism itself. Below is a picture of my internal upper and lower limit mechanism.
    Garage door mechanism
    I did some testing and if I put a volt meter across connections A and C in the picture, I get a reading of 5 volts (4.99V). If I reverse the door and arm A is in travel, I read 5V at A and C, and A and B. I connected a set of LEDs with 380ohm resistors (I didn't have a value closer to 150ohm available) to A/B and A/C and I am able to run the opener without trouble. Would it be safe to assume that I could put a 4N25 optocoupler across there and connect those to a MySensors node to read the position of my door?

    Having the two optocouplers would allow me to have the following truth table to work with:
    A/B | A/C
    --0-- | --1-- Door open
    --1-- | --0-- Door closed
    --1-- | --1-- Door opening or closing
    --0-- | --0-- NOT POSSIBLE

    Determining whether the door is opening or closing could be done by checking the last fully open or fully closed position. If the last position was closed and the state becomes 1 | 1, then the door is opening, and vice versa.

    I could wire a relay to my garage remote to make it a full sensor node.

    Does anyone see any issues in doing this this way?


  • Hero Member

    @dbemowsk A little hard to tell from the picture, but if you can connect LED's which tell you the truth table is right then using optocouplers should work just fine. Enjoy!



  • So here is my dilemma with this project so far. For testing, I connected an optocoupler with an LED on the output side with it's own 5 volt source. I verified connection with another 5 volt source driving the optocoupler through a resistor and I got the LED on the output side to light. I then connected the door opener switch assembly pictured in my original post using A as the negative side and C as the positive side. I could not get the LED to light. After thinking about the logic of how this is probably set up on the door opener, I am figuring that the 5 volt reading I am getting between A and C is most likely from a pull up resistor. I am guessing that connections B and C are inputs to a micro-controller for the logic board of the door opener which is normally logic high from the pull-up resistor, and then when connection A moves back and forth between B and C, it is pulling one of the two to a logic low telling the controller that it has reached either the upper or lower limit.

    The optocouplers I am trying to use in this setup are 4N25's. I am wondering if I could drive a pro mini digital input directly from these lines or if it would be better to find a lower input current optocoupler to work for this application? Or if there is another approach I could take for this I am open for suggestions. Any thoughts?


  • Hero Member

    @dbemowsk Hard to tell exactly what you mean, but .... i would consider using a MOS-FET to drive the Led in the optocoupler.



  • @AWI One of my questions was if I were to connect A to the ground side of my arduino circuit and connect B and C directly to two of the inputs on the arduino without going through the optocoupler, would that be a problem? The current coming off of the B and C legs is probably low because, as I mentioned, it is most likely going through a pull up resistor to keep that logic high. If that signal is going to the input of another micro-controller in the door opener, why couldn't I then connect that signal directly into the arduino and not use the opto?



  • OK, so today I decided to take a small leap and run a test. I created a simple circuit with two LEDs that would trigger with two pushbutton switches. I created a sketch so that when either button was pressed the corresponding LED would turn on.

    /* sketch 1 
    turn on a LED when the button is pressed
    turn it off when the button is not pressed (or released)
    */
    int pinButton1 = 8; //the pin where we connect the button
    int LED1 = 2; //the pin we connect the LED
    int pinButton2 = 7; //the pin where we connect the button
    int LED2 = 9; //the pin we connect the LED
     
    void setup() {
      pinMode(pinButton1, INPUT); //set the button pin as INPUT
      pinMode(LED1, OUTPUT); //set the LED pin as OUTPUT
      pinMode(pinButton2, INPUT); //set the button pin as INPUT
      pinMode(LED2, OUTPUT); //set the LED pin as OUTPUT
    }
     
    void loop() {
      int stateButton1 = digitalRead(pinButton1); //read the state of the button
      if(stateButton1 == 1) { //if is pressed
         digitalWrite(LED1, HIGH); //write 1 or HIGH to led pin
      } else { //if not pressed
         digitalWrite(LED1, LOW);  //write 0 or low to led pin
      }
    
      int stateButton2 = digitalRead(pinButton2); //read the state of the button
      if(stateButton2 == 1) { //if is pressed
         digitalWrite(LED2, HIGH); //write 1 or HIGH to led pin
      } else { //if not pressed
         digitalWrite(LED2, LOW);  //write 0 or low to led pin
      }
    }
    
    

    I then disconnected the digital input lines 7 and 8 from the switches and connected them to points B and C in my original picture, and connected point A to ground. I ran the test and everything worked as expected. The only thing I might change would be to add a couple diodes to B and C coming into the arduino to help prevent any chance of backfeeding into the door opener circuit.

    The next step is to finalize the board and come up with a sketch. This will be my first MySensors node sketch, so I am sure I will be coming to the forum for help.



  • So I have not started creating the sketch yet, but I have outlined the logic and the comments that will go at the top of the sketch. I am looking for feedback on the features/logic of the sketch before I start writing the code.

    /*
    MyGarageDoor for MySensors
    
    Arduino garage door control
    
    June 16,2016
    
    This will allow the MySensors gateway to monitor and control your garage door opener.  This will 
    monitor the internal upper and lower limit switches of the door opener to determine door position 
    for status as well as have a relay switched output that can control either the momentary contact 
    switch connected to the door opener or wired directly to the switch contacts of a door opener 
    remote to activate the door.
    
    This sketch features the following:
    
    * Allows you to monitor the door position and return it's status based on the following:
        1 - When either the upper or lower door limit switches are reached, the door is said to be 
            either fully open or fully closed. When a limit switch is triggered, the lastLimit and 
            currentState variables are set to either OPEN or CLOSED based on the limit switch that was 
            triggered.
        2 - If the upper and lower limit inputs both read high, then the door is said to be in motion.
            The door's direction of motion is determined from the lastLimit variable.  If the lastLimit
            of the door is OPEN, then the currentState variable should be set to CLOSING.  If the 
            lastLimit of the door is CLOSED, then the currentState variable should be set to OPENING.
    * Checks for door obstructions and throws an error by checking the currentState and lastLimit 
      variables when the upper limit is reached.  If the upper limit is triggered and the currentState 
      is set to CLOSING and thelastLimit is set to OPEN, this should indicate that the door reversed 
      when closing and returned to the fully opened position.  The error state should only appear if 
      BOTH the lastLimit and currentState conditions match.  If lastLimit is OPEN and the lastState is 
      OPENING, this would indicate that the door was manually reversed.  This chech should only be 
      needed when the upper limit is reached.
    * Allows you to toggle the opener to either open or close the door.  When the door is toggled, the 
      currentState should be checked and if the state is either OPENING or CLOSING, the currentState 
      should invert telling that the door has changed direction.  This is done to prevent an error if 
      the door is reversed manually when closing.
    
    PARTS LIST:
    * Arduino pro mini
    * nRF24L01 radio module
    * AMS1117 3.3v regulator
    * 0.1uf capacitor
    * 10uf electrolytic capacitor
    * 4.7uf electrolytic capacitor (for radio module)
    * 5v reed relay
    * 2N3904 transistor for relay
    * 1k resistor
    * Two small signal diodes for incoming signals from door opener
    
    Wiring from garage door opener
    Yellow = Uper limit
    Brown = Lower limit
    Gray = Signal ground
    */
    

    Also if someone could point me to a good sketch to start building from, that would be appreciated.



  • OK, so here is my first attempt at a sketch. I have not tried to compile this yet because there are probably errors, and there is something missing that I am stuck on. The main thing is I am wondering how to return the door status with a request from the gateway. And if someone could look at the code and point out some of the errors that I am sure are there it would be appreciated.

    /*
    MyGarageDoor for MySensors
    
    Arduino garage door control
    
    June 16,2016
    
    This will allow the MySensors gateway to monitor and control your garage door opener.  This will 
    monitor the internal upper and lower limit switches of the door opener to determine door position 
    for status as well as have a relay switched output that can control either the momentary contact 
    switch connected to the door opener or wired directly to the switch contacts of a door opener 
    remote to activate the door.
    
    This sketch features the following:
    
    * Allows you to monitor the door position and return it's status based on the following:
        1 - When either the upper or lower door limit switches are reached, the door is said to be 
            either fully open or fully closed. When a limit switch is triggered, the lastLimit and 
            currentState variables are set to either OPEN or CLOSED based on the limit switch that was 
            triggered.
        2 - If the upper and lower limit inputs both read high, then the door is said to be in motion.
            The door's direction of motion is determined from the lastLimit variable.  If the lastLimit
            of the door is OPEN, then the currentState variable should be set to CLOSING.  If the 
            lastLimit of the door is CLOSED, then the currentState variable should be set to OPENING.
    * Checks for door obstructions and throws an error by checking the currentState and lastLimit 
      variables when the upper limit is reached.  If the upper limit is triggered and the currentState 
      is set to CLOSING and the lastLimit is set to OPEN, this should indicate that the door reversed 
      when closing and returned to the fully opened position.  The error state should only appear if 
      BOTH the lastLimit and currentState conditions match.  If lastLimit is OPEN and the lastState is 
      OPENING, this would indicate that the door was manually reversed.  This chech should only be 
      needed when the upper limit is reached.
    * Allows you to toggle the opener to either open or close the door.  When the door is toggled, the 
      currentState should be checked and if the state is either OPENING or CLOSING, the currentState 
      should invert telling that the door has changed direction.  This is done to prevent an error if 
      the door is reversed manually when closing.
    
    PARTS LIST:
    * Arduino pro mini
    * nRF24L01 radio module
    * AMS1117 3.3v regulator
    * 0.1uf capacitor
    * 10uf electrolytic capacitor
    * 4.7uf electrolytic capacitor (for radio module)
    * 5v reed relay
    * 2N3904 transistor for relay
    * 1k resistor
    * Two small signal diodes for incoming signals from door opener
    
    Wiring from garage door opener
    Yellow = Upper limit D7
    Brown = Lower limit D8
    Gray = Signal ground
    
    Relay D5
    */
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <SimpleTimer.h>
    
    #define SKETCH_NAME "MyGarageDoor"
    #define SKETCH_VERSION "1.0"
    
    #define UPPER_LIMIT_SENSOR 7  //Pin used for input from the garage door upper limit sensor
    #define LOWER_LIMIT_SENSOR 8  //Pin used for input from the garage door lower limit sensor
    #define DOOR_ACTUATOR_RELAY 5 //Pin used to toggle the door actuator relay
    #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 TOGGLE_INTERVAL 2000 //Tells how many milliseconds the relay will be held closed
    
    #define CLOSED = 0
    #define OPEN = 1
    #define CLOSING = 2
    #define OPENING = 3
    
    const char *lastLimit[] = { "Closed", "Open" };
    const char *currentState[] = { "Closed", "Open", "Closing", "Opening" };
    
    int cState;     //Current state of the door
    int lState;     //Last state of the door
    int lLimit = 0; //The last full limit position of the door (open or closed)
    
    boolean obstruction = false; //Used for the error condition when the door is unexpectedly reversed
    
    SimpleTimer timer; //The timer object used for toggling the door opener
    
    MySensor gw;
    
    /**
     * setup - Initialize the garage door sensor
     */
    void setup()  { 
      pinMode(UPPER_LIMIT_SENSOR, INPUT);
      pinMode(LOWER_LIMIT_SENSOR, INPUT);
      pinMode(DOOR_ACTUATOR_RELAY, OUTPUT);
    
      Serial.println( SKETCH_NAME ); 
      gw.begin( incomingMessage, AUTO );
      
      gw.sendSketchInfo( SKETCH_NAME, SKETCH_VERSION );
      
      // Register the garage door sensor with the gateway
      gw.present( getNodeId(), S_LIGHT );
    
      //Get the current door state
      getState();
    }
    
    
    void loop() {
      // Alway process incoming messages whenever possible
      gw.process();
    
      //For every cycle while the door is opening or closing, we'll get the state of the door to see 
      //when the door has reached it's upper or lower limit.  Once the door has reached it's fully
      //open or fully closed position, we'll set the last limit to the current state
      if ( (cState == OPENING) || (cState == CLOSING) ) {
        getState();
        if ( (cState == OPEN) || (cState == CLOSED) ) {
          setLastLimit( cState );
        }
      }
    }
    
    void incomingMessage( const MyMessage &message ) {
      // We only expect one type of message from controller. But we better check anyway.
      if ( (message.type==V_STATUS) && (message.getBool() == RELAY_ON) ) {
         //Toggle the door opener
         toggleDoor();
      }
    }
    
    /**
     *toggleDoor - Used to activate the garage door opener
     */
    void toggleDoor() {
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_ON );
      timer.setTimeout( TOGGLE_INTERVAL, relayOff );
    }
    
    /**
     *relayOff - Used to turn off the door opener relay after the TOGGLE_INTERVAL
     */
    void relayOff() {
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_OFF );
    }
    
    /**
     *getState - Used to get the current state of the garage door.  This will set the cState variable
                 to either OPEN, CLOSED, OPENING and CLOSING
     */
    void getState() {
      int upper = digitalRead( UPPER_LIMIT_SENSOR ); //read the upper sensor
      int lower = digitalRead( LOWER_LIMIT_SENSOR ); //read the lower sensor
    
      //Save the last state of the door for later tests
      lState = cState;
    
      //Check if the door is open
      if ((upper == HIGH) && (lower == LOW)) {
        //Set the current state to open
        cState = OPEN;
      //Check if the door is closed
      } else if ((upper == LOW) && (lower == HIGH)) {
        //Set the current state to closed
        cState = CLOSED;
      //Check if the door is in motion
      } else if ((upper == HIGH) && (lower == HIGH)) {
        //If in motion and the last full position of the door was open
        if (lLimit == OPEN) {
          //Set the current state to closing
          cState = CLOSING;
        //If in motion and the last full position of the door was closed
        } else {
          //Set the current state to opening
          cState = OPENING;
        }
      }
    }
    
    void setLastLimit( int limit ) {
      //Here is where we check for our error condition
      if ( (lLimit == OPEN) && (limit == OPEN) && (lState == CLOSING) ) {
        //An obstruction has reversed the door.  No need to set the last limit because it already equals
        //the limit we are trying to set it to
        obstruction = true;
      //If we made it here and the last limit does not equal the limit we are setting then change it.  If
      //the last limit is equal to the limit we are setting then the last state was something other than 
      //closing, so we don't need to do anything.
      } else if ( lLimit != limit ) {
        //Everything okay, set the last limit
        lLimit = limit;
      }
    }
    
    

  • Plugin Developer

    @dbemowsk

    If state is binary ie open or closed, you should add a Message variable in the sketch scope, before setup, of type V_STATUS. Then handle a request/message of type V_STATUS in the incoming message function and send a message with the current state. Look at the relay example sketch.

    The request message will lack a payload.

    Since you have four internal states in the sketch, I would convert them to either open or closed when reporting to the gateway. I would think the gateway is only interested in that binary state. The device should handle logic regarding the state for opening and closing.

    When you present the sensor, you probably don't always want to use the same id for the child as for the node. Now you're using getNodeId to assign child id. I would put a static integer value for the child id. You can define this before setup.

    I haven't looked in detail at all the state logic for the door. I think you know best how that should behave.



  • @martinhjelmare Bear with me on this because I am very new to this.

    About the child ID, thanks for that tip. I wasn't realizing that there were the two different IDs (node ID and child ID). It makes sense though. If you have more than one sensor on a node, you would need to be able to tell which sensor on the node you are working with. I am assuming that most people just go with a starting child node of 0 and increment from there if they add more sensors?

    I am curious why I would not want to send one of the 4 states? Why would I want to convert to just a binary OPEN or CLOSED? One other thing I would like to report back that may affect this is if you notice, I have an obstruction variable that tells whether or not something caused the door to reverse when closing. If I just assumed that CLOSING would eventually become CLOSED, this could mess up the reading on the gateway side and it could potentially get a false reading, i.e.the door may report closed when it is actually open. I would think I would want to send 2 things, first, the current state (cState), and second the boolean obstruction value. This way I can have the automation software alert me if there is a problem closing the door. I would then think that the sub-type of the "req" message could be a V_VAR1. I could combine the error with the state where the error would be -1. What I am not sure on is if the request would return something like -1 to 3 where -1 is an obstruction and 0 to 3 are the four internal states, or if the request would actually return Obstruction, Open, Closed, Opening or Closing?


  • Plugin Developer

    @dbemowsk

    My idea was that the gateway/controller should not have to care about if the device is opening or closing. From an outside view I think that the only important information is position, and in your case, if there is a problem with the door. The device should take care of logic regarding movement direction etc. The position can be reported as binary (open/closed) or perhaps percentage or both. The possible problem could also be reported as a binary state (no problem/problem). Whatever logic you would implement on the controller side for determining the correct state could most likely be implemented in the device, so that correct position is reported and if there is a problem.

    But of course you're free to use all the possibilities of the mysensors API to your liking. Whatever works. That's one of the things that makes mysensors great.

    There's the S_COVER type with V_UP, V_DOWN, V_STOP and V_PERCENTAGE, that you can use besides the binary types.

    Regarding the custom types, V_VAR1 etc, they will contain whatever payload you set.



  • @martinhjelmare
    Many thanks for your input on this.

    I kind of see your point that the gateway/controller should not have to care about if the device is opening or closing. One issue in that though is that if a status request is sent say immediately after the door is triggered to close, it may be 10 seconds or more before a determination can be made as to whether the door is open or closed. Do I then make the gateway/controller wait for the status? Or do I say that if the gateway/controller receives a status of OPENING or CLOSING, it can then check back in say 10 seconds or whatever to get the actual state of the door (OPEN or CLOSED). I guess I don't know how often polling would take place on the controller side.

    One other thing that I am a bit confused on is whether I would need to present two child nodes, one for the trigger relay, and one for the door status, or if I would present one child that would handle the trigger and status both?

    Like I say, I am still pretty new to this and am trying to get my bearings on how things work with MySensors.


  • Plugin Developer

    @dbemowsk

    I think you make a good point that it can be important to know if the door is still moving or not after a command has been sent from the controller to go to a position or to move.

    I would use the V_UP and V_DOWN types for this. Also, it's best if the device pushes updates to the gateway and controller so that you don't need to poll the device from the controller all the time.

    You can have multiple different v_types for one child and s_type, but if you need more than one of the same v_type you should add a child. Also, your controller might limit your options depending on how it has implemented the mysensors API.



  • @martinhjelmare

    V_UP and V_DOWN are a great suggestion. I use a distance sensor on my garage door for the state. This will let me know when a button push occurred or a remote open was used.



  • @martinhjelmare said:

    You can have multiple different v_types for one child and s_type, but if you need more than one of the same v_type you should add a child. Also, your controller might limit your options depending on how it has implemented the mysensors API.

    So with the serial API, are you limited to the variables defined for a sensor, or are those just suggestions. So for S_DIMMER, am I limited to V_UP, V_DOWN, V_STOP, V_PERCENTAGE? The reason I ask is if I used S_DIMMER Can I handle the incoming toggle request using that? That is the reason I asked if I needed to use two child nodes.

    So I am guessing that by pushing updates you are talking about doing a gw.send immediately after a state change. I can see that being much better than having to send a polling request to get the status.


  • Plugin Developer

    @dbemowsk

    There's no inherit connection between s_types and v_types in the API, but the docs for the mysensors API groups them as a suggestion, I would say. The controllers, though, will probably have some kind of limit on the usage of the types. Each controller has to account for its implementation of the mysensors API.

    In the sketch you can combine V_STATUS with for example V_UP, V_DOWN and V_PERCENTAGE for S_COVER, yes, but you have to figure out how the controller will respond to that combination, if it will work or not.

    Regarding pushing updates, yes, after each state change etc, send in the new value.



  • The only thing I would need the incoming status for would be to toggle the opener on. Once toggled on, shutting the relay off is done internally on a timer. The relay would emulate a momentary button press, so it would only be on for a second or two. The rest is just pushing the status back to the controller.

    Then my next step is going to be to actually try to compile this and upload it. What kind of command would I need to send from the serial monitor to activate the relay since I do not have any automation software set up yet?



  • OK, I finally got around to finishing the first sketch that I am going to test for this.

    /*
    MyGarageDoor for MySensors
    
    Arduino garage door control
    
    June 16,2016
    
    This will allow the MySensors gateway to monitor and control your garage door opener.  This will 
    monitor the internal upper and lower limit switches of the door opener to determine door position 
    for status as well as have a relay switched output that can control either the momentary contact 
    switch connected to the door opener or wired directly to the switch contacts of a door opener 
    remote to activate the door.
    
    This sketch features the following:
    
    * Allows you to monitor the door position and return it's status based on the following:
        1 - When either the upper or lower door limit switches are reached, the door is said to be 
            either fully open or fully closed. When a limit switch is triggered, the lastLimit and 
            currentState variables are set to either OPEN or CLOSED based on the limit switch that was 
            triggered.
        2 - If the upper and lower limit inputs both read high, then the door is said to be in motion.
            The door's direction of motion is determined from the lastLimit variable.  If the lastLimit
            of the door is OPEN, then the currentState variable should be set to CLOSING.  If the 
            lastLimit of the door is CLOSED, then the currentState variable should be set to OPENING.
    * Checks for door obstructions and throws an error by checking the currentState and lastLimit 
      variables when the upper limit is reached.  If the upper limit is triggered and the currentState 
      is set to CLOSING and the lastLimit is set to OPEN, this should indicate that the door reversed 
      when closing and returned to the fully opened position.  The error state should only appear if 
      BOTH the lastLimit and currentState conditions match.  If lastLimit is OPEN and the lastState is 
      OPENING, this would indicate that the door was manually reversed.  This chech should only be 
      needed when the upper limit is reached.
    * Allows you to toggle the opener to either open or close the door.  When the door is toggled, the 
      currentState should be checked and if the state is either OPENING or CLOSING, the currentState 
      should invert telling that the door has changed direction.  This is done to prevent an error if 
      the door is reversed manually when closing.
    
    PARTS LIST:
    * Arduino pro mini
    * nRF24L01 radio module
    * AMS1117 3.3v regulator
    * 0.1uf capacitor
    * 10uf electrolytic capacitor
    * 4.7uf electrolytic capacitor (for radio module)
    * 5v reed relay
    * 2N3904 transistor for relay
    * 1k resistor
    * Two small signal diodes for incoming signals from door opener
    
    Wiring from garage door opener
    Yellow = Upper limit D7
    Brown = Lower limit D8
    Gray = Signal ground
    
    Relay D5
    */
    
    #include <SPI.h>
    #include <MySensor.h>
    #include <SimpleTimer.h>
    
    #define SKETCH_NAME "MyGarageDoor"
    #define SKETCH_VERSION "1.0"
    
    #define CHILD_ID 0
    
    #define UPPER_LIMIT_SENSOR 7  //Pin used for input from the garage door upper limit sensor
    #define LOWER_LIMIT_SENSOR 8  //Pin used for input from the garage door lower limit sensor
    #define DOOR_ACTUATOR_RELAY 5 //Pin used to toggle the door actuator relay
    #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 TOGGLE_INTERVAL 2000  //Tells how many milliseconds the relay will be held closed
    
    #define CLOSED 0
    #define OPEN 1
    #define CLOSING 2
    #define OPENING 3
    
    //const char *currentState[] = { "Closed", "Open", "Closing", "Opening" };
    
    int cState;     //Current state of the door
    int lState;     //Last state of the door
    int lLimit = 0; //The last full limit position of the door (open or closed)
    
    boolean obstruction = false; //Used for the error condition when the door is unexpectedly reversed
    
    SimpleTimer timer; //The timer object used for toggling the door opener
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_VAR1);
    
    /**
     * setup - Initialize the garage door sensor
     */
    void setup() { 
      pinMode(UPPER_LIMIT_SENSOR, INPUT);
      pinMode(LOWER_LIMIT_SENSOR, INPUT);
      pinMode(DOOR_ACTUATOR_RELAY, OUTPUT);
    
      Serial.println( SKETCH_NAME ); 
      gw.begin( incomingMessage, AUTO );
      
      gw.sendSketchInfo( SKETCH_NAME, SKETCH_VERSION );
      
      // Register the garage door sensor with the gateway
      gw.present( CHILD_ID, S_BINARY );
    
      //Get the current door state
      getState();
    }
    
    
    void loop() {
      // Alway process incoming messages whenever possible
      gw.process();
    
      //For every cycle while the door is opening or closing, we'll get the state of the door to see 
      //when the door has reached it's upper or lower limit.  Once the door has reached it's fully
      //open or fully closed position, we'll set the last limit to the current state
      if ( (cState == OPENING) || (cState == CLOSING) ) {
        getState();
        if ( (cState == OPEN) || (cState == CLOSED) ) {
          setLastLimit( cState );
        }
    
        //Here we check if the state has changed and update the gateway with the change.  We do this 
        //after all processing of the state because we need to know if there was an obstruction
        if (lState != cState) {
          gw.send( msg.set( obstruction == true ? -1 : cState ) );  //"obstr" : currentState[cState]);
        }
      }
    }
    
    void incomingMessage( const MyMessage &message ) {
      //We only expect one type of message from controller. But we better check anyway.
      if ( (message.type == V_STATUS) && (message.getBool() == RELAY_ON) ) {
         //Toggle the door opener
         toggleDoor();
      }
    }
    
    /**
     *toggleDoor - Used to activate the garage door opener
     */
    void toggleDoor() {
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_ON );
      timer.setTimeout( TOGGLE_INTERVAL, relayOff );
    }
    
    /**
     *relayOff - Used to turn off the door opener relay after the TOGGLE_INTERVAL
     */
    void relayOff() {
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_OFF );
    }
    
    /**
     *getState - Used to get the current state of the garage door.  This will set the cState variable
                 to either OPEN, CLOSED, OPENING and CLOSING
     */
    void getState() {
      int upper = digitalRead( UPPER_LIMIT_SENSOR ); //read the upper sensor
      int lower = digitalRead( LOWER_LIMIT_SENSOR ); //read the lower sensor
    
      //Save the last state of the door for later tests
      lState = cState;
    
      //Check if the door is open
      if ((upper == HIGH) && (lower == LOW)) {
        //Set the current state to open
        cState = OPEN;
      //Check if the door is closed
      } else if ((upper == LOW) && (lower == HIGH)) {
        //Set the current state to closed
        cState = CLOSED;
      //Check if the door is in motion
      } else if ((upper == HIGH) && (lower == HIGH)) {
        //If in motion and the last full position of the door was open
        if (lLimit == OPEN) {
          //Set the current state to closing
          cState = CLOSING;
        //If in motion and the last full position of the door was closed
        } else {
          //Set the current state to opening
          cState = OPENING;
        }
      }
    }
    
    void setLastLimit( int limit ) {
      //Here is where we check for our error condition
      if ( (lLimit == OPEN) && (limit == OPEN) && (lState == CLOSING) ) {
        //An obstruction has reversed the door.  No need to set the last limit because it already equals
        //the limit we are trying to set it to
        obstruction = true;
      //If we made it here and the last limit does not equal the limit we are setting then change it.  If
      //the last limit is equal to the limit we are setting then the last state was something other than 
      //closing, so we don't need to do anything.
      } else if ( lLimit != limit ) {
        //Everything okay, set the last limit
        lLimit = limit;
      }
    }
    

    My problem though is that when I compile it I get a warning:
    WARNING: Category '' in library UIPEthernet is not valid. Setting to 'Uncategorized'
    First off, the sketch in no way uses ethernet. Not even sure what UIPEthernet is, The googling I have done on this warning has led me to some posts that say that people using arduino 1.6.6 and I think even 1.6.7 have reverted back to 1.6.5 and their sketches compiled fine. I am running 1.6.9. I have not tried to find a version of 1.6.5 yet to see if that works, but before I do I have questions.
    First, is this a warning I need to worry about? The sketch compiles and uploads to the pro mini fine.
    Second, would there be any issues reverting to 1.6.5 while using MySensors 1.5?
    Third, could the issue be something with the MySensors library?

    Thanks



  • Garage door circuit
    So here is my circuit. It looks crude right now dangling from my door opener, but I have it working. I have the nRF24 radio on a bit of a pigtail because I am thinking that I may need to mount this somewhere outside the garage door opener housing. If I leave it inside the housing it will be completely enclosed in a metal shield and I am afraid that that may interfere with the radio.

    I had to modify my original sketch that I originally posted here quite a bit. Here is the modified working sketch.

    /*
      MyGarageDoor for MySensors
    
      Arduino garage door control
    
      June 16,2016
    
      This will allow the MySensors gateway to monitor and control your garagedoor opener.  This will
      monitor the internal upper and lower limit switches of the door openerto determine door position
      for status as well as have a relay switched output that can controleither the momentary contact
      switch connected to the door opener or wired directly to the switchcontacts of a door opener
      remote to activate the door.
    
      This sketch features the following:
    
      Allows you to monitor the door position and return it's status based on the following:
        1 - When either the upper or lower door limit switches are reached,the door is said to be
            either fully open or fully closed. When a limit switch is triggered, the lastLimit and
            currentState variables are set to either OPEN or CLOSED based on the limit switch that was
            triggered.
        2 - If the upper and lower limit inputs both read high, then the dooris said to be in motion.
            The door's direction of motion is determined from the lastLimit variable.  If the lastLimit
            of the door is OPEN, then the currentState variable should be setto CLOSING.  If the
            lastLimit of the door is CLOSED, then the currentState variable should be set to OPENING.
      Checks for door obstructions and throws an error by checking the currentState and lastLimit
      variables when the upper limit is reached.  If the upper limit is triggered and the currentState
      is set to CLOSING and the lastLimit is set to OPEN, this should indicate that the door reversed
      when closing and returned to the fully opened position.  The error state should only appear if
      BOTH the lastLimit and currentState conditions match.  If lastLimit isOPEN and the lastState is
      OPENING, this would indicate that the door was manually reversed.  This check should only be
      needed when the upper limit is reached.
      Allows you to toggle the opener to either open or close the door.  When the door is toggled, the
      currentState should be checked and if the state is either OPENING or CLOSING, the currentState
      should invert telling that the door has changed direction.  This is done to prevent an error if
      the door is reversed manually when closing.
    
      PARTS LIST:
      Arduino pro mini
      nRF24L01 radio module
      AMS1117 3.3v regulator
      0.1uf capacitor
      10uf electrolytic capacitor
      4.7uf electrolytic capacitor (for radio module)
      5v reed relay
      2N3904 transistor for relay
      1k resistor
      Two small signal diodes for incoming signals from door opener
    
      Wiring from garage door opener
      Yellow = Upper limit D7
      Brown = Lower limit D8
      Gray = Signal ground
    
      Relay D5
    */
    
    #include <SPI.h>
    #include <MySensor.h>
    #include <SimpleTimer.h>
    
    #define SKETCH_NAME "MyGarageDoor"
    #define SKETCH_VERSION "1.0"
    
    #define CHILD_ID 0
    
    #define UPPER_LIMIT_SENSOR 8  //Pin used for input from the garage door upper limit sensor
    #define LOWER_LIMIT_SENSOR 7  //Pin used for input from the garage door lower limit sensor
    #define DOOR_ACTUATOR_RELAY 5 //Pin used to toggle the door actuator relay
    #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 TOGGLE_INTERVAL 1000  //Tells how many milliseconds the relay will be held closed
    
    #define CLOSED 0
    #define OPEN 1
    #define CLOSING 2
    #define OPENING 3
    
    const char *currentState[] = { "Closed", "Open", "Closing", "Opening" };
    
    int cState;     //Current state of the door
    int lState;     //Last state of the door
    int lLimit = 0; //The last full limit position of the door (open or closed)
    
    boolean obstruction = false; //Used for the error condition when the door is unexpectedly reversed
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_VAR1);
    
    /**
       setup - Initialize the garage door sensor
    */
    void setup() {
    
      pinMode(UPPER_LIMIT_SENSOR, INPUT);
      pinMode(LOWER_LIMIT_SENSOR, INPUT);
      pinMode(DOOR_ACTUATOR_RELAY, OUTPUT);
    
      Serial.println( SKETCH_NAME );
      gw.begin( incomingMessage, AUTO );
    
      gw.sendSketchInfo( SKETCH_NAME, SKETCH_VERSION );
    
      // Register the garage door sensor with the gateway
      gw.present( CHILD_ID, S_BINARY );
    
      //Get the current door state
      getState();
    }
    
    
    void loop() {
    
      // Alway process incoming messages whenever possible
      gw.process();
    
      //Get the state of the door
      getState();
    
      //Here we check if the state has changed and update the gateway with the change.  We do this
      //after all processing of the state because we need to know if there was an obstruction
      if (lState != cState) {
        gw.send( msg.set( obstruction == true ? "OBSTRUCTION" : currentState[cState] ) ); 
    
        //Once the obstruction is checked and sent, we can clear it
        obstruction = false;
    
        //If the current state is full open or full closed we need to set the last limit
        if ( (cState == OPEN) || (cState == CLOSED) ) {
          setLastLimit( cState );
        }
      }
    
      if ( digitalRead( DOOR_ACTUATOR_RELAY ) == 1) {
        relayOff();
      }
    }
    
    void incomingMessage( const MyMessage &message ) {
    
      //We only expect one type of message from controller. But we better check anyway.
      if ( (message.type == V_STATUS) && (message.getBool() == RELAY_ON) ) {
        //Toggle the door opener
        toggleDoor();
      }
    }
    
    /**
      toggleDoor - Used to activate the garage door opener
    */
    void toggleDoor() {
    
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_ON );
      //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
      delay( TOGGLE_INTERVAL );
    }
    
    /**
      relayOff - Used to turn off the door opener relay after the TOGGLE_INTERVAL
    */
    void relayOff() {
    
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_OFF );
    }
    
    /**
      getState - Used to get the current state of the garage door.  This will set the cState variable
                 to either OPEN, CLOSED, OPENING and CLOSING
    */
    void getState() {
      int upper = digitalRead( UPPER_LIMIT_SENSOR ); //read the upper sensor
      int lower = digitalRead( LOWER_LIMIT_SENSOR ); //read the lower sensor
    
      //Save the last state of the door for later tests
      lState = cState;
    
      //Check if the door is open
      if ((upper == HIGH) && (lower == LOW)) {
        //Set the current state to open
        cState = OPEN;
        //Check if the door is closed
      } else if ((upper == LOW) && (lower == HIGH)) {
        //Set the current state to closed
        cState = CLOSED;
        //Check if the door is in motion
      } else if ((upper == HIGH) && (lower == HIGH)) {
        //If in motion and the last full position of the door was open
        if (lLimit == OPEN) {
          //Set the current state to closing
          cState = CLOSING;
          //If in motion and the last full position of the door was closed
        } else {
          //Set the current state to opening
          cState = OPENING;
        }
      }
    }
    
    void setLastLimit( int limit ) {
      //Here is where we check for our error condition
      if ( (lLimit == OPEN) && (limit == OPEN) && (lState == CLOSING) ) {
        //An obstruction has reversed the door.  No need to set the last limit because it already equals
        //the limit we are trying to set it to
        obstruction = true;
        //If we made it here and the last limit does not equal the limit we are setting then change it.  If
        //the last limit is equal to the limit we are setting then the last state was something other than
        //closing, so we don't need to do anything.
      } else if ( lLimit != limit ) {
        //Everything okay, set the last limit
        lLimit = limit;
        obstruction = false;
      }
    }
    

    Here are some results as seen by MYSController

    6/25/2016 23:16:22      TX      1;0;1;0;2;1
    6/25/2016 23:16:22      RX      0;0;3;0;9;send: 0-0-1-1 s=0,c=1,t=2,pt=0,l=1,sg=0,st=ok:1
    6/25/2016 23:16:23      RX      0;0;3;0;9;read: 1-1-0 s=0,c=1,t=24,pt=0,l=7,sg=0:Opening
    6/25/2016 23:16:23      RX      1;0;1;0;24;Opening
    6/25/2016 23:16:34      RX      0;0;3;0;9;read: 1-1-0 s=0,c=1,t=24,pt=0,l=4,sg=0:Open
    6/25/2016 23:16:34      RX      1;0;1;0;24;Open
    6/25/2016 23:16:39      TX      1;0;1;0;2;1
    6/25/2016 23:16:39      RX      0;0;3;0;9;send: 0-0-1-1 s=0,c=1,t=2,pt=0,l=1,sg=0,st=ok:1
    6/25/2016 23:16:40      RX      0;0;3;0;9;read: 1-1-0 s=0,c=1,t=24,pt=0,l=7,sg=0:Closing
    6/25/2016 23:16:40      RX      1;0;1;0;24;Closing
    6/25/2016 23:16:50      RX      0;0;3;0;9;read: 1-1-0 s=0,c=1,t=24,pt=0,l=6,sg=0:Closed
    6/25/2016 23:16:50      RX      1;0;1;0;24;Closed
    6/25/2016 23:19:18      TX      1;0;1;0;2;1
    6/25/2016 23:19:18      RX      0;0;3;0;9;send: 0-0-1-1 s=0,c=1,t=2,pt=0,l=1,sg=0,st=ok:1
    6/25/2016 23:19:19      RX      0;0;3;0;9;read: 1-1-0 s=0,c=1,t=24,pt=0,l=7,sg=0:Opening
    6/25/2016 23:19:19      RX      1;0;1;0;24;Opening
    6/25/2016 23:19:29      RX      0;0;3;0;9;read: 1-1-0 s=0,c=1,t=24,pt=0,l=4,sg=0:Open
    6/25/2016 23:19:29      RX      1;0;1;0;24;Open
    6/25/2016 23:19:42      TX      1;0;1;0;2;1
    6/25/2016 23:19:42      RX      0;0;3;0;9;send: 0-0-1-1 s=0,c=1,t=2,pt=0,l=1,sg=0,st=ok:1
    

    Overall I think it turned out good for my first sensor. Now I just need to figure out how to tidy things up and get the garage door opener case put back together with everything inside and somehow protect the wires and things from the gears (only one of real concern). I am thinking a few zip ties.

    The next step will be getting my Orange Pi to work with Domoticz and controlling MySensor devices.



  • So I finally got a sketch that gives me all of the functions that I need for my garage door. The two main things that I needed were a way to toggle the door opener, and a way to display the current state of the door in Domoticz. Getting to the door to toggle open and closed was easy. The hard part was getting Domoticz to display the state of the door. I was told by @AWI that I needed a V_TEXT variable and a S_TEXT sencode:sor type. He mentioned that these were not available in version 1.5, but they could be added manually in your sketch by adding these lines of code to your sketch:

    // new V_TEXT variable type (development 20150905)
    const int V_TEXT = 47 ;
    // new S_INFO sensor type (development 20150905)
    const int S_INFO = 36 ;
    

    Here is the final working sketch:

     /*
      MyGarageDoor for MySensors
    
      Arduino garage door control
    
      June 16,2016
    
      This will allow the MySensors gateway to monitor and control your garage door opener.  This will
      monitor the internal upper and lower limit switches of the door opener to determine door position
      for status as well as have a relay switched output that can control either the momentary contact
      switch connected to the door opener or wired directly to the switch contacts of a door opener
      remote to activate the door.
    
      This sketch features the following:
    
      Allows you to monitor the door position and return it's status based on the following:
        1 - When either the upper or lower door limit switches are reached, the door is said to be
            either fully open or fully closed. When a limit switch is triggered, the lastLimit and
            currentState variables are set to either OPEN or CLOSED based on the limit switch that was
            triggered.
        2 - If the upper and lower limit inputs both read high, then the door is said to be in motion.
            The door's direction of motion is determined from the lastLimit variable.  If the lastLimit
            of the door is OPEN, then the currentState variable should be set to CLOSING.  If the
            lastLimit of the door is CLOSED, then the currentState variable should be set to OPENING.
      Checks for door obstructions and throws an error by checking the currentState and lastLimit
      variables when the upper limit is reached.  If the upper limit is triggered and the currentState
      is set to CLOSING and the lastLimit is set to OPEN, this should indicate that the door reversed
      when closing and returned to the fully opened position.  The error state should only appear if
      BOTH the lastLimit and currentState conditions match.  If lastLimit is OPEN and the lastState is
      OPENING, this would indicate that the door was manually reversed.  This chech should only be
      needed when the upper limit is reached.
      Allows you to toggle the opener to either open or close the door.  When the door is toggled, the
      currentState should be checked and if the state is either OPENING or CLOSING, the currentState
      should invert telling that the door has changed direction.  This is done to prevent an error if
      the door is reversed manually when closing.
    
      PARTS LIST:
      Arduino pro mini
      nRF24L01 radio module
      AMS1117 3.3v regulator
      0.1uf capacitor
      10uf electrolytic capacitor
      4.7uf electrolytic capacitor (for radio module)
      5v reed relay
      2N3904 transistor for relay
      1k resistor
      Two small signal diodes for incoming signals from door opener
    
      Wiring from garage door opener
      Yellow = Upper limit D7
      Brown = Lower limit D8
      Gray = Signal ground
    
      Relay D5
    
      Version history:
      1.1 : Added the V_TEXT variable and S_INFO sensor type, and created the "Status" child
    */
    
    #include <SPI.h>
    #include <MySensor.h>
    
    #define SKETCH_NAME "MyGarageDoor"
    #define SKETCH_VERSION "1.1"
    
    #define CHILD_ID_OPENER 0
    #define CHILD_ID_STATUS 1
    
    #define UPPER_LIMIT_SENSOR 8  //Pin used for input from the garage door upper limit sensor
    #define LOWER_LIMIT_SENSOR 7  //Pin used for input from the garage door lower limit sensor
    #define DOOR_ACTUATOR_RELAY 5 //Pin used to toggle the door actuator relay
    #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 TOGGLE_INTERVAL 1000  //Tells how many milliseconds the relay will be held closed
    
    #define CLOSED 0
    #define OPEN 1
    #define CLOSING 2
    #define OPENING 3
    
    // new V_TEXT variable type (development 20150905)
    const int V_TEXT = 47 ;
    // new S_INFO sensor type (development 20150905)
    const int S_INFO = 36 ;
    
    const char *currentState[] = { "Closed", "Open", "Closing", "Opening" };
    
    //Current state of the door
    int cState;     
    
    //Last state of the door
    int lState;     
    
    //The last full limit position of the door (open or closed)
    int lLimit = 0; 
    
    //Used for the error condition when the door is unexpectedly reversed
    boolean obstruction = false;
    
    MySensor gw;
    MyMessage msgOpener(CHILD_ID_OPENER, V_STATUS);
    MyMessage msgStatus(CHILD_ID_STATUS, V_TEXT);
    
    /**
     * setup - Initialize the garage door sensor
     */
    void setup() {
      
      pinMode(UPPER_LIMIT_SENSOR, INPUT);
      pinMode(LOWER_LIMIT_SENSOR, INPUT);
      pinMode(DOOR_ACTUATOR_RELAY, OUTPUT);
    
      Serial.println( SKETCH_NAME );
      gw.begin( incomingMessage, AUTO );
    
      gw.sendSketchInfo( SKETCH_NAME, SKETCH_VERSION );
    
      // Register the garage door sensor with the gateway
      gw.present( CHILD_ID_OPENER, S_BINARY );
      gw.present( CHILD_ID_STATUS, S_INFO );
    }
    
    /**
     * loop - The main program loop
     */
    void loop() {
      
      // Alway process incoming messages whenever possible
      gw.process();
    
      //Get the state of the door
      getState();
    
      //Here we check if the state has changed and update the gateway with the change.  We do this
      //after all processing of the state because we need to know if there was an obstruction
      if (lState != cState) {
        gw.send( msgStatus.set( obstruction == true ? "OBSTRUCTION" : currentState[cState] ) );  
    
        //Once the obstruction is checked and sent, we can clear it
        obstruction = false;
    
        //If the current state is full open or full closed we need to set the last limit
        if ( (cState == OPEN) || (cState == CLOSED) ) {
          setLastLimit( cState );
        }
      }
    
      //If the relay is on, shut it off
      if ( digitalRead( DOOR_ACTUATOR_RELAY ) == 1) {
        relayOff();                       
      }
    }
    
    /**
     * incomingMessage - Process the incoming messages and watch for an incoming boolean 1
     *                   to toggle the garage door opener.
     */
    void incomingMessage( const MyMessage &message ) {
      
      //We only expect one type of message from controller. But we better check anyway.
      if ( (message.type == V_STATUS) && (message.getBool() == RELAY_ON) ) {
        //Toggle the door opener
        toggleDoor();
      }
    }
    
    /**
     * toggleDoor - Used to activate the garage door opener
     */
    void toggleDoor() {
    
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_ON );
      //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
      delay( TOGGLE_INTERVAL );
    }
    
    /**
     * relayOff - Used to turn off the door opener relay after the TOGGLE_INTERVAL
     */
    void relayOff() {
    
      digitalWrite( DOOR_ACTUATOR_RELAY, RELAY_OFF );
      //Added this to tell the controller that we shut off the relay
      gw.send( msgOpener.set(0) );
    }
    
    /**
     * getState - Used to get the current state of the garage door.  This will set the cState variable
     *            to either OPEN, CLOSED, OPENING and CLOSING
     */
    void getState() {
      int upper = digitalRead( UPPER_LIMIT_SENSOR ); //read the upper sensor
      int lower = digitalRead( LOWER_LIMIT_SENSOR ); //read the lower sensor
    
      //Save the last state of the door for later tests
      lState = cState;
    
      //Check if the door is open
      if ((upper == HIGH) && (lower == LOW)) {
        //Set the current state to open
        cState = OPEN;
        //Check if the door is closed
      } else if ((upper == LOW) && (lower == HIGH)) {
        //Set the current state to closed
        cState = CLOSED;
        //Check if the door is in motion
      } else if ((upper == HIGH) && (lower == HIGH)) {
        //If in motion and the last full position of the door was open
        if (lLimit == OPEN) {
          //Set the current state to closing
          cState = CLOSING;
          //If in motion and the last full position of the door was closed
        } else {
          //Set the current state to opening
          cState = OPENING;
        }
      }
    }
    
    /**
     * setLastLimit - Checks the limit passed with the last limit (lLimit) and sets the last limit
     *                if there was a change.  It also checks the states against the limits and issues 
     *                an obstruction error if the limit and last limit
     *                
     * @param limit  integer limit - The limit to be checked
     */
    void setLastLimit( int limit ) {
      //Here is where we check for our error condition
      if ( (lLimit == OPEN) && (limit == OPEN) && (lState == CLOSING) ) {
        //An obstruction has reversed the door.  No need to set the last limit because it already equals
        //the limit we are trying to set it to
        obstruction = true;
        //If we made it here and the last limit does not equal the limit we are setting then change it.  If
        //the last limit is equal to the limit we are setting then the last state was something other than
        //closing, so we don't need to do anything.
      } else if ( lLimit != limit ) {
        //Everything okay, set the last limit
        lLimit = limit;
        obstruction = false;
      }
    }
    

Log in to reply
 

208
Online

6.7k
Users

7.6k
Topics

80.4k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.