// DimmableLight: a lamp that Alexa switches and dims (Alexa.PowerController, Alexa.BrightnessController). // // The lamp is the on-board LED, dimmed with PWM. SetBrightness sets a value of 0 to 100, AdjustBrightness changes // it by a difference; the result stays within 0 to 100. #include #include #include // Wi-Fi and the credentials of your Alex2MQTT account const char *WIFI_SSID = ""; const char *WIFI_PASSWORD = ""; const char *ALEXA_USERNAME = ""; const char *ALEXA_PASSWORD = ""; const char *ALEXA_ROOT_TOPIC = ""; // The on-board LED: GPIO2 on a Wemos D1 mini, lit when the pin is low #ifndef LED_BUILTIN #define LED_BUILTIN 2 #endif Alex2ESP alexa; bool lampOn = false; int brightness = 100; // percent // Joins the Wi-Fi network and returns after 30 s at the latest. Without a connection the sketch carries on: the // ESP8266 keeps trying, and alexa.loop() opens the MQTT session once Wi-Fi is up. void connectWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.printf("\n[WIFI] Connecting to \"%s\"\n", WIFI_SSID); unsigned long started = millis(); while (WiFi.status() != WL_CONNECTED && millis() - started < 30000) { delay(100); } if (WiFi.status() == WL_CONNECTED) { Serial.printf("[WIFI] Connected, IP address %s\n", WiFi.localIP().toString().c_str()); } else { Serial.printf("[WIFI] No connection after 30 s (status %d): check WIFI_SSID and WIFI_PASSWORD. Still trying.\n", WiFi.status()); } } // The pin is low for the part of the time the LED is lit void writeLamp() { analogWrite(LED_BUILTIN, lampOn ? 100 - brightness : 100); } // The state of the lamp: what the answer to a directive and the answer to ReportState carry void sendState(AlexaStatusMessage message) { message.addHealthProp(EndpointHealth::OK) .addPowerControllerProp(lampOn ? PowerController::ON : PowerController::OFF) .addBrightnessControllerProp(brightness) .send(); } void onDirective(AlexaDirective &directive) { if (directive.isReportState()) { sendState(directive.stateReport()); return; } if (directive.is("TurnOn") || directive.is("TurnOff")) { lampOn = directive.is("TurnOn"); } else if (directive.is("SetBrightness")) { brightness = constrain(directive.payload["brightness"] | brightness, 0, 100); } else if (directive.is("AdjustBrightness")) { brightness = constrain(brightness + (directive.payload["brightnessDelta"] | 0), 0, 100); } else { directive.error(AlexaErrorType::INVALID_DIRECTIVE, "This lamp switches and dims").send(); return; } writeLamp(); sendState(directive.response()); } void setup() { Serial.begin(74880); pinMode(LED_BUILTIN, OUTPUT); analogWriteRange(100); writeLamp(); connectWiFi(); // MQTT user name, MQTT password, root topic alexa.begin(ALEXA_USERNAME, ALEXA_PASSWORD, ALEXA_ROOT_TOPIC); // The name Alexa shows, and the id of the endpoint: every device of an account has its own AlexaDevice *lamp = alexa.getDevice("Dimmable Lamp", "esp-dimmable-light"); lamp->setDisplayCategory(DisplayCategory::LIGHT); lamp->addCapability(AlexaInterfaces::PowerController); lamp->addCapability(AlexaInterfaces::BrightnessController); lamp->addCapability(AlexaInterfaces::EndpointHealth); lamp->onDirective(onDirective); } void loop() { alexa.loop(); }