|
| 1 | +/* |
| 2 | + * Example for SinricPro Contactsensor device: |
| 3 | + * - Setup contactsensor device |
| 4 | + * - Support onPowerState to turn on / turn off contactsensor |
| 5 | + * - Checks a contact sensor connected to CONTACT_PIN and send event if state changed |
| 6 | + * |
| 7 | + * If you encounter any issues: |
| 8 | + * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md |
| 9 | + * - ensure all dependent libraries are installed |
| 10 | + * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide |
| 11 | + * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies |
| 12 | + * - open serial monitor and check whats happening |
| 13 | + * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk |
| 14 | + * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one |
| 15 | + */ |
| 16 | + |
| 17 | +// Uncomment the following line to enable serial debug output |
| 18 | +//#define ENABLE_DEBUG |
| 19 | + |
| 20 | +#ifdef ENABLE_DEBUG |
| 21 | + #define DEBUG_ESP_PORT Serial |
| 22 | + #define NODEBUG_WEBSOCKETS |
| 23 | + #define NDEBUG |
| 24 | +#endif |
| 25 | + |
| 26 | +#include <Arduino.h> |
| 27 | +#ifdef ESP8266 |
| 28 | + #include <ESP8266WiFi.h> |
| 29 | +#endif |
| 30 | +#ifdef ESP32 |
| 31 | + #include <WiFi.h> |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "SinricPro.h" |
| 35 | +#include "SinricProContactsensor.h" |
| 36 | + |
| 37 | +#define WIFI_SSID "YOUR-WIFI-SSID" |
| 38 | +#define WIFI_PASS "YOUR-WIFI-PASSWORD" |
| 39 | +#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" |
| 40 | +#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" |
| 41 | +#define CONTACT_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx" |
| 42 | +#define BAUD_RATE 9600 // Change baudrate to your need |
| 43 | + |
| 44 | +#define CONTACT_PIN 5 // PIN where contactsensor is connected to |
| 45 | + // LOW = contact is open |
| 46 | + // HIGH = contact is closed |
| 47 | + |
| 48 | +bool myPowerState = true; // assume device is turned on |
| 49 | +bool lastContactState = false; |
| 50 | +unsigned long lastChange = 0; |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Checks contactsensor connected to CONTACT_PIN |
| 54 | + * |
| 55 | + * If contactsensor state has changed, send event to SinricPro Server |
| 56 | + * state from digitalRead(): |
| 57 | + * HIGH = contactsensor is closed |
| 58 | + * LOW = contactsensor is open |
| 59 | + */ |
| 60 | +void handleContactsensor() { |
| 61 | + if (!myPowerState) return; // if device switched off...do nothing |
| 62 | + |
| 63 | + unsigned long actualMillis = millis(); |
| 64 | + if (actualMillis - lastChange < 250) return; // debounce contact state transitions (same as debouncing a pushbutton) |
| 65 | + |
| 66 | + bool actualContactState = digitalRead(CONTACT_PIN); // read actual state of contactsensor |
| 67 | + |
| 68 | + if (actualContactState != lastContactState) { // if state has changed |
| 69 | + Serial.printf("Contactsensor is %s now\r\n", actualContactState?"open":"closed"); |
| 70 | + lastContactState = actualContactState; // update last known state |
| 71 | + lastChange = actualMillis; // update debounce time |
| 72 | + SinricProContactsensor &myContact = SinricPro[CONTACT_ID]; // get contact sensor device |
| 73 | + myContact.sendContactEvent(actualContactState); // send event with actual state |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Callback for setPowerState request |
| 79 | + * |
| 80 | + * @param deviceId String containing deviceId (useful if this callback used by multiple devices) |
| 81 | + * @param[in] state bool true=turn on device / false=turn off device |
| 82 | + * @param[out] state bool true=device turned on / false=device turned off |
| 83 | + * @return true request handled properly |
| 84 | + * @return false request can't be handled because some kind of error happened |
| 85 | + */ |
| 86 | +bool onPowerState(const String &deviceId, bool &state) { |
| 87 | + Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off"); |
| 88 | + myPowerState = state; |
| 89 | + return true; // request handled properly |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +// setup function for WiFi connection |
| 94 | +void setupWiFi() { |
| 95 | + Serial.printf("\r\n[Wifi]: Connecting"); |
| 96 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 97 | + |
| 98 | + while (WiFi.status() != WL_CONNECTED) { |
| 99 | + Serial.printf("."); |
| 100 | + delay(250); |
| 101 | + } |
| 102 | + IPAddress localIP = WiFi.localIP(); |
| 103 | + Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); |
| 104 | +} |
| 105 | + |
| 106 | +// setup function for SinricPro |
| 107 | +void setupSinricPro() { |
| 108 | + // add device to SinricPro |
| 109 | + SinricProContactsensor& myContact = SinricPro[CONTACT_ID]; |
| 110 | + |
| 111 | + // set callback function to device |
| 112 | + myContact.onPowerState(onPowerState); |
| 113 | + |
| 114 | + // setup SinricPro |
| 115 | + SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); |
| 116 | + SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); |
| 117 | + SinricPro.begin(APP_KEY, APP_SECRET); |
| 118 | +} |
| 119 | + |
| 120 | +// main setup function |
| 121 | +void setup() { |
| 122 | + Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); |
| 123 | + |
| 124 | + pinMode(CONTACT_PIN, INPUT); |
| 125 | + |
| 126 | + setupWiFi(); |
| 127 | + setupSinricPro(); |
| 128 | +} |
| 129 | + |
| 130 | +void loop() { |
| 131 | + handleContactsensor(); |
| 132 | + SinricPro.handle(); |
| 133 | +} |
0 commit comments