1. 程式人生 > 實用技巧 >Arduino ESP8266 傳送HTTP請求 獲取蘇寧伺服器時間

Arduino ESP8266 傳送HTTP請求 獲取蘇寧伺服器時間

參考:傳送HTTP請求

參考:獲取時間

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>


//在這裡輸入你家的WiFi名字和密碼
const char* ssid     = "hg2020"; 

const char* password = "12345678";   

HTTPClient http;

String GetUrl;

String response;

void setup() {

  // 連線到你家的WiFi

  delay(3000);

  Serial.begin(115200);

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }


  Serial.println("");

  Serial.println("WiFi connected");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());


  // 連線蘇寧網站的授時網頁

  GetUrl = "http://quan.suning.com/getSysTime.do";

  http.setTimeout(5000);

  http.begin(GetUrl);


}


void loop() {

  // 從網站獲得網頁內容

  int httpCode = http.GET();

  if (httpCode > 0) {

      Serial.printf("[HTTP] GET... code: %d\n", httpCode);

      if (httpCode == HTTP_CODE_OK) {

        //讀取響應內容

        response = http.getString();
 
        Serial.println(response);

      }

  } else {

      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());

  }

  http.end();

  delay(3000);

}