-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alexa_Ligar_Leds.ino
200 lines (168 loc) · 3.78 KB
/
Alexa_Ligar_Leds.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
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
#define R1 15
#define R2 2
#define R3 4
#define R4 5
// prototypes
boolean connectWifi();
//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
// Change this!!
// WiFi Credentials
const char* ssid = "NomeDaSuaRedeWifi"; // Atualize esses campos
const char* password = "SenhaDaSuaRedeWifi"; // Atualize esses campos
// device names
String Device_1_Name = "LED 1";
String Device_2_Name = "LED 2";
String Device_3_Name = "LED 3";
String Device_4_Name = "LED 4";
boolean wifiConnected = false;
Espalexa espalexa;
void setup()
{
Serial.begin(115200);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);
// Initialise wifi connection
wifiConnected = connectWifi();
if (wifiConnected)
{
// Define your devices here.
espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
espalexa.addDevice(Device_2_Name, secondLightChanged);
espalexa.addDevice(Device_3_Name, thirdLightChanged);
espalexa.addDevice(Device_4_Name, fourthLightChanged);
espalexa.begin();
}
else
{
while (1)
{
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
delay(2500);
}
}
}
void loop()
{
espalexa.loop();
delay(1);
}
//our callback functions
void firstLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
digitalWrite(R1, HIGH);
Serial.println("Device1 ON");
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
digitalWrite(R1, LOW);
Serial.println("Device1 OFF");
}
}
void secondLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
digitalWrite(R2, HIGH);
Serial.println("Device2 ON");
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
digitalWrite(R2, LOW);
Serial.println("Device2 OFF");
}
}
void thirdLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
digitalWrite(R3, HIGH);
Serial.println("Device3 ON");
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
digitalWrite(R3, LOW);
Serial.println("Device3 OFF");
}
}
void fourthLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
digitalWrite(R4, HIGH);
Serial.println("Device4 ON");
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
digitalWrite(R4, LOW);
Serial.println("Device4 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 > 20) {
state = false; break;
}
i++;
}
Serial.println("");
if (state) {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed.");
}
return state;
}