1.1.0: begin(username, password, rootTopic) - the library signature now matches every example, the readme and the website (as published since 2024 no sketch could authenticate: the 64-hex password became the MQTT username). Discovery answers go out over MQTT directly, every device, resumed from loop() under heap pressure within the backend's 5 s window, instead of the 5-slot HTTP queue that silently dropped the rest (which the backend's reconcile then deleted from Alexa). JSON aligned with Alexa / alex2node: event.payload present, ActionMapping payload an object, semantics only when a capability has mappings. Pointer-stable device/capability containers (std::deque), the MQTT payload is copied by length before parsing (no write past the buffer), fragmented directive ids are ignored (a Discover is answered on its first fragment), deprecated ArduinoJson 7 calls replaced, credential Serial prints removed, ESP32 include guards (untested; the ESP8266 is the target). Examples: WIFI_PASSWORD, LED_BUILTIN fallback, string+int print fixes. library.json + library.properties restored (1.1.0; AsyncMqttClient, ArduinoJson 7, ESP Async TCP), LICENSE (MIT), a real keywords.txt, .gitattributes text=auto eol=lf (tree normalised to LF). readme: Forgejo URLs, platformio.ini snippet, Library Manager install, begin() order, INTERIOR_BLIND for blinds, 1.1.0 changelog. All five examples compile warning-free for d1_mini (PlatformIO 6.2, espressif8266) on pve-B450.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
David 2026-09-28 04:58:38 +00:00
parent 1313f47856
commit 87caad25be
20 changed files with 3117 additions and 2782 deletions

View file

@ -1,93 +1,99 @@
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// Define constants for WiFi and Alexa Client configuration
const char* WIFI_SSID = "";
const char* ALEXA_USERNAME = "";
const char* ALEXA_PASSWORD = "";
const char* ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
// Initial state for the light (PowerController)
PowerController outputState = PowerController::OFF;
void setup() {
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("Test Lamp", "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
// Add a power controller capability to the device
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.send();
});
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
if (type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.send();
}
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state
digitalWrite(LED_BUILTIN, outputState != PowerController::ON);
}
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// The on-board LED: GPIO2 on a Wemos D1 mini (active-low). Defined here for boards whose variant leaves it out.
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
// Define constants for WiFi and Alexa Client configuration
const char* WIFI_SSID = "";
const char* WIFI_PASSWORD = "";
const char* ALEXA_USERNAME = "";
const char* ALEXA_PASSWORD = "";
const char* ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
// Initial state for the light (PowerController)
PowerController outputState = PowerController::OFF;
void setup() {
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client (MQTT username, MQTT password, root topic)
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("Test Lamp", "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
// Add a power controller capability to the device
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.send();
});
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
if (type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.send();
}
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state (the on-board LED is active-low)
digitalWrite(LED_BUILTIN, outputState != PowerController::ON);
}

View file

@ -1,127 +1,133 @@
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice *device1;
// Initial state for the blinds (PowerController)
PowerController outputState = PowerController::OFF;
void setup()
{
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
String deviceName="Bedroom Blinds";
// Register a new device with a unique ID and name
device1 = alexClient.getDevice(deviceName, "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
//a power controller will allow us to "turn on/off" the blinds (not really usefull but is possable)
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Add a toggle controller capability to the device
AlexaInterface* toggleController=device1->addCapability(AlexaInterfaceType::TOGGLE_CONTROLLER);
//toggle controllers are unique in that they require some additinal attrabutes by default we first need to set an instance name
toggleController->setInstance("ESP-01.Toggle");
//we also need to set a name for the interface, this is a sub device listed under the main device name
//to allow open/close commands as well as on/off commands we can use the same name as device name
toggleController->addFriendlyName(deviceName.c_str(),"en-US");
// Example of adding action mappings to map open/close to on/off
ActionMapping closeMapping({AlexaActions::Close}, "TurnOn");
ActionMapping openMapping({AlexaActions::Open}, "TurnOff");
// the full supported list of alexa action mappings can be found on
// https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-discovery-objects.html#action-mapping
//note, only 'generic controller interffaces support action mappings for a full list visit
//https://developer.amazon.com/en-US/docs/alexa/device-apis/generic-controllers.html
toggleController->addActionMapping(closeMapping);
toggleController->addActionMapping(openMapping);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument &directive, const AlexaInterfaceType &type)
{
Serial.println("Report state requested.");
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddToggleControllerProp(outputState,"ESP-01.Toggle")
.AddPowerControllerProp(outputState)
.send(); });
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument &directive, const AlexaInterfaceType &type)
{
if (type == AlexaInterfaceType::TOGGLE_CONTROLLER||type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddToggleControllerProp(outputState,"ESP-01.Toggle")
.AddPowerControllerProp(outputState)
.send();
} });
}
void loop()
{
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state
digitalWrite(LED_BUILTIN, outputState != PowerController::ON);
}
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// The on-board LED: GPIO2 on a Wemos D1 mini (active-low). Defined here for boards whose variant leaves it out.
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *WIFI_PASSWORD = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice *device1;
// Initial state for the blinds (PowerController)
PowerController outputState = PowerController::OFF;
void setup()
{
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client (MQTT username, MQTT password, root topic)
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
String deviceName="Bedroom Blinds";
// Register a new device with a unique ID and name
device1 = alexClient.getDevice(deviceName, "ESP-01");
// Set the device's display category to INTERIOR_BLIND
device1->setDisplayCategory(DisplayCategory::INTERIOR_BLIND);
//a power controller will allow us to "turn on/off" the blinds (not really usefull but is possable)
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Add a toggle controller capability to the device
AlexaInterface* toggleController=device1->addCapability(AlexaInterfaceType::TOGGLE_CONTROLLER);
//toggle controllers are unique in that they require some additinal attrabutes by default we first need to set an instance name
toggleController->setInstance("ESP-01.Toggle");
//we also need to set a name for the interface, this is a sub device listed under the main device name
//to allow open/close commands as well as on/off commands we can use the same name as device name
toggleController->addFriendlyName(deviceName.c_str(),"en-US");
// Example of adding action mappings to map open/close to on/off
ActionMapping closeMapping({AlexaActions::Close}, "TurnOn");
ActionMapping openMapping({AlexaActions::Open}, "TurnOff");
// the full supported list of alexa action mappings can be found on
// https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-discovery-objects.html#action-mapping
//note, only 'generic controller interffaces support action mappings for a full list visit
//https://developer.amazon.com/en-US/docs/alexa/device-apis/generic-controllers.html
toggleController->addActionMapping(closeMapping);
toggleController->addActionMapping(openMapping);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument &directive, const AlexaInterfaceType &type)
{
Serial.println("Report state requested.");
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddToggleControllerProp(outputState,"ESP-01.Toggle")
.AddPowerControllerProp(outputState)
.send(); });
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument &directive, const AlexaInterfaceType &type)
{
if (type == AlexaInterfaceType::TOGGLE_CONTROLLER||type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddToggleControllerProp(outputState,"ESP-01.Toggle")
.AddPowerControllerProp(outputState)
.send();
} });
}
void loop()
{
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state (the on-board LED is active-low)
digitalWrite(LED_BUILTIN, outputState != PowerController::ON);
}

