In this article, we will introduce to you the information related to the use of ESP8266 and OLED screen to display B station through pictures, text and videos.
Effect
Preparation
esp82660.96-inch OLED screen DuPont line several B stations id
Experimental Environment
esp8266 version 2.7.1U8g2_ArduinoTimeArduinoJsonArduinoHttpClient If the installation fails due to network reasons when installing these libraries, you can directly download the offline library and place it in the libraries directory under the IDE.
Code
/***************************************************************************
ESP8266 with 0.96inch OLED pin
VCC --- VCC
GND --- GND
SDA --- SDA(D1)
SCL --- SCL(D2)
******************************************************************
#if defined(ESP32) //ESP32
#include WiFi.h
#include HTTPClient.h
#include WebServer.h
#include ESPmDNS.h
#elif defined(ESP8266) //ESP8266
#include ESP8266WiFi.h
#include ESP8266HTTPClient.h
#include ESP8266WebServer.h
#include ESP8266mDNS.h
#else
#error 'Please check your mode setting,it must be esp8266 or esp32.'
#endif
#include ArduinoJson.h
#include U8g2lib.h
#include Wire.h
#include Ticker.h
//Timer
Ticker timer;
int count=0;
boolean flag=true;
//JSON
DynamicJsonBuffer jsonBuffer(256); //ArduinoJson V5
//Display screen If the pins are different, you need to modify it here
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/D2, /* data=*/D1);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/D2, /* data=*/D1, /* reset=*/U8X8_PIN_NONE); //Here D1 D2 is the corresponding welding pin
//WiFi name and password
const char *ssid='PDCN'; //Fill in your wifi name here
const char *password='1234567890';//Fill in your wifi password here
//Dot map of 24*24 small TV
const unsigned char bilibilitv_24u[] U8X8_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
};
//B station API URL : follow, view, likes
String NAME='Xiaoyaozi'; //Change it into your own name
String UID='430579369'; //Change it to your own UID
String followUrl='http://api.bilibili.com/x/relation/stat?vmid=' + UID; //Number of fans
String viewAndLikesUrl='http://api.bilibili.com/x/web-interface/card?mid=' + UID; //Number of plays and likes
long follower=0; //Number of fans
long view=0; //Number of plays
long likes=0; //Number of likes received
void setup()
{
//OLED initialization
u8g2.begin();
u8g2.enableUTF8Print();
u8g2.clearDisplay();
u8g2.setFont(u8g2_font_wqy12_t_gb2312a);
u8g2.drawXBMP( 16 , 9 , 24 , 24 , bilibilitv_24u );
u8g2.setCursor(45, 19);
u8g2.print('Powered by');
u8g2.setCursor(45, 31);
u8g2.print('Xiaoyaozi's big cousin');
u8g2.setFont(u8g2_font_wqy12_t_gb2312a);
u8g2.setCursor(10, 50);
u8g2.print('blog.bbskali.cn');
u8g2.sendBuffer();
delay(5000);
u8g2.setFont(u8g2_font_wqy12_t_gb2312b);
u8g2.setFontPosTop();
u8g2.clearDisplay();
Serial.begin(115200);
//WiFi connection
WiFi.begin(ssid, password);
while (WiFi.status() !=WL_CONNECTED)
{
delay(500);
Serial.print('.');
}
Serial.println('');
Serial.println('WiFi connected');
timer.attach(60, timerCallback); //Every 1 minute
//The first call to obtain the data function, which is convenient for display when powered on
getFollower(followerUrl);
getViewAndLikes(viewAndLikesUrl);
}
void loop()
{
While (flag)
{
if (count==0)
{
//display data
Serial.println('count=0, display data');
u8g2.firstPage();
do
{
display(follower, likes, view);
} while (u8g2.nextPage());
flag=false;
} else if (count==1) {
//get follower
Serial.println('count=1, get follower');
getFollower(followerUrl);
flag=false;
} else if (count==2) {
//get view and likes
Serial.println('count=2, get view and likes');
getViewAndLikes(viewAndLikesUrl);
flag=false;
}
}
}
//Timer callback function
void timerCallback()
{
count++;
if (count==3)
{
count=0;
}
flag=true;
}
//Get the number of fans on B station
void getFollower(String url)
{
HTTPClient http;
http.begin(url);
int httpCode=http.GET();
Serial.printf('[HTTP] GET. code: %d\n', httpCode);
if (httpCode==200)
{
Serial.println('Get OK');
String resBuff=http.getString();
//---------- ArduinoJson V5 ----------
JsonObject root=jsonBuffer.parseObject(resBuff);
if (!root.success())
{
Serial.println('parseObject() failed');
return;
}
follower=root['data']['follower'];
Serial.print('Fans:');
Serial.println(follower);
}
else
{
Serial.printf('[HTTP] GET. failed, error: %d\n', httpCode);
}
http.end();
}
//Get the number of plays and likes on B station
void getViewAndLikes(String url)
{
HTTPClient http;
http.begin(url);
int httpCode=http.GET();
Serial.printf('[HTTP] GET. code: %d\n', httpCode);
if (httpCode==200)
{
Serial.println('Get OK');
String resBuff=http.getString();
//---------- ArduinoJson V5 ----------
JsonObject root=jsonBuffer.parseObject(resBuff);
if (!root.success())
{
Serial.println('parseObject() failed');
return;
}
likes=root['data']['like_num'];
view=root['data']['archive_count'];
Serial.print('Likes:');
Serial.println(likes);
Serial.print('View:');
Serial.println(view);
}
else
{
Serial.printf('[HTTP] GET. failed, error: %d\n', httpCode);
}
http.end();
}
//OLED display data
void display(long follower, long likes, long view)
{
u8g2.clearDisplay();
u8g2.setCursor(5, 25);
u8g2.print('Number of fans:' + String(follower));
u8g2.setCursor(5, 39);
u8g2.print('Number of likes:' + String(likes));
u8g2.setCursor(5, 52);
u8g2.print('Number of manuscripts:' + String(view));
u8g2.setCursor(5, 6);
u8g2.print('bilibili@' + String(NAME)); //Change it to your own name
}
Summary of Problems
After the code is uploaded, this problem occurs when the display screen does not light up. Check whether your connection is normal. Whether the corresponding pin D1 D2 is suitable. Of course, you can modify the following commands to customize the pins.
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/D2, /* data=*/D1, /* reset=*/U8X8_PIN_NONE); Regarding the interface, most API interfaces on Bilibili are currently https. For https we need to configure the certificate information in esp8266. More troublesome. I searched the interface in the article for a long time before I found two interfaces of the available http protocol. (Of course, it will also hang up at any time). So, on this basis, the way I thought of is to save the json data from the API interface locally using python. Then just access the json data locally. At the same time, do scheduled tasks to update data in real time.
Recommended Comments