Arduino RS232 to WiFi

In this tutorial, we are going to learn how to use the Arduino Uno R4 WiFi to create a converter that connects Serial RS232 devices to WiFi. With this setup, the Arduino will read data from a serial RS232 interface and send it to a TCP server, either in the same local network or over the internet. It can also receive data from the TCP server and send it back through the serial RS232 interface.

Following these steps will help you set up flexible communication bridges between serial RS-232 devices and TCP/IP servers using Arduino.

Arduino RS232 to WiFi converter

Hardware Required

1×Arduino UNO R4 WiFi
1×USB Cable Type-C
1×TTL to RS232 Module
1×Jumper Wires
1×(Optional) RS232 to USB Cable
1×(Optional) RS232 Gender Changer
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About RS232 and TCP

If you do not know about how to use RS232 and TCP communication with Arduino, learn about them in the following tutorials:

How RS232 to WiFi converter Works

  • Arduino connects to a serial device via the RS232 interface.
  • Arduino acts like a TCP client, connecting to a TCP server. The TCP server could be a software running on your computer or another Arduino programmed to act as a TCP server.
  • Arduino reads information from the serial RS232 interface and sends it to the TCP server.
  • Arduino also reads data from the TCP connection and sends it back through the serial RS232 interface.

Wiring Diagram

  • Wiring diagram if using hardware serial
Arduino TTL to RS232 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram if using software serial
Arduino RS-232 to TTL Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code for Hardware Serial

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rs232-to-wifi */ #include <WiFiS3.h> const char* WIFI_SSID = "YOUR_WIFI_SSID"; // CHANGE TO YOUR WIFI SSID const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; // CHANGE TO YOUR WIFI PASSWORD const char* TCP_SERVER_ADDR = "192.168.0.26"; // CHANGE TO TCP SERVER'S IP ADDRESS const int TCP_SERVER_PORT = 1470; WiFiClient TCP_client; void setup() { Serial.begin(9600); Serial.println("Arduino: TCP CLIENT"); // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true) ; } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } Serial.print("Attempting to connect to SSID: "); Serial.println(WIFI_SSID); // attempt to connect to WiFi network: while (WiFi.begin(WIFI_SSID, WIFI_PASSWORD) != WL_CONNECTED) { delay(10000); // wait 10 seconds for connection: } Serial.print("Connected to WiFi "); Serial.println(WIFI_SSID); // connect to TCP server if (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) Serial.println("Connected to TCP server"); else Serial.println("Failed to connect to TCP server"); } void loop() { if (TCP_client.connected()) { // read data from TCP and send them to RS232 interface if (TCP_client.available()) { char c = TCP_client.read(); Serial.write(c); } // read data from RS232 interface and send them to TCP if (Serial.available()) { char c = Serial.read(); TCP_client.write(c); } } else { Serial.println("Connection is disconnected"); TCP_client.stop(); // reconnect to TCP server if (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) { Serial.println("Reconnected to TCP server"); } else { Serial.println("Failed to reconnect to TCP server"); delay(1000); } } }

Arduino Code for Software Serial

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rs232-to-wifi */ #include <WiFiS3.h> #include <SoftwareSerial.h> SoftwareSerial rs232(7, 6); // RX: 7, TX: 6 const char* WIFI_SSID = "YOUR_WIFI_SSID"; // CHANGE TO YOUR WIFI SSID const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; // CHANGE TO YOUR WIFI PASSWORD const char* TCP_SERVER_ADDR = "192.168.0.26"; // CHANGE TO TCP SERVER'S IP ADDRESS const int TCP_SERVER_PORT = 1470; WiFiClient TCP_client; void setup() { Serial.begin(9600); rs232.begin(9600); Serial.println("Arduino: TCP CLIENT"); // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true) ; } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } Serial.print("Attempting to connect to SSID: "); Serial.println(WIFI_SSID); // attempt to connect to WiFi network: while (WiFi.begin(WIFI_SSID, WIFI_PASSWORD) != WL_CONNECTED) { delay(10000); // wait 10 seconds for connection: } Serial.print("Connected to WiFi "); Serial.println(WIFI_SSID); // connect to TCP server if (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) Serial.println("Connected to TCP server"); else Serial.println("Failed to connect to TCP server"); } void loop() { if (TCP_client.connected()) { // read data from TCP and send them to RS232 interface if (TCP_client.available()) { char c = TCP_client.read(); rs232.write(c); } // read data from RS232 interface and send them to TCP if (rs232.available()) { char c = rs232.read(); TCP_client.write(c); } } else { Serial.println("Connection is disconnected"); TCP_client.stop(); // reconnect to TCP server if (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) { Serial.println("Reconnected to TCP server"); } else { Serial.println("Failed to reconnect to TCP server"); delay(1000); } } }

Testing

You can do a test by sending data in the following flows:

  • Serial Software (on your PC) → RS-232 → Arduino → WiFi → TCP Server Software (on your PC).
  • TCP Server Software (on your PC) → WiFi → Arduino → RS-232 → Serial Software (on your PC).
Arduino RS232 to PC communication

To do it, follow the below steps:

  • If this is the first time you use Arduino Uno R4, see how to setup environment for Arduino Uno R4 on Arduino IDE.
  • Connect Arduino Uno R4 WiFi to your PC by using TTL-to-RS232 module and RS232-to-USB cable as above wiring diagram
  • Install a Serial Terminal program like Tera Term or PuTTY
  • Install a TCP server software program like ezTerm
  • Open the Serial program and configure the Serial parameters (COM port, baurate...)
  • Open the TCP server program and configure it as TCP Server, then click Listen button
ezTerm TCP Server
  • Open Command Prompt on your PC.
  • Find the IP address of your PC by running the below command:
ipconfig
  • The output looks like below:
Command Prompt
C:\WINDOWS\system32>ipconfig Windows IP Configuration Ethernet adapter: Subnet Mask . . . . . . . . . . . : 255.0.0.0 IPv4 Address. . . . . . . . . . . : 192.168.0.26 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . :
  • Update the IP address of TCP Server (Your PC) in the Arduino code. In the above example: 192.168.0.26
  • Compile and upload code to Arduino board by clicking Upload button on Arduino IDE
  • Type some data from the Serial program to send it to Arduino via Serial.
  • If successful, you will see the echo data on the TCP server software.
  • Type some data from the TCP server program to send it to Arduino via TCP.
  • If successful, you will see the echo data on the Serial program.
Arduino Serial to TCP

If you want to use a comercial RS232-To-Ethernet converter, you can buy CSE-H53N Serial To Ethernet Converter

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES