logo elektroda
logo elektroda
X
logo elektroda

TL;DR

  • An ESP8266-based TCP/IP to RS232/RS485 converter creates remote serial ports for Linux and Windows over LAN or Internet.
  • It extends UARTtransparentbridge code with MAX485 support and is programmed in Arduino IDE.
  • On Linux, socat creates a virtual UART at /dev/espS0, and the build uses an ESP8266 with a MAX485 transceiver.
  • The converter was tested with an ORNO OR-WE-504 indicator.
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
📢 Listen (AI):
  • TCP-IP / RS485 ESP8266 converter

    Simple TCPIP / RS485 converter.
    The device allows you to create a remote port rs232 / rs485 in linux and windows systems and allows communication with devices via lan / internet. The project is based on UARTtransparentbridge code, but MAX485 support has been added to it. ESP8266 programmed in ArduinoIDE, source code and "schematic" attached.
    On linux, after booting the device, just use 'socat':
    Code: Text
    Log in, to see the code

    When this command is issued, a virtual UART will be created in / dev / espS0.

    Tested on the ORNO OR-WE-504 indicator.

    TCP-IP / RS485 ESP8266 converter
    Attachments:
    • es-rs485.zip (114.4 KB) You must be logged in to download this attachment.

    Cool? Ranking DIY
    About Author
    dktr
    Level 26  
    Offline 
    dktr wrote 929 posts with rating 702, helped 45 times. Live in city Ostróda. Been with us since 2003 year.
  • ADVERTISEMENT
  • #2 16847338
    george2002

    Level 21  
    Is it something like esp-link? https://hackaday.io/page/1304-virtual-serial-port-tunnel-to-use-with-esp-link " target="_blank" rel="nofollow noopener ugc" class="postlink inline" title="" > https://hackaday.io/page/1304-virtual-serial-port-tunnel-to-use-with-esp-link because esp-link has a lot of options, only the RS485 support is probably not implemented from what I read quickly :)
    How about stability? and it works at what speeds and whether they can be changed easily or the code needs to be uploaded to esp8266 to make it different :) ?
    Company Account:
    GS electronic Grzegorz Stoliński
    2 Pułku Lotniczego 18, Kraków, 31-857 | Company Website: www.gselectronic.pl
  • #3 16847583
    dktr
    Level 26  
    Yes, it will be the same as ESPlink, in 1200,2400,9600 it works fine but requires changes in the code - serial.begin () I have nothing else with rs485 except this orno indicator, I polled the counter all night every 1 sec and all the results are correct, not a single transmission error.
  • #4 16851255
    jajen
    Level 17  
    Does this converter work at 3.3v with esp? It shouldn't because they're at 5v.
  • #5 16851429
    levy^
    Level 17  
    it should work, but at much shorter distances
  • ADVERTISEMENT
  • #7 16851871
    ditomek
    Level 22  
    On what basis do you calculate the time needed to control the RE / DE signal?
    Doesn't cut anything with longer thongs?
  • #8 16851892
    byrrt
    Level 21  
    Not supporting RS485 (DE signal) is not a problem:
    TCP-IP / RS485 ESP8266 converter
    Proven - works, also in many production devices.

    Added after 1 [minutes]:

    ditomek wrote:
    On what basis do you calculate the time needed to control the RE / DE signal?

    This is not calculated, only the DE signal leaves after leaving the last byte from the send buffer.
  • #9 16852120
    ditomek
    Level 22  
    on the first post, re_de is controlled from the ESP pin, so the diagram you pasted later does not make sense. By the way, I use a similar one myself in my projects.
    I am asking this because in arduino the time needed to control re_de is not as easy as it seems.
    in general Serial.print is backed up by the interrupt system and for longer strings the handling ends faster than actually sending the data.
    This means that since you have a short delay after print and re_de switch, the transmit direction will change to receive faster than the send buffer will be emptied. This is the case with the Atmega. I wonder what ESP is like, that's why I asked.
  • #10 16853468
    piterek-23
    Level 33  
    byrrt wrote:
    Not supporting RS485 (DE signal) is not a problem:
    TCP-IP / RS485 ESP8266 converter
    Proven - works, also in many production devices.

    And how to select elements when UART works at the 3V3 level?
    MAX485 to MAX3485, what about the rest?
  • ADVERTISEMENT
  • #11 16854477
    byrrt
    Level 21  
    piterek-23 wrote:
    MAX485 to MAX3485, what about the rest?

    what about the rest? stays the same ...
  • ADVERTISEMENT
  • #12 16854507
    piterek-23
    Level 33  
    I meant the transistor and resistors because the entire power supply will change from 5V to 3V3. If nothing needs to be changed that's great, but I preferred to ask.
    Thanks, I'll be testing soon ;)
  • #13 16968894
    uzi18
    Level 24  
    george2002 wrote:
    Is it something like esp-link? https://hackaday.io/page/1304-virtual-serial-port-tunnel-to-use-with-esp-link " target="_blank" rel="nofollow noopener ugc" class="postlink inline" title="" > https://hackaday.io/page/1304-virtual-serial-port-tunnel-to-use-with-esp-link because esp-link has a lot of options, only the RS485 support is probably not implemented from what I read quickly :)
    How about stability? and it works at what speeds and whether they can be changed easily or the code needs to be uploaded to esp8266 to make it different :) ?


    There is an esp-link latka with rs485 support, you have to look at repo / issue
  • #14 17028333
    piterek-23
    Level 33  
    Gentlemen, everything works great, but ... but there is a small problem ...
    If, for example, I reset the router, ESP loses the connection and can not reconnect - just do ESP reset and everything is OK again.
    Can it be improved somehow? :)
  • #15 17028505
    uzi18
    Level 24  
    Are you talking about the software from 1 post?
  • #16 17028531
    piterek-23
    Level 33  
    uzi18 wrote:
    Are you talking about the software from 1 post?

    Yes.
  • #17 17032661
    dktr
    Level 26  
    I made a fix, after breaking the wifi connection, the program waits for reconnection and resumes work.
    Attachments:
    • es-rs485.zip (114.36 KB) You must be logged in to download this attachment.
  • #18 17038880
    jajen
    Level 17  
    Hello,

    I also use this converter. I get an error compiling this new version

    es-rs485: 19: error:'earchwifi 'was not declared in this scope

    How can I fix it?
  • #19 17040043
    dktr
    Level 26  
    I think I broke something and I didn't attach this file, but the one from this attachment compiles without error.
    Another thing is that ESP itself will reconnect to the network and will work, but the socat will probably collapse and you need to fire it again. For full automation on the server, you can make a file, e.g. cgi or php, which will launch socat and this file will be asked by ESP when connected to the network.
    Attachments:
    • es-rs485_v2.zip (763 Bytes) You must be logged in to download this attachment.
  • #20 17040164
    jajen
    Level 17  
    Already ok. New version of arduin ide helped. Thanks
  • #21 17043584
    piterek-23
    Level 33  
    dktr wrote:
    I think I broke something and I didn't attach this file, but the one from this attachment compiles without error.

    Everything seems to be working as it should. When ESP was working, I did a router reset several times and each time ESP reconnected.
    dktr wrote:
    Another thing is that ESP itself will reconnect to the network and will work, but the socat will probably collapse and you need to fire it again.

    That's right, you should re-use the socat command ... :(
    dktr wrote:
    For full automation on the server, you can make a file, e.g. cgi or php, which will launch socat and this file will be asked by ESP when connected to the network.

    Would you have an idea for such a script?
    The only thing left to be happy is "automation" of socat ;)

    I tried something like this:
    Code: Bash
    Log in, to see the code

    The link / dev / ttyUSB10 is created, but no longer reading the counter.

    Many thanks for the WiFi fix ;)
  • #22 17043800
    dktr
    Level 26  
    In the next version of the software, the function of downloading the file from the server when connected to the wireless network has been added:

    const String url = "http://192.168.2.40/esp_rs485.php?sign=";

    in this variable you must specify the address of the file on the server which, after querying it, will compile and restart socat. By the way, you can download the signal level with which esp8266 connected to the network into the variable.

    In php:
    Attachments:
    • es-rs485_v2.zip (1.1 KB) You must be logged in to download this attachment.
  • #23 17046162
    piterek-23
    Level 33  
    Can you suggest how it should all look exactly, how to automate it?

    I have soft "es-rs485_v2" on ESP, and in it I changed to my:
    WiFiServer localServer (4321);
    IPAddress staticIP (192, 168, 1, 221);
    IPAddress gateway (192, 168, 1, 1);
    IPAddress subnet (255, 255, 255, 0);
    const String url = "http://192.168.1.2/esp_rs485.php?sign=";
    WiFi.begin ("my_net", "my_password");

    On the server I created a php file with the following content:
    Code: PHP
    Log in, to see the code

    Now what, what about socat?
    If I manually add:
    Code: Bash
    Log in, to see the code

    It all seems okay
  • #24 17047739
    byrrt
    Level 21  
    byrrt wrote:

    Not supporting RS485 (DE signal) is not a problem:
    TCP-IP / RS485 ESP8266 converter
    Proven - works, also in many production devices.


    Recently, I was returning to the topic and it would be bad if I had not written that - although this solution works, it is not free from defects. In this case, the transmitter in RS485 is turned on only when the USART in the prock sends zero .. When one - the system switches to the receiver. The effect is this:

    TCP-IP / RS485 ESP8266 converter

    On the left - transmission - using the circuit from the diagram I have posted, on the right - from the system in which DE is properly controlled. It looks like this when terminators are attached at the ends of the line, without them it always looks good (even with DE controlled only in transmitting zero).
    Yes for posterity ..

    If we have one transmission speed, we can control the delay system DE.

    TCP-IP / RS485 ESP8266 converter

    The elements must be selected according to the speed.
  • #25 17049203
    uzi18
    Level 24  
    Termination on short lengths of wire usually does not work properly.
    How long do you have the line?
  • #26 17049217
    byrrt
    Level 21  
    It always works, it has no effect, because here you just make a voltage divider (polarizing resistor - terminators - polarizing resistor). In the version with a transistor without a delay system, the transmitter transmits only 0, instead of transmitting it, it releases the lines slowly ..
  • #28 17238634
    po_48
    Level 9  
    Hello, I have a problem with uploading the es-rs485_v2.ino file by Arduino
    what am i doing wrong?
  • #29 17240110
    dktr
    Level 26  
    po_48 wrote:
    Hello, I have a problem with uploading the es-rs485_v2.ino file by Arduino
    what am i doing wrong?


    You need a glass ball. Any details?
  • #30 17261636
    po_48
    Level 9  
    Arduino: 1.8.1 (Windows XP), Tile: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, QIO, 8M (7M SPIFFS), 2, v2 Lower Memory, Disabled, None, All Flash Contents, 115200 "

    In file included from c: \ documents and settings \ admin \ local settings \ application data \ arduino15 \ packages \ esp8266 \ tools \ xtensa-lx106-elf-gcc \ 1.20.0-26-gb404fb9-2 \ xtensa-lx106-elf \ include \ c ++ \ 4.8.2 \ utility: 68: 0,

    from c: \ documents and settings \ admin \ local settings \ application data \ arduino15 \ packages \ esp8266 \ tools \ xtensa-lx106-elf-gcc \ 1.20.0-26-gb404fb9-2 \ xtensa-lx106-elf \ include \ c ++ \ 4.8.2 \ algorithm: 60,

    from C: \ Documents and Settings \ admin \ Local Settings \ Application Data \ Arduino15 \ packages \ esp8266 \ hardware \ esp8266 \ 2.4.1 \ cores \ esp8266 / Arduino.h: 254,

    from sketch \ es-rs485_v2.ino.cpp: 1:

    c: \ documents and settings \ admin \ local settings \ application data \ arduino15 \ packages \ esp8266 \ tools \ xtensa-lx106-elf-gcc \ 1.20.0-26-gb404fb9-2 \ xtensa-lx106-elf \ include \ c ++ \ 4.8.2 \ xtensa-lx106-elf \ bits \ c ++ config.h: 426: 29: fatal error: bits / os_defines.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1
    Compilation error for Generic ESP8266 Module.

    This report should contain more information if in
    File -> Preferences will be enabled "Show
    detailed information during compilation "
