#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
// prototypes
boolean connectWifi();
//on/off callbacks
void officeLightsOn();
void officeLightsOff();
void BoxOn();
void BoxOff();
// Change this before you flash
//#######################################
const char* ssid = "ssid_here"; //enter your access point/wifi router name
const char* password = "password"; //enter router password
// change gpio pins as you need it.
//I am using ESP8266 EPS-12E GPIO16 and GPIO14
const int outputPin = 12;
//#######################################
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *box = NULL;
void setup()
{
Serial.begin(115200);
pinMode(outputPin, OUTPUT);
// Initialise wifi connection
wifiConnected = connectWifi();
if(wifiConnected){
upnpBroadcastResponder.beginUdpMulticast();
// Define your switches here. Max 14
// Format: Alexa invocation name, local port no, on callback, off callback
box = new Switch("box", 81, BoxOn, BoxOff);
Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*box);
}
}
void loop()
{
if(wifiConnected){
upnpBroadcastResponder.serverLoop();
box->serverLoop();
}
}
void BoxOn() {
Serial.print("Switch 2 turn on ...");
digitalWrite(outputPin, HIGH);
}
void BoxOff() {
Serial.print("Switch 2 turn off ...");
digitalWrite(outputPin, LOW);
}
// 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;
}
Comments