-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_pc.ino
254 lines (231 loc) · 6.72 KB
/
led_pc.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
249
250
251
252
253
254
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <FastLED.h>
#include "SinricPro.h"
#include "SinricProLight.h"
#define BAUD_RATE 115200
#define WIFI_SSID "REDE_WIFI"
#define WIFI_PASS "SENHA_WIFI"
#define APP_KEY "KEY_SINRICPRO"
#define APP_SECRET "SENHA_SINRICPRO"
#define BROKER_USER "USUARIO_BROKER_MQTT"
#define BROKER_PASS "SENHA_BROKER_MQTT"
#define BROKER_URL "ENDERECO_SERVER_MQTT"
#define LED_SETUP_ID "ID_DISPOSITIVO_SINRICPRO"
#define LED_LAMP_ID "ID_DISPOSITIVO_SINRICPRO"
WiFiClient espClient;
PubSubClient client(espClient);
bool powerState;
int globalBrightness = 100;
char mensagem[16];
char mensagemTratada[16];
char sinalBuffer[16];
unsigned int lengthMsg = 0;
bool statusLeds = false;
bool ligaRainbow = false;
unsigned int velRainbow = 220;
unsigned int larguraRainbow = 10;
int r = 0;
int g = 0;
int b = 0;
#define NUM_LEDS 60
#define DATA_PIN 13
CRGB leds[NUM_LEDS];
bool ledSetupOnPower(const String &deviceId, bool &state) {
powerState = state;
if (state) {
Serial.println("liga led_pc");
client.publish("/led_pc/rainbow", "1");
} else {
Serial.println("desliga led_pc");
client.publish("/led_pc/rainbow", "0");
}
return true; // request handled properly
}
bool ledLampOnPower(const String &deviceId, bool &state) {
powerState = state;
if (state) {
Serial.println("liga led_lamp");
client.publish("/led_lamp/rainbow", "1");
} else {
Serial.println("desliga led_lamp");
client.publish("/led_lamp/rainbow", "0");
}
return true; // request handled properly
}
bool ledSetupOnBrightness(const String &deviceId, int &brightness) {
globalBrightness = brightness;
//FastLED.setBrightness(map(brightness, 0, 100, 0, 255));
//FastLED.show();
return true;
}
bool ledSetupOnAdjustBrightness(const String &deviceId, int brightnessDelta) {
globalBrightness += brightnessDelta;
brightnessDelta = globalBrightness;
//FastLED.setBrightness(map(globalBrightness, 0, 100, 0, 255));
//FastLED.show();
return true;
}
bool ledSetupOnColor(const String &deviceId, byte &red, byte &green, byte &blue) {
char hexCode[8];
sprintf(hexCode,"#%02x%02x%02x",red,green,blue);
Serial.println(hexCode);
client.publish("/led_pc/corFixa", hexCode);
return true;
}
bool ledLampOnColor(const String &deviceId, byte &red, byte &green, byte &blue) {
char hexCode[8];
sprintf(hexCode,"#%02x%02x%02x",red,green,blue);
Serial.println(hexCode);
client.publish("/led_lamp/corFixa", hexCode);
return true;
}
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
IPAddress localIP = WiFi.localIP();
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", localIP.toString().c_str());
}
void setupSinricPro() {
SinricProLight &ledSetup = SinricPro[LED_SETUP_ID];
ledSetup.onPowerState(ledSetupOnPower);
ledSetup.onBrightness(ledSetupOnBrightness);
ledSetup.onAdjustBrightness(ledSetupOnAdjustBrightness);
ledSetup.onColor(ledSetupOnColor);
SinricProLight &ledLamp = SinricPro[LED_LAMP_ID];
ledLamp.onPowerState(ledLampOnPower);
//ledLamp.onBrightness(onBrightness);
//ledLamp.onAdjustBrightness(onAdjustBrightness);
ledLamp.onColor(ledLampOnColor);
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.restoreDeviceStates(true);
SinricPro.begin(APP_KEY, APP_SECRET);
}
void desligaLeds() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
FastLED.show();
delay(10);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String topico = topic;
lengthMsg = length;
for (int i = 0; i < lengthMsg; i++) {
mensagem[i] = (char)payload[i];
}
Serial.print(topic);
Serial.print(" ");
Serial.print(mensagem);
Serial.println();
if (topico == "/led_pc/rainbow") {
if (mensagem[0] == '1') {
ligaRainbow = true;
} else if (mensagem[0] == '0') {
ligaRainbow = false;
desligaLeds();
}
client.publish("/led_pc/corFixa", "#000000");
}
if (topico == "/led_pc/corFixa") {
for (int i = 0; i < lengthMsg; i++) {
mensagemTratada[i] = mensagem[i + 1];
}
String hexstring = mensagemTratada;
if (hexstring != "000000") {
ligaRainbow = false;
int number = (int) strtol( &hexstring[1], NULL, 16);
r = number >> 16;
g = number >> 8 & 0xFF;
b = number & 0xFF;
Serial.print(r);
Serial.print(g);
Serial.print(b);
Serial.print("\n");
CRGB rgbval(r, g, b);
fill_solid(leds, NUM_LEDS, rgbval);
FastLED.setBrightness(255);
FastLED.show();
}
}
if (topico == "/led_pc/velocidade") {
int centena = 0;
int dezena = 0;
int unidade = 0;
if (lengthMsg == 3) {
centena = ((int)mensagem[0] - 48) * 100;
dezena = ((int)mensagem[1] - 48) * 10;
unidade = (int)mensagem[2] - 48;
} else if (lengthMsg == 2) {
centena = 0;
dezena = ((int)mensagem[0] - 48) * 10;
unidade = (int)mensagem[1] - 48;
} else if (lengthMsg == 1) {
centena = 0;
dezena = 0;
unidade = (int)mensagem[0] - 48;
}
velRainbow = centena + dezena + unidade;
}
if (topico == "/led_pc/largura") {
int dezena = 0;
int unidade = 0;
if (lengthMsg == 2) {
dezena = ((int)mensagem[0] - 48) * 10;
unidade = (int)mensagem[1] - 48;
} else {
dezena = 0;
unidade = (int)mensagem[0] - 48;
}
larguraRainbow = dezena + unidade;
}
//limpa mensagem
for (int i = 0; i < lengthMsg; i++) {
mensagem[i] = '\0';
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("\nConnecting to ");
Serial.println(BROKER_URL);
if (client.connect("/led_pc", BROKER_USER, BROKER_PASS)) {
Serial.print("\nConnected to ");
Serial.println(BROKER_URL);
client.subscribe("/led_pc/corFixa");
client.subscribe("/led_pc/velocidade");
client.subscribe("/led_pc/largura");
client.subscribe("/led_pc/rainbow");
} else {
Serial.println("\nTrying connect again");
delay(5000);
}
}
}
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setCorrection(0xFFA0FF);
FastLED.setTemperature(0xFF9329);
setupWiFi();
setupSinricPro();
client.setServer(BROKER_URL, 10978);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if (ligaRainbow) {
fill_rainbow(leds, NUM_LEDS, (millis() * (255 - velRainbow) / 255), larguraRainbow);
FastLED.setBrightness(255);
FastLED.show();
}
client.loop();
SinricPro.handle();
}