ESP8266开发(4)——如何将ESP8266设置为TCP客户端接受信息

目录

前言

一、基础设置

二、连接WIFI

三、连接服务器

四、获取信息

五、完整代码

注:关于STA模式的说明


前言

上一篇中写了如何在PC上搭建TCP服务器,本例中作为通信的客户端采用的是ESP8266,本文记录如何将ESP8266作为TCP客户端。

一、基础设置

这里涉及到一些库的引用和全局变量的设置

#include<ESP8266WiFi.h>
#include<WiFiClient.h>
const char*STAssid=""; //这里填需要连接的WIFI名称         
const char*STApassword=""; //这里填需要连接的WIFI密码
const char*host="192.168.1.2"; //这里填服务器IP
const int port=5050; //这里填端口
WiFiClient client; //声明一个客户端对象

二、连接WIFI

连接WIFI,如果没有连上就继续连接,不进行后续操作

void getwifi(){
  while(WiFi.status()!=WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
}

三、连接服务器

如果已经连接了WIFI,下一步连接服务器,如果没有连接上就继续连接,不进行后续操作

void ycconnect(){
  Serial.println("开始连接服务器");
  client.connect(host,port);
  delay(100);
}

四、获取信息

若ESP8266已经连上WIFI且连上服务器,则开始接收服务器发送来的信息

String Read_Tcp(){
  String data="";
  while(client.available()>0){
    data += char(client.read());
    delay(20);
    }
    return data;
}
void Tcp_Handler(String data){
  if(data!=""){
    Serial.print("收到服务器信息:");
    Serial.println(data);
  }
}

五、完整代码

#include<ESP8266WiFi.h>
#include<WiFiClient.h>
const char*STAssid="";         
const char*STApassword=""; 
const char*host="192.168.1.2";
const int port=5050;
WiFiClient client;

//连接路由器
void getwifi(){
  while(WiFi.status()!=WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
}

//连接服务器
void ycconnect(){
  Serial.println("开始连接服务器");
  client.connect(host,port);
  delay(1);
}

void setup() {  
  Serial.begin(9600);
  delay(100); // 保持连接稳定
  WiFi.mode(WIFI_STA); //将ESP8266设置为STA模式
  WiFi.begin(STAssid,STApassword);
  getwifi();
  Serial.println("连接路由器成功");
  client.connect(host,port);
  Serial.println("连接服务器成功");
  delay(100);
}
 
void loop() {
  if(WiFi.status()!=WL_CONNECTED){
    WiFi.disconnect();
    WiFi.begin(STAssid,STApassword);
    getwifi();
  }
  else{
//如果没有连接到服务器
    if(!client.connected()){
      ycconnect();
      return;
    }
  }
  
  Tcp_Handler(Read_Tcp());
}

//读取服务器信息,返回string
String Read_Tcp(){
  String data="";
  while(client.available()>0){
    data += char(client.read());
    delay(20);
    }
    return data;
}

//处理服务器信息
void Tcp_Handler(String data){
  if(data!=""){
    Serial.print("收到服务器信息:");
    Serial.println(data);
  }
}

// 分割字符串的函数
String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

// 求数组长度的函数
template <class T>
int getArrayLen(T& array){
   return (sizeof(array) / sizeof(array[0]));
}

注:关于STA模式的说明

一般WIFI的模式分为AP模式和STA模式

(1)AP(Access Point)提供无线接入服务,允许其它无线设备接入,提供数据访问,AP与AP之间允许相互连接。图2-1 工作在AP的模式下

(2)STA(Station)类似于无线终端,sta本身并不接受无线的接入,它可以连接到AP,一般无线网卡即工作在该模式。图2-2 工作在STA的模式下

(图片来源:WIFI模块中AP模式和STA模式的区别_santirenpc的博客-CSDN博客_sta模式) 

猜你喜欢

转载自blog.csdn.net/qq_41904236/article/details/127139775