使用esp8266动态监控原神树脂

 

主要代码

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
//温度传感器
#include <DS18B20.h>
#include <string.h>
//网络请求
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
//json解析库
#include <ArduinoJson.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

//wifi的账号密码
#ifndef STASSID
#define STASSID "pi"
#define STAPSK  "pi123456"
#endif
//用户的id与cookie 注意这里的cookie是需要前面有一个空格的
#define COOKIE ""
#define USERID ""

const char* ssid     = STASSID;
const char* password = STAPSK;

//这随便接一个gpio口,然后将gpio口给进去
DS18B20 ds(13);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void showOLED(char* text){
  display.setFont(&FreeSerif9pt7b);
  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(WHITE);        
  display.setCursor(5,20);          
  //反底显示 
  // display.invertDisplay(true); 
  display.drawRect(0, 0, 128, 64, WHITE);
  display.println(text);
  display.display();
}

//生成动态密钥
void getDSkey(){
  const char * AppVersion = "2.0.1";
  const char * APISalt = "4a8knnbk5pbjqsrudp3dq484m9axoc5g";
  
}

ESP8266WiFiMulti WiFiMulti;
void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println("SSD1306 allocation failed");
    for(;;);
  }
  delay(2000);
  showOLED("Version0.1");
  delay(2000); 
  
  //connected wifi
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(ssid, password);
}


void loop() {
  //树脂状态
  int current_resin = 0;
  //判断网络状态
  if (WiFiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print("un connected net");
  }else{
    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
    client->setInsecure();
    HTTPClient https;
    https.begin(*client, "https://api-takumi-record.mihoyo.com/game_record/app/genshin/api/dailyNote?server=cn_gf01&role_id=" USERID); 
    //DS: 1647425427,184277,d8a86c42b97fe128d16bbaf7a61ec55f
    https.addHeader("DS", " 1647421619,144278,5367ff7646e0677dbf795ca42a51e5c8");
    https.addHeader("Cookie", COOKIE);
    https.addHeader("Referer", " https://webstatic.mihoyo.com/app/community-game-records/index.html?v=6");
    https.addHeader("x-rpc-app_version", " 2.16.1");
    https.addHeader("x-rpc-client_type", " 5");
    int httpCode = https.GET();
    if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          //反序列化json
          DynamicJsonDocument doc(1024);
          deserializeJson(doc, payload);
          current_resin = doc["data"]["current_resin"].as<int>();
          Serial.print(payload);
          Serial.printf("current_resin = %d \n",current_resin);
        }
    https.end();
  }
  //  Serial.println(ds.selectNext());
  while (ds.selectNext()) {
    float temp = ds.getTempC();
    Serial.printf("temp:%.2f\n",temp);
    char * info = "x";
    sprintf(info, "temp:%.2f\nResin:%d", temp,current_resin); 
    showOLED(info);
  }
  delay(5000);
}

说明

  • 该代码需要配置cookie,原神游戏id,wifi账号和密码,由于需要在请求头中携带动态生成的DS密钥,md5算法部分后期再进行开发,目前只能算是半成品
  • 主要逻辑部分再loop方法中,请求的是米游社的接口,有大概3-5分钟的延迟,屏幕使用的是SSD1306,代码都比较简单,不难理解