Components and supplies
ESP8266 ESP-12E
Arduino Nano R3
Relay (generic)
Tools and machines
Soldering iron (generic)
Apps and platforms
Alexa Voice Service
Project description
Code
the code
c_cpp
Here you have to know which echo dot you are using this code is for a V2 echo dot
1#include <ESP8266WiFi.h> 2#include <ESP8266WebServer.h> 3#include <WiFiUdp.h> 4#include <functional> 5#include "switch.h" 6#include "UpnpBroadcastResponder.h" 7#include "CallbackFunction.h" 8 9// prototypes 10boolean connectWifi(); 11int zeit = 450;// 600 = 1min //60= 6sek/ 10 = 1sek 12int count = 0; 13//on/off callbacks 14bool LichtAn(); 15bool LichtAus(); 16//bool kitchenLightsOn(); 17//bool kitchenLightsOff(); 18 19// Change this before you flash 20const char* ssid = "TP-LINK_28CE"; 21const char* password = "20348939"; 22 23boolean wifiConnected = false; 24 25UpnpBroadcastResponder upnpBroadcastResponder; 26 27Switch *Licht = NULL; 28//Switch *kitchen = NULL; 29 30bool isOfficeLightsOn = false; 31//bool isKitchenLightstsOn = false; 32 33 34boolean connectWifi(){ 35 boolean state = true; 36 int i = 0; 37 WiFi.mode(WIFI_STA); 38 WiFi.setAutoReconnect(true); 39 WiFi.begin(ssid, password); 40 Serial.println(""); 41 Serial.println("Connecting to WiFi"); 42 43 // Wait for connection 44 Serial.print("Connecting ..."); 45 while (WiFi.status() != WL_CONNECTED) { 46 delay(500); 47 Serial.print("."); 48 if (i > 100){ 49 state = false; 50 break; 51 } 52 i++; 53 } 54 55 if (state){ 56 Serial.println(""); 57 Serial.print("Connected to "); 58 Serial.println(ssid); 59 Serial.print("IP address: "); 60 Serial.println(WiFi.localIP()); 61 } 62 else { 63 Serial.println(""); 64 Serial.println("Connection failed."); 65 } 66 67 return state; 68} 69void wait() 70{ 71 delay(100); 72 count++; 73} 74void setup() 75{ 76 pinMode(4,OUTPUT); 77 78 Serial.begin(9600); 79 Serial.println("Start"); 80 // Initialise wifi connection 81 wifiConnected = connectWifi(); 82 83 if(wifiConnected){ 84 upnpBroadcastResponder.beginUdpMulticast(); 85 86 // Define your switches here. Max 10 87 // Format: Alexa invocation name, local port no, on callback, off callback 88 Licht = new Switch("Licht", 80, LichtAn, LichtAus); 89 //kitchen = new Switch("kitchen lights", 81, kitchenLightsOn, kitchenLightsOff); 90 91 Serial.println("Adding switches upnp broadcast responder"); 92 upnpBroadcastResponder.addDevice(*Licht); 93 //upnpBroadcastResponder.addDevice(*kitchen); 94 } 95} 96 97void loop() 98{ 99 if(isOfficeLightsOn && count <zeit) //Nachlaufzeit hochlaufen lassen in 100ms Schritten bis zu 45Sekunden 100 { 101 102 wait(); 103 104 } 105 else // Wenn 45s erreicht abschalten oder ber "Alexa Licht Aus" 106 { 107 digitalWrite(4,0); 108 isOfficeLightsOn =LOW; 109 110 111 } 112 if( !isOfficeLightsOn) 113 { 114 count = 0; 115 } 116 117 if(wifiConnected){ 118 upnpBroadcastResponder.serverLoop(); 119 120 //kitchen->serverLoop(); 121 Licht->serverLoop(); 122 123 } 124 /* Evtl. wenn auto reconnect nicht funktioniert. 125 if(WiFi.status()!= WL_CONNECTED) 126 { 127 ESP.reset(); 128 }*/ 129 130} 131 132bool LichtAn() { 133 Serial.println("Switch 1 turn on ..."); 134 digitalWrite(4,1); 135 isOfficeLightsOn = true; 136 return isOfficeLightsOn; 137} 138 139bool LichtAus() { 140 Serial.println("Switch 1 turn off ..."); 141 digitalWrite(4,0); 142 isOfficeLightsOn = false; 143 return isOfficeLightsOn; 144} 145/* 146bool kitchenLightsOn() { 147 Serial.println("Switch 2 turn on ..."); 148 149 isKitchenLightstsOn = true; 150 return isKitchenLightstsOn; 151} 152 153bool kitchenLightsOff() { 154 Serial.println("Switch 2 turn off ..."); 155 156 isKitchenLightstsOn = false; 157 return isKitchenLightstsOn; 158} 159*/ 160// connect to wifi returns true if successful or false if not 161 162
the code
c_cpp
Here you have to know which echo dot you are using this code is for a V2 echo dot
1#include <ESP8266WiFi.h> 2#include <ESP8266WebServer.h> 3#include 4 <WiFiUdp.h> 5#include <functional> 6#include "switch.h" 7#include "UpnpBroadcastResponder.h" 8#include 9 "CallbackFunction.h" 10 11// prototypes 12boolean connectWifi(); 13int zeit 14 = 450;// 600 = 1min //60= 6sek/ 10 = 1sek 15int count = 0; 16//on/off callbacks 17 18bool LichtAn(); 19bool LichtAus(); 20//bool kitchenLightsOn(); 21//bool kitchenLightsOff(); 22 23// 24 Change this before you flash 25const char* ssid = "TP-LINK_28CE"; 26const char* 27 password = "20348939"; 28 29boolean wifiConnected = false; 30 31UpnpBroadcastResponder 32 upnpBroadcastResponder; 33 34Switch *Licht = NULL; 35//Switch *kitchen = NULL; 36 37bool 38 isOfficeLightsOn = false; 39//bool isKitchenLightstsOn = false; 40 41 42boolean 43 connectWifi(){ 44 boolean state = true; 45 int i = 0; 46 WiFi.mode(WIFI_STA); 47 48 WiFi.setAutoReconnect(true); 49 WiFi.begin(ssid, password); 50 Serial.println(""); 51 52 Serial.println("Connecting to WiFi"); 53 54 // Wait for connection 55 Serial.print("Connecting 56 ..."); 57 while (WiFi.status() != WL_CONNECTED) { 58 delay(500); 59 Serial.print("."); 60 61 if (i > 100){ 62 state = false; 63 break; 64 } 65 i++; 66 67 } 68 69 if (state){ 70 Serial.println(""); 71 Serial.print("Connected 72 to "); 73 Serial.println(ssid); 74 Serial.print("IP address: "); 75 76 Serial.println(WiFi.localIP()); 77 } 78 else { 79 Serial.println(""); 80 81 Serial.println("Connection failed."); 82 } 83 84 return state; 85} 86void 87 wait() 88{ 89 delay(100); 90 count++; 91} 92void setup() 93{ 94 pinMode(4,OUTPUT); 95 96 97 Serial.begin(9600); 98 Serial.println("Start"); 99 // Initialise 100 wifi connection 101 wifiConnected = connectWifi(); 102 103 if(wifiConnected){ 104 105 upnpBroadcastResponder.beginUdpMulticast(); 106 107 // Define your switches 108 here. Max 10 109 // Format: Alexa invocation name, local port no, on callback, 110 off callback 111 Licht = new Switch("Licht", 80, LichtAn, LichtAus); 112 //kitchen 113 = new Switch("kitchen lights", 81, kitchenLightsOn, kitchenLightsOff); 114 115 116 Serial.println("Adding switches upnp broadcast responder"); 117 upnpBroadcastResponder.addDevice(*Licht); 118 119 //upnpBroadcastResponder.addDevice(*kitchen); 120 } 121} 122 123void loop() 124{ 125 126 if(isOfficeLightsOn && count <zeit) //Nachlaufzeit hochlaufen lassen in 100ms 127 Schritten bis zu 45Sekunden 128 { 129 130 wait(); 131 132 } 133 else 134 // Wenn 45s erreicht abschalten oder ber "Alexa Licht 135 Aus" 136 { 137 digitalWrite(4,0); 138 isOfficeLightsOn =LOW; 139 140 141 142 } 143 if( !isOfficeLightsOn) 144 { 145 count = 0; 146 } 147 148 149 if(wifiConnected){ 150 upnpBroadcastResponder.serverLoop(); 151 152 //kitchen->serverLoop(); 153 154 Licht->serverLoop(); 155 156 } 157 /* Evtl. wenn auto reconnect 158 nicht funktioniert. 159 if(WiFi.status()!= WL_CONNECTED) 160 { 161 ESP.reset(); 162 163 }*/ 164 165} 166 167bool LichtAn() { 168 Serial.println("Switch 1 turn 169 on ..."); 170 digitalWrite(4,1); 171 isOfficeLightsOn = true; 172 return 173 isOfficeLightsOn; 174} 175 176bool LichtAus() { 177 Serial.println("Switch 1 178 turn off ..."); 179 digitalWrite(4,0); 180 isOfficeLightsOn = false; 181 182 return isOfficeLightsOn; 183} 184/* 185bool kitchenLightsOn() { 186 Serial.println("Switch 187 2 turn on ..."); 188 189 isKitchenLightstsOn = true; 190 return isKitchenLightstsOn; 191} 192 193bool 194 kitchenLightsOff() { 195 Serial.println("Switch 2 turn off ..."); 196 197 isKitchenLightstsOn 198 = false; 199 return isKitchenLightstsOn; 200} 201*/ 202// connect to wifi returns 203 true if successful or false if not 204 205
Downloadable files
Schematic
Schematic
Comments
Only logged in users can leave comments
FelixAdiy
0 Followers
•0 Projects
Table of contents
Intro
4
0