Arduino RS485 to WiFi

In this guide, we'll explore using the Arduino Uno R4 WiFi to build a Serial RS485 to WiFi converter. With this setup, the Arduino will read data from a serial RS485 interface and transmit it to a TCP server, either within the same local area network (LAN) or remotely over the internet. It can also receive data from the TCP server and send it back via the serial RS485 interface.

These steps will enable you to establish versatile communication bridges between serial RS-485 devices and TCP/IP server using Arduino.

Arduino RS485 to WiFi converter

Hardware Required

1×Arduino UNO R4 WiFi
1×USB Cable Type-C
1×TTL to RS485 Module
1×Jumper Wires
1×(Optional) RS485 to USB Cable
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 kit:

1×DIYables Sensor Kit 30 types, 69 units
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 RS485 and TCP

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

How RS485 to WiFi converter Works

  • Arduino connects to a serial device via serial RS485 interface
  • Arduino works as a TCP client that connects to a TCP server (can be a TCP server software on your PC or another Arduino programmed as a TCP server)
  • Arduino reads data from the serial RS485 interface and sends it to the TCP server
  • Arduino reads data from the TCP connection and sends it to the serial RS485 interface

Wiring Diagram

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

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram if using software serial
Arduino RS-485 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-rs485-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 RS485 interface if (TCP_client.available()) { char c = TCP_client.read(); Serial.write(c); } // read data from RS485 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-rs485-to-wifi */ #include <WiFiS3.h> #include <SoftwareSerial.h> // define the SoftwareSerial object and their pins SoftwareSerial rs485(6, 7); // RX: 6, TX: 7 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); rs485.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 RS485 interface if (TCP_client.available()) { char c = TCP_client.read(); rs485.write(c); } // read data from RS485 interface and send them to TCP if (rs485.available()) { char c = rs485.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-485 → Arduino → WiFi → TCP Server Software (on your PC).
  • TCP Server Software (on your PC) → WiFi → Arduino → RS-485 → Serial Software (on your PC).
Arduino RS485 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-RS485 module and RS485-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 RS485-To-Ethernet converter, you can buy CSE-H55N2 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