热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ESP8266tcp透传AP+STA

AP建立WIFI,接受STA连接,串口数据和TCP互传#include<ESP8266WiFi.h>constchar*ssidesp8266_666

AP

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

#include 
  
const char *ssid = "esp8266_666";
const char *password = "12345678";
WiFiServer server(8266);
void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.print("Setting soft-AP ... ");
    
IPAddress softLocal(192,168,1,1);  
IPAddress softGateway(192,168,1,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());
  
}
    float roll, pitch, heading;
void loop()
{
 WiFiClient client = server.available();
 if (client)
  {
    Serial.println("\n[Client connected]");
    while (client.connected())
    {
 
 //  将串口数据打印给TCP
  if(Serial.available()){
    size_t len = Serial.available();
    uint8_t sbuf[len];
    Serial.readBytes(sbuf, len);
   
    client.write(sbuf, len);
    delay(1);
     
    }

    
    roll=roll+10; pitch=pitch+10; heading=heading+10;
    Serial.print("Orientation: ");
    Serial.print(heading);
    Serial.print(" ");
    Serial.print(pitch);
    Serial.print(" ");
    Serial.println(roll);

     delay(1000);
         
  // 将TCP数据打印给串口
      if (client.available())
      {
       //  String line = client.readStringUntil(13);// arduino换行符号  ascll码 13
        String line = client.readStringUntil('\r');
        Serial.println(line);    
          
      }
    }
    delay(1);
 
  //  client.stop();
    Serial.println("[Client disonnected]");
  }
  
 }
  

  

  

STA

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

#include 

const char* ssid = "esp8266_666";
const char* password = "12345678";

const char* host = "192.168.1.1";
  const int httpPort = 8266;
  
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

WiFiClient client;


void setup(void)
{
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s\n", ssid);
  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());


  
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  
}

void loop() {
  
 // client.print("abc\r");
 // delay(1000);
   while(client.available()){
   //int newLine = 13; 
   // String line = client.readStringUntil(13);
    String line = client.readStringUntil('\r');
    Serial.println(line);
  }
  
  }

  

 

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

#include 

const char* ssid = "esp8266_666";
const char* password = "12345678";

const char* host = "192.168.1.1";
  const int httpPort = 8266;
  
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

WiFiClient client;


void setup(void)
{
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s\n", ssid);
  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());


  
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  
}

void loop() {
  

    

    client.print("abc\r");
    delay(1000);
   while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  }

  

  


推荐阅读
author-avatar
你的念头_780
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有