Created
October 23, 2023 10:16
-
-
Save kakopappa/f5853e57b534d08fbc1df2d299c71265 to your computer and use it in GitHub Desktop.
Alexa Push button Tutorial: https://help.sinric.pro/pages/tutorials/doorbell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
#include <Arduino.h> | |
#if defined(ESP8266) | |
#include <ESP8266WiFi.h> | |
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040) | |
#include <WiFi.h> | |
#endif | |
#include "SinricPro.h" | |
#include "SinricProDoorbell.h" | |
// Grab it from https://portal.sinric.pro. | |
#define APP_KEY "" //Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" | |
#define APP_SECRET "" //Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" | |
#define DOORBELL_ID "" //Should look like "6534xxxxxxxc08c6ea1b" | |
#define SSID "" // Your WiFi SSID | |
#define PASS "" // Your WiFi Password | |
#define BAUD_RATE 115200 // Change baudrate to your need | |
#if defined(ESP8266) | |
#define BUTTON_PIN D4 | |
#elif defined(ESP32) | |
#define BUTTON_PIN 34 | |
#elif (ARDUINO_ARCH_RP2040) | |
#define BUTTON_PIN 7 | |
#endif | |
int button_state; | |
int lastbutton_state = LOW; | |
unsigned long last_debounce_time = 0; | |
unsigned long debounce_delay = 50; // increase if needed. | |
SinricProDoorbell& myDoorbell = SinricPro[DOORBELL_ID]; | |
void handleButtonPress() { | |
int reading = digitalRead(BUTTON_PIN); | |
if (reading != lastbutton_state) { | |
last_debounce_time = millis(); | |
} | |
if ((millis() - last_debounce_time) > debounce_delay) { | |
if (reading != button_state) { | |
button_state = reading; | |
if (button_state == HIGH) { | |
Serial.printf("[handleButtonPress()]: Button pressed!\r\n"); | |
bool success = myDoorbell.sendDoorbellEvent(); | |
if(!success) { | |
Serial.printf("[handleButtonPress()]: Something went wrong...could not send Event to server!\r\n"); | |
} | |
} | |
} | |
} | |
lastbutton_state = reading; | |
} | |
void setupWiFi() { | |
Serial.printf("\r\n[setupWiFi()]: Connecting"); | |
#if defined(ESP8266) | |
WiFi.setSleepMode(WIFI_NONE_SLEEP); | |
WiFi.setAutoReconnect(true); | |
#elif defined(ESP32) | |
WiFi.setSleep(false); | |
WiFi.setAutoReconnect(true); | |
#endif | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
IPAddress localIP = WiFi.localIP(); | |
Serial.printf("connected!\r\n[setupWiFi()]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); | |
} | |
void setupSinricPro() { | |
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); | |
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setupButton() { | |
pinMode(BUTTON_PIN, INPUT); | |
} | |
void setup() { | |
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
setupButton(); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
handleButtonPress(); | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment