nodemcu学习记录5

写一个网页客户端,获取百度网页内容

对上节课带代码进行扩展

#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

//定义两个宏 WiFi和密码

#define WIFINAME "BYJY"

#define WIFIPW "13153139496"

#define HTTPIP "14.215.177.37" //百度

#define HTTPPORT 80             //端口

void setup()

{

  // put your setup code here, to run once:

  pinMode(BUILTIN_LED, OUTPUT);

  Serial.begin(115200);

  Serial.println("");

  WiFi.begin(WIFINAME, WIFIPW);

  Serial.println("Connecting...");

  while (WiFi.status() != WL_CONNECTED)

  {

    delay(500);

    Serial.print(".");

  }

  Serial.println();

  Serial.print("Connected, IP Address:");

  Serial.println(WiFi.localIP());

}

//没5秒钟请求依次数据

void loop()

{

  // put your main code here, to run repeatedly:

  HTTPClient http;

  Serial.println("Try link to http.");

  http.begin(HTTPIP, HTTPPORT, "/");

  int code = http.GET();

  if (code)

  {

    Serial.printf("HTTP code : %d\n", code);

    if(code==200)

    {

      String payload = http.getString();

      Serial.printf("%s", payload.c_str());

    }

  }

  else

  {

    Serial.printf("Can't link to server");

  }

  delay(5000);

}

发布了15 篇原创文章 · 获赞 0 · 访问量 231

猜你喜欢

转载自blog.csdn.net/cloudstep/article/details/104170388