Arduino ESP8266/ESP32 TCP/UDP通讯例程
- 🔧需要配合上位机软件:
网络调试助手
(http://www.cmsoft.cn/software.html
)
📝ESP8266/ESP32 作为TCP客户端使用
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiClient.h>
const char* ssid = "########";
const char* password = "********";
const char* serverIP = "192.168.0.113";
const int serverPort = 7777;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Connected");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
Serial.print("Connecting to server ");
Serial.print(serverIP);
Serial.print(":");
Serial.println(serverPort);
if (client.connect(serverIP, serverPort)) {
Serial.println("Connected to server");
} else {
Serial.println("Connection failed");
}
}
void loop() {
if (client.connected()) {
if (client.available())
{
String response = client.readStringUntil('\n');
Serial.print("Server response: ");
Serial.println(response);
}
} else {
Serial.println("Connection lost");
client.connect(serverIP, serverPort);
}
}
📝ESP8266/ESP32 作为TCP服务端使用
/*
* 在ESP8266/32上使用Arduino编程语言开发TCP服务端,可以使用Serial.println()函数来发送调试信息到串口。在接收和发送调试信息的过程中,可以使用以下代码作为参考:
* 首先连接到Wi-Fi网络,然后创建一个TCP服务器并开始监听连接。在主循环中,通过server.available()检查是否有客户端连接,如果有则进入内部循环。在内部循环中,通过client.available()检查是否有数据可读取,如果有,则读取并打印接收到的请求信息,并通过client.println()发送响应。当客户端断开连接时,通过client.stop()停止连接,并打印相应的调试信息。
可以通过串口监视器查看ESP8266/32发送和接收的调试信息。在Arduino IDE中,选择对应的串口和波特率,然后点击串口监视器按钮即可查看。
*
*/
const char* ssid = "########";//填写wifi,名称
const char* password = "********";//填写wifi,密码
WiFiServer server(7777);//服务端端口号
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Connected");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());//打印本机IP地址
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New client connected");
while (client.connected()) {
if (client.available()) {
String request = client.readStringUntil('\r');
Serial.print("Received request: ");
Serial.println(request);
client.println("Hello from ESP Device");
Serial.println("Response sent");
}
}
client.stop();
Serial.println("Client disconnected");
}
}
📘ESP8266/32 UDP通讯
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiUdp.h>
WiFiUDP udp;
unsigned int localPort = 7777;
const char* serverIP = "192.168.0.113";
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
const char* ssid = "########";
const char* password = "********";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
udp.begin(localPort);
}
void loop() {
int packetSize = udp.parsePacket();
if (packetSize) {
int len = udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
if (len > 0) {
packetBuffer[len] = 0;
Serial.print("Received packet: ");
Serial.println(packetBuffer);
}
}
udp.beginPacket(serverIP, localPort);
udp.write("Hello from ESP Device");
udp.endPacket();
delay(1000);
}