第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开发板

一、WiFi的三种工作模式

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

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

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

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

二、配置WiFi模式

1.STA模式

复制代码
#include <Arduino.h>
#include <WiFi.h>
//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收发数据

打开网络调试助手:

连接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 <Arduino.h>

// put function declarations here:

#include <WiFi.h>
//#include <WiFiClient.h>
 
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
相关推荐
TechWayfarer4 小时前
查询IP所在地的3种方案:从API到离线库,风控场景怎么选?
开发语言·网络·python·网络协议·tcp/ip
ylscode7 小时前
微软Exchange Server曝高危零日漏洞:朝鲜黑客利用“Toast攻击“入侵企业邮件系统
网络·安全·web安全
heimeiyingwang7 小时前
【架构实战】可观测性体系:从监控到全链路追踪
网络·数据库·架构
weixin_511840478 小时前
2026年5月4日 OCS技术方案路线选择与优劣深度调研报告
网络·人工智能
绝知此事8 小时前
Netty实战:从零构建高性能TCP通信服务(含心跳检测)
java·网络·spring boot·网络协议·tcp/ip
小初生ZLD9 小时前
AI开发者的网络卡点:Anthropic连接超时实战避坑
网络
Bobolink_9 小时前
跨境网络中“高延迟”问题的技术成因与解决路径
网络·网络优化·跨境网络
呉師傅9 小时前
UPS滴滴告警!如何测量UPS电池内阻【UPS学习】
运维·服务器·网络·学习·电脑
@insist12310 小时前
信息安全工程师-工控安全产品体系与行业实践全解析
网络·安全·软考·信息安全工程师·软件水平考试
段一凡-华北理工大学10 小时前
2026 高炉炼铁智能化技术全景与演进路径~系列文章03:高炉工业数据治理标准化与全生命周期血缘体系
网络·人工智能·高炉炼铁·工业智能体·炉温监测·高炉智能化