一个esp8266开发板向两个esp8266开发板发送数据的代码怎么写
时间: 2024-04-16 19:22:41 浏览: 99
要实现一个ESP8266开发板向两个ESP8266开发板发送数据,你可以使用ESP8266的WiFi功能和网络通信库来实现。以下是一个示例代码,演示了如何发送数据给两个ESP8266开发板:
```cpp
#include <ESP8266WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const IPAddress receiver1IP(192, 168, 1, 100); // 第一个接收器的IP地址
const IPAddress receiver2IP(192, 168, 1, 101); // 第二个接收器的IP地址
const int receiverPort = 1234; // 接收器的端口号
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");
}
void loop() {
if (!client.connected()) {
if (client.connect(receiver1IP, receiverPort)) {
client.println("Hello from sender to receiver 1");
client.stop();
}
else {
Serial.println("Connection to receiver 1 failed");
}
if (client.connect(receiver2IP, receiverPort)) {
client.println("Hello from sender to receiver 2");
client.stop();
}
else {
Serial.println("Connection to receiver 2 failed");
}
delay(5000); // 每隔5秒发送一次数据
}
}
```
请确保将代码中的`your_SSID`和`your_PASSWORD`替换为你的WiFi网络的名称和密码。此外,还需要将`receiver1IP`和`receiver2IP`替换为两个接收器的实际IP地址。
阅读全文