官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html
STA (客户端)手机连接路由器
S1 *简单的连接WIFI
自己当手机,连接wifi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <ESP8266WiFi.h> void setup() { Serial.begin(115200); Serial.println(); WiFi.begin( "network-name" , "pass-to-network" ); Serial.print( "Connecting" ); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print( "." ); } Serial.println(); Serial.print( "Connected, IP address: " ); Serial.println(WiFi.localIP()); } void loop() {} |
S2 *添加WIFI备用,自动连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /* * This sketch trys to Connect to the best AP based on a given list * */ #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> ESP8266WiFiMulti wifiMulti; void setup() { Serial.begin(115200); Serial.setDebugOutput( true ); Serial.println(); delay(10); wifiMulti.addAP( "dongdong" , "dongdong" ); wifiMulti.addAP( "ssid_from_AP_2" , "your_password_for_AP_2" ); wifiMulti.addAP( "ssid_from_AP_3" , "your_password_for_AP_3" ); Serial.println( "Connecting Wifi..." ); if (wifiMulti.run() == WL_CONNECTED) { Serial.println( "" ); Serial.println( "WiFi connected" ); Serial.println( "IP address: " ); Serial.println(WiFi.localIP()); } } void loop() { if (wifiMulti.run() != WL_CONNECTED) { Serial.println( "WiFi not connected!" ); delay(1000); } } |
S3 连接WIFI,主动设置静态地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <ESP8266WiFi.h> const char * ssid = "dongdong" ; const char * password = "dongdong" ; String name= "DD_Station_01" ; IPAddress staticIP(192,168,1,22); IPAddress gateway(192,168,1,9); IPAddress subnet(255,255,255,0); void setup( void ) { Serial.begin(115200); Serial.println(); Serial. printf ( "Connecting to %s\n" , ssid); WiFi.begin(ssid, password); WiFi.config(staticIP, gateway, subnet); // 修改主机名 WiFi.hostname(name); Serial. printf ( "New hostname: %s\n" , WiFi.hostname().c_str()); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print( "." ); } Serial.println(); Serial.print( "Connected, IP address: " ); Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 Serial. printf ( "SSID: %s\n" , WiFi.SSID().c_str()); // 连接的WIFI名 } void loop() {} |
S4 http连接网络,访问网页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | /* * This sketch sends data via HTTP GET requests to data.sparkfun.com service. * * You need to get streamId and privateKey at data.sparkfun.com and paste them * below. Or just customize this script to talk to other HTTP servers. * */ #include <ESP8266WiFi.h> const char * ssid = "Doit" ; const char * password = "doit3305" ; const char * host = "data.sparkfun.com" ; const char * streamId = "ESPDUINO_STA" ; const char * privateKey = "pzRb9dawqocbP9n0K0M9" ; void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print( "Connecting to " ); Serial.println(ssid); 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()); } int value = 0; void loop() { delay(5000); ++value; Serial.print( "connecting to " ); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println( "connection failed" ); return ; } // We now create a URI for the request String url = "/input/" ; url += streamId; url += "?private_key=" ; url += privateKey; url += "&value=" ; url += value; Serial.print( "Requesting URL: " ); Serial.println(url); // This will send the request to the server client.print(String( "GET " ) + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n" ); delay(10); // Read all the lines of the reply from server and print them to Serial while (client.available()){ String line = client.readStringUntil( '\r' ); Serial.print(line); } Serial.println(); Serial.println( "closing connection" ); } |
AP(服务器) 自己当WIFI
A1 自己当WIFI wifi名称+密码+ IP 不提供服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <ESP8266WiFi.h> const char *ssid = "Charlie Testing AP" ; const char *password = "12345678" ; void setup() { Serial.begin(115200); Serial.println(); Serial.print( "Setting soft-AP ... " ); IPAddress softLocal(192,168,128,1); IPAddress softGateway(192,168,128,1); IPAddress softSubnet(255,255,255,0); WiFi.softAPConfig(softLocal, softGateway, softSubnet); WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); Serial.print( "AP IP address: " ); Serial.println(myIP); } void loop() { delay(3000); } |
A2 1主动设置自己的WIFI wifi名称+密码+ IP 2建立一个服务接收手机的请求和信息 3网页返回给手机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <ESP8266WiFi.h> const char *ssid = "Charlie Testing AP" ; const char *password = "12345678" ; WiFiServer server(80); void setup() { Serial.begin(115200); Serial.println(); Serial.print( "Setting soft-AP ... " ); IPAddress softLocal(192,168,128,1); IPAddress softGateway(192,168,128,1); IPAddress softSubnet(255,255,255,0); WiFi.softAPConfig(softLocal, softGateway, softSubnet); WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); Serial.print( "AP IP address: " ); Serial.println(myIP); server.begin(); Serial. printf ( "Web server started, open %s in a web browser\n" , WiFi.localIP().toString().c_str()); } void loop() { WiFiClient client = server.available(); if (client) { Serial.println( "\n[Client connected]" ); while (client.connected()) { // read line by line what the client (web browser) is requesting if (client.available()) { String line = client.readStringUntil( '\r' ); Serial.print(line); // wait for end of client's request, that is marked with an empty line if (line.length() == 1 && line[0] == '\n' ) { client.println(prepareHtmlPage()); break ; } } } delay(1); // give the web browser time to receive the data // close the connection: client.stop(); Serial.println( "[Client disonnected]" ); } } // prepare a web page to be send to a client (web browser) String prepareHtmlPage() { String htmlPage = String( "HTTP/1.1 200 OK\r\n" ) + "Content-Type: text/html\r\n" + "Connection: close\r\n" + // the connection will be closed after completion of the response "Refresh: 5\r\n" + // refresh the page automatically every 5 sec "\r\n" + "<!DOCTYPE HTML>" + "<html>" + "Analog input: " + String(analogRead(A0)) + "</html>" + "\r\n" ; return htmlPage; } |
STA (客户端)+ AP(服务器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <ESP8266WiFi.h> #include <WiFiUdp.h> /******************* STA 当手机 *****************************/ //设置STA网络参数 IPAddress sip(192, 168, 1, 29); //本地IP IPAddress sip1(192, 168, 1, 1); //本地网关 IPAddress sip2(255, 255, 255, 0); //本地子网掩码 //设置STA const char *ssid = "Netcore_wsn" ; const char *password = "99325408322" ; /**********************************************************/ /******************* AP 当wifi *****************************/ IPAddress xip(192, 168,2, 2); //下位远程IP //设置AP网络参数 IPAddress lxip(192, 168,2, 1); //AP端IP IPAddress lxip1(192, 168,2, 1); //AP端网关 IPAddress lxip2(255, 255,255, 0); //AP端子网掩码 //设置AP账号密码 const char *ssid1 = "Netcore_wsn1" ; //AP wifi名 const char *password1 = "99325408322" ; //AP wifi密码 /**********************************************************/ IPAddress Serverip(192, 168, 1, 4); //上位机远程IP unsigned int localPort = 9999; //本地端口 unsigned int remoteport = 9999; //远程端口 WiFiUDP udp; char packetBuffer[255]; //收发缓冲区 void setup() { Serial.begin(115200); //初始化串口波特率 delay(5000); //延时5S WiFi.mode(WIFI_AP_STA); //设置模式为AP+STA /******************* AP 当WIFI *****************************/ WiFi.softAPConfig(lxip,lxip1,lxip2); //设置AP网络参数 WiFi.softAP(ssid1,password1,1); //设置AP账号密码 /******************************************************************/ Serial.print( "apip:" ); Serial.println(WiFi.softAPIP()); // AP 自己当WIFI 自己设置的内网地址 /******************* STA 当手机连接WIFI *****************************/ WiFi.begin(ssid,password); //连接指定路由 WiFi.config(sip,sip1,sip2); //设置本地网络参数 Serial.print( "Is connection routing, please wait" ); while (WiFi.status()!=WL_CONNECTED) //等待路由连接 { delay(500); Serial.print( "." ); } /******************************************************************/ Serial.println( " " ); udp.begin(localPort); //监听指定端口 Serial.print( "ip:" ); Serial.println(WiFi.localIP()); // STA 当手机连接WIFI 自己设置的静态地址 } void loop() { if (udp.parsePacket()) { udp.read(packetBuffer,255); //读取数据 udp.beginPacket(Serverip,remoteport); udp.write(packetBuffer,255); udp.endPacket(); Serial.println(packetBuffer); udp.beginPacket(xip,remoteport); udp.write(packetBuffer,255); udp.endPacket(); memset (packetBuffer, 0, 255); //清除缓冲器数值 } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律