In this class, you will create serial port to TCP converter using Arduino code running on ESP32 processor. We will use one of device which uses such processor: Moduino X ESP32. For TCP communication WiFi module will be used.
You will need:
- Moduino X2 (may be also X1) ESP32 device (check this website to find out more
- PC with Linux operating system
- socat application
- RS-232/RS-485 port in your computer or USB to RS-232/RS-485 converter (for programmming and testing)
In our example data sent to serial port (which is used as terminal port in regular Micropython ESP32 device) will be send via WiFi using TCP protocol. It also decodes incomming TCP packets and writes them to serial port.Then virtual serial port can be opened for that TCP packets and perform serial communication.We will use socat application for that.
DescriptionProgram does following things:
- configures RS port with 115200 bitrate, 8 for databits, None for parity, 1 for stopbits
- connects to WiFi network with data from ssid and password constants
- enables ethernet communication with following parameters:
IP: 192.168.0.98network mask: 255.255.255.0gateway: 192.168.0.99dns addresses: 8.8.8.8, 8.8.4.4starts server on 1234 port
- waits for client - code supports only one at the same time
- when client arrives:
- reads incomming packets from wifi client and sends them to RS port- reads incomming packets from rs port and sends them to wifi client- when client disconnects - program returns to waiting for new one, in other case - repeats readings
Given values (IP, port, etc) can be changed.
Program codeThis is full code of our example:
#include <WiFi.h>
// debug log, set to 1 to enable
#define ENABLE_DEBUG_LOG 0
// wifi config
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASS";
// ethernet config
const IPAddress local_IP(192, 168, 0, 98);
const IPAddress gateway(192, 168, 0, 99);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress primaryDNS(8, 8, 8, 8);
const IPAddress secondaryDNS(8, 8, 4, 4);
// rs-server config
const int serverPort = 1234;
// rs port config
const int baudrate = 115200;
const int rs_config = SERIAL_8N1;
// reading buffor config
#define BUFFER_SIZE 1024
// global objects
WiFiServer server;
byte buff[BUFFER_SIZE];
void debug_log(char* str) {
#if ENABLE_DEBUG_LOG == 1
Serial.println(str);
#endif
}
void setup() {
// init rs port
Serial.begin(baudrate, rs_config);
// init wifi connection
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
debug_log("Failed to configure network settings");
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
debug_log("connecting to WiFi network");
delay(500);
}
#if ENABLE_DEBUG_LOG == 1
Serial.println("connected to WiFi");
Serial.println("IP adddr: ");
Serial.println(WiFi.localIP());
#endif
delay(1000);
//start server
server = WiFiServer(serverPort);
server.begin();
delay(1000);
debug_log("server started");
}
void loop() {
// wait for client
WiFiClient client = server.available();
if (!client)
return;
debug_log("client found");
while (client.connected()) {
int size = 0;
// read data from wifi client and send to serial
while ((size = client.available())) {
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
client.read(buff, size);
Serial.write(buff, size);
Serial.flush();
}
// read data from serial and send to wifi client
while ((size = Serial.available())) {
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial.readBytes(buff, size);
client.write(buff, size);
client.flush();
}
}
debug_log("client disconnected");
client.stop();
}
Setup stepsTo test this program, you need to follow these steps:
Step 1. Preapare your PCYou need Arduino IDE with ESP32 plugin. Check this document for information how to configure it:Running Arduino code on Moduino X ESP32
You also need socat application to create virtual serial port in the system.To install it, you can use apt-get:
sudo apt-get install socat
Step 2. Flash ModuinoYou need to flash device with above code:
- start Arduino IDE and set appropriate settings in Tools menu:
- put whole code to IDE's window
- replace YOUR_WIFI_SSID and YOUR_WIFI_PASS with credentials of your WiFi network
- you can enable debug messages by setting 1 to ENABLE_DEBUG_LOG (enable it for first run)
- choose Sketch → Upload
To test converter, after flashing:
- unplug GND from BOOT pin and restart the device
- open physical port with picocom:
picocom /dev/ttyUSB0 -b 115200
- restart device, you should get following commands (in debug mode):
- wait for a while (or wait until server started message if you set debug mode)
- create virtual RS port connected to Moduino device on configured port:
socat pty,link=/dev/ttyVirt0,raw,echo=0 tcp:192.168.0.98:1234
- connect to virtual port:
picocom /dev/ttyVirt0 -b 115200
- start typing some text, you should see it on ttyUSB0 port:
- start typing some text in picocom connected with ttyUSB0 - you should see it on ttyVirt0:
When you ended with tests you may want to disable all debug messages - to make converted not adding any data to messages on its serial port.To disable messages from our example, set ENABLE_DEBUG_LOG to 0.Disabling following ESP32 starting messages:
works only with Moduino X2.You need to connect IN2 port from Terminal 3 to GND:
Thank you for your participation in this tutorial!
Comments