I am trying to convert serial data to TCP and vice versa. I am connected to WiFi – I can ping and connect to my ESP32 server, but as soon as I try to send any data from TCP to the serial port I get “Connection refused by remote host”
I am using the Hercules tool which gives this response: (tried to paste an image here) – the text is as follows: Sending ICMP ECHO REQUEST to module
Received ICMP ECHO REPLY
Connecting to 192.168.0.234 …
Connected to 192.168.0.234
Sending ASCII DATASending ASCII DATA
Connection refused by remote host
Connection closed
My Loop code is this:
// Check for data from Serial2 and send it to the WiFi BellClient
if (Serial2.available()) {
size_t len = Serial2.available();
char sbuf[len];
Serial2.readBytes(sbuf, len);
Serial.print(“Sending data to BellClient: “);
for (size_t i = 0; i < len; i++) {
Serial.print(sbuf[i]);
}
Serial.println();
if (BellClient && BellClient.connected()) {
BellClient.write((const uint8_t*)sbuf, len);
}
}
// Check for data from the WiFi BellClient and send it to Serial2
if (BellClient.connected() && BellClient.available()) {
while (BellClient.available()) {
char incomingData = BellClient.read();
Serial.print(“Received data from BellClient: “);
Serial.print(incomingData);
Serial2.write(incomingData);
}
}
Any good suggestions?
Hi.
I’m sorry for taking so long to get back to you. For some reason, I didn’t receive a notification of your question.
Are you following any particular project/tutorial?
Regards,
Sara
Not really. Part of this is included in your WEB-server book with the button control section 2.3 using the <WiFi.h>. However this TCP conversion is not directly connected to the WEB-server part. It is a simple logon problem similar to a WEB socket connection (I think) – except that this is WiFi. I was just hoping I did something stupid and just had overlooked it…
Hi again.
I’m sorry, but I don’t think we have something similar in our projects…
I never tested that kind of connection in that way. Maybe it is better to try to search for a simple working example first, before trying to integrate it with other projects.
Can you better describe what you’re trying to achieve?
Regards,
Sara
Hi Sara. Yes – good idea – the KISS principle.. I’m sorry to disturb with this – but maybee an idea for you? I am trying to setup this WEB-server where I upload the button controls via a UART to TCP connection and read back the response. I will try to get the UART- to TCP working first, since your WEB-server button control project allready works. I’ll let you know what I am doing wrong… 🙂
I finally got it working. The reason I got “Connection refused by remote host” is still a mystery for me. Perhaps I simply did not start the server?
Here is a brief summary of the core code.
You need to get connected to WiFI – not shown here. This is the conversion UART to TCP and vice versa.
#include <WiFi.h>
#include <SPIFFS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
WiFiClient Client;
WiFiServer ESP32server(Port); // Set the ESP32 server port
bool isClientConnected;
void handleClientConnection() {
if (isClientConnected) {
isClientConnected = true;
return;
}
// Check for new client connections on port
if (!Client.connected()) {
if (isClientConnected) {
Serial.println(“Client disconnected”);
isClientConnected = false;
}
Client = ESP32server.available();
if (Client.connected()) {
Serial.println(“New Client connected”);
isClientConnected = true;
}
}
}
void setup() {
Serial.begin(115200);
ESP32server.begin(Port);
}
void loop() {
handleClientConnection();
// Forward data from Client to Serial1 (UART)
while (Client.available() && Serial1.availableForWrite()) {
char c = Client.read();
Serial1.write(c);
}
// Forward data from Serial1 (UART) to Client
while (Serial1.available() && Client.connected()) {
char c = Serial1.read();
Client.write(c);
}
delay(10);
}