add: cookie to get bilibili view, ram optimization

This commit is contained in:
liuyihui 2022-01-10 03:15:46 +08:00
parent 75739c0f69
commit c5b6d731ba
6 changed files with 56 additions and 76 deletions

View File

@ -6,7 +6,7 @@
#include <utils.h>
// Overlays
int overlaysCount = 1;
const uint8_t overlaysCount = 1;
OverlayCallback overlays[] = { timeOverlay };
void timeOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
@ -15,7 +15,7 @@ void timeOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
}
// frames
int frameCount = 3;
const uint8_t frameCount = 3;
FrameCallback frames[] = { oledWebFrame, oledBilibiliFrame, oledOsFrame };
void oledTitle(OLEDDisplay *display, String s, int16_t x, int16_t y) {
@ -72,5 +72,5 @@ void oledOsFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int
// Version
display->drawString(0+x, 16+y, "Ver: " + Board.Version);
display->drawString(0+x, 30+y, "IP : " + Board.ip.toString());
display->drawString(0+x, 44+y, "Mem: " + (String)(80.0-ESP.getFreeHeap()/1024) + "/80 kB");
display->drawString(0+x, 44+y, "Mem: " + (String)(80.0-ESP.getFreeHeap()/1024.0) + "/80 kB");
}

View File

