-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetman.cpp
278 lines (227 loc) · 6.9 KB
/
netman.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <ArduinoJson.h>
#include <MQTT.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include "storage.h"
#include "netman.h"
#include "ledcontroller.h"
#define MQTTPORT 1883
#define MSGTOPIC "_msg"
#define BRIGHTNESSTOPIC "_brightness"
#define BGBRIGHTNESSTOPIC "_bgbrightness"
#define MODETOPIC "_mode"
#define TEXTCOLORTOPIC "_textcolor"
#define BGCOLORTOPIC "_bgcolor"
#define SCROLLSPEEDTOPIC "_scrollspeed"
MQTTClient client(2048);
WiFiClient net;
String msgTopic = MSGTOPIC;
String brightnessTopic = BRIGHTNESSTOPIC;
String bgBrightnessTopic = BGBRIGHTNESSTOPIC;
String modeTopic = MODETOPIC;
String textColorTopic = TEXTCOLORTOPIC;
String bgColorTopic = BGCOLORTOPIC;
String scrollSpeedTopic = SCROLLSPEEDTOPIC;
String deviceData;
DynamicJsonDocument inputDocument(2048);
void parseMessage(String message) {
auto error = deserializeJson(inputDocument, message);
if (!error) {
uint8_t r = inputDocument["r"],
g = inputDocument["g"],
b = inputDocument["b"];
int x = inputDocument["x"];
int y = inputDocument["y"];
int a = inputDocument["align"];
String msg = inputDocument["msg"];
CRGB color(r,g,b);
Serial.print("Received \"");
Serial.print(msg);
Serial.print("\" with color ");
Serial.println(color);
if (!a && (x != 0 || y != 0)) {
LedPrintAt(x, y, inputDocument["msg"].as<String>().c_str(), color);
} else {
LedPrint(inputDocument["msg"].as<String>().c_str(), color);
}
} else {
Serial.print("Invalid JSON: ");
Serial.println(error.c_str());
Serial.println(message);
}
}
void parseColor(String message) {
auto error = deserializeJson(inputDocument, message);
if (!error) {
uint8_t r = inputDocument["r"],
g = inputDocument["g"],
b = inputDocument["b"];
CRGB color = CRGB(r,g,b);
LedPrintSetColor(color);
Serial.print("Received text color");
Serial.println(color);
} else {
Serial.print("Invalid JSON: ");
Serial.println(error.c_str());
Serial.println(message);
}
}
void parseBGColor(String message) {
auto error = deserializeJson(inputDocument, message);
if (!error) {
uint8_t r = inputDocument["r"],
g = inputDocument["g"],
b = inputDocument["b"];
CRGB color = CRGB(r,g,b);
SetBackgroundColor(color);
Serial.print("Received background color");
Serial.println(color);
} else {
Serial.print("Invalid JSON: ");
Serial.println(error.c_str());
Serial.println(message);
}
}
void parseScrollSpeed(int speed) {
if (speed > 80 || speed < 5) {
return;
}
SetScrollSpeed(speed);
}
void messageReceived(String &topic, String &payload) {
if (topic == msgTopic) {
parseMessage(payload);
return;
}
if (topic == brightnessTopic) {
SetBrightness(payload.toFloat());
return;
}
if (topic == bgBrightnessTopic) {
SetBackgroundBrightness(payload.toFloat());
return;
}
if (topic == modeTopic) {
SetMode(payload.toInt());
return;
}
if (topic == textColorTopic) {
parseColor(payload);
return;
}
if (topic == bgColorTopic) {
parseBGColor(payload);
return;
}
if (topic == scrollSpeedTopic) {
parseScrollSpeed(payload.toInt());
return;
}
Serial.print("Unknown topic: ");
Serial.println(topic);
}
void Subscribe(String &topic) {
topic = String(WiFi.getHostname()) + topic;
client.subscribe(topic);
Serial.print("Subscribing to topic ");
Serial.println(topic);
}
void RefreshDeviceData() {
DynamicJsonDocument jsonBuffer(1024);
JsonObject root = jsonBuffer.to<JsonObject>();
JsonObject network = jsonBuffer.createNestedObject("network");
JsonArray functions = jsonBuffer.createNestedArray("functions");
root["name"] = WiFi.getHostname();
root["uid"] = String((long int)ESP.getEfuseMac(), HEX) + WiFi.macAddress();
root["ssid"] = WiFi.SSID();
network["ip"] = WiFi.localIP().toString();
network["netmask"] = WiFi.subnetMask().toString();
network["gateway"] = WiFi.gatewayIP().toString();
network["rssi"] = WiFi.RSSI();
JsonObject displayFunction = functions.createNestedObject();
JsonObject brightnessFunction = functions.createNestedObject();
JsonObject bgBrightnessFunction = functions.createNestedObject();
JsonObject modeFunction = functions.createNestedObject();
JsonObject textColorFunction = functions.createNestedObject();
JsonObject bgColorFunction = functions.createNestedObject();
JsonObject scrollSpeedFunction = functions.createNestedObject();
displayFunction["type"] = "display";
displayFunction["subtopic"] = MSGTOPIC;
displayFunction["name"] = "Text Display";
brightnessFunction["type"] = "float_variable";
brightnessFunction["subtopic"] = BRIGHTNESSTOPIC;
brightnessFunction["name"] = "Text Brightness";
bgBrightnessFunction["type"] = "float_variable";
bgBrightnessFunction["subtopic"] = BGBRIGHTNESSTOPIC;
bgBrightnessFunction["name"] = "Background Brightness";
modeFunction["type"] = "int_variable";
modeFunction["subtopic"] = MODETOPIC;
modeFunction["name"] = "Display Mode";
textColorFunction["type"] = "json";
textColorFunction["subtopic"] = TEXTCOLORTOPIC;
textColorFunction["name"] = "Text Color";
bgColorFunction["type"] = "json";
bgColorFunction["subtopic"] = BGCOLORTOPIC;
bgColorFunction["name"] = "Background Color";
scrollSpeedFunction["type"] = "int";
scrollSpeedFunction["subtopic"] = SCROLLSPEEDTOPIC;
scrollSpeedFunction["name"] = "Scroll Speed";
deviceData = "";
serializeJson(root, deviceData);
}
void clientConnect() {
String mqttUser = GetMQTTUser();
String mqttPass = GetMQTTPass();
Serial.println("Connecting...");
LedPrint("MQTT Connecting...", CRGB::Yellow);
while (!client.connect(WiFi.getHostname(), mqttUser.c_str(), mqttPass.c_str())) {
Serial.print(".");
delay(100);
LedLoop();
yield();
}
Serial.println();
Serial.println("Connected!");
LedPrint("MQTT", CRGB::Green);
// build setup data
RefreshDeviceData();
// Reset
msgTopic = String(MSGTOPIC);
brightnessTopic = String(BRIGHTNESSTOPIC);
bgBrightnessTopic = String(BGBRIGHTNESSTOPIC);
modeTopic = String(MODETOPIC);
textColorTopic = String(TEXTCOLORTOPIC);
bgColorTopic = String(BGCOLORTOPIC);
scrollSpeedTopic = String(SCROLLSPEEDTOPIC);
// Subscribe
Subscribe(msgTopic);
Subscribe(brightnessTopic);
Subscribe(bgBrightnessTopic);
Subscribe(modeTopic);
Subscribe(textColorTopic);
Subscribe(bgColorTopic);
Subscribe(scrollSpeedTopic);
}
void SetupMQTT() {
String mqttHost = GetMQTTHost();
Serial.println("Setting up MQTT");
client.begin(mqttHost.c_str(), MQTTPORT, net);
client.onMessage(messageReceived);
clientConnect();
Serial.println("Done");
}
long lastAnounce = 0;
void Announce() {
client.publish("announce", deviceData);
}
void MQTTLoop() {
client.loop();
if (!client.connected()) {
clientConnect();
}
if (millis() - lastAnounce > 1000) {
lastAnounce = millis();
RefreshDeviceData();
Announce();
}
}