image.png
ESP8266获取实时油价信息
原理:ESP8266联网后,访问油价API,返回相关的油价信息,并显示到屏幕上
【实用】⭐
【难度】
【好玩】



【API解析】


API文档 http://api.xemowo.top/apidetail/21.html
示例API http://api.xemowo.top/api/oil.php?city=guangdong

请求参数说明:
city 查询城市的拼音 必须

返回参数说明:
city 查询城市
oil_92 92号汽油
oil_95 95号汽油
oil_98 98号柴油
oil_0 0号柴油

返回示例:
{"city":"广东","oil_92":"7.68","oil_95":"8.32","oil_98":"9.26","oil_0":"7.34"}
复制代码


【代码解析】


代码下载 https://wwu.lanzouj.com/ijWXg1arsa7e

所需库文件:
库文件下载 https://wwu.lanzouj.com/iTyAz1er1h3e
image.png


填写API地址:
image.png

自动配网:
image.png

连接WiFi并测试WiFi可用性:
image.png

发起API请求:
image.png

解析API:
image.png

屏幕输出内容:
image.png

1.gif
【自动配网】
1.gif

1.配置WIFI
image.png

2.选择wifi,并且输入密码
image.png

3.保存后,自动重启连接wifi
image.png


1.gif
【示例代码】
1.gif

//转载请保留联系方式!
  • //恶魔联系方式↓
  • //QQ:3046981740
  • //邮箱:xemowo@qq.com
  • //详细教程/文件下载:http://api.xemowo.top

  • #include <ArduinoJson.h>
  • #include <ESP8266WiFi.h>
  • #include <ESP8266HTTPClient.h>
  • #include <ESP8266WebServer.h>
  • #include <WiFiManager.h>
  • #include <U8g2lib.h>           //屏幕库
  • U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0 , U8X8_PIN_NONE);
  • //屏幕D1--SCL  D2--SDA
  • //任何找不到的库文件,都可以联系恶魔,发蓝奏云网盘

  • WiFiClient client;


  • #define URL "http://www.example.com"  
  • //自动配网
  • //测试网络是否可用

  • #define LZURL "hAttp://api.xemowo.top/include/oil.php?city=guangdong"
  • //API接口
  • //city后面接城市的拼音,以省份为准,例如:广东省=guangdong


  • void setup() {
  •   u8g2.begin();
  •   u8g2.clearBuffer();// 清空显示设备内部缓冲区
  •   u8g2.setFont(u8g2_font_wqy12_t_gb2312);
  •   Serial.begin(115200);
  •   Serial.println("加载完成");
  •   u8g2.firstPage();
  •   do
  •   {

  •     u8g2.enableUTF8Print();//enable UTF8
  •     u8g2.setCursor(0, 10);
  •     u8g2.print("【ESP8266_AUTO】");
  •     u8g2.setCursor(0, 25);
  •     u8g2.print(">192.168.4.1");
  •     u8g2.setCursor(0, 40);
  •     u8g2.print(">请连接WIFI并配置信息");
  •     u8g2.setCursor(0, 55);
  •     u8g2.print("【恶魔工作室】");

  •   }

  •   while (u8g2.nextPage());
  •   // 连接WiFi
  •   // 建立WiFiManager对象
  •   WiFiManager wifiManager;

  •   // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
  •   wifiManager.autoConnect("ESP8266_AUTOTEST");

  •   Serial.print("WiFi Connected!");

  •   httpClientRequest();
  •   //connectWiFi();

  • }





  • // 发送HTTP请求并且将服务器响应通过串口输出
  • void httpClientRequest() {


  •   //重点1 创建 HTTPClient 对象
  •   HTTPClient httpClient;

  •   //重点2 通过begin函数配置请求地址。此处也可以不使用端口号和PATH而单纯的
  •   httpClient.begin(client, URL);
  •   Serial.print("URL: "); Serial.println(URL);

  •   //重点3 通过GET函数启动连接并发送HTTP请求
  •   int httpCode = httpClient.GET();
  •   Serial.print("Send GET request to URL: ");
  •   Serial.println(URL);

  •   //重点4. 如果服务器响应HTTP_CODE_OK(200)则从服务器获取响应体信息并通过串口输出
  •   //如果服务器不响应HTTP_CODE_OK(200)则将服务器响应状态码通过串口输出
  •   if (httpCode == HTTP_CODE_OK) {
  •     // 使用getString函数获取服务器响应体内容
  •     String responsePayload = httpClient.getString();
  •     Serial.println("Server Response Payload: ");
  •     Serial.println(responsePayload);
  •   } else {
  •     Serial.println("Server Respose Code:");
  •     Serial.println(httpCode);
  •   }

  •   //重点5. 关闭ESP8266与服务器连接
  •   httpClient.end();
  • }







  • void loop() {
  • wechat();
  • //循环

  • }




  • void wechat() {

  •   Serial.println("励志短语");
  •   //新建有一个HTTPClient的对象httpClient
  •   HTTPClient LZClient;
  •   Serial.print("LZURL: "); Serial.println(LZURL);
  •   //设定请求的url;
  •   LZClient.begin(client, LZURL);
  •   //此处我是访问局域网的一个web地址,请根据实际情况对上面的链接进行更换

  •   //设定get请求,并且返回请求码;

  •   int Code = LZClient.GET();
  •   Serial.print(Code);
  •   //设定浏览器ua
  • LZClient.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.200");

  •   //如果get请求返回HTTP_CODE_OK,则代表和服务器请求成功;
  •   // if (Code==HTTP_CODE_OK){

  •   //获取请求url的浏览器html代码;
  •   String resp = LZClient.getString();

  •   //向串口输出html代码

  •   Serial.println(resp);

  • // Stream& input;

  • StaticJsonDocument<192> doc;

  • DeserializationError error = deserializeJson(doc, resp);

  • if (error) {
  •   Serial.print(F("deserializeJson() failed: "));
  •   Serial.println(error.f_str());
  •   return;
  • }


  • //解析json信息
  • const char* city = doc["city"]; // "广东"
  • const char* oil_92 = doc["oil_92"]; // "8.45"
  • const char* oil_95 = doc["oil_95"]; // "9.15"
  • const char* oil_98 = doc["oil_98"]; // "10.29"
  • const char* oil_0 = doc["oil_0"]; // "8.12"








  •   LZClient.end();
  • //结束访问


  •   u8g2.firstPage();
  •   do
  •   {

  •     u8g2.enableUTF8Print();//enable UTF8
  •   //  u8g2.setCursor(110, 55);
  •    // u8g2.print("[3]");
  •     u8g2.setCursor(0, 10);
  •     u8g2.print("【92油价】");
  •     u8g2.setCursor(60, 10);
  •     u8g2.print(oil_92);
  •     u8g2.setCursor(0, 25);
  •     u8g2.print("【95油价】");
  •     u8g2.setCursor(60, 25);
  •     u8g2.print(oil_95);
  •     u8g2.setCursor(0, 40);
  •     u8g2.print("【98油价】");
  •     u8g2.setCursor(60, 40);
  •     u8g2.print(oil_98);
  •     u8g2.setCursor(0, 55);
  •     u8g2.print("【0油价】");
  •     u8g2.setCursor(60, 55);
  •     u8g2.print(oil_0);

  •   }

  •   while (u8g2.nextPage());
  • }
  • 复制代码

    1.gif
    【注意】
    1.gif
    API为免费使用,请勿攻击,请勿商用
    有任何不懂,请发邮件xemowo@qq.com