@ -5,37 +5,6 @@
#include <SSD1306Wire.h>
#include <OLEDDisplayUi.h>
// wifi已连接图标 16*16
const unsigned char Wifi_Connected_Icon[] PROGMEM = {
0x18,0x00,0x0c,0x00,0x8c,0x01,0x86,0x01,0xc6,0x00,0xc3,0x18,0x63,0x18,0x63,0xcc,
0x63,0xcc,0x63,0x18,0xc3,0x18,0xc6,0x00,0x86,0x01,0x8c,0x01,0x0c,0x00,0x18,0x00
};
// wifi未连接图标 16*16
const unsigned char Wifi_Unconnected_Icon[] PROGMEM = {
0x18,0xc0,0x0c,0xe0,0x8c,0x71,0x86,0x39,0xc6,0x1c,0xc3,0x1e,0x63,0x1f,0xe3,0xcf,
0xe3,0xcd,0xe3,0x18,0xf3,0x18,0xfe,0x00,0x9e,0x01,0x8e,0x01,0x0f,0x00,0x1b,0x00
};
// 路由器图标 16*16
const unsigned char Router_Icon[] PROGMEM = {
0x38,0xfc,0x70,0x84,0xe0,0xb4,0xc0,0xa5,0x80,0xa7,0x00,0xa7,0x00,0x86,0x00,0x84,
0x00,0x84,0x00,0x86,0x00,0x87,0x80,0xb7,0xc0,0xb5,0xe0,0x84,0x70,0x84,0x38,0xfc,
};
// 板子图标 16*16
const unsigned char Board_Icon[] PROGMEM = {
0xff,0xff,0x01,0x80,0x3f,0x80,0x23,0x80,0x23,0x80,0x3f,0x80,0x01,0x80,0xff,0xff,
0x00,0x00,0x30,0x0c,0xe0,0x07,0x06,0x60,0x1c,0x38,0xf0,0x0f,0x00,0x00,0x00,0x00
};
// bilibili 24*24
const unsigned char Bilibilitv_Icon[] PROGMEM = {
0x00,0x00,0x02,0x00,0x00,0x03,0x30,0x00,0x01,0xe0,0x80,0x01,
0x80,0xc3,0x00,0x00,0xef,0x00,0xff,0xff,0xff,0x03,0x00,0xc0,
0xf9,0xff,0xdf,0x09,0x00,0xd0,0x09,0x00,0xd0,0x89,0xc1,0xd1,
0xe9,0x81,0xd3,0x69,0x00,0xd6,0x09,0x91,0xd0,0x09,0xdb,0xd0,
0x09,0x7e,0xd0,0x0d,0x00,0xd0,0x4d,0x89,0xdb,0xfb,0xff,0xdf,
0x03,0x00,0xc0,0xff,0xff,0xff,0x78,0x00,0x1e,0x30,0x00,0x0c
};
const uint8_t Active_Symbol_Icon[] PROGMEM = {
B00000000,
B00000000,
@ -64,12 +33,12 @@ void oledMQTTFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, i
void oledBilibiliFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void oledOsFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
extern int frameCount;
extern const uint8_t frameCount;
extern FrameCallback frames[];
void timeOverlay(OLEDDisplay *display, OLEDDisplayUiState* state);
extern int overlaysCount;
extern const uint8_t overlaysCount;
extern OverlayCallback overlays[];
#endif

View File

@ -6,7 +6,7 @@
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>
@ -120,23 +120,24 @@ long weekView = 0;
long monthView = 0;
static String UID = "450115492";
static String followerUrl = "https://api.bilibili.com/x/relation/stat?vmid=" + UID;
static String viewAndLikesUrl = "https://api.bilibili.com/x/space/upstat?mid=" + UID;
static String followerUrl = "http://api.bilibili.com/x/relation/stat?vmid=" + UID;
static String viewAndLikesUrl = "http://api.bilibili.com/x/space/upstat?mid=" + UID;
static String webViewerUrl = "https://api.foolishfox.cn/data/board.json";
static bool clientFlag = false;
static HTTPClient httpFllower;
static HTTPClient httpViewAndLikes;
static HTTPClient httpWebView;
static WiFiClientSecure client;
StaticJsonDocument<512> jsonBuffer;
static const char UserAgent[] PROGMEM = "UserAgent";
static const char Cookie[] PROGMEM = "Cookie";
void getFollower() {
httpFllower.begin(client, followerUrl);
static WiFiClient client;
std::unique_ptr<BearSSL::WiFiClientSecure>clientSecure(new BearSSL::WiFiClientSecure);
StaticJsonDocument<256> jsonBuffer;
int resCode = httpFllower.GET();
static void getFollower() {
HTTPClient http;
http.begin(client, followerUrl);
int resCode = http.GET();
if( resCode == 200 ) {
String resBuffer = httpFllower.getString();
String resBuffer = http.getString();
DeserializationError error = deserializeJson(jsonBuffer, resBuffer);
if( error ) {
@ -150,15 +151,20 @@ void getFollower() {
Serial.printf("[HTTP] GET Follower failed, error: %d\n", resCode);
}
httpFllower.end();
http.end();
}
void getViewAndLikes() {
httpViewAndLikes.begin(client, viewAndLikesUrl);
static void getViewAndLikes() {
HTTPClient http;
http.begin(client, viewAndLikesUrl);
int resCode = httpViewAndLikes.GET();
http.addHeader("Host", "api.bilibili.com");
http.addHeader("User-Agent", UserAgent);
http.addHeader("Cookie", Cookie);
int resCode = http.GET();
if( resCode == 200 ) {
String resBuffer = httpViewAndLikes.getString();
String resBuffer = http.getString();
DeserializationError error = deserializeJson(jsonBuffer, resBuffer);
if( error ) {
@ -173,15 +179,17 @@ void getViewAndLikes() {
Serial.printf("[HTTP] GET View & Likes failed, error: %d\n", resCode);
}
httpViewAndLikes.end();
http.end();
}
void getWebView() {
httpWebView.begin(client, webViewerUrl + "?time=" + ntpTime());
static void getWebView() {
HTTPClient https;
clientSecure->setInsecure();
https.begin(*clientSecure, webViewerUrl);
int resCode = httpWebView.GET();
int resCode = https.GET();
if( resCode == 200 ) {
String resBuffer = httpWebView.getString();
String resBuffer = https.getString();
DeserializationError error = deserializeJson(jsonBuffer, resBuffer);
if( error ) {
@ -197,15 +205,11 @@ void getWebView() {
Serial.printf("[HTTP] GET Web View failed, error: %d\n", resCode);
}
httpWebView.end();
https.end();
}
void getData() {
if( !clientFlag ) {
client.setInsecure();
clientFlag = true;
}
getWebView();
getFollower();
getViewAndLikes();
getWebView();
}

View File

@ -28,17 +28,12 @@ extern BoardInfo Board;
extern long follower;
extern long view;
extern long likes;
extern long dayView;
extern long weekView;
extern long monthView;
void initNtp();
String ntpTime();
void getData();
void getFollower();
void getViewAndLikes();
void getWebView();
#endif

View File

@ -15,6 +15,8 @@ framework = arduino
board_build.f_cpu = 80000000L
upload_speed = 115200
monitor_speed = 115200
; https://docs.platformio.org/en/latest/platforms/espressif8266.html#serial-debug
; build_flags = -DDEBUG_ESP_SSL -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_PORT=Serial
lib_deps =
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1
tzapu/WiFiManager@^0.16.0

View File

@ -13,9 +13,8 @@
#include <Ticker.h>
// Global arguments
Ticker temp;
Ticker task;
int minCount = 0;
uint8_t minCount = 0;
SSD1306Wire display(0x3c, SDA, SCL); // 128 * 64 display
OLEDDisplayUi ui(&display);
@ -28,14 +27,14 @@ void minCountCallBack();
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
temp.attach(1, blink);
task.attach(1, blink);
initOLED();
initWiFi();
initNtp();
temp.detach();
task.detach();
digitalWrite(LED_BUILTIN, LOW);
minCountCallBack();
task.attach(60, minCountCallBack);
@ -60,6 +59,10 @@ void blink() {
void initWiFi() {
WiFiManager manager;
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 32, "WiFi Connecting");
display.display();
manager.setDebugOutput(true);
manager.setRemoveDuplicateAPs(true);
manager.setMinimumSignalQuality(40);
@ -77,8 +80,9 @@ void initWiFi() {
}
void configModeCallback(WiFiManager *config) {
temp.attach(0.1, blink);
task.attach(0.1, blink);
display.clear();
oledTitle(&display, "Info Board", 0, 0);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 16, "SSID : " + (String)WiFiSSID);
@ -108,6 +112,12 @@ void initOLED() {
void minCountCallBack() {
if( minCount%60 == 0 ) {
minCount = 0;
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 32, "Updating Data");
display.display();
getData();
}
minCount += 1;