-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathecho.ino
248 lines (208 loc) · 6.47 KB
/
echo.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
245
246
247
248
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
//final working code for Nodemcu v3 and compatible for 8 Channel Power Relay..
//fb.com/insider7enjoy
//programmed on 13-Sep-2017
//PhantomCluster.com
// prototypes
boolean connectWifi();
//on/off callbacks
void lightOneOn();
void lightOneOff();
void lightTwoOn();
void lightTwoOff();
void lightThreeOn();
void lightThreeOff();
void lightFourOn();
void lightFourOff();
void outletOneOn();
void outletOneOff();
void outletTwoOn();
void outletTwoOff();
void outletThreeOn();
void outletThreeOff();
void outletFourOn();
void outletFourOff();
// Change this before you flash
const char* ssid = "Change it with your wifi name";
const char* password = "change it with your wifi password";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *lightOne = NULL;
Switch *lightTwo = NULL;
Switch *lightThree = NULL;
Switch *lightFour = NULL;
Switch *outletOne = NULL;
Switch *outletTwo = NULL;
Switch *outletThree = NULL;
Switch *outletFour = NULL;
//relay pin setup for funct
int relayOne = 5;
int relayTwo = 4;
int relayThree = 0;
int relayFour = 2;
int relayFive = 14;
int relaySix = 12;
int relaySeven = 13;
int relayEight = 15;
void setup()
{
Serial.begin(115200);
// 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
lightOne = new Switch("Light One", 80, lightOneOn, lightOneOff);
lightTwo = new Switch("Light Two", 81, lightTwoOn, lightTwoOff);
lightThree = new Switch("Light Three", 82, lightThreeOn, lightThreeOff);
lightFour = new Switch("Light Four", 83, lightFourOn, lightFourOff);
outletOne = new Switch("Outlet One", 84,outletOneOn, outletOneOff);
outletTwo = new Switch("Outlet Two", 85, outletTwoOn, outletTwoOff);
outletThree = new Switch("Outlet Three", 86, outletThreeOn, outletThreeOff);
outletFour = new Switch("Outlet Four", 87,outletFourOn, outletFourOff);
Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*lightOne);
upnpBroadcastResponder.addDevice(*lightTwo);
upnpBroadcastResponder.addDevice(*lightThree);
upnpBroadcastResponder.addDevice(*lightFour);
upnpBroadcastResponder.addDevice(*outletOne);
upnpBroadcastResponder.addDevice(*outletTwo);
upnpBroadcastResponder.addDevice(*outletThree);
upnpBroadcastResponder.addDevice(*outletFour);
//relay pins setup i Used D1,D2,D3,D4,D5,D6,D7,D8 followed as assigned below, if you are willing to change Pin or planning to use extra please Check Image in Github File..:)
pinMode (5, OUTPUT);
pinMode (4, OUTPUT);
pinMode (0, OUTPUT);
pinMode (2, OUTPUT);
pinMode (14, OUTPUT);
pinMode (12, OUTPUT);
pinMode (13, OUTPUT);
pinMode (15, OUTPUT);
digitalWrite (5,HIGH);
digitalWrite (4,HIGH);
digitalWrite (0,HIGH);
digitalWrite (2,HIGH);
digitalWrite (14,HIGH);
digitalWrite (12,HIGH);
digitalWrite (13,HIGH);
digitalWrite (15,HIGH);
}
}
void loop()
{
if(wifiConnected){
upnpBroadcastResponder.serverLoop();
lightOne->serverLoop();
lightTwo->serverLoop();
lightThree->serverLoop();
lightFour->serverLoop();
outletOne->serverLoop();
outletTwo->serverLoop();
outletThree->serverLoop();
outletFour->serverLoop();
}
}
void lightOneOff() {
Serial.print("Switch 1 turn off ...");
digitalWrite(relayOne, HIGH); // sets relayOne off
}
void lightOneOn() {
Serial.print("Switch 1 turn on ...");
digitalWrite(relayOne, LOW); // sets relayOne on
}
void lightTwoOff() {
Serial.print("Switch 2 turn off ...");
digitalWrite(relayTwo, HIGH); // sets relayOne o
}
void lightTwoOn() {
Serial.print("Switch 2 turn on ...");
digitalWrite(relayTwo, LOW); // sets relayOne on
}
void lightThreeOff() {
Serial.print("Switch 3 turn off ...");
digitalWrite(relayThree, HIGH); // sets relayOne on
}
void lightThreeOn() {
Serial.print("Switch 3 turn on ...");
digitalWrite(relayThree, LOW); // sets relayOne on
}
void lightFourOff() {
Serial.print("Switch4 turn off ...");
digitalWrite(relayFour, HIGH); // sets relayOne on
}
void lightFourOn() {
Serial.print("Switch 4 turn on ...");
digitalWrite(relayFour, LOW); // sets relayOne on
}
//sockets
void outletOneOff() {
Serial.print("Socket 1 turn off ...");
digitalWrite(relayFive, HIGH); // sets relayOne on
}
void outletOneOn() {
Serial.print("Socket 1turn on ...");
digitalWrite(relayFive, LOW); // sets relayOne off
}
void outletTwoOff() {
Serial.print("Socket 2 turn off ...");
digitalWrite(relaySix, HIGH); // sets relayOne on
}
void outletTwoOn() {
Serial.print("Socket 2 turn on ...");
digitalWrite(relaySix, LOW); // sets relayOne on
}
void outletThreeOff() {
Serial.print("Socket 3 turn off ...");
digitalWrite(relaySeven, HIGH); // sets relayOne on
}
void outletThreeOn() {
Serial.print("Socket 3 turn on ...");
digitalWrite(relaySeven, LOW); // sets relayOne off
}
void outletFourOff() {
Serial.print("Socket 4 turn off ...");
digitalWrite(relayEight, HIGH); // sets relayOne on
}
void outletFourOn() {
Serial.print("Socket 4 turn on ...");
digitalWrite(relayEight, LOW); // sets relayOne on
}
// 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;
}