There is a lot going on I know.... but here they are:
//http://firstswitch use this to access server on local network if don't use ip address
#include <ESP8266WebServer.h> // creates web server
#include <NTPClient.h> // gets time from online
#include <WiFiUdp.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> // allows multiple AP the possibility to connect if the one does not
#include <ArduinoOTA.h> // allows OTA uploads and host name drop down menu in port
#include <EEPROM.h> // memory storage for when power is removed or other things
#include <SDK_Version_Common.h> // needed to send email out
#include <ESP8266mDNS.h> // allows you to access server with web addres .local
#include <Espalexa.h>
volatile boolean deviceState = false; //boolean to determine if the output feeding relay is on (true) or off (false)
volatile boolean wifiNotConnected = false;
//int serialSwitch; //only needed for testing output from serial
const int debounceDelay = 100; // length of debounce time for switch
const byte switch1 = D1;
const byte output = D8;
const char* host = "3 way test"; // set this to what you want to show up in the port tab when doing OTA uploads
void firstLightChanged(uint8_t brightness); // callback function to the espAlexa library
const long utcOffsetInSeconds = -18000;//this sets the time zone
byte Hour;
byte Minute;
byte Second;
byte hourToOpen = 12;
byte minuteToOpen = 8;
byte hourToClose = 18;
byte minuteToClose = 8;
volatile boolean doorMovingUp = false;
volatile boolean doorMovingDown = false;
volatile boolean emergencyFlag;
volatile boolean problemEmailSent = false;
struct dataStruct
{
byte hourClosed;
byte minuteClosed;
byte hourOpened;
byte minuteOpened;
volatile boolean switchOn;
};
dataStruct theStruct;
long currentMillis;
long emergencyOffMillis = 10000;
int delayMillis = 10000;
Espalexa espalexa; // must have this as an instance maybe
EspalexaDevice* alexaState; //name used to be able to change alexas state when an outside source switches
ESP8266WiFiMulti wifiMulti; // for multiple wifi ssid's
ESP8266WebServer server(80);
WiFiUDP ntpUDP; // Define NTP Client to get time
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
void setup() {
Serial.begin(115200);
delay(100);
wifiMulti.addAP(""); // add Wi-Fi networks you want to connect to
wifiMulti.addAP("");
wifiMulti.addAP("");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
if (millis() >= 30000){ // if not connected within about 30 seconds break out of while loop
wifiNotConnected = true;
break;
}
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
ArduinoOTA.setHostname(host); // this is the line needed to set the name of the device under the "port" column when selecting for OTA uploads host is a variable set it at top
ArduinoOTA.begin(); //This is for OTA uploading sketch
if (!MDNS.begin("3 way test")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
if (millis() >= 40000){ // if not connected within about 40 seconds break out of while loop
break;
}
}
}
Serial.println("mDNS responder started");
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.on("/switch_on", handle_switch_on);
server.on("/switch_Off", handle_switch_Off);
server.begin();
Serial.println("HTTP server started");
server.onNotFound([](){
if (!espalexa.handleAlexaApiCall(server.uri(),server.arg(0)))
{
server.send(404, "text/plain", "Not found");
}
});
Serial.println("HTTP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
pinMode(switch1, INPUT);
pinMode(output, OUTPUT);
// attachInterrupt(digitalPinToInterrupt(switch1), isrSwitch, CHANGE);
//espalexa.addDevice("switch1", firstLightChanged); // name for alexa this will add a device, but you can't change state of alexa
// espalexa.addDevice(Device_1_Name, firstLightChanged);
alexaState = new EspalexaDevice("3 way test", firstLightChanged);// this "coop door" is name for alexa if you don't use global variable string like above Device_1_Name
espalexa.addDevice(alexaState);
espalexa.begin(&server); // need this if doing alexa and a server
digitalWrite(output, LOW); //Device is off on startup
deviceState = false; // set the state correct on startup
EEPROM.begin(512);
//defaultData();
checkDoorState();
}
void loop() {
currentMillis = millis();
ArduinoOTA.handle(); // This is for OTA uploading sketch
server.handleClient();
espalexa.loop(); // needed to constantly check for alex updates
MDNS.update();
timeClient.update();
Second = (timeClient.getSeconds());
Minute = (timeClient.getMinutes());
Hour = (timeClient.getHours());
if (Hour > 12) {
Hour = Hour - 12;
}
static unsigned long lastCheckWifi = 0;
if (millis() - lastCheckWifi >= 300000) //
{
lastCheckWifi = millis();
checkWifi();
}
static unsigned long lastCheck = 0;
if (millis() - lastCheck >= debounceDelay) //50 ms debounce
{
lastCheck = millis();
checkSwitch();
}
}
void checkWifi(){
if(wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
Serial.print("checkWifi () called");
reconnectWifi();
}
}
void reconnectWifi(){
Serial.print("ReconnectWifi () called");
if (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
wifiMulti.run();
Serial.println("should be delayed to run wifi");
delay(250);
//wifiNotConnected = true;
}
else{
// wifiNotConnected = false;
Serial.print("wifi reconnected!");
}
}
void checkSwitch(){
byte switchState;
static byte lastSwitchState;
switchState = digitalRead(switch1);
//check if this switch has changed state
if (switchState != lastSwitchState)
{
//update to the changed switch state and perform actions on change
lastSwitchState = switchState;
if (deviceState == false){ // if deviceState (light,fan,etc.) is false (off) turn relay on
relayOn();
}
else{
relayOff(); // if deviceState (light,fan,etc.) is true (on) turn relay off
}
}
}
void firstLightChanged(uint8_t brightness) { // This is the callback function to the espalexa library. If brigtness changes 255 or 0 relayON/relayOFF
if (brightness == 255) {
//Serial.print("switch ON");
Serial.println(brightness);
relayOn();
}
else
{
// Serial.println("switch OFF");
Serial.println(brightness);
relayOff();
}
}
void relayOn(){
digitalWrite(output, HIGH); // turn device on
deviceState = true; //change state of device boolean
Serial.println("turning on");
alexaState -> setValue(255); // this sets the status of Alexa "on" if the manual switch is toggled so that alexa will report the correct state on APP
}
void relayOff() {
digitalWrite(output, LOW);// turn device off
deviceState = false;// change state of device boolean
Serial.println("turning off");
alexaState -> setValue(0); // this sets the status of Alexa "off" if the manual switch is toggled so that alexa will report the correct state on APP
}
void handle_OnConnect() {
server.send(200, "text/html", SendHTML(Hour, Minute, Second, theStruct.hourOpened, theStruct.hourClosed, theStruct.minuteOpened, theStruct.minuteClosed, theStruct.switchOn));
}
void handle_switch_on() {
switchOn();
server.send(200, "text/html", SendHTML(Hour, Minute, Second, theStruct.hourOpened, theStruct.hourClosed, theStruct.minuteOpened, theStruct.minuteClosed, theStruct.switchOn));
}
void handle_switch_Off() {
switchOff();
server.send(200, "text/html", SendHTML(Hour, Minute, Second, theStruct.hourOpened, theStruct.hourClosed, theStruct.minuteOpened, theStruct.minuteClosed, theStruct.switchOn));
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
void switchOn() {
//digitalWrite(RelayUp, HIGH);
// doorMovingUp = true;
relayOn();
emergencyOffMillis = millis() + delayMillis;
theStruct.switchOn = true;
}
void switchOff() {
// digitalWrite (RelayDown, HIGH);
// doorMovingDown = true;
relayOff();
emergencyOffMillis = millis() + delayMillis;
theStruct.switchOn = false;
}
String SendHTML(byte Hour, byte Minute, byte Second, byte hourOpened, byte hourClosed, byte minuteOpened, byte minuteClosed, volatile boolean switchOn) {
String ptr = "<!DOCTYPE html> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>Switch Trial</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #3498db;}\n";
ptr += ".button-on:active {background-color: #2980b9;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "<script>\n";
ptr += "setInterval(loadDoc,1000);\n";
ptr += "function loadDoc() {\n";
ptr += "var xhttp = new XMLHttpRequest();\n";
ptr += "xhttp.onreadystatechange = function() {\n";
ptr += "if (this.readyState == 4 && this.status == 200) {\n";
ptr += "document.body.innerHTML =this.responseText}\n";
ptr += "};\n";
ptr += "xhttp.open(\"GET\", \"/\", true);\n";
ptr += "xhttp.send();\n";
ptr += "}\n";
ptr += "</script>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<div id=\"nick\">\n";
ptr += "<h1>Switch</h1>\n";
ptr += "<p>The time is: ";
ptr += Hour;
ptr += ":";
if (Minute < 10){
ptr += "0";
}
ptr += Minute;
ptr += "::";
ptr += Second;
if (switchOn == true)
{
ptr += "<p>Switch is on</p><a class=\"button button-off\" href=\"/switch_Off\">switch Off</a>\n";
}
if (switchOn == false)
{
ptr += "<p>Switch Off</p><a class=\"button button-on\" href=\"/switch_on\">switch on</a>\n";
}
ptr += "</div>\n";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
void checkDoorState()
{
EEPROM.get(20, theStruct);
}
/*void defaultData()
{
theStruct.hourClosed = 11;
theStruct.minuteClosed =59;
theStruct.hourOpened = 11;
theStruct.minuteOpened = 59;
theStruct.switchOn = true;
EEPROM.put(20, theStruct);
EEPROM.commit ();
}*/
Client:
//http://firstswitch use this to access server on local network if don't use ip address
#include <ESP8266WebServer.h> // creates web server
#include <NTPClient.h> // gets time from online
#include <WiFiUdp.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> // allows multiple AP the possibility to connect if the one does not
#include <ArduinoOTA.h> // allows OTA uploads and host name drop down menu in port
#include <EEPROM.h> // memory storage for when power is removed or other things
#include <SDK_Version_Common.h> // needed to send email out
#include <ESP8266mDNS.h> // allows you to access server with web addres .local
#include <Espalexa.h>
#include "ESP8266HTTPClient.h"
volatile boolean deviceState = false; //boolean to determine if the output feeding relay is on (true) or off (false)
volatile boolean wifiNotConnected = false;
volatile boolean toggle = false;
//int serialSwitch; //only needed for testing output from serial
const int debounceDelay = 100; // length of debounce time for switch
const byte switch1 = D4;
const byte output = D8;
const char* host = "Grady's Closet"; // set this to what you want to show up in the port tab when doing OTA uploads
void firstLightChanged(uint8_t brightness); // callback function to the espAlexa library
ESP8266WiFiMulti wifiMulti; // for multiple wifi ssid's
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
delay(100);
wifiMulti.addAP(""); // add Wi-Fi networks you want to connect to
wifiMulti.addAP("");
wifiMulti.addAP("");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
if (millis() >= 30000){ // if not connected within about 30 seconds break out of while loop
wifiNotConnected = true;
break;
}
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
ArduinoOTA.setHostname(host); // this is the line needed to set the name of the device under the "port" column when selecting for OTA uploads host is a variable set it at top
ArduinoOTA.begin(); //This is for OTA uploading sketch
if (!MDNS.begin("Grady's Closet")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
if (millis() >= 40000){ // if not connected within about 40 seconds break out of while loop
break;
}
}
}
Serial.println("mDNS responder started");
/*
server.on("/", handle_OnConnect); // these handle the different addresses that can be accessed by button clicks or typed in manual to go to a URL
server.onNotFound(handle_NotFound);
server.on("/open_Door", handle_open_Door);
server.on("/close_Door", handle_close_Door);
*/
// server.begin();
Serial.println("HTTP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
pinMode(switch1, INPUT_PULLUP);
pinMode(output, OUTPUT);
digitalWrite(output, LOW); //Device is off on startup
deviceState = false; // set the state correct on startup
}
void loop() {
ArduinoOTA.handle(); // This is for OTA uploading sketch
//server.handleClient();
MDNS.update();
static unsigned long lastCheckWifi = 0;
if (millis() - lastCheckWifi >= 300000) //
{
lastCheckWifi = millis();
checkWifi();
}
static unsigned long lastCheck = 0;
if (millis() - lastCheck >= 30000) //50 ms debounce
{
lastCheck = millis();
//checkSwitch();
lampsOn();
}
}
void checkWifi(){
if(wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
Serial.print("checkWifi () called");
reconnectWifi();
}
}
void reconnectWifi(){
Serial.print("ReconnectWifi () called");
if (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
wifiMulti.run();
Serial.println("should be delayed to run wifi");
delay(250);
//wifiNotConnected = true;
}
else{
// wifiNotConnected = false;
Serial.print("wifi reconnected!");
}
}
void checkSwitch(){
byte switchState;
static byte lastSwitchState;
switchState = digitalRead(switch1);
//check if this switch has changed state
if (switchState != lastSwitchState)
{
//update to the changed switch state and perform actions on change
lastSwitchState = switchState;
lampsOn(); // if deviceState (light,fan,etc.) is true (on) turn relay off
}
}
void lampsOn() {
const char* host2= "192.168.1.188/switch_on";
HTTPClient http;
WiFiClientSecure client;
client.setInsecure();
client.connect(host2, 80);
http.begin(client, host2);
Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
String payload;
Serial.print("Fetching From: ");
Serial.println(host2);
if (http.GET() == HTTP_CODE_OK)
payload = http.getString();
Serial.println(payload);
Serial.print("switching false");
}
Thanks