-
Notifications
You must be signed in to change notification settings - Fork 5
/
bbMonitor.ino
361 lines (305 loc) · 10.8 KB
/
bbMonitor.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <Preferences.h>
#include <ESPmDNS.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>
#include <Ticker.h>
#include <esp_log.h>
#define P1 38
#define P2 5
#define P3 6
#define P4 7
#define P5 15
#define P6 16
#define P7 17
#define P8 18
#define P9 8
#define P10 9
#define P11 10
#define P12 11
#define P13 12
#define P14 13
#define P15 14
#define P16 21
#define P17 35
#define P18 36
const int pins[] = {P1, P2, P3, P4, P5, P6, P7, P8};
int luminence = 128;
int smoothTime = 0;
bool isConfigingAnimation = false;
#define LINES 2
#define EACH_PIXEL_COUNT 2
int cols = sizeof(pins) / sizeof(pins[0]) / LINES;
#define STRIP1_PIN 39 // Example pin for strip 1, replace with your actual pin
#define STRIP2_PIN 40 // Example pin for strip 2, replace with your actual pin
#define PIXEL_COUNT 8 // Replace with the actual number of NeoPixels per strip
NeoPixelBus<NeoGrbFeature, NeoEsp32Rmt1Ws2812xMethod> strip1(PIXEL_COUNT, STRIP1_PIN);
NeoPixelBus<NeoGrbFeature, NeoEsp32Rmt2Ws2812xMethod> strip2(PIXEL_COUNT, STRIP2_PIN);
NeoPixelAnimator animations1(PIXEL_COUNT);
NeoPixelAnimator animations2(PIXEL_COUNT);
NeoPixelAnimator animationsConfig(PIXEL_COUNT);
AnimEaseFunction easing = NeoEase::CubicIn;
#define MAX 250
Preferences preferences;
bool mdnsOn = false;
AsyncWebServer server(80);
AsyncWebSocket ws("/");
int targetPWMValues[sizeof(pins) / sizeof(pins[0])]; // Array to store target PWM values for pins
int currentPWMValues[sizeof(pins) / sizeof(pins[0])]; // Array to store current PWM values for pins
unsigned long smoothStartTime;
DynamicJsonDocument jsonDoc(512);
Ticker meter;
Ticker configuring;
Ticker ipStatus;
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len, AsyncWebSocketClient *client) {
AwsFrameInfo *info = static_cast<AwsFrameInfo*>(arg);
static String message;
if (info->index == 0) {
// This is a new message, clear our message string
message = "";
}
// Append the data from this frame to our message
for(size_t i=0; i<len; i++) {
message += (char)data[i];
}
if (info->final) {
// The final frame has been received, process the message
handleWebSocketText((uint8_t *)message.c_str(), message.length(),client);
}
}
void resetConfigingAnimation(){
isConfigingAnimation = false;
ESP_LOGV("bbMonitor","reset isConfigingAnimation");
}
void handleWebSocketText(uint8_t * payload, size_t length, AsyncWebSocketClient *client) {
// Parse JSON data
if (memcmp(payload, "getConfig", sizeof("getConfig") - 1) == 0) {
ESP_LOGV("bbMonitor","getConfig");
String config_current = preferences.getString("config","{\"config\":{\"data\":[\"cpu_usage[0]\",\"cpu_usage[1]\",\"cpu_usage[2]\",\"cpu_usage[3]\",\"cpu_usage[4]\",\"cpu_usage[5]\",\"cpu_usage[6]\",\"cpu_usage[7]\"],\"brightNess\":128,\"smoothTime\":0}}");
client->text(config_current);
}
else if(memcmp(payload, "configuring", sizeof("configuring") - 1) == 0){
ESP_LOGV("bbMonitor","configuring");
setConfigAnimation();
configuring.once(2,resetConfigingAnimation);
}
else{
DeserializationError error = deserializeJson(jsonDoc, payload, length);
if (error) {
ESP_LOGV("bbMonitor","Failed to parse JSON");
return;
}
// Check if the "data" key exists
if (jsonDoc.containsKey("data")) {
JsonArray data = jsonDoc["data"];
// Iterate through the array and set analogWrite values for defined pins
smoothStartTime = millis();
for (int i = 0; i < data.size(); i++) {
// Ensure pinIndex is within bounds
if (i < sizeof(pins) / sizeof(pins[0])) {
float pinValue = data[i].as<float>(); // Extract the value from the array element
int pinIndex = i;
targetPWMValues[i] = MAX * pinValue;
ESP_LOGV("bbMonitor","Set PWM value for Pin ");
// Determine the strip and pixel index based on pinIndex
int stripIndex = pinIndex < cols ? 0 : 1;
int pixelIndex = (pinIndex & cols - 1) * EACH_PIXEL_COUNT;
// Set NeoPixel animation based on pinValue
setNeoPixelAnimation(stripIndex, pixelIndex, pinValue);
} else {
ESP_LOGV("bbMonitor","Invalid Pin Index");
}
}
}
else if(jsonDoc.containsKey("config")){
//write the entire json.config string to the preferences config
luminence = jsonDoc["config"]["brightNess"].as<int>();
smoothTime = jsonDoc["config"]["smoothTime"].as<int>();
String jsonString;
serializeJson(jsonDoc, jsonString);
preferences.putString("config", jsonString);
}
else {
ESP_LOGV("bbMonitor","No 'data' key found in JSON");
}
}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
ws.cleanupClients();
break;
case WS_EVT_DISCONNECT:
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len, client);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
void setup() {
preferences.begin("bbMonitor", false);
Serial.begin(115200);
// put your setup code here, to run once:
pinMode(P1,OUTPUT);
pinMode(P2,OUTPUT);
pinMode(P3,OUTPUT);
pinMode(P4,OUTPUT);
pinMode(P5,OUTPUT);
pinMode(P6,OUTPUT);
pinMode(P7,OUTPUT);
pinMode(P8,OUTPUT);
//Setup WiFi
String ssid = preferences.getString("ssid","Hollyshit_A");
String passwd = preferences.getString("passwd","00197633");
String config = preferences.getString("config","{\"config\":{\"data\":[\"cpu_usage[0]\",\"cpu_usage[1]\",\"cpu_usage[2]\",\"cpu_usage[3]\",\"cpu_usage[4]\",\"cpu_usage[5]\",\"cpu_usage[6]\",\"cpu_usage[7]\"],\"brightNess\":128,\"smoothTime\":0}}");
if(ssid != "null" && passwd != "null"){
WiFi.begin(ssid.c_str(),passwd.c_str());
}
//Load Config
DeserializationError error = deserializeJson(jsonDoc, config.c_str());
if (error) {
ESP_LOGV("bbMonitor","Failed to parse JSON");
}
else{
luminence = jsonDoc["config"]["brightNess"].as<int>();
smoothTime = jsonDoc["config"]["smoothTime"].as<int>();
}
// Setup LED
strip1.Begin();
strip2.Begin();
RgbColor color = RgbColor(25, 25, luminence);
for(int i = 0; i < PIXEL_COUNT; i ++) {
strip1.SetPixelColor(i, color);
strip2.SetPixelColor(i, color);
}
strip1.Show();
strip2.Show();
// Setup WebSocket
ws.onEvent(onEvent);
server.addHandler(&ws);
server.begin();
meter.attach(0.01,tickMeter); //Update Meter @100Hz
ipStatus.attach(1,printIP); //print IP @1Hz
ESP_LOGV("bbMonitor","bbMonitor Setup Done");
}
void printIP() {
DynamicJsonDocument docIP(256); // Create a JSON document
// Get the local IP address
String ip = WiFi.localIP().toString();
// Add the IP address to the JSON document
docIP["ip"] = ip;
// Serialize the JSON document to a string
String serialized;
serializeJson(docIP, serialized);
// Print the serialized JSON
Serial.println(serialized);
}
void setupMDNS(){
if (!MDNS.begin("bbMonitor")) {
ESP_LOGV("bbMonitor","Error setting up MDNS responder!");
}
else{
MDNS.addService("bbMonitor", "tcp", 80);
mdnsOn = true;
}
}
void tickMeter(){
unsigned long currentTime = millis();
for (int i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) {
int targetValue = targetPWMValues[i];
int currentValue = currentPWMValues[i];
float progress = (smoothTime > 0) ? float(currentTime - smoothStartTime) / smoothTime : 1.0;
if(progress > 1.0) progress = 1.0;
int newValue = currentValue + (targetValue - currentValue) * progress;
analogWrite(pins[i], newValue);
if(progress == 1.0){ //transition done
currentPWMValues[i] = targetPWMValues[i];
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if(!mdnsOn) setupMDNS();
animations1.UpdateAnimations();
animations2.UpdateAnimations();
animationsConfig.UpdateAnimations();
strip1.Show();
strip2.Show();
if (Serial.available()) {
// Read the serial data
String serialData = Serial.readStringUntil('\n');
// Parse JSON
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, serialData);
if (error) {
ESP_LOGV("bbMonitor","Failed to parse JSON");
return;
}
if (doc.containsKey("ssid") && doc.containsKey("password")) {
// Extract SSID and password from JSON
const char* ssid = doc["ssid"];
const char* password = doc["password"];
// Store SSID and password in preferences
preferences.putString("ssid", ssid);
preferences.putString("passwd", password);
// Connect to WiFi
ESP_LOGV("bbMonitor","Connecting to new WiFi...");
WiFi.disconnect();
WiFi.begin(ssid, password);
}
}
}
uint16_t hue = 0;
unsigned long lastUpdate = 0;
int animationInterval = 10; // Adjust animation speed here
void setConfigAnimation() {
// Implement your rainbow loop animation here
isConfigingAnimation = true;
animationsConfig.StartAnimation(0, 2000, [=](const AnimationParam& param) {
int hue = (param.progress * 360);
for (int i = 0; i < PIXEL_COUNT; i++) {
float pixelHue = static_cast<float>(hue + (i * 360 / PIXEL_COUNT)) / 360.0f;
strip1.SetPixelColor(i, HsbColor(pixelHue, 1.0f, 1.0f));
}
for (int i = 0; i < PIXEL_COUNT; i++) {
float pixelHue = static_cast<float>(hue + (i * 360 / PIXEL_COUNT)) / 360.0f;
strip2.SetPixelColor(i, HsbColor(pixelHue, 1.0f, 1.0f));
}
strip1.Show();
strip2.Show();
});
}
void setNeoPixelAnimation(int stripIndex, int pixelIndex, float pinValue) {
if(isConfigingAnimation == false){
// Define animation parameters
RgbColor green(0, luminence, 0);
RgbColor red(luminence, 0, 0);
// Calculate color based on pinValue
RgbColor targetColor = RgbColor::LinearBlend(green, red, pinValue);
// Set animation
if (stripIndex == 0) {
AnimUpdateCallback animUpdate = [=](const AnimationParam& param)
{
float progress = easing(param.progress);
RgbColor color = RgbColor::LinearBlend(strip1.GetPixelColor(pixelIndex), targetColor, progress);
for(int i = 0; i < EACH_PIXEL_COUNT; i ++) {
strip1.SetPixelColor(pixelIndex + i, color);
}
};
animations1.StartAnimation(pixelIndex, 1000, animUpdate);
} else {
animations2.StartAnimation(pixelIndex, 1000, [targetColor, pixelIndex](const AnimationParam& param) {
RgbColor color = RgbColor::LinearBlend(strip2.GetPixelColor(pixelIndex), targetColor, param.progress);
for(int i = 0; i < EACH_PIXEL_COUNT; i ++) {
strip2.SetPixelColor(pixelIndex + i, color);
}
});
}
}
}