Hello. I am a novice to programing. I copied this website schematics and code , I am c connecting two Esp32 through wifi , to control a blink LED. first esp32 is the push button and the second is the LED . On the first ESP32 I have this issue after uploading the sketch Failed to reconnect to TCP server. The second one is good and and working. https://esp32io.com/tutorials/communication-between-two-esp32#google_vignette
22:17:13.532 -> Connection is disconnected
22:17:16.490 -> Failed to reconnect to TCP server
22:17:16.554 -> Connection is disconnected
22:17:19.503 -> Failed to reconnect to TCP server
22:17:19.535 -> Connection is disconnected
22:17:22.494 -> Failed to reconnect to TCP server
// ESP32: TCP CLIENT + A BUTTON/SWITCH
#include <ezButton.h>
#include <WiFi.h>
#define BUTTON_PIN 21 // ESP32 pin GPIO21 connected to button
const char* ssid = "YOUR_WIFI_SSID"; // CHANGE TO YOUR WIFI SSID
const char* password = "YOUR_WIFI_PASSWORD"; // CHANGE TO YOUR WIFI PASSWORD
const char* serverAddress = "192.168.0.180"; // CHANGE TO ESP32#2'S IP ADDRESS
const int serverPort = 4080;
ezButton button(BUTTON_PIN); // create ezButton
WiFiClient TCPclient;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
Serial.println("ESP32: TCP CLIENT + A BUTTON/SWITCH");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// connect to TCP server (Arduino #2)
if (TCPclient.connect(serverAddress, serverPort)) {
Serial.println("Connected to TCP server");
} else {
Serial.println("Failed to connect to TCP server");
}
}
void loop() {
button.loop(); // MUST call the loop() function first
if (!TCPclient.connected()) {
Serial.println("Connection is disconnected");
TCPclient.stop();
// reconnect to TCP server (Arduino #2)
if (TCPclient.connect(serverAddress, serverPort)) {
Serial.println("Reconnected to TCP server");
} else {
Serial.println("Failed to reconnect to TCP server");
}
}
if (button.isPressed()) {
TCPclient.write('1');
TCPclient.flush();
Serial.println("- The button is pressed, sent command: 1");
}
if (button.isReleased()) {
TCPclient.write('0');
TCPclient.flush();
Serial.println("- The button is released, sent command: 0");
}
}
// ESP32 #2: TCP SERVER + AN LED
#include <WiFi.h>
#define LED_PIN 18 // ESP32 pin GPIO18 connected to LED
#define SERVER_PORT 4080
const char* ssid = "YOUR_WIFI_SSID"; // CHANGE TO YOUR WIFI SSID
const char* password = "YOUR_WIFI_PASSWORD"; // CHANGE TO YOUR WIFI PASSWORD
WiFiServer TCPserver(SERVER_PORT);
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Serial.println("ESP32 #2: TCP SERVER + AN LED");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print your local IP address:
Serial.print("ESP32 #2: TCP Server IP address: ");
Serial.println(WiFi.localIP());
Serial.println("ESP32 #2: -> Please update the serverAddress in ESP32 #1 code");
// Start listening for a TCP client (from ESP32 #1)
TCPserver.begin();
}
void loop() {
// Wait for a TCP client from ESP32 #1:
WiFiClient client = TCPserver.available();
if (client) {
// Read the command from the TCP client:
char command = client.read();
Serial.print("ESP32 #2: - Received command: ");
Serial.println(command);
if (command == '1')
digitalWrite(LED_PIN, HIGH); // Turn LED on
else if (command == '0')
digitalWrite(LED_PIN, LOW); // Turn LED off
Serial.println("- The button is released, sent command: 0");
}