-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bilibili_LEDMatrix_V1.ino
458 lines (412 loc) · 14.1 KB
/
Bilibili_LEDMatrix_V1.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
*鸣谢
https://youtu.be/bePgZIXHSkM
(c)2017 Pawel A. Hernik
感谢视频原作者分享的源代码以及提供的思路。
*作者
AlexCheng Bilibili频道:咸鱼Cosmos 欢迎订阅!
*日期
2018/11/1
*/
// 硬件接线说明
// ESP-01:
// GPIO 2 - DataIn
// GPIO 1 - LOAD/CS
// GPIO 0 - CLK
// ------------------------
// NodeMCU 1.0 :
// D8 - DataIn
// D7 - LOAD/CS
// D6 - CLK
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define NUM_MAX 4
#define ROTATE 90
// for ESP-01 module
//#define DIN_PIN 2 // D4
//#define CS_PIN 3 // D9/RX
//#define CLK_PIN 0 // D3
// for NodeMCU 1.0
#define DIN_PIN 15 // D8
#define CS_PIN 13 // D7
#define CLK_PIN 12 // D6
#include "max7219.h"
#include "fonts.h"
// =======================================================================
// 配置信息
// =======================================================================
const char* ssid = "Cosmos"; // WiFi名
const char* password = "abc123456"; //WiFi密码
String channelId = "96458132"; //bilibili UID
long utcOffset = 8; //时区,默认 +8 为东八区(中国北京)
// =======================================================================
void setup()
{
Serial.begin(115200);
initMAX7219();
sendCmdAll(CMD_SHUTDOWN, 1);
sendCmdAll(CMD_INTENSITY, 0);
WiFi.enableAP(false); //关闭AP热点
Serial.print("Connecting WiFi ");
WiFi.begin(ssid, password);
printStringWithShift(" WiFi ...~", 15, font, ' ');
while (WiFi.status() != WL_CONNECTED) {
Serial.print("."); delay(500);
}
Serial.println("");
Serial.print("Connected: "); Serial.println(WiFi.localIP());
Serial.println("Getting data ...");
printStringWithShift("Bi ... ~", 15, font, ' ');
}
// =======================================================================
long viewCount, viewCount24h = -1, viewsGain24h;
long subscriberCount, subscriberCount1h = -1, subscriberCount24h = -1, subsGain1h = 0, subsGain24h = 0;
long videoCount;
int cnt = 0;
unsigned long time1h, time24h;
long localEpoc = 0;
long localMillisAtUpdate = 0;
int h, m, s;
String date;
void loop()
{
if (cnt <= 0) {
if ((getYTData() == 0) && (getVideoData() == 0) && (getplayData() == 0)) {
cnt = 1; // data is refreshed every 50 loops
if (subscriberCount1h < 0) {
time1h = time24h = millis();
subscriberCount1h = subscriberCount24h = subscriberCount;
viewCount24h = viewCount;
}
if (millis() - time1h > 1000 * 60 * 60) {
time1h = millis();
subscriberCount1h = subscriberCount;
}
if (millis() - time24h > 1000 * 60 * 60 * 24) {
time24h = millis();
subscriberCount24h = subscriberCount;
viewCount24h = viewCount;
}
subsGain1h = subscriberCount - subscriberCount1h;
subsGain24h = subscriberCount - subscriberCount24h;
viewsGain24h = viewCount - viewCount24h;
}
}
cnt--;
updateTime();
int del = 3000;
int scrollDel = 20;
char txt[10];
//+++++++++++++++++++++粉丝数+++++++++++++++++++++//
printStringWithShift(" Fans: ", scrollDel, font, ' '); // eng
printValueWithShift(subscriberCount, scrollDel, 0);
delay(del);
//+++++++++++++++++++++1小时增加粉丝数(无则不显示)+++++++++++++++++++++//
if (subsGain1h > 0) {
printStringWithShift(" Fans gain 1h: ", scrollDel, font, ' '); // eng
printValueWithShift(subsGain1h, scrollDel, 1);
delay(del);
}
//+++++++++++++++++++++24小时粉丝数(无则不显示)+++++++++++++++++++++//
if (subsGain24h > 0) {
printStringWithShift(" Fans gain 24h: ", scrollDel, font, ' '); // eng
printValueWithShift(subsGain24h, scrollDel, 1);
delay(del);
}
//+++++++++++++++++++++直播数(无则不显示)+++++++++++++++++++++//
if (viewCount != viewCount24h) {
printStringWithShift(" Live: ", scrollDel, font, ' '); // eng
printValueWithShift(viewCount, scrollDel, 0);
delay(del);
}
//+++++++++++++++++++++播放数+++++++++++++++++++++//
printStringWithShift(" Play: ", scrollDel, font, ' '); // eng
printValueWithShift(videoCount, scrollDel, 0);
delay(del);
//+++++++++++++++++++++时间(根据前面设定的时区决定)+++++++++++++++++++++//
sprintf(txt, " %02d:%02d ", h, m);
printStringWithShift(txt, scrollDel, font, ' '); // real time
delay(del);
}
// =======================================================================
int dualChar = 0;
unsigned char convertPolish(unsigned char _c)
{
unsigned char c = _c;
if (c == 196 || c == 197 || c == 195) {
dualChar = c;
return 0;
}
if (dualChar) {
switch (_c) {
case 133: c = 1 + '~'; break; // 'ą'
case 135: c = 2 + '~'; break; // 'ć'
case 153: c = 3 + '~'; break; // 'ę'
case 130: c = 4 + '~'; break; // 'ł'
case 132: c = dualChar == 197 ? 5 + '~' : 10 + '~'; break; // 'ń' and 'Ą'
case 179: c = 6 + '~'; break; // 'ó'
case 155: c = 7 + '~'; break; // 'ś'
case 186: c = 8 + '~'; break; // 'ź'
case 188: c = 9 + '~'; break; // 'ż'
//case 132: c = 10+'~'; break; // 'Ą'
case 134: c = 11 + '~'; break; // 'Ć'
case 152: c = 12 + '~'; break; // 'Ę'
case 129: c = 13 + '~'; break; // 'Ł'
case 131: c = 14 + '~'; break; // 'Ń'
case 147: c = 15 + '~'; break; // 'Ó'
case 154: c = 16 + '~'; break; // 'Ś'
case 185: c = 17 + '~'; break; // 'Ź'
case 187: c = 18 + '~'; break; // 'Ż'
default: break;
}
dualChar = 0;
return c;
}
switch (_c) {
case 185: c = 1 + '~'; break;
case 230: c = 2 + '~'; break;
case 234: c = 3 + '~'; break;
case 179: c = 4 + '~'; break;
case 241: c = 5 + '~'; break;
case 243: c = 6 + '~'; break;
case 156: c = 7 + '~'; break;
case 159: c = 8 + '~'; break;
case 191: c = 9 + '~'; break;
case 165: c = 10 + '~'; break;
case 198: c = 11 + '~'; break;
case 202: c = 12 + '~'; break;
case 163: c = 13 + '~'; break;
case 209: c = 14 + '~'; break;
case 211: c = 15 + '~'; break;
case 140: c = 16 + '~'; break;
case 143: c = 17 + '~'; break;
case 175: c = 18 + '~'; break;
default: break;
}
return c;
}
// =======================================================================
int charWidth(char ch, const uint8_t *data)
{
int len = pgm_read_byte(data);
return pgm_read_byte(data + 1 + ch * len);
}
// =======================================================================
int showChar(char ch, const uint8_t *data)
{
int len = pgm_read_byte(data);
int i, w = pgm_read_byte(data + 1 + ch * len);
scr[NUM_MAX * 8] = 0;
for (i = 0; i < w; i++)
scr[NUM_MAX * 8 + i + 1] = pgm_read_byte(data + 1 + ch * len + 1 + i);
return w;
}
// =======================================================================
void printCharWithShift(unsigned char c, int shiftDelay, const uint8_t *data, int offs)
{
c = convertPolish(c);
if (c < offs || c > MAX_CHAR) return;
c -= offs;
int w = showChar(c, data);
for (int i = 0; i < w + 1; i++) {
delay(shiftDelay);
scrollLeft();
refreshAll();
}
}
// =======================================================================
void printStringWithShift(const char *s, int shiftDelay, const uint8_t *data, int offs)
{
while (*s) printCharWithShift(*s++, shiftDelay, data, offs);
}
// =======================================================================
// printValueWithShift():
// converts int to string
// centers string on the display
// chooses proper font for string/number length
// can display sign - or +
void printValueWithShift(long val, int shiftDelay, int sign)
{
const uint8_t *digits = digits5x7; // good for max 5 digits
if (val > 1999999) digits = digits3x7; // good for max 8 digits
else if (val > 99999) digits = digits4x7; // good for max 6-7 digits
String str = String(val);
if (sign) {
if (val < 0) str = ";" + str; else str = "<" + str;
}
const char *s = str.c_str();
int wd = 0;
while (*s) wd += 1 + charWidth(*s++ - '0', digits);
wd--;
int wdL = (NUM_MAX * 8 - wd) / 2;
int wdR = NUM_MAX * 8 - wdL - wd;
//Serial.println(wd); Serial.println(wdL); Serial.println(wdR);
s = str.c_str();
while (wdL > 0) {
printCharWithShift(':', shiftDelay, digits, '0');
wdL--;
}
while (*s) printCharWithShift(*s++, shiftDelay, digits, '0');
while (wdR > 0) {
printCharWithShift(':', shiftDelay, digits, '0');
wdR--;
}
}
// =====================订阅数==================================================
const char *ytHost = "api.bilibili.com";
int getYTData()
{
WiFiClientSecure client;
Serial.print("connecting to "); Serial.println(ytHost);
if (!client.connect(ytHost, 443)) {
Serial.println("connection failed");
return -1;
}
String cmd = String("GET /x/relation/stat?vmid=") + channelId + " HTTP/1.1\r\n" +
"Host: " + ytHost + "\r\nUser-Agent: ESP8266/1.1\r\nConnection: close\r\n\r\n";
client.print(cmd);
int repeatCounter = 10;
while (!client.available() && repeatCounter--) {
Serial.println("y."); delay(500);
}
String line, buf = "";
int startJson = 0, dateFound = 0;
while (client.connected() && client.available()) {
line = client.readStringUntil('\n');
if (line[0] == '{') startJson = 1;
if (startJson) {
for (int i = 0; i < line.length(); i++)
if (line[i] == '[' || line[i] == ']') line[i] = ' ';
buf += line + "\n";
}
if (!dateFound && line.startsWith("Date: ")) {
dateFound = 1;
date = line.substring(6, 22);
h = line.substring(23, 25).toInt();
m = line.substring(26, 28).toInt();
s = line.substring(29, 31).toInt();
localMillisAtUpdate = millis();
localEpoc = (h * 60 * 60 + m * 60 + s);
}
}
client.stop();
DynamicJsonBuffer jsonBuf;
JsonObject &root = jsonBuf.parseObject(buf);
if (!root.success()) {
Serial.println("parseObject() failed");
printStringWithShift("json error!", 30, font, ' ');
delay(10);
return -1;
}
subscriberCount = root["data"]["follower"];
return 0;
}
// =====================直播参数==================================================
const char *videoHost = "api.live.bilibili.com";
int getVideoData()
{
WiFiClientSecure client;
Serial.print("connecting to "); Serial.println(videoHost);
if (!client.connect(videoHost, 443)) {
Serial.println("connection failed");
return -1;
}
String cmd = String("GET /room/v1/Room/getRoomInfoOld?mid=") + channelId + " HTTP/1.1\r\n" +
"Host: " + videoHost + "\r\nUser-Agent: ESP8266/1.1\r\nConnection: close\r\n\r\n";
client.print(cmd);
int repeatCounter = 10;
while (!client.available() && repeatCounter--) {
Serial.println("y."); delay(500);
}
String line, buf = "";
int startJson = 0, dateFound = 0;
while (client.connected() && client.available()) {
line = client.readStringUntil('\n');
if (line[0] == '{') startJson = 1;
if (startJson) {
for (int i = 0; i < line.length(); i++)
if (line[i] == '[' || line[i] == ']') line[i] = ' ';
buf += line + "\n";
}
if (!dateFound && line.startsWith("Date: ")) {
dateFound = 1;
date = line.substring(6, 22);
h = line.substring(23, 25).toInt();
m = line.substring(26, 28).toInt();
s = line.substring(29, 31).toInt();
localMillisAtUpdate = millis();
localEpoc = (h * 60 * 60 + m * 60 + s);
}
}
client.stop();
DynamicJsonBuffer jsonBuf;
JsonObject &root = jsonBuf.parseObject(buf);
if (!root.success()) {
Serial.println("parseObject() failed");
printStringWithShift("json error!", 30, font, ' ');
delay(10);
return -1;
}
viewCount = root["data"]["online"];
return 0;
}
// =====================播放数参数==================================================
const char *playHost = "api.bilibili.com";
int getplayData()
{
WiFiClientSecure client;
Serial.print("connecting to "); Serial.println(playHost);
if (!client.connect(playHost, 443)) {
Serial.println("connection failed");
return -1;
}
String cmd = String("GET /x/space/upstat?mid=") + channelId + " HTTP/1.1\r\n" +
"Host: " + playHost + "\r\nUser-Agent: ESP8266/1.1\r\nConnection: close\r\n\r\n";
client.print(cmd);
int repeatCounter = 10;
while (!client.available() && repeatCounter--) {
Serial.println("y."); delay(500);
}
String line, buf = "";
int startJson = 0, dateFound = 0;
while (client.connected() && client.available()) {
line = client.readStringUntil('\n');
if (line[0] == '{') startJson = 1;
if (startJson) {
for (int i = 0; i < line.length(); i++)
if (line[i] == '[' || line[i] == ']') line[i] = ' ';
buf += line + "\n";
}
if (!dateFound && line.startsWith("Date: ")) {
dateFound = 1;
date = line.substring(6, 22);
h = line.substring(23, 25).toInt();
m = line.substring(26, 28).toInt();
s = line.substring(29, 31).toInt();
localMillisAtUpdate = millis();
localEpoc = (h * 60 * 60 + m * 60 + s);
}
}
client.stop();
DynamicJsonBuffer jsonBuf;
JsonObject &root = jsonBuf.parseObject(buf);
if (!root.success()) {
Serial.println("parseObject() failed");
printStringWithShift("json error!", 30, font, ' ');
delay(10);
return -1;
}
videoCount = root["data"]["archive"]["view"];
return 0;
}
// =======================================================================
void updateTime()
{
long curEpoch = localEpoc + ((millis() - localMillisAtUpdate) / 1000);
long epoch = round(curEpoch + 3600 * utcOffset + 86400L) % 86400L;
h = ((epoch % 86400L) / 3600) % 24;
m = (epoch % 3600) / 60;
s = epoch % 60;
}
// =======================================================================