AI智能
改变未来

通过WIFI,访问一个网站

 

[code]#include<ESP8266WiFi.h>//WIFI参数const char* ssid = \"Xiaomi_1EDE\";       //你的wifi名const char* password = \"1234567890\";    //你的wifi密码const char* host = \"www.163.com\";       //访问的域名void setup() {// put your setup code here, to run once:Serial.begin(115200);Serial.println();Serial.println();Serial.print(\"Connecting to\");Serial.println(ssid);WiFi.begin(ssid,password);                  //启动WIFIwhile(WiFi.status()!=WL_CONNECTED){         //判断WiFi的连接状态,如果没有连接成功,等待delay(500);Serial.print(\".\");}Serial.println(\"\");Serial.println(\"WiFi connected\");Serial.print(\"IP address:\");Serial.println(WiFi.localIP());    //获取本机的IP}void loop() {// put your main code here, to run repeatedly:WiFiClient client;     //创建客户端const int httpPort = 80;   //端口号为80,if(!client.connect(host,httpPort)){  //如果连接失败,退出Serial.println(\"connection failed\");return ;}client.print(String(\"GET /\")+\" HTTP/1.1\\r\\n\");client.print(String(\"Host: \")+\"www.163.com\\r\\n\");   //这里域名要好上面的一致client.print(\"Connection: close\\r\\n\\r\\n\");delay(500);while(client.available()){String line = client.readStringUntil(\'\\r\');Serial.print(line);}Serial.println();Serial.println(\"closing connection\");delay(5000);}

 

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 通过WIFI,访问一个网站