From 8a1d862d56f60a54c2ab6850dddf587c80f9a9e8 Mon Sep 17 00:00:00 2001 From: chaos511 <43281665+chaos511@users.noreply.github.com> Date: Wed, 18 Dec 2024 23:46:38 -0600 Subject: [PATCH] Add support for TEMPERATURE_SENSOR --- examples/tempSensor.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ readme.md | 31 ++++++++++++------- src/AlexaInterface.h | 4 ++- src/AlexaStatusMessage.h | 17 ++++++++++ src/AlexaUtils.cpp | 2 +- 5 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 examples/tempSensor.cpp diff --git a/examples/tempSensor.cpp b/examples/tempSensor.cpp new file mode 100644 index 0000000..067cc8f --- /dev/null +++ b/examples/tempSensor.cpp @@ -0,0 +1,67 @@ +#include + +#ifdef ESP32 +#include +#else +#include +#endif + +#include + +// 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(); +} diff --git a/readme.md b/readme.md index 4cfdcf3..583a727 100644 --- a/readme.md +++ b/readme.md @@ -1,22 +1,30 @@ # Alex2ESP -Alex2ESP is a lightweight C++ library for integrating ESP8266 and ESP32 microcontrollers with Amazon Alexa smart home APIs. The library simplifies the process of creating Alexa-compatible devices using MQTT. +Alex2ESP is a lightweight Arduino/PlatformIO library for integrating ESP8266 and ESP32 microcontrollers with Amazon Alexa smart home APIs. The library simplifies the process of creating Alexa-compatible devices using MQTT. -For more details on how to configure Alex2ESP, visit [Alex2ESP Documentation](https://alex2mqtt.stormysdream.club/). +For more details on how to configure Alex2ESP, visit [Alex2MQTT Documentation](https://alex2mqtt.stormysdream.club/). --- + + ## Features -- Supports various Alexa smart home capabilities. -- Provides action mapping for advanced customization. -- Simple integration with Wi-Fi and MQTT. +- Supports "All" Alexa smart home capabilities. +- Provides action mapping for advanced directive customization including Open,Close,Raise,and Lower commands. +- Allows custom JSON injection in both discovery and state reporting to allow for unsupported devices. - Event-driven architecture for handling Alexa directives. +--- + +## Alternatives + + one of the most popular library for controlling esp like devices using alexa is [FauxmoESP]{https://github.com/vintlabs/fauxmoESP} + however FauxmoESP works by emulating a light bulb so it is limited in what commands it accepts. --- ## Quick Start ### Installation -Download the library and include it in your Arduino IDE project: +Download the library as zip or via PlatformIO and include it in your project: ```cpp #include ``` @@ -96,15 +104,17 @@ toggleController->addActionMapping(openMapping); ## Interface Types | Alexa Interface Type | Status | |-----------------------------------------------------|-------------| -| AlexaInterfaceType::AUTOMATION_MANAGEMENT | Supported* | +| AlexaInterfaceType::ENDPOINT_HEALTH | Fully Supported | +| AlexaInterfaceType::POWER_CONTROLLER | Fully Supported | | AlexaInterfaceType::BRIGHTNESS_CONTROLLER | Fully Supported | +| AlexaInterfaceType::TOGGLE_CONTROLLER | Fully Supported | +| AlexaInterfaceType::TEMPERATURE_SENSOR | Fully Supported | + +| AlexaInterfaceType::AUTOMATION_MANAGEMENT | Supported* | | AlexaInterfaceType::CHANNEL_CONTROLLER | Supported* | | AlexaInterfaceType::COLOR_CONTROLLER | Supported* | | AlexaInterfaceType::COLOR_TEMPERATURE_CONTROLLER | Supported* | | AlexaInterfaceType::CONTACT_SENSOR | Supported* | -| AlexaInterfaceType::POWER_CONTROLLER | Fully Supported | -| AlexaInterfaceType::ENDPOINT_HEALTH | Supported* | -| AlexaInterfaceType::TOGGLE_CONTROLLER | Fully Supported | | AlexaInterfaceType::APPLICATION_STATE_REPORTER | Supported* | | AlexaInterfaceType::AUDIO_PLAY_QUEUE | Supported* | | AlexaInterfaceType::AUTHORIZATION_CONTROLLER | Supported* | @@ -146,7 +156,6 @@ toggleController->addActionMapping(openMapping); | AlexaInterfaceType::SMART_VISION_SNAPSHOT_PROVIDER | Supported* | | AlexaInterfaceType::SPEAKER | Supported* | | AlexaInterfaceType::STEP_SPEAKER | Supported* | -| AlexaInterfaceType::TEMPERATURE_SENSOR | Supported* | | AlexaInterfaceType::THERMOSTAT_CONTROLLER | Supported* | | AlexaInterfaceType::THERMOSTAT_CONTROLLER_CONFIGURATION | Supported* | | AlexaInterfaceType::THERMOSTAT_CONTROLLER_HVAC_COMPONENTS | Supported* | diff --git a/src/AlexaInterface.h b/src/AlexaInterface.h index 2ed5c02..9e857ac 100644 --- a/src/AlexaInterface.h +++ b/src/AlexaInterface.h @@ -481,6 +481,9 @@ case AlexaInterfaceType::ENDPOINT_HEALTH: return {"connectivity"}; case AlexaInterfaceType::TOGGLE_CONTROLLER: return {"toggleState"}; +case AlexaInterfaceType::TEMPERATURE_SENSOR: + return {"temperature"}; + case AlexaInterfaceType::APPLICATION_STATE_REPORTER: case AlexaInterfaceType::AUDIO_PLAY_QUEUE: case AlexaInterfaceType::AUTHORIZATION_CONTROLLER: @@ -522,7 +525,6 @@ case AlexaInterfaceType::SMART_VISION_OBJECT_DETECTION_SENSOR: case AlexaInterfaceType::SMART_VISION_SNAPSHOT_PROVIDER: case AlexaInterfaceType::SPEAKER: case AlexaInterfaceType::STEP_SPEAKER: -case AlexaInterfaceType::TEMPERATURE_SENSOR: case AlexaInterfaceType::THERMOSTAT_CONTROLLER: case AlexaInterfaceType::THERMOSTAT_CONTROLLER_CONFIGURATION: case AlexaInterfaceType::THERMOSTAT_CONTROLLER_HVAC_COMPONENTS: diff --git a/src/AlexaStatusMessage.h b/src/AlexaStatusMessage.h index c10329b..d5a65cf 100644 --- a/src/AlexaStatusMessage.h +++ b/src/AlexaStatusMessage.h @@ -18,6 +18,11 @@ enum class PowerController ON, OFF }; +enum class TemperatureSensorScale +{ + CELSIUS, + FAHRENHEIT +}; class AlexaStatusMessage { @@ -65,6 +70,18 @@ public: String value = (powerController == PowerController::ON) ? "ON" : "OFF"; return AddProperty(AlexaInterfaceType::POWER_CONTROLLER, "powerState", value, uncertaintyInMs); } + + AlexaStatusMessage &AddTemperatureSensorProp(TemperatureSensorScale tempSensor,float value, unsigned int uncertaintyInMs = 0) + { + JsonDocument tempValue; + tempValue["scale"] = "CELSIUS"; + tempValue["value"] = value; + if((tempSensor == TemperatureSensorScale::FAHRENHEIT)){ + tempValue["value"] = (value - 32) * 5.0 / 9.0; + } + return AddProperty(AlexaInterfaceType::TEMPERATURE_SENSOR, "temperature", tempValue.as(), uncertaintyInMs); + } + AlexaStatusMessage &AddBrightnessControllerProp(unsigned int brightness, unsigned int uncertaintyInMs = 0) { return AddProperty(AlexaInterfaceType::BRIGHTNESS_CONTROLLER, "brightness", brightness, uncertaintyInMs); diff --git a/src/AlexaUtils.cpp b/src/AlexaUtils.cpp index 19dc748..fa8335f 100644 --- a/src/AlexaUtils.cpp +++ b/src/AlexaUtils.cpp @@ -1,6 +1,6 @@ #include "AlexaUtils.h" -#define Alex2ESP_DEBUG +// #define Alex2ESP_DEBUG uint8_t AlexaUtils::nextMessageId = 0;