Alexa voice controlled lamp (June 2018)

IOT switch for home automation DIY Using ESP8266 and Wifi /IOT /Amazon Alexa voice control of loads. A circuit with triac BT139, opto isolator MOC3021, other accessories and program code for switching ON/OFF ac loads from 110v 60HZ to 220v 50HZ.  It also has the source code on Arduino editor for the user to modify as per his needs. As regards using the Arduino,the FTDI (USB to Serial) for loading the program many articles are available on the net so I don’t feel like repeating the same. Watch my Video

All that one has to do is to copy the modified code below to Arduino editor ,(provide appropriate header files,select ESP as board which are all well explained in many other article by others)

suggested link

The source code.

Just replace the WiFi name in place of Enter here your WiFi SSID and replace your wifi password in place of Enter here your WiFi Password  both inside the double quote.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>

void prepareIds();
boolean connectWifi();
boolean connectUDP();
void startHttpServer();
void turnOnRelay();
void turnOffRelay();
/*********************************/
/*********************************/
/*********************************/
// You only need to modify following settings to get it to work worth Alexa and your network
const char* ssid = “Enter here your WiFi SSID”;
const char* password = “Enter here your WiFi Password”;
String device_name= “Light”;// you can change the device name from “Light” to anything
// local port to listen on
/*********************************/
/*********************************/
/*********************************/

unsigned int localPort = 1900; // local port to listen on

WiFiUDP UDP;
boolean udpConnected = false;
IPAddress ipMulti(239, 255, 255, 250);
unsigned int portMulti = 1900; // local port to listen on

ESP8266WebServer HTTP(80);

boolean wifiConnected = false;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

String serial;
String persistent_uuid;

const int relayPin = 2;

boolean cannotConnectToWifi = false;

void setup() {
Serial.begin(115200);

// Setup Relay
pinMode(relayPin, OUTPUT);

prepareIds();

// Initialise wifi connection
wifiConnected = connectWifi();

// only proceed if wifi connection successful
if(wifiConnected){
udpConnected = connectUDP();

if (udpConnected){
// initialise pins if needed
startHttpServer();
}
}
}

void loop() {

HTTP.handleClient();
delay(1);

// if there’s data available, read a packet
// check if the WiFi and UDP connections were successful
if(wifiConnected){
if(udpConnected){
// if there’s data available, read a packet
int packetSize = UDP.parsePacket();

if(packetSize) {
Serial.println(“”);
Serial.print(“Received packet of size “);
Serial.println(packetSize);
Serial.print(“From “);
IPAddress remote = UDP.remoteIP();

for (int i =0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(“.”);
}
}

Serial.print(“, port “);
Serial.println(UDP.remotePort());

int len = UDP.read(packetBuffer, 255);

if (len > 0) {
packetBuffer[len] = 0;
}

String request = packetBuffer;
//Serial.println(“Request:”);
//Serial.println(request);

if(request.indexOf(‘M-SEARCH’) > 0) {
if(request.indexOf(“urn:Belkin:device:**”) > 0) {
Serial.println(“Responding to search request …”);
respondToSearch();
}
}
}

delay(10);
}
} else {
// Turn on/off to indicate cannot connect ..
}
}

void prepareIds() {
uint32_t chipId = ESP.getChipId();
char uuid[64];
sprintf_P(uuid, PSTR(“38323636-4558-4dda-9188-cda0e6%02x%02x%02x”),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff);

serial = String(uuid);
persistent_uuid = “Socket-1_0-” + serial;
//device_name = “box”;
}

void respondToSearch() {
Serial.println(“”);
Serial.print(“Sending response to “);
Serial.println(UDP.remoteIP());
Serial.print(“Port : “);
Serial.println(UDP.remotePort());

IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, “%d.%d.%d.%d”, localIP[0], localIP[1], localIP[2], localIP[3]);

String response =
“HTTP/1.1 200 OKrn”
“CACHE-CONTROL: max-age=86400rn”
“DATE: Fri, 15 Apr 2016 04:56:29 GMTrn”
“EXT:rn”
“LOCATION: http://” + String(s) + “:80/setup.xmlrn”
“OPT: “http://schemas.upnp.org/upnp/1/0/”; ns=01rn”
“01-NLS: b9200ebb-736d-4b93-bf03-835149d13983rn”
“SERVER: Unspecified, UPnP/1.0, Unspecifiedrn”
“ST: urn:Belkin:device:**rn”
“USN: uuid:” + persistent_uuid + “::urn:Belkin:device:**rn”
“X-User-Agent: redsonicrnrn”;

UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(response.c_str());
UDP.endPacket();

Serial.println(“Response sent !”);
}

void startHttpServer() {
HTTP.on(“/index.html”, HTTP_GET, [](){
Serial.println(“Got Request index.html …n”);
HTTP.send(200, “text/plain”, “Hello World!”);
});

HTTP.on(“/upnp/control/basicevent1”, HTTP_POST, []() {
Serial.println(“########## Responding to /upnp/control/basicevent1 … ##########”);

//for (int x=0; x <= HTTP.args(); x++) {
// Serial.println(HTTP.arg(x));
//}

String request = HTTP.arg(0);
Serial.print(“request:”);
Serial.println(request);

if(request.indexOf(“<BinaryState>1</BinaryState>”) > 0) {
Serial.println(“Got Turn on request”);
turnOnRelay();
}

if(request.indexOf(“<BinaryState>0</BinaryState>”) > 0) {
Serial.println(“Got Turn off request”);
turnOffRelay();
}

HTTP.send(200, “text/plain”, “”);
});

HTTP.on(“/eventservice.xml”, HTTP_GET, [](){
Serial.println(” ########## Responding to eventservice.xml … ########n”);
String eventservice_xml = “<?scpd xmlns=”urn:Belkin:service-1-0″?>”
“<actionList>”
“<action>”
“<name>SetBinaryState</name>”
“<argumentList>”
“<argument>”
“<retval/>”
“<name>BinaryState</name>”
“<relatedStateVariable>BinaryState</relatedStateVariable>”
“<direction>in</direction>”
“</argument>”
“</argumentList>”
“<serviceStateTable>”
“<stateVariable sendEvents=”yes”>”
“<name>BinaryState</name>”
“<dataType>Boolean</dataType>”
“<defaultValue>0</defaultValue>”
“</stateVariable>”
“<stateVariable sendEvents=”yes”>”
“<name>level</name>”
“<dataType>string</dataType>”
“<defaultValue>0</defaultValue>”
“</stateVariable>”
“</serviceStateTable>”
“</action>”
“</scpd>rn”
“rn”;

HTTP.send(200, “text/plain”, eventservice_xml.c_str());
});

HTTP.on(“/setup.xml”, HTTP_GET, [](){
Serial.println(” ########## Responding to setup.xml … ########n”);

IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, “%d.%d.%d.%d”, localIP[0], localIP[1], localIP[2], localIP[3]);

String setup_xml = “<?xml version=”1.0″?>”
“<root>”
“<device>”
“<deviceType>urn:Belkin:device:controllee:1</deviceType>”
“<friendlyName>”+ device_name +”</friendlyName>”
“<manufacturer>Belkin International Inc.</manufacturer>”
“<modelName>Emulated Socket</modelName>”
“<modelNumber>3.1415</modelNumber>”
“<UDN>uuid:”+ persistent_uuid +”</UDN>”
“<serialNumber>221517K0101769</serialNumber>”
“<binaryState>0</binaryState>”
“<serviceList>”
“<service>”
“<serviceType>urn:Belkin:service:basicevent:1</serviceType>”
“<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>”
“<controlURL>/upnp/control/basicevent1</controlURL>”
“<eventSubURL>/upnp/event/basicevent1</eventSubURL>”
“<SCPDURL>/eventservice.xml</SCPDURL>”
“</service>”
“</serviceList>”
“</device>”
“</root>rn”
“rn”;

HTTP.send(200, “text/xml”, setup_xml.c_str());

Serial.print(“Sending :”);
Serial.println(setup_xml);
});

HTTP.begin();
Serial.println(“HTTP Server started ..”);
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
boolean state = true;
int i = 0;

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println(“”);
Serial.println(“Connecting to WiFi”);

// Wait for connection
Serial.print(“Connecting …”);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
if (i > 10){
state = false;
break;
}
i++;
}

if (state){
Serial.println(“”);
Serial.print(“Connected to “);
Serial.println(ssid);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
}
else {
Serial.println(“”);
Serial.println(“Connection failed.”);
}

return state;
}

boolean connectUDP(){
boolean state = false;

Serial.println(“”);
Serial.println(“Connecting to UDP”);

if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
Serial.println(“Connection successful”);
state = true;
}
else{
Serial.println(“Connection failed”);
}

return state;
}

void turnOnRelay() {
digitalWrite(relayPin, HIGH ); // turn on relay with voltage HIGH
}

void turnOffRelay() {
digitalWrite(relayPin,LOW ); // turn off relay with voltage LOW
}

Scroll to Top