esp32模块像客户端发送数据与TCP/IP数据传输实现代码
时间: 2023-03-25 13:03:33 浏览: 101
你好,以下是回答:
ESP32模块像客户端发送数据的代码示例:
```
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* host = "your_server_address";
const int port = 80;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
WiFiClient client;
if (client.connect(host, port)) {
Serial.println("Connected to server");
client.println("GET / HTTP/1.1");
client.println("Host: your_server_address");
client.println("Connection: close");
client.println();
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
client.stop();
} else {
Serial.println("Failed to connect to server");
}
delay(500);
}
```
TCP/IP数据传输的实现代码示例:
```
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* host = "your_server_address";
const int port = 80;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
WiFiClient client;
if (client.connect(host, port)) {
Serial.println("Connected to server");
client.write("Hello, server!");
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
} else {
Serial.println("Failed to connect to server");
}
delay(500);
}
```
希望能够帮到你!
阅读全文