Advertisement
gryzli133

RollerShutterNodeCoverMarekv2

Sep 5th, 2017
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.46 KB | None | 0 0
  1. // Enable debug prints to serial monitor
  2. #define MY_DEBUG
  3.  
  4. #define MY_GATEWAY_SERIAL
  5.  
  6. // Enable and select radio type attached
  7. //#define MY_RADIO_NRF24
  8.  
  9. //#define MY_RF24_PA_LEVEL RF24_PA_LOW
  10.  
  11. //#define MY_REPEATER_FEATURE
  12.  
  13. // uncomment if we want to manually assign an ID
  14. #define MY_NODE_ID 1
  15.  
  16. #include <Bounce2.h>
  17. #include <MySensors.h>
  18. #include <SPI.h>
  19.  
  20. #define BUTTON_UP_PIN 4  // Arduino Digital I/O pin number for up button
  21. #define BUTTON_DOWN_PIN 5  // Arduino Digital I/O pin number for down button
  22. //#define BUTTON_STOP_PIN 5  // Arduino Digital I/O pin number for stop button
  23. //#define RELAY_DIR_PIN 6  // Arduino Digital I/O pin number for direction relay
  24. //#define RELAY_POWER_PIN 7  // Arduino Digital I/O pin number for power relay
  25. #define RELAY_UP_PIN 6
  26. #define RELAY_DOWN_PIN 7
  27. #define RELAY_ON 1
  28. #define RELAY_OFF 0
  29. //#define RELAY_DOWN 1
  30. //#define RELAY_UP 0
  31. #define DIRECTION_DOWN 0
  32. #define DIRECTION_UP 1
  33. #define SKETCH_NAME "Cover by Marek"
  34. #define SKETCH_VER "1.0"
  35. #define CHILD_ID_COVER 0   // sensor Id of the sensor child
  36. #define STATE_UP 100 // 100 is open - up
  37. #define STATE_DOWN 0 // 0 is closed - down
  38. //#define CHILD_ID_CALIBRATE 1   // sensor Id of the sensor child to calibrate
  39. #define CHILD_ID_SET 1   // sensor Id of the sensor child to init the roll time
  40. #define PRESENT_MESSAGE "Cover sensor for hass"
  41. const int LEVELS = 100; //the number of levels
  42. float rollTime = 20.0; //the overall rolling time of the shutter
  43. const bool IS_ACK = false; //is to acknowlage
  44. static bool initial_state_sent = false;//for hass we need at list one state send at begining
  45.  
  46. // debouncing parameters
  47. int value = 0;
  48. int oldValueUp = 0;
  49. int oldValueDown = 0;
  50. int oldValueStop = 0;
  51. //static unsigned long last_interrupt_time_up = 0;
  52. //static unsigned long last_interrupt_time_down = 0;
  53. //static unsigned long debounce_time = 200;
  54.  
  55. Bounce debouncerUp = Bounce();
  56. Bounce debouncerDown = Bounce();
  57. Bounce debouncerStop = Bounce();
  58.  
  59. // shutter position parameters
  60. float timeOneLevel = rollTime / LEVELS;
  61. int requestedShutterLevel = 0;
  62. int currentShutterLevel = 0;
  63. unsigned long lastLevelTime = 0;
  64. bool isMoving = false;
  65. int directionUpDown;
  66.  
  67. enum CoverState {
  68.   STOP,
  69.   UP, // Window covering. Up.
  70.   DOWN, // Window covering. Down.
  71. };
  72.  
  73. static int coverState = STOP;
  74.  
  75. MyMessage msgUp(CHILD_ID_COVER, V_UP);
  76. MyMessage msgDown(CHILD_ID_COVER, V_DOWN);
  77. MyMessage msgStop(CHILD_ID_COVER, V_STOP);
  78. MyMessage msgPercentage(CHILD_ID_COVER, V_PERCENTAGE);
  79. //MyMessage msgCode(CHILD_ID_SET, V_IR_SEND);
  80.  
  81. void sendState() {
  82.   // Send current state and status to gateway.
  83.   send(msgUp.set(coverState == UP));
  84.   send(msgDown.set(coverState == DOWN));
  85.   send(msgStop.set(coverState == STOP));
  86.   send(msgPercentage.set(currentShutterLevel));
  87. }
  88.  
  89. void shuttersUp(void) {
  90.   #ifdef MY_DEBUG
  91.   Serial.println("Shutters going up");
  92.   #endif
  93.   if (digitalRead(RELAY_DOWN_PIN) == RELAY_ON) {
  94.     digitalWrite(RELAY_DOWN_PIN, RELAY_OFF);
  95.     wait(50);
  96.   }
  97.   digitalWrite(RELAY_UP_PIN, RELAY_ON);
  98.  
  99.   directionUpDown = DIRECTION_UP;
  100.   isMoving = true;
  101.   coverState = UP;
  102.   sendState();
  103. }
  104.  
  105. void shuttersDown(void) {
  106.   #ifdef MY_DEBUG
  107.   Serial.println("Shutters going down");
  108.   #endif
  109.   if (digitalRead(RELAY_UP_PIN) == RELAY_ON) {
  110.     digitalWrite(RELAY_UP_PIN, RELAY_OFF);
  111.     wait(50);
  112.   }
  113.   digitalWrite(RELAY_DOWN_PIN, RELAY_ON);
  114.  
  115.   directionUpDown = DIRECTION_DOWN;
  116.   isMoving = true;
  117.   coverState = DOWN;
  118.   sendState();
  119. }
  120.  
  121. void shuttersHalt(void) {
  122. #ifdef MY_DEBUG
  123.   Serial.println("Shutters halted");
  124. #endif
  125.   digitalWrite(RELAY_UP_PIN, RELAY_OFF);
  126.   digitalWrite(RELAY_DOWN_PIN, RELAY_OFF);
  127.  
  128.   isMoving = false;
  129.   requestedShutterLevel = currentShutterLevel;
  130. #ifdef MY_DEBUG
  131.   Serial.println("saving state to: ");
  132.   Serial.println(String(currentShutterLevel));
  133. #endif
  134.   saveState(CHILD_ID_COVER, currentShutterLevel);
  135.   coverState = STOP;
  136.   sendState();
  137. }
  138.  
  139. void changeShuttersLevel(int level) {
  140.   int dir = (level > currentShutterLevel) ? DIRECTION_UP : DIRECTION_DOWN;
  141.   if (isMoving && dir != directionUpDown) {
  142.     shuttersHalt();
  143.   }
  144.   requestedShutterLevel = level;
  145. }
  146.  
  147. void initShutters() {
  148. #ifdef MY_DEBUG
  149.   Serial.println("Init Cover");
  150. #endif
  151.   shuttersUp();
  152.   wait((rollTime + timeOneLevel * LEVELS) * 1000);
  153.   currentShutterLevel = STATE_UP;
  154.   requestedShutterLevel = currentShutterLevel;
  155. }
  156.  
  157. void receive(const MyMessage &message) {
  158. #ifdef MY_DEBUG
  159.   Serial.println("recieved incomming message");
  160.   Serial.println("Recieved message for sensor: ");
  161.   Serial.println(String(message.sensor));
  162.   Serial.println("Recieved message with type: ");
  163.   Serial.println(String(message.type));
  164. #endif
  165.   if (message.sensor == CHILD_ID_COVER) {
  166.     switch (message.type) {
  167.       case V_UP:
  168.         //Serial.println(", New status: V_UP");
  169.         changeShuttersLevel(STATE_UP);
  170.         //state = UP;
  171.         //sendState();
  172.         break;
  173.  
  174.       case V_DOWN:
  175.         //Serial.println(", New status: V_DOWN");
  176.         changeShuttersLevel(STATE_DOWN);
  177.         //state = DOWN;
  178.         //sendState();
  179.         break;
  180.  
  181.       case V_STOP:
  182.         //Serial.println(", New status: V_STOP");
  183.         shuttersHalt();
  184.         //state = IDLE;
  185.         //sendState();
  186.         break;
  187.  
  188.       case V_PERCENTAGE:
  189.         //Serial.println(", New status: V_PERCENTAGE");
  190.         //          if (!initial_state_sent) {
  191.         //            #ifdef MY_DEBUG
  192.         //            Serial.println("Receiving initial value from controller");
  193.         //            #endif
  194.         //            initial_state_sent = true;
  195.         //          }
  196.         int per = message.getInt();
  197.         if (per > STATE_UP) {
  198.           per = STATE_UP;
  199.         }
  200.         changeShuttersLevel(per);
  201.         //InitShutters(message.getInt());//send value < 0 or > 100 to calibrate
  202.         //sendState();
  203.         break;
  204.     }
  205.   }
  206. else if (message.sensor ==  CHILD_ID_SET) {
  207.  
  208.     if (message.type == V_VAR1) {
  209.       #ifdef MY_DEBUG
  210.       Serial.println(", New status: V_VAR1, with payload: ");
  211.       #endif      
  212.       String strRollTime = message.getString();
  213.       rollTime = strRollTime.toFloat();
  214.       #ifdef MY_DEBUG
  215.       Serial.println("rolltime value: ");
  216.       Serial.println(String(rollTime));
  217.       #endif
  218.       saveState(CHILD_ID_SET, rollTime);
  219.     }
  220.   }
  221. #ifdef MY_DEBUG
  222.   Serial.println("exiting incoming message");
  223. #endif
  224.   return;
  225. }
  226.  
  227. void before() {
  228.  
  229.   // Setup the button
  230.   pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
  231.   // Activate internal pull-up
  232. //  digitalWrite(BUTTON_UP_PIN, HIGH);
  233.   //  attachInterrupt(digitalPinToInterrupt(BUTTON_UP_PIN), upButtonPress, FALLING);
  234.  
  235.   pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
  236.   // Activate internal pull-up
  237. //  digitalWrite(BUTTON_DOWN_PIN, HIGH);
  238.   //  attachInterrupt(digitalPinToInterrupt(BUTTON_DOWN_PIN), downButtonPress, FALLING);
  239.  
  240. //  pinMode(BUTTON_STOP_PIN, INPUT_PULLUP);
  241.   // Activate internal pull-up
  242. //  digitalWrite(BUTTON_STOP_PIN, HIGH);
  243.  
  244.   // After setting up the button, setup debouncer
  245.   debouncerUp.attach(BUTTON_UP_PIN);
  246.   debouncerUp.interval(5);
  247.   // After setting up the button, setup debouncer
  248.   debouncerDown.attach(BUTTON_DOWN_PIN);
  249.   debouncerDown.interval(5);
  250.   // After setting up the button, setup debouncer
  251. //  debouncerStop.attach(BUTTON_STOP_PIN);
  252. //  debouncerStop.interval(5);
  253.  
  254.   // Make sure relays are off when starting up
  255.   digitalWrite(RELAY_UP_PIN, RELAY_OFF);
  256.   // Then set relay pins in output mode
  257.   pinMode(RELAY_UP_PIN, OUTPUT);
  258.  
  259.   // Make sure relays are off when starting up
  260.   digitalWrite(RELAY_DOWN_PIN, RELAY_OFF);
  261.   // Then set relay pins in output mode
  262.   pinMode(RELAY_DOWN_PIN, OUTPUT);
  263. }
  264.  
  265. void presentation() {
  266.   // Send the sketch version information to the gateway and Controller
  267.   sendSketchInfo(SKETCH_NAME, SKETCH_VER);
  268.   // Register all sensors to gw (they will be created as child devices)
  269.   present(CHILD_ID_COVER, S_COVER, PRESENT_MESSAGE, IS_ACK);
  270.   // present(CHILD_ID_SET, S_CUSTOM);
  271. }
  272.  
  273. void setup(void) {
  274.   //set up roll time if the saved value is not 255
  275.   #ifdef MY_DEBUG
  276.   Serial.println("getting rolltime from eeprom: ");
  277.   #endif
  278.   float tmpRollTime = loadState(CHILD_ID_SET);
  279.   if (tmpRollTime != 0xff) {
  280.     rollTime = tmpRollTime;
  281.   }
  282.   #ifdef MY_DEBUG
  283.   Serial.println(String(rollTime));
  284.   #endif
  285.  
  286.   int state = loadState(CHILD_ID_COVER);
  287.  
  288.   #ifdef MY_DEBUG
  289.   Serial.println("getting state from eeprom: ");
  290.   Serial.println(String(state));
  291.   #endif
  292.  
  293.   if (state == 0xff) {
  294.     initShutters();
  295.   } else {
  296.     changeShuttersLevel(state);
  297.   }
  298. }
  299.  
  300. void loop(void) {
  301.   if (!initial_state_sent) {
  302. #ifdef MY_DEBUG
  303.     Serial.println("Sending initial value");
  304. #endif
  305.     sendState();
  306.    
  307.    // send(msgCode.set('20.0'));
  308.     //    #ifdef MY_DEBUG
  309.     //    Serial.println("Requesting initial value from controller");
  310.     //    #endif
  311.     //    request(CHILD_ID_COVER, V_PERCENTAGE);
  312.     //    wait(2000, C_SET, V_PERCENTAGE);
  313.     initial_state_sent = true;
  314.   }
  315.  
  316.   debouncerUp.update();
  317.   value = debouncerUp.read();
  318.   if (value == 0 && value != oldValueUp) {
  319.     if(isMoving){
  320.       shuttersHalt();
  321.     }  
  322.     else{
  323.     changeShuttersLevel(STATE_UP);
  324.     }
  325.     //state = UP;
  326.     //sendState();
  327.   }
  328.   oldValueUp = value;
  329.  
  330.   debouncerDown.update();
  331.   value = debouncerDown.read();
  332.   if (value == 0 && value != oldValueDown) {
  333.     if(isMoving){
  334.       shuttersHalt();
  335.     }  
  336.     else{
  337.     changeShuttersLevel(STATE_DOWN);
  338.     }    
  339.     //state = DOWN;
  340.     //sendState();
  341.   }
  342.   oldValueDown = value;
  343.  
  344. /*  debouncerStop.update();
  345.   value = debouncerStop.read();
  346.   if (value == 0 && value != oldValueStop) {
  347.     shuttersHalt();
  348.     //state = IDLE;
  349.     //sendState();
  350.   }
  351.   oldValueStop = value;
  352. */
  353.  
  354.   if (isMoving) {
  355.     unsigned long _now = millis();
  356.     if (_now - lastLevelTime >= timeOneLevel * 1000) {
  357.       if (directionUpDown == DIRECTION_UP) {
  358.         currentShutterLevel += 1;
  359.       } else {
  360.         currentShutterLevel -= 1;
  361.       }
  362.       #ifdef MY_DEBUG
  363.       Serial.println(String(requestedShutterLevel));
  364.       Serial.println(String(currentShutterLevel));
  365.       #endif
  366.       lastLevelTime = millis();
  367.       send(msgPercentage.set(currentShutterLevel));
  368.     }
  369.     if (currentShutterLevel == requestedShutterLevel) {
  370.       shuttersHalt();
  371.     }
  372.   }
  373.   else if (requestedShutterLevel != currentShutterLevel) {
  374.     if (requestedShutterLevel > currentShutterLevel) {
  375.       shuttersUp();
  376.     }
  377.     else {
  378.       shuttersDown();
  379.     }
  380.     lastLevelTime = millis();
  381.   }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement