第16篇ESP32 platformio_arduino框架 wifi联网_连接WiFi热点并连接tcp server收发数据进行通讯

第1篇:Arduino与ESP32开发板的安装方法

第2篇:ESP32 helloword第一个程序示范点亮板载LED

第3篇:vscode搭建esp32 arduino开发环境

第4篇:vscode+platformio搭建esp32 arduino开发环境

​​​​​​第5篇:doit_esp32_devkit_v1使用pmw呼吸灯实验

第6篇:ESP32连接无源喇叭播放音乐《涛声依旧》

第7篇:ESP32连接按钮点亮LED无源喇叭播放声音

​​​​​​第8篇:ESP32连接超声波HC-SR04测距点亮LED无源喇叭播放声音

第9篇:ESP32超声波HC-SR04Arduino类库编写

第10篇:ESP32外部中断功能的使用

第11篇:ESP32vscode_platformio_idf框架helloworld点亮LED

第12篇:ESP32模拟SPI驱动12864LCD_ST7920显示屏

第13篇:ESP32 idf wifi联网使用SNTP同步网络时间LCD ST7920液晶屏显示
第14篇ESP32 idf wifi联网_WiFi STA 模式(连接到WIFI)LCD ST7920液晶屏显示
第15篇ESP32 idf框架 wifi联网_WiFi AP模式_手机连接到esp32开发板

第16篇ESP32 platformio_arduino框架 wifi联网_连接WiFi热点并连接tcp server收发数据进行通讯_第1张图片 

一、WiFi的三种工作模式

ESP32有三种工作模式:AP模式、STA模式以及AP+STA模式。

AP模式就是ESP32产生一个WiFi信号,让电脑来连接;

STA模式就是ESP32去连接一个已有的WiFi(注意,ESP32和电脑要处于同一局域网下,连接同一个热点);

AP+STA模式就是既产生WiFi又连接WiFi。

二、配置WiFi模式
1.STA模式

#include 
#include 
//STA版
void connect_wifi_STA()
{
  
  Serial.begin(115200);
  WiFi.begin(wifi_ssid_STA, wifi_password_STA);         //连接WIFI
  Serial.print("Connected");
  //循环,直到连接成功
  while(WiFi.status() != WL_CONNECTED){
    Serial.println(".");
    delay(500);
  }
  
  IPAddress local_IP = WiFi.localIP();
  Serial.print("WIFI is connected,The local IP address is: "); //连接成功的提示
  Serial.println(local_IP); 
}

连接WiFi热点并连接tcp server收发数据

打开网络调试助手:

第16篇ESP32 platformio_arduino框架 wifi联网_连接WiFi热点并连接tcp server收发数据进行通讯_第2张图片

连接WIFI:

WiFi.mode(WIFI_STA);

  WiFi.begin(ssid,password);

连接tcp server:

client.connect(host, tcpPort)

host:tcp server的IP,这里测试本机电脑的IP

tcpPort:这里设置1234

需要修改为需要连接的WIFI热点:

const char* ssid = "xxx";//WiFi名称

const char* password = "xxx";//WiFi密码

const char* host = "192.168.10.100";//连接 server 的IP地址,网络调试助手进行配置,这里IP需要修改与调试助手一直,设置本机电脑IP

完成示范代码:

#include 

// put function declarations here:

#include 
//#include 
 
const char* ssid = "xxx";//WiFi名称
const char* password = "xxx@";//WiFi密码
const char* host = "192.168.10.100";//连接 server 的IP地址,网络调试助手进行配置
 
WiFiServer server(1234);  //服务器端口号,范围  0-65535
const int tcpPort = 1234;
//WiFiClient client = server.available();
 
void setup() {
  // put your setup code here, to run once:
  int j = 0 ;
  Serial.begin(115200);//初始化波特率
  delay(1000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid,password);
  Serial.print("\r\nConnecting to ");//serial:串口函数,serial.print:经由串口的打印函数
  Serial.print(ssid);
  Serial.println("...");
 
  while(WiFi.status()!= WL_CONNECTED){//返回值由WiFi连接状态决定
    delay(1000);//while循环每一秒检查一次
    Serial.print("Waiting for ");
    Serial.print(j++);
    Serial.println("sss...");
  }
  //delay(1000);
  Serial.println("Mode:STA");
  Serial.println("WIFI connected *_*");
  Serial.println("IP adress:");
  Serial.println(WiFi.localIP());
 
}
int ind=0;
char data[100];
String lint="";
 
 
void loop() {
  // put your main code here, to run repeatedly:
  WiFiClient client = server.available();//创建客户端对象
  
  while (!client.connected())//若未连接到服务端,则客户端进行连接。
    {
        if (!client.connect(host, tcpPort))//实际上这一步就在连接服务端,如果连接上,该函数返回true
        {
            Serial.println("connection....");//串口输出
            delay(2000);
 
        }
    }
 
  if (client.connected())
  {
    Serial.println("Connected to server *_* ");
    while (client.connected())
    {
      while (client.available()>0) 
      {
        data[ind++] = client.read();
       //Serial.write(client.read());
      // client.print(data); //回复收到的数据
       //Serial.print(data);
      }
      ind =0;
      if(data[ind]>0){
        lint = data;
        client.print(lint);//WiFi打印
        Serial.print(lint);
        data[ind]=0;
        Serial.println();
      }
      
     // client.stop();
      
    }
    
  }     
}

 platformio.ini配置:

upload_port COM修改为ESP连接电脑的USB COM口

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
upload_speed = 921600
upload_port = COM8
monitor_port = 115200

你可能感兴趣的:(ESP32趣味开发,网络,esp32)