欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3487|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

【零知ESP8266教程】快速入門(mén)18 ESP8266HTTPClient庫(kù) 獲取天氣請(qǐng)求

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:349555 發(fā)表于 2019-10-31 11:47 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

上次我們一起學(xué)習(xí)用ESP8266開(kāi)發(fā)板創(chuàng)建一個(gè)熱點(diǎn),即發(fā)送射頻信號(hào),就像自己的智能手機(jī)可以打開(kāi)熱點(diǎn),使得他人連接,我們智能手機(jī)的角色就是向外發(fā)送射頻信號(hào),然而,用自己的手機(jī)去連接WiFi,那手機(jī)充當(dāng)?shù)慕巧褪墙邮丈漕l信號(hào)的啦。。

同理,零知ESP8266開(kāi)發(fā)板是WiFi模塊,既然有發(fā)送信號(hào)的功能(創(chuàng)建熱點(diǎn)),當(dāng)然也有接收信號(hào)的功能。這次的分享我們來(lái)讓ESP8266開(kāi)發(fā)板接收信息,一起開(kāi)始實(shí)現(xiàn)吧。
一、硬件
電腦,windows系統(tǒng)
零知ESP8266開(kāi)發(fā)板
micro-usb線
二、
(1)軟件庫(kù):
本示例使用零知-ESP8266來(lái)獲取天氣信息,首先需要安裝庫(kù):


也可以在GitHub下載,注意要下載5.~版本

(2)解壓,然后打開(kāi)零知開(kāi)源軟件,界面如下:


(3)安裝到庫(kù)




也可以解壓直接復(fù)制到你lingzhi_library存放的位置

這樣就完成安裝了,屆時(shí)要記得刷新一下,關(guān)閉軟件。

