|
| 1 | +/* |
| 2 | + * Example shows how to use D1 mini with a relay shield. |
| 3 | + * |
| 4 | + * Use WeMos D1 Mini board from Tools -> Boards |
| 5 | + * |
| 6 | + * Relay shield: https://docs.wemos.cc/en/latest/d1_mini_shiled/relay.html |
| 7 | + * |
| 8 | + * If you encounter any issues: |
| 9 | + * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md |
| 10 | + * - ensure all dependent libraries are installed |
| 11 | + * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide |
| 12 | + * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies |
| 13 | + * - open serial monitor and check whats happening |
| 14 | + * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk |
| 15 | + * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one |
| 16 | + */ |
| 17 | + |
| 18 | +// Uncomment the following line to enable serial debug output |
| 19 | +//#define ENABLE_DEBUG |
| 20 | + |
| 21 | +#ifdef ENABLE_DEBUG |
| 22 | + #define DEBUG_ESP_PORT Serial |
| 23 | + #define NODEBUG_WEBSOCKETS |
| 24 | + #define NDEBUG |
| 25 | +#endif |
| 26 | + |
| 27 | +#include <Arduino.h> |
| 28 | +#ifdef ESP8266 |
| 29 | + #include <ESP8266WiFi.h> |
| 30 | +#endif |
| 31 | +#ifdef ESP32 |
| 32 | + #include <WiFi.h> |
| 33 | +#endif |
| 34 | + |
| 35 | +#include "SinricPro.h" |
| 36 | +#include "SinricProSwitch.h" |
| 37 | + |
| 38 | +#define WIFI_SSID "YOUR-WIFI-SSID" |
| 39 | +#define WIFI_PASS "YOUR-WIFI-PASSWORD" |
| 40 | +#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" |
| 41 | +#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" |
| 42 | +#define SWITCH_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx" |
| 43 | +#define BAUD_RATE 9600 // Change baudrate to your need |
| 44 | + |
| 45 | +#define RELAY_PIN D1 // * Relay Shield transistor closes relay when D1 is HIGH |
| 46 | + |
| 47 | +bool myPowerState = false; |
| 48 | + |
| 49 | +/* bool onPowerState(String deviceId, bool &state) |
| 50 | + * |
| 51 | + * Callback for setPowerState request |
| 52 | + * parameters |
| 53 | + * String deviceId (r) |
| 54 | + * contains deviceId (useful if this callback used by multiple devices) |
| 55 | + * bool &state (r/w) |
| 56 | + * contains the requested state (true:on / false:off) |
| 57 | + * must return the new state |
| 58 | + * |
| 59 | + * return |
| 60 | + * true if request should be marked as handled correctly / false if not |
| 61 | + */ |
| 62 | +bool onPowerState(const String &deviceId, bool &state) { |
| 63 | + Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off"); |
| 64 | + myPowerState = state; |
| 65 | + digitalWrite(RELAY_PIN, myPowerState?HIGH:LOW); |
| 66 | + return true; // request handled properly |
| 67 | +} |
| 68 | + |
| 69 | +// setup function for WiFi connection |
| 70 | +void setupWiFi() { |
| 71 | + Serial.printf("\r\n[Wifi]: Connecting"); |
| 72 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 73 | + |
| 74 | + while (WiFi.status() != WL_CONNECTED) { |
| 75 | + Serial.printf("."); |
| 76 | + delay(250); |
| 77 | + } |
| 78 | + Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str()); |
| 79 | +} |
| 80 | + |
| 81 | +// setup function for SinricPro |
| 82 | +void setupSinricPro() { |
| 83 | + // add device to SinricPro |
| 84 | + SinricProSwitch& mySwitch = SinricPro[SWITCH_ID]; |
| 85 | + |
| 86 | + // setup SinricPro |
| 87 | + SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); |
| 88 | + SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); |
| 89 | + SinricPro.begin(APP_KEY, APP_SECRET); |
| 90 | +} |
| 91 | + |
| 92 | +// main setup function |
| 93 | +void setup() { |
| 94 | + pinMode(RELAY_PIN, OUTPUT); // define Relay GPIO as output |
| 95 | + |
| 96 | + Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); |
| 97 | + setupWiFi(); |
| 98 | + setupSinricPro(); |
| 99 | +} |
| 100 | + |
| 101 | +void loop() { |
| 102 | + SinricPro.handle(); |
| 103 | +} |
0 commit comments