This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
doorbell.ino
139 lines (111 loc) · 3.96 KB
/
doorbell.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/****************************************************************************************************************************
doorbell.ino
For ESP32/ESP8266 boards
Based on and modified from SinricPro libarary (https://github.com/sinricpro/)
to support other boards such as SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
Built by Khoi Hoang https://github.com/khoih-prog/SinricPro_Generic
Licensed under MIT license
**********************************************************************************************************************************/
/*
Example for how to use SinricPro Doorbell device:
- setup a doorbell device
- send event to sinricPro server if button is pressed
If you encounter any issues:
- check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
- ensure all dependent libraries are installed
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
- open serial monitor and check whats happening
- check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
- visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
#if !(defined(ESP8266) || defined(ESP32))
#error This code is intended to run on the ESP32/ESP8266 boards ! Please check your Tools->Board setting.
#endif
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#if (ESP8266)
#include <ESP8266WiFi.h>
#elif (ESP32)
#include <WiFi.h>
#endif
#include "SinricPro_Generic.h"
#include "SinricProDoorbell.h"
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define DOORBELL_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need
// change this to your button PIN
// on NodeMCU D3 / GPIO-0 is flash button PIN so you can use the builtin flash button
#define BUTTON_PIN 0
// checkButtonpress
// reads if BUTTON_PIN gets LOW and send Event
void checkButtonPress()
{
static unsigned long lastBtnPress;
unsigned long actualMillis = millis();
if (actualMillis - lastBtnPress > 500)
{
if (digitalRead(BUTTON_PIN) == LOW)
{
Serial.println("Ding dong...");
lastBtnPress = actualMillis;
// get Doorbell device back
SinricProDoorbell& myDoorbell = SinricPro[DOORBELL_ID];
// send doorbell event
myDoorbell.sendDoorbellEvent();
}
}
}
// setup function for WiFi connection
void setupWiFi()
{
Serial.print("\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
Serial.print("\n[WiFi]: IP-Address is ");
Serial.println(WiFi.localIP());
}
// setup function for SinricPro
void setupSinricPro()
{
// add doorbell device to SinricPro
SinricPro.add<SinricProDoorbell>(DOORBELL_ID);
// setup SinricPro
SinricPro.onConnected([]()
{
Serial.println("Connected to SinricPro");
});
SinricPro.onDisconnected([]()
{
Serial.println("Disconnected from SinricPro");
});
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup()
{
pinMode(BUTTON_PIN, INPUT_PULLUP); // BUTTIN_PIN as INPUT
Serial.begin(BAUD_RATE);
while (!Serial);
Serial.println("\nStarting doorbell on " + String(ARDUINO_BOARD));
Serial.println("Version : " + String(SINRICPRO_VERSION_STR));
setupWiFi();
setupSinricPro();
}
void loop()
{
checkButtonPress();
SinricPro.handle();
}