View file

@ -1,110 +1,116 @@
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
// Initial state for the light (PowerController)
PowerController outputState = PowerController::OFF;
int brightness=100;
void setup() {
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("Test Lamp", "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
// Add a power controller capability to the device
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Add a brightness controller to the device to allow dimming
device1->addCapability(AlexaInterfaceType::BRIGHTNESS_CONTROLLER);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.send();
});
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
if (type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
}else if (type == AlexaInterfaceType::BRIGHTNESS_CONTROLLER) {
if(directive["header"]["name"]=="SetBrightness"){
brightness=directive["payload"]["brightness"];
Serial.println("setting brightness to: "+brightness);
outputState = PowerController::ON;
}
}else{
return;
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.send();
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state and brightness
if(outputState == PowerController::ON){
analogWrite(LED_BUILTIN,map(brightness,0,100,255,0));
}else{
digitalWrite(LED_BUILTIN, true);
}
}
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// The on-board LED: GPIO2 on a Wemos D1 mini (active-low). Defined here for boards whose variant leaves it out.
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *WIFI_PASSWORD = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
// Initial state for the light (PowerController)
PowerController outputState = PowerController::OFF;
int brightness=100;
void setup() {
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client (MQTT username, MQTT password, root topic)
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("Test Lamp", "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
// Add a power controller capability to the device
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Add a brightness controller to the device to allow dimming
device1->addCapability(AlexaInterfaceType::BRIGHTNESS_CONTROLLER);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.send();
});
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
if (type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
}else if (type == AlexaInterfaceType::BRIGHTNESS_CONTROLLER) {
if(directive["header"]["name"]=="SetBrightness"){
brightness=directive["payload"]["brightness"];
Serial.printf("setting brightness to: %d\n", brightness);
outputState = PowerController::ON;
}
}else{
return;
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.send();
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state and brightness (the on-board LED is active-low)
if(outputState == PowerController::ON){
analogWrite(LED_BUILTIN,map(brightness,0,100,255,0));
}else{
digitalWrite(LED_BUILTIN, true);
}
}

View file