📢 Listen (AI):

Topic summary

✨ The discussion revolves around a TCP-IP to RS485 converter utilizing the ESP8266, which allows remote communication with RS232/RS485 devices over LAN/Internet. The project is based on the UARTtransparentbridge code with added MAX485 support, programmed in Arduino IDE. Users share experiences regarding stability, baud rates, and the need for code modifications for different speeds. Concerns about voltage levels (3.3V vs. 5V) and the proper control of the DE/RE signal for RS485 communication are addressed. Solutions for reconnection issues after router resets and automation of the socat command are discussed. Additionally, users inquire about the necessary libraries for compilation and share troubleshooting tips for Arduino IDE errors.
Generated by the language model.

FAQ

TL;DR: "54 % flash used, all the results are correct" [Elektroda, dktr, #17264295; #16847583]. 1200–9600 baud verified, MAX3485 lets 3.3 V reach full RS-485 length. Inline Wi-Fi auto-reconnect added in v2.

Why it matters: this open-source bridge lets any PC or PLC talk Modbus-RTU over Wi-Fi with parts that cost under €5.

Quick Facts

• Verified baud rates: 1 200, 2 400, 9 600 bps [Elektroda, dktr, post #16847583] • Flash footprint: 238 kB (≈54 % of 434 kB) on generic ESP-12 F [Elektroda, dktr, post #17264295] • Typical current draw: 80–170 mA for ESP-12 F @ 3.3 V (ESP8266 Datasheet) • MAX485 requires 5 V; MAX3485 works 3.0–3.6 V, 52 mA max (Maxim DS3999) • socat command: pty,link=/dev/espS0,raw tcp::8888 [Elektroda, dktr, post #16846354]

What exactly does the ESP8266 TCP-IP/RS485 converter do?

The sketch opens a Wi-Fi TCP server (default port 8888), wraps every byte received on UART0, and forwards it unchanged to the RS-485 transceiver; the reverse path works identically. Your PC sees a virtual serial port created by socat and speaks Modbus-RTU as if a local COM port were present [Elektroda, dktr, post #16846354]

I see “ESP8266WiFi.h: No such file”. What’s missing?

The ESP8266 core libraries are absent. Install the ESP8266 platform through Arduino Board Manager or use PlatformIO’s “framework-arduinoespressif8266” [Elektroda, dktr, post #20320111]

How does the new auto-reconnect work after Wi-Fi loss?

Version 2 waits until Wi-Fi reconnects, then restarts the TCP listener. The MCU stays online, but any socat process must be relaunched from the host side [Elektroda, dktr, post #17032661]

Is there a Windows replacement for socat?

Yes—com0com + hub4com or the free “tty2tcp” utility create a virtual COM port and forward it to the ESP’s IP:8888 endpoint. Operation is identical to the Linux socat string (tty2tcp Manual).

How should I wire DE/RE on a Wemos D1 mini?

Connect RO→D7 (GPIO13), DI→D5 (GPIO14) and tie MAX3485 DE+RE to D2 (GPIO4). Ensure a 10 kΩ pull-down keeps DE low during reset to avoid bus collisions [Elektroda, pela222, post #21101804]

Do I need line termination and bias resistors?

At lengths under 3 m termination rarely improves signals, but once terminators were added the transistor-only DE trick showed severe undershoot [Elektroda, byrrt, post #17047739] Use 120 Ω at both ends plus 680 Ω pull-ups/downs for reliable multi-node buses (Maxim AN1090).

Can the bridge carry Modbus-RTU transparently?

Yes. Send a raw RTU frame over the TCP socket; the ESP adds no header. One user reads a WE-504 power meter by pointing Perl-MBclient directly at 192.168.1.87:8888 [Elektroda, zychzbig, post #21153086]

What happens if socat crashes during long polling?

The ESP keeps its socket open. Frames pile up until the TCP stack resets, causing a visible 1-2 s pause and dropped Modbus queries—an edge case fixed by auto-restarting socat every minute via a shell script [Elektroda, piterek-23, post #17043584]

Any quick test method without real RS-485 hardware?

Loop the ESP’s UART TX to RX and open two terminal windows on the virtual port: characters typed in one echo in the other, proving full duplex operation over Wi-Fi.
Generated by the language model.
ADVERTISEMENT