-
Notifications
You must be signed in to change notification settings - Fork 7
/
esp8266_smart_plug.ino
177 lines (129 loc) · 3.57 KB
/
esp8266_smart_plug.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#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
bool turnOnRelay();
bool turnOffRelay();
// Change this before you flash
const char* ssid = "your ssid here";
const char* password = "your ssid password here";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *outlet = NULL;
bool isturnOnRelay = false;
// For NodeMCU boards, use these constants
// const int relayPin = D6;
// const int switchPin = D3;
// const int ledPin = D7;
// For non-NodeMCU boards (Sonoff, Wemos), use these constants
const int relayPin = 12;
const int switchPin = 0;
const int ledPin = 13;
int switchState = 0;
boolean cannotConnectToWifi = false;
void setup()
{
Serial.begin(115200);
// Setup Relay
pinMode(relayPin, OUTPUT);
// Setup Switch
pinMode(switchPin, INPUT);
// Setup LED
pinMode(ledPin, OUTPUT);
//Default Relay state is on when unit is initially powered on
turnOffRelay();
switchState = 0;
// Initialise wifi connection
wifiConnected = connectWifi();
if(wifiConnected){
upnpBroadcastResponder.beginUdpMulticast();
// Define your switches here. Max 10
// Format: Alexa invocation name, local port no, on callback, off callback
outlet = new Switch("smart plug", 80, turnOnRelay, turnOffRelay);
Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*outlet);
}
}
void loop()
{
if (digitalRead(switchPin)){
delay(250);
if (!digitalRead(switchPin)){
switchState = !switchState;
if (switchState){
digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
digitalWrite(ledPin, LOW ); // turn on LED with voltage HIGH
switchState = 1;
}
else
{
digitalWrite(relayPin, LOW); // turn off relay with voltage LOW
digitalWrite(ledPin, HIGH); // turn off LED with voltage LOW
switchState = 0;
}
delay(500);
}
}
{
if(wifiConnected){
upnpBroadcastResponder.serverLoop();
outlet->serverLoop();
}
}
}
bool turnOnRelay() {
Serial.println("Switch 1 turn on ...");
isturnOnRelay = true;
digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
digitalWrite(ledPin, LOW ); // turn on LED with voltage HIGH
Serial.println("Turning on relay");
switchState = 1;
return isturnOnRelay;
}
bool turnOffRelay() {
Serial.println("Switch 1 turn off ...");
isturnOnRelay = false;
digitalWrite(relayPin, LOW); // turn off relay with voltage LOW
digitalWrite(ledPin, HIGH); // turn off LED with voltage LOW
Serial.println("Turning off relay");
switchState = 0;
return isturnOnRelay;
}
// 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;
}