@ -1,122 +1,128 @@
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
// Initial state for the light (PowerController)
PowerController outputState = PowerController::OFF;
int brightness=100;
int colorTemp=0;
void setup() {
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("Test Lamp ESP", "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
// Add a power controller capability to the device
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Add a brightness controller to the device to allow dimming
device1->addCapability(AlexaInterfaceType::BRIGHTNESS_CONTROLLER);
// Add a color temperature controller
device1->addCapability(AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.AddColorTemperatureControllerProp(colorTemp)
.send();
});
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
if (type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
}else if (type == AlexaInterfaceType::BRIGHTNESS_CONTROLLER) {
if(directive["header"]["name"]=="SetBrightness"){
brightness=directive["payload"]["brightness"];
Serial.println("setting brightness to: "+brightness);
outputState = PowerController::ON;
}
}else if (type == AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER) {
if(directive["header"]["name"]=="SetColorTemperature"){
colorTemp=directive["payload"]["colorTemperatureInKelvin"];
Serial.println("setting color temperature to: "+colorTemp);
outputState = PowerController::ON;
}
}else{
return;
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.AddColorTemperatureControllerProp(colorTemp)
.send();
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state and brightness
if(outputState == PowerController::ON){
analogWrite(LED_BUILTIN,map(brightness,0,100,255,0));
}else{
digitalWrite(LED_BUILTIN, true);
}
}
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// The on-board LED: GPIO2 on a Wemos D1 mini (active-low). Defined here for boards whose variant leaves it out.
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *WIFI_PASSWORD = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
// Initial state for the light (PowerController)
PowerController outputState = PowerController::OFF;
int brightness=100;
int colorTemp=0;
void setup() {
// Initialize the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client (MQTT username, MQTT password, root topic)
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("Test Lamp ESP", "ESP-01");
// Set the device's display category to LIGHT
device1->setDisplayCategory(DisplayCategory::LIGHT);
// Add a power controller capability to the device
device1->addCapability(AlexaInterfaceType::POWER_CONTROLLER);
// Add a brightness controller to the device to allow dimming
device1->addCapability(AlexaInterfaceType::BRIGHTNESS_CONTROLLER);
// Add a color temperature controller
device1->addCapability(AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.AddColorTemperatureControllerProp(colorTemp)
.send();
});
// Register event listener for "Event" directive to handle state changes
device1->registerEvent("Event", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
if (type == AlexaInterfaceType::POWER_CONTROLLER) {
if (directive["header"]["name"] == "TurnOn") {
outputState = PowerController::ON;
Serial.println("Turning ON");
} else if (directive["header"]["name"] == "TurnOff") {
outputState = PowerController::OFF;
Serial.println("Turning OFF");
}
}else if (type == AlexaInterfaceType::BRIGHTNESS_CONTROLLER) {
if(directive["header"]["name"]=="SetBrightness"){
brightness=directive["payload"]["brightness"];
Serial.printf("setting brightness to: %d\n", brightness);
outputState = PowerController::ON;
}
}else if (type == AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER) {
if(directive["header"]["name"]=="SetColorTemperature"){
colorTemp=directive["payload"]["colorTemperatureInKelvin"];
Serial.printf("setting color temperature to: %d\n", colorTemp);
outputState = PowerController::ON;
}
}else{
return;
}
// Send the updated state after change
device1->buildStatusMessage(directive["header"]["correlationToken"], true)
.AddHealthProp(EndpointHealth::OK)
.AddPowerControllerProp(outputState)
.AddBrightnessControllerProp(brightness)
.AddColorTemperatureControllerProp(colorTemp)
.send();
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
// Update the state of the LED based on the power controller state and brightness (the on-board LED is active-low)
if(outputState == PowerController::ON){
analogWrite(LED_BUILTIN,map(brightness,0,100,255,0));
}else{
digitalWrite(LED_BUILTIN, true);
}
}

View file

@ -1,67 +1,68 @@
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
void setup() {
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("ESP Temp", "ESP-01");
// Set the device's display category to TEMPERATURE_SENSOR
device1->setDisplayCategory(DisplayCategory::TEMPERATURE_SENSOR);
// Add a temperature sensor capability to the device
device1->addCapability(AlexaInterfaceType::TEMPERATURE_SENSOR);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddTemperatureSensorProp(TemperatureSensorScale::FAHRENHEIT,69)
.send();
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
}
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Alex2ESP.h>
// Define constants for WiFi and Alexa Client configuration
const char *WIFI_SSID = "";
const char *WIFI_PASSWORD = "";
const char *ALEXA_USERNAME = "";
const char *ALEXA_PASSWORD = "";
const char *ALEXA_ROOT_TOPIC = "";
// Create the Alexa client object
Alex2ESP alexClient;
AlexaDevice* device1;
void setup() {
// Initialize serial communication for debugging
Serial.begin(74880);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("[WIFI] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.printf("[WIFI] Connected to SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
// Initialize the Alexa client (MQTT username, MQTT password, root topic)
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
// Register a new device with a unique ID and name
device1 = alexClient.getDevice("ESP Temp", "ESP-01");
// Set the device's display category to TEMPERATURE_SENSOR
device1->setDisplayCategory(DisplayCategory::TEMPERATURE_SENSOR);
// Add a temperature sensor capability to the device
device1->addCapability(AlexaInterfaceType::TEMPERATURE_SENSOR);
// Register event listener for "ReportState" directive
device1->registerEvent("ReportState", [](const JsonDocument& directive, const AlexaInterfaceType& type) {
// Build and send status report
device1->buildStatusMessage(directive["header"]["correlationToken"])
.AddHealthProp(EndpointHealth::OK)
.AddTemperatureSensorProp(TemperatureSensorScale::FAHRENHEIT,69)
.send();
});
}
void loop() {
// Process Alexa client communication
alexClient.loop();
}