-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266.ino
244 lines (207 loc) · 5.93 KB
/
esp8266.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// prototypes
boolean connectWifi();
//on/off callbacks
void lightOneOn();
void lightOneOff();
void lightTwoOn();
void lightTwoOff();
void outletOneOn();
void outletOneOff();
void outletTwoOn();
void outletTwoOff();
// Change this before you flash
const char* ssid = "Skynet";
const char* password = "8039795700";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *lightOne = NULL;
Switch *lightTwo = NULL;
Switch *outletOne = NULL;
Switch *outletTwo = NULL;
// Set Relay Pins
int relayOne = 14;
int relayTwo = 15;
int relayThree = 03;
int relayFour = 01;
// Addr: 0x3F, 20 chars & 4 lines. Sometimes display boards use address 0x27
LiquidCrystal_I2C lcd(0x3F, 4, 20); //Frentally display, use 0x3F if not working try 0x27
void setup()
{
//Initalize LCD
lcd.init();
lcd.noBacklight();
lcd.backlight();
lcd.begin(20, 4);
//Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();
//Serial.print("WiFi Connected");
if (wifiConnected) {
upnpBroadcastResponder.beginUdpMulticast();
// Show WiFi status on LCD along with SSID of network
lcd.setCursor(0, 0);
lcd.print(" WiFi Connected ");
lcd.print(ssid);
// Define your switches here. Max 14
// Format: Alexa invocation name, local port no, on callback, off callback
lightOne = new Switch("Light One", 80, lightOneOn, lightOneOff);
lightTwo = new Switch("Light Two", 81, lightTwoOn, lightTwoOff);
outletOne = new Switch("Outlet One", 82, outletOneOn, outletOneOff);
outletTwo = new Switch("Outlet Two", 83, outletTwoOn, outletTwoOff);
//Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*lightOne);
upnpBroadcastResponder.addDevice(*lightTwo);
upnpBroadcastResponder.addDevice(*outletOne);
upnpBroadcastResponder.addDevice(*outletTwo);
//Set relay pins to outputs
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(03, OUTPUT);
pinMode(01, OUTPUT);
//Create Polling Message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Polling Status ");
lcd.setCursor(0, 2);
lcd.print(" Of Smart Devices ");
delay(2000);
//Set each relay pin to HIGH this display status messagefor each relay
digitalWrite(relayOne, HIGH); // sets relayOne on
lcd.clear();
lcd.print("Light One: Off ");
delay(500);
digitalWrite(relayTwo, HIGH); // sets relayOne on
lcd.setCursor(0, 1);
lcd.print("Light Two: Off ");
delay(500);
digitalWrite(relayThree, HIGH); // sets relayOne on
lcd.setCursor(0, 2);
lcd.print("Outlet One: Off ");
delay(500);
digitalWrite(relayFour, HIGH); // sets relayOne on
delay(500);
lcd.setCursor(0, 3);
lcd.print("Outlet Two: Off ");
//Create system initialized message
lcd.clear();
lcd.setCursor(0, 0);
delay(1000);
lcd.print(" System Initialzed ");
delay(1000);
lcd.setCursor(0, 2);
lcd.print(" Ready For Commands ");
delay(2000);
//Set up device status message
lcd.clear();
lcd.print("Light One: Off ");
delay(500);
lcd.setCursor(0, 1);
lcd.print("Light Two: Off ");
delay(500);
lcd.setCursor(0, 2);
lcd.print("Outlet One: Off ");
delay(500);
lcd.setCursor(0, 3);
lcd.print("Outlet Two: Off ");
}
}
void loop()
{
if (wifiConnected) {
upnpBroadcastResponder.serverLoop();
lightOne->serverLoop();
lightTwo->serverLoop();
outletOne->serverLoop();
outletTwo->serverLoop();
}
}
void lightOneOn() {
// Serial.print("Switch 1 turn on ...");
digitalWrite(relayOne, LOW); // sets relayOne on
lcd.setCursor(0, 0);
lcd.print("Light One: On ");
}
void lightOneOff() {
// Serial.print("Switch 1 turn off ...");
digitalWrite(relayOne, HIGH); // sets relayOne off
lcd.setCursor(0, 0);
lcd.print("Light One: Off ");
}
void lightTwoOn() {
// Serial.print("Switch 2 turn on ...");
digitalWrite(relayThree, LOW); // sets relayTwo on
lcd.setCursor(0, 1);
lcd.print("Light Two: On ");
}
void lightTwoOff() {
// Serial.print("Switch 2 turn off ...");
digitalWrite(relayThree, HIGH); // sets relayTwo Off
lcd.setCursor(0, 1);
lcd.print("Light Two: Off ");
}
//sockets
void outletOneOn() {
// Serial.print("Socket 1 turn on ...");
digitalWrite(relayFour, LOW); // sets relayThree on
lcd.setCursor(0, 2);
lcd.print("Outlet One: On ");
}
void outletOneOff() {
// Serial.print("Socket 1 turn off ...");
digitalWrite(relayFour, HIGH); // sets relayThree off
lcd.setCursor(0, 2);
lcd.print("Outlet One: Off ");
}
void outletTwoOn() {
// Serial.print("Socket 2 turn on ...");
digitalWrite(relayTwo, LOW); // sets relayFour on
lcd.setCursor(0, 3);
lcd.print("Outlet Two: On ");
}
void outletTwoOff() {
// Serial.print("Socket 2 turn off ...");
digitalWrite(relayTwo, HIGH); // sets relayFour off
lcd.setCursor(0, 3);
lcd.print("Outlet Two: Off ");
}
// 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;
}