HUGE IMPROVEMENTS since the last iteration!!!
I’ve replaced the WemoManager library that I was using with the Espalexa library, which solves several issues:
- It’s simpler to implement. One library. One callback.
- It resolves the issue that many were having with updated Echo devices not discovering correctly
- … and drumroll… It now allows LEVELS! You can specify any level from 0-100%.
For anyone who wants to dim a lamp, open the blinds halfway, or change the volume the stereo (@Pavlo @jon), it’s now possible. The Espalexa library takes advantage of Hue bulb emulation to expose a “brightness” parameter. That level, however you express it to Alexa, is converted to an unsigned integer 0-255 for us to use however we see fit.
Here’s the code that I used to test the functionality:
/* Wemo / Hue emulation for ESP8266 control with Alexa, Blynk and IFTTT.
*
* https://github.com/tzapu/WiFiManager
* https://github.com/Aircoookie/Espalexa
* https://www.blynk.cc/
*
* In order to control multiple devices with a single Blynk dashboard, each ESP8266
* should be programmed with a unique virtual pin corresponding to a Blynk
* dashboard widget (switch, slider, step).
*
* For IFTTT control, use the Maker Channel with the following settings:
* URL: http://blynk-cloud.com:8080/YOUR_TOKEN/V1 Substitute your own token and vitual pin
* Method: PUT
* Content type: application/json
* Body: {"1"] Use 0 for OFF, 1 for ON. For custom levels use 0-255.
*/
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <Espalexa.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
// Blynk token and virtual pin number
#define VPIN V10 //Use a unique virtual pin for each device using the same Blynk token / dashboard
char auth[] = "TOKEN"; //Get token from Blynk
const int OutputPin = D1; //Relay switching pin. Relay is pin 12 on the SonOff
Espalexa espalexa;
void setup(){
Serial.begin(115200);
WiFiManager wifi;
wifi.autoConnect("MyDevice"); // Connect to wifi
espalexa.addDevice("Switch1", UpdateSwitch1); //Parameters: (device name, callback function).
espalexa.begin();
pinMode(OutputPin, OUTPUT);
digitalWrite(OutputPin, LOW);
Blynk.config(auth);
ArduinoOTA.begin();
}
void loop()
{
espalexa.loop();
Blynk.run();
ArduinoOTA.handle();
}
//------------ Callback functions. Level can be set from 0-255. -------------
void UpdateSwitch1(uint8_t level) { // Espalexa callback
SetNewLevel(&level);
}
BLYNK_WRITE(VPIN){ // Blynk & IFTTT callback
uint8_t level = param.asInt();
SetNewLevel(&level);
}
void SetNewLevel(uint8_t * pLevel){
Serial.print("New level= ");
Serial.println(*pLevel);
if (*pLevel) {
digitalWrite(OutputPin, HIGH); // Use analog.Writes for PWM control
}
else {
digitalWrite(OutputPin, LOW);
}
}
Enjoy!