Add support for BRIGHTNESS_CONTROLLER

This commit is contained in:
chaos511 2024-12-18 20:51:35 -06:00
parent 85d59985d2
commit 7f5357dadf
3 changed files with 116 additions and 3 deletions

View file

@ -0,0 +1,110 @@
#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);
}
}

View file

@ -33,7 +33,7 @@ First create an instance of the alexa client and initilize it with your MQTT Cre
Alex2ESP alexClient; Alex2ESP alexClient;
``` ```
Credentials can be found at https://alex2mqtt.stormysdream.club/ after logging in with amazin Credentials can be found at https://alex2mqtt.stormysdream.club/ after logging in with amazon.
```cpp ```cpp
alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC); alexClient.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC);
``` ```
@ -97,7 +97,7 @@ toggleController->addActionMapping(openMapping);
| Alexa Interface Type | Status | | Alexa Interface Type | Status |
|-----------------------------------------------------|-------------| |-----------------------------------------------------|-------------|
| AlexaInterfaceType::AUTOMATION_MANAGEMENT | Supported* | | AlexaInterfaceType::AUTOMATION_MANAGEMENT | Supported* |
| AlexaInterfaceType::BRIGHTNESS_CONTROLLER | Supported* | | AlexaInterfaceType::BRIGHTNESS_CONTROLLER | Fully Supported |
| AlexaInterfaceType::CHANNEL_CONTROLLER | Supported* | | AlexaInterfaceType::CHANNEL_CONTROLLER | Supported* |
| AlexaInterfaceType::COLOR_CONTROLLER | Supported* | | AlexaInterfaceType::COLOR_CONTROLLER | Supported* |
| AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER | Supported* | | AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER | Supported* |

View file

@ -65,7 +65,10 @@ public:
String value = (powerController == PowerController::ON) ? "ON" : "OFF"; String value = (powerController == PowerController::ON) ? "ON" : "OFF";
return AddProperty(AlexaInterfaceType::POWER_CONTROLLER, "powerState", value, uncertaintyInMs); return AddProperty(AlexaInterfaceType::POWER_CONTROLLER, "powerState", value, uncertaintyInMs);
} }
AlexaStatusMessage &AddBrightnessControllerProp(unsigned int brightness, unsigned int uncertaintyInMs = 0)
{
return AddProperty(AlexaInterfaceType::BRIGHTNESS_CONTROLLER, "brightness", brightness, uncertaintyInMs);
}
AlexaStatusMessage &AddToggleControllerProp(PowerController powerController,String instanceName, unsigned int uncertaintyInMs = 0) AlexaStatusMessage &AddToggleControllerProp(PowerController powerController,String instanceName, unsigned int uncertaintyInMs = 0)
{ {
String value = (powerController == PowerController::ON) ? "ON" : "OFF"; String value = (powerController == PowerController::ON) ? "ON" : "OFF";