Arduio for esp8266
官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html
很有价值的参考文献:http://blog.csdn.net/sadshen/article/details/46883245
参考的例程:https://github.com/SmartArduino/ESPDuino/tree/master/Book
网友参考:http://www.arduino.cn/thread-18958-1-1.html
手机一键配置软件
下载ESPTOUCH软件:https://github.com/EspressifApp/IOT-Espressif-Apk
ESPTOUCH的源码:https://github.com/EspressifApp/EsptouchForAndroid
要想端到端通信两步
1 AP或STA,将自己接入某网络或者自己当网路。
2 建立一个server 或者 client ,在第一里形成的网络里去通信。
1 SF1 STA模式-连接路由器-设置AP-建立sever-手机通过软件 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 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /* WiFiTelnetToSerial - 使用通用异步收发传输器(Universal Asynchronous Receiver/Transmitter,UART)与ESPDUINO进行TCP透传的例子 */ #include <ESP8266WiFi.h> //定义可连接的客户端数目最大值 #define MAX_SRV_CLIENTS 1 const char * ssid = "Doit" ; const char * password = "doit3305" ; WiFiServer server(23); WiFiClient serverClients[MAX_SRV_CLIENTS]; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); Serial.print( "\nConnecting to " ); Serial.println(ssid); uint8_t i = 0; while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); if (i == 21){ //超时(20x500=10000,10秒钟),提示连接失败 Serial.print( "Could not connect to" ); Serial.println(ssid); while (1) delay(500); } //启动UART传输和服务器 server.begin(); server.setNoDelay( true ); Serial.print( "Ready! Use 'telnet " ); Serial.print(WiFi.localIP()); //获得服务器本地IP Serial.println( " 23' to connect" ); } void loop() { uint8_t i; //检测服务器端是否有活动的客户端连接 if (server.hasClient()){ for (i = 0; i < MAX_SRV_CLIENTS; i++){ //查找空闲或者断开连接的客户端,并置为可用 if (!serverClients[i] || !serverClients[i].connected()){ if (serverClients[i]) serverClients[i].stop(); serverClients[i] = server.available(); Serial.print( "New client: " ); Serial.println(i); continue ; } } //若没有可用客户端,则停止连接 WiFiClient serverClient = server.available(); serverClient.stop(); } //检查客户端的数据 for (i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ if (serverClients[i].available()){ //从Telnet客户端获取数据,并推送到URAT端口 while (serverClients[i].available()) Serial.write(serverClients[i].read()); } } } //检查UART端口数据 if (Serial.available()){ size_t len = Serial.available(); uint8_t sbuf[len]; Serial.readBytes(sbuf, len); //将UART端口数据推送到所有已连接的telnet客户端,实现双向通信 for (i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].write(sbuf, len); delay(1); } } } } |
1.2 SF1 STA模式-连接路由器-设置AP-建立sever-手机通过 网页+软件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 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 | #include <ESP8266WiFi.h> const char * ssid = "dongdong" ; const char * password = "dongdong" ; WiFiServer server(80); void setup() { Serial.begin(115200); Serial.println(); <br>IPAddress staticIP(192,168,1,22);<br>IPAddress gateway(192,168,1,9);<br>IPAddress subnet(255,255,255,0);<br> Serial. printf ( "Connecting to %s " , ssid); WiFi.begin(ssid, password);<br><br>WiFi.config(staticIP, gateway, subnet);<br><br> while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print( "." ); } Serial.println( " connected" ); server.begin(); Serial. printf ( "Web server started, open %s in a web browser\n" , WiFi.localIP().toString().c_str()); } // 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; } void loop() { WiFiClient client = server.available(); // wait for a client (web browser) to connect 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]" ); } } |
2 SF2
2.1 SAT模式-连接WIFI-建立client+远程访问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 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include <ESP8266WiFi.h> const char * ssid = "Doit" ; //change to your own ssid const char * password = "doit3305" ; //change to your own password const char * serverIP = "115.29.109.104" ; int serverPort = 6535; WiFiClient client; bool bConnected = false ; char buff[512]; int nm = 0; void setup() { Serial.begin(115200); delay(10); 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()); } void loop() { delay(1); if (bConnected == false ) { if (!client.connect(serverIP, serverPort)) { Serial.println( "connection failed" ); delay(5000); return ; } bConnected = true ; Serial.println( "connection ok" ); } else if (client.available()) { Serial.println( "Data is coming" ); while (client.available()) { buff[nm++] = client.read(); if (nm >= 511) break ; } buff[nm] = 0x00; nm=0; Serial.print(buff); client.print(buff); client.flush(); } } |
2.2 SAT模式-连接WIFI-建立client+远程访问网页
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 | #include <ESP8266WiFi.h> const char * ssid = "********" ; const char * password = "********" ; const char * host = "www.example.com" ; void setup() { Serial.begin(115200); Serial.println(); Serial. printf ( "Connecting to %s " , ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print( "." ); } Serial.println( " connected" ); } void loop() { WiFiClient client; Serial. printf ( "\n[Connecting to %s ... " , host); if (client.connect(host, 80)) { Serial.println( "connected]" ); Serial.println( "[Sending a request]" ); client.print(String( "GET /" ) + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n" + "\r\n" ); Serial.println( "[Response:]" ); while (client.connected()) { if (client.available()) { String line = client.readStringUntil( '\n' ); Serial.println(line); } } client.stop(); Serial.println( "\n[Disconnected]" ); } else { Serial.println( "connection failed!]" ); client.stop(); } delay(5000); } |
3 AF1 AP-Sever
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 | #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;<br>} |
【推荐】国内首个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 绘制太阳,地球,月球 运作规律