I have been using the esp 32 and the esp 8266 to communicate then using the serial monitor (both are in the same network), the idea is to send and receive messages in both, but when I run the code and send a message, the first message goes as expected, but after the first, the following messages are unable to reach the other esp, I don’t know how to solve this.
Code in esp 32:
#include <WiFi.h>
#include <WiFiClient.h>
const char* ssid = "test";
const char* password = "test";
WiFiServer server(8888);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Conectando-se a ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Conectado à rede Wi-Fi");
Serial.println("Endereço IP: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("Servidor iniciado");
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("Novo cliente conectado");
// Lê os dados enquanto o cliente estiver conectado
while (client.connected()) {
// Verifica se há dados disponíveis para leitura
while (client.available()) {
char c = client.read();
Serial.print(c); // Imprime os dados recebidos no Serial Monitor
}
delay(10); // Pequena pausa para permitir que mais dados sejam recebidos
}
Serial.println("Cliente desconectado");
client.stop(); // Encerra a conexão com o cliente
}
}
code in esp 8266
#include <ESP8266WiFi.h>
const char* ssid = "test";
const char* password = "test";
const char* esp32IP = "";
const int esp32Port = 8888;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Conectando-se a ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Conectado à rede Wi-Fi");
Serial.println("Endereço IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (Serial.available()) {
if (!client.connected()) {
if (client.connect(esp32IP, esp32Port)) {
Serial.println("Conectado ao ESP32");
while (Serial.available()) {
char c = Serial.read();
client.print(c); // Envia a mensagem para o ESP32
}
client.println();
} else {
Serial.println("Falha ao conectar-se ao ESP32");
return;
}
}
}
delay(100);
}
I expect to communicate both in the same network.
Victor Sousa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.