三、
重新打開(kāi)零知開(kāi)源軟件,然后燒錄以下代碼:

  1. <font color="rgb(77, 77, 77)"><font face="&quot;"><font style="font-size: 16px">/**
  2. * Demo:
  3. *    演示Http請(qǐng)求天氣接口信息
  4. * @author 云上上云
  5. * @date 2019/06/01
  6. */
  7. #include <ESP8266WiFi.h>
  8. #include <ArduinoJson.h>
  9. #include <ESP8266HTTPClient.h>

  10. //以下三個(gè)定義為調(diào)試定義
  11. #define DebugBegin(baud_rate)    Serial.begin(baud_rate)
  12. #define DebugPrintln(message)    Serial.println(message)
  13. #define DebugPrint(message)    Serial.print(message)

  14. const char* AP_SSID     = "**********";         //  **********-- 使用時(shí)請(qǐng)修改為當(dāng)前你的 wifi ssid
  15. const char* AP_PSK = "**********";         //  **********-- 使用時(shí)請(qǐng)修改為當(dāng)前你的 wifi 密碼
  16. const char* HOST = "http://api點(diǎn)seniverse<font color="rgb(77, 77, 77)"><font face="&quot;">點(diǎn)</font></font>com";
  17. const char* APIKEY = "wcmquevztdy1jpca";        //API KEY
  18. const char* CITY = "shenzhen";
  19. const char* LANGUAGE = "zh-Hans";//zh-Hans 簡(jiǎn)體中文  會(huì)顯示亂碼

  20. const unsigned long BAUD_RATE = 115200;                   // serial connection speed
  21. const unsigned long HTTP_TIMEOUT = 5000;               // max respone time from server

  22. // 我們要從此網(wǎng)頁(yè)中提取的數(shù)據(jù)的類(lèi)型
  23. struct WeatherData {
  24.   char city[16];//城市名稱(chēng)
  25.   char weather[32];//天氣介紹(多云...)
  26.   char temp[16];//溫度
  27.   char udate[32];//更新時(shí)間
  28. };

  29. HTTPClient http;
  30. String GetUrl;
  31. String response;
  32. WeatherData weatherData;

  33. void setup() {
  34.   // put your setup code here, to run once:
  35.   WiFi.mode(WIFI_STA);     //設(shè)置esp8266 工作模式
  36.   DebugBegin(BAUD_RATE);
  37.   DebugPrint("Connecting to ");//
  38.   DebugPrintln(AP_SSID);
  39.   WiFi.begin(AP_SSID, AP_PSK);   //連接wifi
  40.   WiFi.setAutoConnect(true);
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     //這個(gè)函數(shù)是wifi連接狀態(tài),返回wifi鏈接狀態(tài)
  43.     delay(500);
  44.     DebugPrint(".");
  45.   }
  46.   DebugPrintln("");
  47.   DebugPrintln("WiFi connected");
  48.   DebugPrintln("IP address: " + WiFi.localIP());

  49.   //拼接get請(qǐng)求url  
  50.   GetUrl = String(HOST) + "/v3/weather/now.json?key=";
  51.   GetUrl += APIKEY;
  52.   GetUrl += "&location=";
  53.   GetUrl += CITY;
  54.   GetUrl += "&language=";
  55.   GetUrl += LANGUAGE;
  56.   //設(shè)置超時(shí)
  57.   http.setTimeout(HTTP_TIMEOUT);
  58.   //設(shè)置請(qǐng)求url
  59.   http.begin(GetUrl);
  60.   //以下為設(shè)置一些頭  其實(shí)沒(méi)什么用 最重要是后端服務(wù)器支持
  61.   http.setUserAgent("esp8266");//用戶代理版本
  62.   http.setAuthorization("esp8266","yssy");//用戶校驗(yàn)信息
  63. }

  64. void loop() {
  65.   //心知天氣  發(fā)送http  get請(qǐng)求
  66.   int httpCode = http.GET();
  67.   if (httpCode > 0) {
  68.       Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  69.       //判斷請(qǐng)求是否成功
  70.       if (httpCode == HTTP_CODE_OK) {
  71.         //讀取響應(yīng)內(nèi)容
  72.         response = http.getString();
  73.         DebugPrintln("Get the data from Internet!");
  74.         DebugPrintln(response);
  75.         //解析響應(yīng)內(nèi)容
  76.         if (parseUserData(response, &weatherData)) {
  77.           //打印響應(yīng)內(nèi)容
  78.           printUserData(&weatherData);
  79.         }
  80.       }
  81.   } else {
  82.       Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  83.   }
  84.   http.end();
  85.   delay(1000);//每1s調(diào)用一次
  86. }

  87. /**
  88. * @Desc 解析數(shù)據(jù) Json解析
  89. * 數(shù)據(jù)格式如下:
  90. * {
  91. *    "results": [
  92. *        {
  93. *            "location": {
  94. *                "id": "WX4FBXXFKE4F",
  95. *                "name": "北京",
  96. *                "country": "CN",
  97. *                "path": "北京,北京,中國(guó)",
  98. *                "timezone": "Asia/Shanghai",
  99. *                "timezone_offset": "+08:00"
  100. *            },
  101. *            "now": {
  102. *                "text": "多云",
  103. *                "code": "4",
  104. *                "temperature": "23"
  105. *            },
  106. *            "last_update": "2017-09-13T09:51:00+08:00"
  107. *        }
  108. *    ]
  109. *}
  110. */
  111. bool parseUserData(String content, struct WeatherData* weatherData) {
  112. //    -- 根據(jù)我們需要解析的數(shù)據(jù)來(lái)計(jì)算JSON緩沖區(qū)最佳大小
  113. //   如果你使用StaticJsonBuffer時(shí)才需要
  114. //    const size_t BUFFER_SIZE = 1024;
  115. //   在堆棧上分配一個(gè)臨時(shí)內(nèi)存池
  116. //    StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  117. //    -- 如果堆棧的內(nèi)存池太大,使用 DynamicJsonBuffer jsonBuffer 代替
  118.   DynamicJsonBuffer jsonBuffer;

  119.   JsonObject& root = jsonBuffer.parseObject(content);

  120.   if (!root.success()) {
  121.     DebugPrintln("JSON parsing failed!");
  122.     return false;
  123.   }

  124.   //復(fù)制我們感興趣的字符串
  125.   strcpy(weatherData->city, root["results"][0]["location"]["name"]);
  126.   strcpy(weatherData->weather, root["results"][0]["now"]["text"]);
  127.   strcpy(weatherData->temp, root["results"][0]["now"]["temperature"]);
  128.   strcpy(weatherData->udate, root["results"][0]["last_update"]);
  129.   //  -- 這不是強(qiáng)制復(fù)制,你可以使用指針,因?yàn)樗麄兪侵赶颉皟?nèi)容”緩沖區(qū)內(nèi),所以你需要確保
  130.   //   當(dāng)你讀取字符串時(shí)它仍在內(nèi)存中
  131.   return true;
  132. }

  133. // 打印從JSON中提取的數(shù)據(jù)
  134. void printUserData(const struct WeatherData* weatherData) {
  135.   DebugPrintln("Print parsed data :");
  136.   DebugPrint("City : ");
  137.   DebugPrint(weatherData->city);
  138.   DebugPrint(", \t");
  139.   DebugPrint("Weather : ");
  140.   DebugPrint(weatherData->weather);
  141.   DebugPrint(",\t");
  142.   DebugPrint("Temp : ");
  143.   DebugPrint(weatherData->temp);
  144.   DebugPrint(" C");
  145.   DebugPrint(",\t");
  146.   DebugPrint("Last Updata : ");
  147.   DebugPrint(weatherData->udate);
  148.   DebugPrintln("\r\n");
  149. }</font></font></font>
復(fù)制代碼


2、驗(yàn)證,上傳程序。

四、點(diǎn)擊“調(diào)試”,就可看到結(jié)果啦,如下圖:



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表