|
| 1 | +/* |
| 2 | + * This example needs SinricPro 2.2.2 |
| 3 | + * |
| 4 | + * If you encounter any issues: |
| 5 | + * - enable serial debug (see section below) |
| 6 | + * - open serial monitor and check whats happening |
| 7 | + * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one |
| 8 | +*/ |
| 9 | + |
| 10 | +// Uncomment the following line to enable serial debug output |
| 11 | +//#define ENABLE_DEBUG |
| 12 | + |
| 13 | +#ifdef ENABLE_DEBUG |
| 14 | + #define DEBUG_ESP_PORT Serial |
| 15 | + #define NODEBUG_WEBSOCKETS |
| 16 | + #define NDEBUG |
| 17 | +#endif |
| 18 | + |
| 19 | +#include <Arduino.h> |
| 20 | +#ifdef ESP8266 |
| 21 | + #include <ESP8266WiFi.h> |
| 22 | +#endif |
| 23 | +#ifdef ESP32 |
| 24 | + #include <WiFi.h> |
| 25 | +#endif |
| 26 | + |
| 27 | +#include "SinricPro.h" |
| 28 | +#include "SinricProLock.h" |
| 29 | + |
| 30 | +#define WIFI_SSID "YOUR_WIFI_SSID" |
| 31 | +#define WIFI_PASS "YOUR_WIFI_PASSWORD" |
| 32 | +#define APP_KEY "YOUR_APP_KEY_HERE" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" |
| 33 | +#define APP_SECRET "YOUR_APP_SECRET_HERE" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" |
| 34 | +#define GARAGEDOOR_ID "YOUR_DEVICE_ID_HERE" // Should look like "5dc1564130xxxxxxxxxxxxxx" |
| 35 | +#define BAUD_RATE 9600 // Change baudrate to your need |
| 36 | + |
| 37 | +#define RELAY D5 // PIN where relay is attachted to |
| 38 | +#define ENDSTOP_OPEN D6 // PIN where open position reedswitch is connected to (high = door is open | low = door is NOT open) |
| 39 | +#define ENDSTOP_CLOSED D7 // PIN where close position reedswitch is connected to (high = door is closed | low = door is NOT closed) |
| 40 | +#define MOVE_TIMEOUT 20000 // Timout (20 seconds) for door movement -> please use a value which fits to your open/close time but not more than 20 seconds |
| 41 | +#define RELAY_TIME 4000 // Relay high time (4 seconds) - usually between 2500 and 5000 |
| 42 | + |
| 43 | + |
| 44 | +// Door state definitions |
| 45 | +#define DOOR_MOVING 0 // Door is not in close and not in open position...somewere between, might be moving |
| 46 | +#define DOOR_OPEN 1 // Door is in open position |
| 47 | +#define DOOR_CLOSED 2 // Door is in closed position |
| 48 | +#define DOOR_MALFUNCTION 3 // Malfunction! Door can't be open and closed at the same time |
| 49 | + |
| 50 | +/* function getDoorState() |
| 51 | + * returs door state (see section above) |
| 52 | + */ |
| 53 | +int getDoorState() { |
| 54 | + if (digitalRead(ENDSTOP_CLOSED)==false && digitalRead(ENDSTOP_OPEN)==false) return DOOR_MOVING; |
| 55 | + if (digitalRead(ENDSTOP_CLOSED)==false && digitalRead(ENDSTOP_OPEN)==true) return DOOR_OPEN; |
| 56 | + if (digitalRead(ENDSTOP_CLOSED)==true && digitalRead(ENDSTOP_OPEN)==false) return DOOR_CLOSED; |
| 57 | + return DOOR_MALFUNCTION; |
| 58 | +} |
| 59 | + |
| 60 | +int lastDoorState; // last known doorstate |
| 61 | + |
| 62 | +/* turnOnRelayAndWait |
| 63 | + * |
| 64 | + * function: turn on relay for a given time and waits until endstop gets triggered or timeout happened |
| 65 | + * |
| 66 | + * input: relay_pin = pin which is RELAY connected to |
| 67 | + * relay_time = time in milliseconds to keep RELAY high |
| 68 | + * endstop_pin = pin which is MAGNET connected to |
| 69 | + * endstop_timeout = timeout time in milliseconds |
| 70 | + * |
| 71 | + * return: true if enstop is triggered within timeout time |
| 72 | + * false if endstop was not triggered within timeout time |
| 73 | +*/ |
| 74 | +bool turnOnRelayAndWait(int relay_pin, unsigned long relay_time, int endstop_pin, unsigned long endstop_timeout) { |
| 75 | + unsigned long startMillis = millis(); |
| 76 | + |
| 77 | + bool timeout = false; // timeout flag |
| 78 | + bool success = false; // success flag |
| 79 | + Serial.printf("RELAY: HIGH\r\n"); |
| 80 | + |
| 81 | + digitalWrite(relay_pin, HIGH); // turn on relay |
| 82 | + bool relay_High = true; // relay flag |
| 83 | + |
| 84 | + while (success==false && timeout==false) { // while operation is not completed and within timeout time |
| 85 | + unsigned long actualMillis = millis(); |
| 86 | + |
| 87 | + success = digitalRead(endstop_pin); // check endstop |
| 88 | + timeout = (actualMillis - startMillis >= endstop_timeout); // check for timeout |
| 89 | + |
| 90 | + // if relay_time is reached, turn off relay |
| 91 | + if (relay_High == true && actualMillis - startMillis >= relay_time){ |
| 92 | + Serial.printf("RELAY: LOW\r\n"); |
| 93 | + digitalWrite(relay_pin, LOW); |
| 94 | + relay_High = false; |
| 95 | + } |
| 96 | + yield(); // make sure to keep esp stay alive ;) |
| 97 | + } |
| 98 | + digitalWrite(relay_pin, LOW); // turn off relay, just to be safe! |
| 99 | + lastDoorState = getDoorState(); // update lastDoorState to prevent event sending |
| 100 | + return success; |
| 101 | +} |
| 102 | + |
| 103 | +bool onLockState(String deviceId, bool &lockState) { |
| 104 | + int doorState = getDoorState(); |
| 105 | + if (doorState == DOOR_MALFUNCTION){ |
| 106 | + Serial.printf("Malfunction! Door is reporting to be open and closed at the same time!\r\n"); |
| 107 | + SinricPro.setResponseMessage("Error: malfunction!"); |
| 108 | + return false; |
| 109 | + } |
| 110 | + bool success = false; |
| 111 | + if (lockState) { // close door |
| 112 | + if (doorState == DOOR_CLOSED) return true; // door is allready closed return true |
| 113 | + Serial.printf("Closing door...\r\n"); |
| 114 | + success = turnOnRelayAndWait(RELAY, RELAY_TIME, ENDSTOP_CLOSED, MOVE_TIMEOUT); // close the door |
| 115 | + Serial.printf("%s\r\n", success?"Door is closed now.":"Error! Door did not close properly (timeout)!"); |
| 116 | + } else { // open door |
| 117 | + if (doorState == DOOR_OPEN) return true; // door is allready open...return true |
| 118 | + Serial.printf("Opening door...\r\n"); |
| 119 | + success = turnOnRelayAndWait(RELAY, RELAY_TIME, ENDSTOP_OPEN, MOVE_TIMEOUT); // open the door |
| 120 | + Serial.printf("%s\r\n", success?"Door is open now.":"Error! Door did not open properly (timeout)!"); |
| 121 | + } |
| 122 | + if (!success) SinricPro.setResponseMessage("Error: timeout!"); |
| 123 | + return success; |
| 124 | +} |
| 125 | + |
| 126 | +void handleManualDoorState() { |
| 127 | + int actualDoorState = getDoorState(); |
| 128 | + if (actualDoorState != lastDoorState) { |
| 129 | + SinricProLock& garageDoor = SinricPro[GARAGEDOOR_ID]; |
| 130 | + switch (actualDoorState) { |
| 131 | + case DOOR_OPEN : Serial.printf("Door has been opened manually.\r\n"); |
| 132 | + garageDoor.sendLockStateEvent(false); |
| 133 | + break; |
| 134 | + case DOOR_CLOSED : garageDoor.sendLockStateEvent(true); |
| 135 | + Serial.printf("Door has been closed manually.\r\n"); |
| 136 | + break; |
| 137 | + case DOOR_MOVING : Serial.printf("Door is moving.\r\n"); |
| 138 | + break; |
| 139 | + default : Serial.printf("WARNING! DOOR HAS A MALFUNCTION!\r\n"); |
| 140 | + break; |
| 141 | + } |
| 142 | + lastDoorState = actualDoorState; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +void setupWiFi() { |
| 148 | + Serial.printf("\r\n[Wifi]: Connecting"); |
| 149 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 150 | + |
| 151 | + while (WiFi.status() != WL_CONNECTED) { |
| 152 | + Serial.printf("."); |
| 153 | + delay(250); |
| 154 | + } |
| 155 | + IPAddress localIP = WiFi.localIP(); |
| 156 | + Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); |
| 157 | +} |
| 158 | + |
| 159 | +void setupSinricPro() { |
| 160 | + SinricProLock &garageDoor = SinricPro[GARAGEDOOR_ID]; |
| 161 | + garageDoor.onLockState(onLockState); |
| 162 | + |
| 163 | + SinricPro.begin(APP_KEY, APP_SECRET); |
| 164 | +} |
| 165 | + |
| 166 | +void setup() { |
| 167 | + Serial.begin(BAUD_RATE); |
| 168 | + pinMode(RELAY, OUTPUT); |
| 169 | + pinMode(ENDSTOP_OPEN, INPUT); |
| 170 | + pinMode(ENDSTOP_CLOSED, INPUT); |
| 171 | + lastDoorState = getDoorState(); |
| 172 | + setupWiFi(); |
| 173 | + setupSinricPro(); |
| 174 | +} |
| 175 | + |
| 176 | +void loop() { |
| 177 | + SinricPro.handle(); |
| 178 | + handleManualDoorState(); |
| 179 | +} |
0 commit comments