In my house, Amazon echo is located in every room, and the lighting of living room is controlled by Alexa.
Tried to use the new enebulls I wrote earlier, and I was able to connect the lighting and lighting of the Amazon Echo and mbed, and I was working on the web with node red, but I thought it would be possible to complete it on the local net, so I checked the device for the ESP series Because you found a library that can be found on the exa enabled device, try Atom Lite.
The ATM Lite setup method is similar to atom matrix.
The grove relay is connected so that lighting can be turned on / off. (the program also has the LED Lite on / off, so you won't have a relay if you try.)
Adding LibrariesThe library found is fauxmoesp.You can install it in the library manager of arduinoide.
I also use the library called asynctcp in fauxmoesp. Download and install from gitub in ZIP format.
ProgramI made the program with reference to fauxmoesp.
Since foxmoesp has been configured by setting the device name and callback, it can be easily applied.
Since the callback function is light and commented, we change the variable in the callback function to detect and process it in the loop function.
You can turn on / off switch on Atom Lite.
#include "M5Atom.h"
#include <WiFi.h>
#include "fauxmoESP.h"
const int RelayPin = 32;
const char DEVICE_NAME[] = "light";
typedef enum {
NO_EVENT = 0,
LIGHT_ON,
LIGHT_OFF
} EVENT;
fauxmoESP fauxmo;
// Wi-Fi SSID
const char* ssid = "ssid";
// WiFi password
const char* password = "pass";
volatile EVENT event = NO_EVENT;
// Callback function is called an event coming from Alexa to the registered de//vice
void AlexaCallback(unsigned char device_id, const char *device_name, bool state, unsigned char value) {
Serial.printf("Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if (strcmp(device_name, DEVICE_NAME) == 0) {
if(state) {
event = LIGHT_ON;
} else {
event = LIGHT_OFF;
}
}
}
// Control ON / OFF
void LightControl(bool value) {
if(value){
Serial.println("ON");
M5.dis.drawpix(0, 0xf00000);
digitalWrite(RelayPin, HIGH);
} else {
Serial.println("OFF");
M5.dis.drawpix(0, 0x000000);
digitalWrite(RelayPin, LOW);
}
}
void setup() {
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
M5.begin(true, false, true);
delay(50);
M5.dis.drawpix(0, 0x000000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("WiFi connected\r\nIP address: ");
Serial.println(WiFi.localIP());
fauxmo.createServer(true);
fauxmo.setPort(80);
fauxmo.enable(true);
// register device
fauxmo.addDevice(DEVICE_NAME);
// Register callback function
fauxmo.onSetState(AlexaCallback);
}
void loop() {
static bool state =false;
if (M5.Btn.wasPressed()) {
if(state) {
event = LIGHT_OFF;
} else {
event = LIGHT_ON;
}
}
switch (event) {
case LIGHT_ON:
LightControl(true);
state = true;
event = NO_EVENT;
break;
case LIGHT_OFF:
LightControl(false);
state = false;
event = NO_EVENT;
break;
default:
break;
}
fauxmo.handle();
delay(50);
M5.update();
}
ResultIn the Amazon echo or sumaho Alexa app, the device will be detected if you are looking for a device. If you find a new device, it's a success.
Turn the light on or off the light of "light light" or "erase light" to Amazon echo or sumaho's Alexa app.
jksoft
Comments