ESP8266 tcp透传AP+STA

AP

建立WIFI,接受STA连接,串口数据和TCP互传 

1#include <ESP8266WiFi.h> 2 3const char *ssid = "esp8266_666"; 4const char *password = "12345678"; 5WiFiServer server(8266); 6void setup() 7{ 8 Serial.begin(115200); 9 Serial.println(); 10 Serial.print("Setting soft-AP ... "); 11 12IPAddress softLocal(192,168,1,1); 13IPAddress softGateway(192,168,1,1); 14IPAddress softSubnet(255,255,255,0); 15 16WiFi.softAPConfig(softLocal, softGateway, softSubnet); 17WiFi.softAP(ssid, password); 18 19IPAddress myIP = WiFi.softAPIP(); 20 Serial.print("AP IP address: "); 21 Serial.println(myIP); 22 server.begin(); 23 Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); 24 25} 26 float roll, pitch, heading; 27void loop() 28{ 29 WiFiClient client = server.available(); 30 if (client) 31 { 32 Serial.println("\n[Client connected]"); 33 while (client.connected()) 34 { 35 36 // 将串口数据打印给TCP 37 if(Serial.available()){ 38 size_t len = Serial.available(); 39 uint8_t sbuf[len]; 40 Serial.readBytes(sbuf, len); 41 42 client.write(sbuf, len); 43 delay(1); 44 45 } 46 47 48 roll=roll+10; pitch=pitch+10; heading=heading+10; 49 Serial.print("Orientation: "); 50 Serial.print(heading); 51 Serial.print(" "); 52 Serial.print(pitch); 53 Serial.print(" "); 54 Serial.println(roll); 55 56 delay(1000); 57 58 // 将TCP数据打印给串口 59 if (client.available()) 60 { 61 // String line = client.readStringUntil(13);// arduino换行符号 ascll码 13 62 String line = client.readStringUntil('\r'); 63 Serial.println(line); 64 65 } 66 } 67 delay(1); 68 69 // client.stop(); 70 Serial.println("[Client disonnected]"); 71 } 72 73 }

STA

连接WIFI,连接AP,串口数据互传

1#include <ESP8266WiFi.h> 2 3const char* ssid = "esp8266_666"; 4const char* password = "12345678"; 5 6const char* host = "192.168.1.1"; 7 const int httpPort = 8266; 8 9IPAddress staticIP(192,168,1,22); 10IPAddress gateway(192,168,1,1); 11IPAddress subnet(255,255,255,0); 12 13WiFiClient client; 14 15 16void setup(void) 17{ 18 Serial.begin(115200); 19 Serial.println(); 20 21 Serial.printf("Connecting to %s\n", ssid); 22 WiFi.config(staticIP, gateway, subnet); 23 WiFi.begin(ssid, password); 24 while (WiFi.status() != WL_CONNECTED) 25 { 26 delay(500); 27 Serial.print("."); 28 } 29 Serial.println(); 30 Serial.print("Connected, IP address: "); 31 Serial.println(WiFi.localIP()); 32 33 34 35 if (!client.connect(host, httpPort)) { 36 Serial.println("connection failed"); 37 return; 38 } 39 40 41} 42 43void loop() { 44 45 // client.print("abc\r"); 46 // delay(1000); 47 while(client.available()){ 48 //int newLine = 13; 49 // String line = client.readStringUntil(13); 50 String line = client.readStringUntil('\r'); 51 Serial.println(line); 52 } 53 54 }

测试实例 发送  abc/秒,串口打印输出

1#include <ESP8266WiFi.h> 2 3const char* ssid = "esp8266_666"; 4const char* password = "12345678"; 5 6const char* host = "192.168.1.1"; 7 const int httpPort = 8266; 8 9IPAddress staticIP(192,168,1,22); 10IPAddress gateway(192,168,1,1); 11IPAddress subnet(255,255,255,0); 12 13WiFiClient client; 14 15 16void setup(void) 17{ 18 Serial.begin(115200); 19 Serial.println(); 20 21 Serial.printf("Connecting to %s\n", ssid); 22 WiFi.config(staticIP, gateway, subnet); 23 WiFi.begin(ssid, password); 24 while (WiFi.status() != WL_CONNECTED) 25 { 26 delay(500); 27 Serial.print("."); 28 } 29 Serial.println(); 30 Serial.print("Connected, IP address: "); 31 Serial.println(WiFi.localIP()); 32 33 34 35 if (!client.connect(host, httpPort)) { 36 Serial.println("connection failed"); 37 return; 38 } 39 40 41} 42 43void loop() { 44 45 46 47 48 client.print("abc\r"); 49 delay(1000); 50 while(client.available()){ 51 String line = client.readStringUntil('\r'); 52 Serial.print(line); 53 } 54 55 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )