#include //wifi库
#include //json解析库
#include //http连接库
#include
#include //u8g2oled库
#include
#define BOARD_I2C_SCL 22
#define BOARD_I2C_SDA 21
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
const char * ID = "XEMOWO_PC";//wifi账号
const char * PASSWORD = "1SUNjiajun";//wifi密码
const char* one;//api第一行内容
const char* two;//api第二行内容
const char* three;//api第三行内容
const char* four;//api第四行内容
String API = "";
String url_xinzhi = "";
String WeatherURL = "";
HTTPClient http;
String GitURL(String api)
{
url_xinzhi = "http://api.xemowo.top/api/yan/yan.php?oled=1";//oled=1则表示使用oled专属格式
return url_xinzhi;
}
void ParseWeather(String url)
{
http.begin(url);
int httpGet = http.GET();
if(httpGet > 0)
{
Serial.printf("HTTPGET is %d",httpGet);
if(httpGet == HTTP_CODE_OK)
{
StaticJsonDocument<500> doc;
String json = http.getString();
Serial.println("解析");
Serial.println(json);
deserializeJson(doc, json);
one = doc["one"]; // "人生就是一列开往坟墓"
two = doc["two"]; // "的列车,路途上会有很"
three = doc["three"]; // "多站,很难有人可以至"
four = doc["four"]; // "始至终陪着走完。\n"
}
else
{
Serial.printf("ERROR1!!");
ParseWeather(WeatherURL);
}
}
else
{
Serial.printf("ERROR2!!");
ParseWeather(WeatherURL);
}
http.end();
}
void setup() {
// put your setup code here, to run once:
u8g2.setFont( u8g2_font_wqy12_t_gb2312 );//设置为中文字体
u8g2.begin();
u8g2.enableUTF8Print();
Serial.begin(115200);
Serial.println("Init u8g2 ....");
Serial.println("WiFi:");
Serial.println(ID);
Serial.println("PASSWORLD:");
Serial.println(PASSWORD);
WiFi.begin(ID,PASSWORD);
u8g2.setCursor (0, 10);
u8g2.println("连接wifi中");
u8g2.sendBuffer();
while(WiFi.status()!=WL_CONNECTED)
{
delay(10000);//延迟大一点,不然联网连不上!
Serial.println("正在连接...");
u8g2.setCursor (0, 10);
u8g2.println("连接wifi中");
u8g2.sendBuffer();
}
Serial.println("连接成功!");
//==================wifi连接==================
u8g2.clearBuffer();
WeatherURL = GitURL(API);
delay(100);
}
void loop() {
ParseWeather(WeatherURL);
Serial.println(WeatherURL);
u8g2.clearBuffer();//清平
delay(10);
u8g2.setCursor (0, 10);
u8g2.println(one);
u8g2.setCursor (0, 25);
u8g2.println(two);
u8g2.setCursor (0, 40);
u8g2.println(three);
u8g2.setCursor (0, 55);
u8g2.println(four);
u8g2.sendBuffer();//将内容上传到缓存区
delay(5000);
}
作者: 小恶魔owo, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-4067534.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论