c_cpp ESP8266 HTTP GET通过WiFi

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp ESP8266 HTTP GET通过WiFi相关的知识,希望对你有一定的参考价值。

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

char ssid[] = "<YOUR SSID>";
char password[] = "<YOUR PSK>";

void setup() {
  Serial.begin(115200);
  while(!Serial) {}

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
  }
  Serial.println("Connected!");

  String result = fetchMessage("<ENDPOINT URL>");
  Serial.println(result);

  WiFi.disconnect();
  Serial.println("Disconnected!");
}

String fetchMessage(char host[]) {
  HTTPClient http;

  http.begin(host);
  int httpCode = http.GET();

  String payload = "";

  if (httpCode > 0) {
    Serial.printf("HTTP GET ... code: %d\n", httpCode);
    Serial.println(HTTP_CODE_OK);
    if (httpCode == HTTP_CODE_OK) {
      payload = http.getString();
    }
  } else {
    Serial.printf("HTTP GET failed, error: %s\n", http.errorToString(httpCode).c_str());
  }

  http.end();
  return payload;
}

void loop() {}

以上是关于c_cpp ESP8266 HTTP GET通过WiFi的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp ESP8266的HTTP请求 - 来自http://bitsofgyan.com/index.php/2016/05/01/http-requests-with-esp8266/

ESP8266通过http post方法获取网络数据

c_cpp ESP8266GCodeSender

c_cpp esp8266-webserver.ino

c_cpp esp8266.tv.ino

c_cpp esp8266oled.ino