From 51c2aca1474ae0920924c1ea2976d6476cab3ecf Mon Sep 17 00:00:00 2001 From: arne Date: Sat, 4 Jan 2025 15:30:25 +0100 Subject: [PATCH] feat: add example code using WifiManager and esp_alexa --- esp_alexa/esp_alexa.ino | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 esp_alexa/esp_alexa.ino diff --git a/esp_alexa/esp_alexa.ino b/esp_alexa/esp_alexa.ino new file mode 100644 index 0000000..358f24f --- /dev/null +++ b/esp_alexa/esp_alexa.ino @@ -0,0 +1,58 @@ +#include +#include // https://github.com/tzapu/WiFiManager +#include + +// Create an instance of Espalexa +Espalexa espalexa; + +// Pin definitions for the lights +const int lightPin1 = LED_BUILTIN; + +// State variables for the lights +bool lightState1 = false; + +// Callback functions for Alexa commands +void light1Changed(uint8_t brightness) { + lightState1 = brightness > 0; + digitalWrite(lightPin1, lightState1 ? HIGH : LOW); + Serial.println("Light 1 State: " + String(lightState1)); +} + +void setup() { + // Start the serial communication + Serial.begin(115200); + delay(1000); + + // Set up the pins as outputs + pinMode(lightPin1, OUTPUT); + + // Create an instance of WiFiManager + WiFiManager wifiManager; + + // Uncomment the following line to reset saved WiFi credentials + // wifiManager.resetSettings(); + + // Automatically connect to WiFi + // If it fails, it will start an access point with the specified name + // and wait for you to connect and configure the credentials + if (!wifiManager.autoConnect("AutoConnectAP")) { + Serial.println("Failed to connect and hit timeout"); + // Reset and try again, or go to deep sleep + ESP.reset(); + delay(1000); + } + + // If you get here, you have connected to the WiFi + Serial.println("Connected to WiFi!"); + + // Add devices to Espalexa + espalexa.addDevice("Licht", light1Changed); + + // Start Espalexa + espalexa.begin(); +} + +void loop() { + // Handle Alexa requests + espalexa.loop(); +} \ No newline at end of file