• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

TCP to UART conversion

Q&A Forum › Category: ESP32 › TCP to UART conversion
0 Vote Up Vote Down
Steen Spaten asked 2 years ago

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? 

Question Tags: TCP to UART conversion
7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

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

0 Vote Up Vote Down
Steen Spaten answered 2 years ago

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…

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

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

0 Vote Up Vote Down
Steen Spaten answered 2 years ago

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…  🙂

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Ok.
Then, let me know the results.
Regards,
Sara

0 Vote Up Vote Down
Steen Spaten answered 1 year ago

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);
}

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Great.
I’m glad you got it working and thanks for sharing your results.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.