|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "MySensors Cover" |
| 4 | +description: "Instructions how to integrate MySensors covers into Home Assistant." |
| 5 | +date: 2016-09-25 11:30 +0100 |
| 6 | +sidebar: true |
| 7 | +comments: false |
| 8 | +sharing: true |
| 9 | +footer: true |
| 10 | +logo: mysensors.png |
| 11 | +ha_category: Cover |
| 12 | +ha_release: 0.30 |
| 13 | +--- |
| 14 | + |
| 15 | +Integrates MySensors covers into Home Assistant. See the [main component] for configuration instructions. |
| 16 | + |
| 17 | +The following actuator types are supported: |
| 18 | + |
| 19 | +##### MySensors version 1.4 |
| 20 | +S_TYPE | V_TYPE |
| 21 | +------------|------------- |
| 22 | +S_COVER | V_UP, V_DOWN, V_STOP, [V_DIMMER or V_LIGHT] |
| 23 | + |
| 24 | +##### MySensors version 1.5 and higher |
| 25 | +S_TYPE | V_TYPE |
| 26 | +------------|------------- |
| 27 | +S_COVER | V_UP, V_DOWN, V_STOP, [V_PERCENTAGE or V_STATUS] |
| 28 | + |
| 29 | +All V_TYPES above are required. Use V_PERCENTAGE (or V_DIMMER) if you know the exact position of the cover in percent, use V_STATUS (or V_LIGHT) if you don't. |
| 30 | + |
| 31 | +For more information, visit the [serial api] of MySensors. |
| 32 | + |
| 33 | +### {% linkable_title Example sketch %} |
| 34 | + |
| 35 | +```cpp |
| 36 | +/* |
| 37 | + * Documentation: http://www.mysensors.org |
| 38 | + * Support Forum: http://forum.mysensors.org |
| 39 | + */ |
| 40 | + |
| 41 | +// Enable debug prints to serial monitor |
| 42 | +#define MY_DEBUG |
| 43 | +#define MY_RADIO_NRF24 |
| 44 | + |
| 45 | +#include <MySensors.h> |
| 46 | +#define SN "Cover" |
| 47 | +#define SV "1.1" |
| 48 | + |
| 49 | +// Actuators for moving the cover up and down respectively. |
| 50 | +#define COVER_UP_ACTUATOR_PIN 2 |
| 51 | +#define COVER_DOWN_ACTUATOR_PIN 3 |
| 52 | +// Sensors for finding out when the cover has reached its up/down position. |
| 53 | +// These could be simple buttons or linear hall sensors. |
| 54 | +#define COVER_UP_SENSOR_PIN 4 |
| 55 | +#define COVER_DOWN_SENSOR_PIN 5 |
| 56 | + |
| 57 | +#define CHILD_ID 0 |
| 58 | + |
| 59 | +// Internal representation of the cover state. |
| 60 | +enum State { |
| 61 | + IDLE, |
| 62 | + UP, // Window covering. Up. |
| 63 | + DOWN, // Window covering. Down. |
| 64 | +}; |
| 65 | + |
| 66 | +static int state = IDLE; |
| 67 | +static int status = 0; // 0=cover is down, 1=cover is up |
| 68 | +static bool initial_state_sent = false; |
| 69 | +MyMessage upMessage(CHILD_ID, V_UP); |
| 70 | +MyMessage downMessage(CHILD_ID, V_DOWN); |
| 71 | +MyMessage stopMessage(CHILD_ID, V_STOP); |
| 72 | +MyMessage statusMessage(CHILD_ID, V_STATUS); |
| 73 | + |
| 74 | +void sendState() { |
| 75 | + // Send current state and status to gateway. |
| 76 | + send(upMessage.set(state == UP)); |
| 77 | + send(downMessage.set(state == DOWN)); |
| 78 | + send(stopMessage.set(state == IDLE)); |
| 79 | + send(statusMessage.set(status)); |
| 80 | +} |
| 81 | + |
| 82 | +void setup() { |
| 83 | + pinMode(COVER_UP_SENSOR_PIN, INPUT); |
| 84 | + pinMode(COVER_DOWN_SENSOR_PIN, INPUT); |
| 85 | +} |
| 86 | + |
| 87 | +void presentation() { |
| 88 | + sendSketchInfo(SN, SV); |
| 89 | + |
| 90 | + present(CHILD_ID, S_COVER); |
| 91 | +} |
| 92 | + |
| 93 | +void loop() { |
| 94 | + if (!initial_state_sent) { |
| 95 | + sendState(); |
| 96 | + initial_state_sent = true; |
| 97 | + } |
| 98 | + |
| 99 | + if (state == IDLE) { |
| 100 | + digitalWrite(COVER_UP_ACTUATOR_PIN, LOW); |
| 101 | + digitalWrite(COVER_DOWN_ACTUATOR_PIN, LOW); |
| 102 | + } |
| 103 | + |
| 104 | + if (state == UP && digitalRead(COVER_UP_SENSOR_PIN) == HIGH) { |
| 105 | + Serial.println("Cover is up."); |
| 106 | + // Update status and state; send it to the gateway. |
| 107 | + status = 1; |
| 108 | + state = IDLE; |
| 109 | + sendState(); |
| 110 | + // Actuators will be disabled in next loop() iteration. |
| 111 | + } |
| 112 | + |
| 113 | + if (state == DOWN && digitalRead(COVER_DOWN_SENSOR_PIN) == HIGH) { |
| 114 | + Serial.println("Cover is down."); |
| 115 | + // Update status and state; send it to the gateway. |
| 116 | + status = 0; |
| 117 | + state = IDLE; |
| 118 | + sendState(); |
| 119 | + // Actuators will be disabled in next loop() iteration. |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void receive(const MyMessage &message) { |
| 124 | + if (message.type == V_UP) { |
| 125 | + // Set state to covering up and send it back to the gateway. |
| 126 | + state = UP; |
| 127 | + sendState(); |
| 128 | + Serial.println("Moving cover up."); |
| 129 | + |
| 130 | + // Activate actuator until the sensor returns HIGH in loop(). |
| 131 | + digitalWrite(COVER_UP_ACTUATOR_PIN, HIGH); |
| 132 | + } |
| 133 | + |
| 134 | + if (message.type == V_DOWN) { |
| 135 | + // Set state to covering up and send it back to the gateway. |
| 136 | + state = DOWN; |
| 137 | + sendState(); |
| 138 | + Serial.println("Moving cover down."); |
| 139 | + // Activate actuator until the sensor returns HIGH in loop(). |
| 140 | + digitalWrite(COVER_DOWN_ACTUATOR_PIN, HIGH); |
| 141 | + } |
| 142 | + |
| 143 | + if (message.type == V_STOP) { |
| 144 | + // Set state to idle and send it back to the gateway. |
| 145 | + state = IDLE; |
| 146 | + sendState(); |
| 147 | + Serial.println("Stopping cover."); |
| 148 | + |
| 149 | + // Actuators will be switched off in loop(). |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | +
|
| 154 | +[main component]: /components/mysensors/ |
| 155 | +[serial api]: https://www.mysensors.org/download/serial_api_20 |
0 commit comments