Arduino RS232 naar WiFi

In deze handleiding leert u hoe u de Arduino Uno R4 WiFi gebruikt om een converter te maken die Serial RS232-apparaten koppelt aan WiFi. Met deze opstelling leest de Arduino gegevens van een seriële RS232-interface en verstuurt deze naar een TCP-server, die zich kan bevinden binnen hetzelfde lokale netwerk of via internet op afstand. Ook kan de Arduino data ontvangen van de TCP-server en deze terugsturen via de seriële RS232-interface.

Door deze stappen te volgen stelt u flexibele communicatiebruggen in tussen seriële RS232-apparaten en TCP/IP-servers met behulp van Arduino.

Arduino RS232 naar WiFi converter

Over RS232 en TCP

Als u nog niet bekend bent met het gebruik van RS232 en TCP communicatie met Arduino, leer er meer over in de volgende tutorials:

Hoe de RS232 naar WiFi converter werkt

  • De Arduino verbindt zich via de RS232-interface met een serieel apparaat.
  • De Arduino functioneert als TCP-client en maakt verbinding met een TCP-server. De TCP-server kan een softwareprogramma zijn op uw computer of een andere Arduino geprogrammeerd als TCP-server.
  • De Arduino leest informatie van de seriële RS232-interface en stuurt deze door naar de TCP-server.
  • De Arduino leest ook data van de TCP-verbinding en stuurt deze terug via de seriële RS232-interface.

Aansluitschema

  • Aansluitschema bij gebruik van hardware serial
Arduino TTL naar RS232 Aansluitschema

This image is created using Fritzing. Click to enlarge image

  • Aansluitschema bij gebruik van software serial
Arduino RS-232 naar TTL Aansluitschema

This image is created using Fritzing. Click to enlarge image

Arduino code voor hardware serial

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/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 voor software serial

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/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); } } }

Testen

U kunt een test uitvoeren door data te versturen volgens de volgende route:

  • Serial Software (op uw PC) → RS-232 → Arduino → WiFi → TCP Server Software (op uw PC).
  • TCP Server Software (op uw PC) → WiFi → Arduino → RS-232 → Serial Software (op uw PC).
Arduino RS232 naar PC communicatie

Ga als volgt te werk:

  • Als u voor het eerst de Arduino Uno R4 gebruikt, zie dan hoe u de omgeving instelt voor Arduino Uno R4 in de Arduino IDE.
  • Verbind de Arduino Uno R4 WiFi met uw PC via de TTL-naar-RS232 module en RS232-naar-USB kabel volgens bovenstaand aansluitschema.
  • Installeer een seriële terminalprogramma zoals Tera Term of PuTTY
  • Installeer een TCP-server softwareprogramma zoals ezTerm
  • Open het seriële programma en configureer de seriële parameters (COM-poort, baudrate, etc.)
  • Open het TCP-serverprogramma en stel deze in als TCP Server, klik daarna op deknop Listen
ezTerm TCP Server
  • Open de Command Prompt op uw PC.
  • Zoek het IP-adres van uw PC door het volgende commando uit te voeren:
ipconfig
  • De output ziet er ongeveer zo uit:
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 . . . . . . . . . :
  • Pas het IP-adres van de TCP Server (uw PC) aan in de Arduino-code. In bovenstaand voorbeeld is dat: 192.168.0.26
  • Compileer en upload de code naar de Arduino door op de knop Upload in de Arduino IDE te klikken.
  • Typ wat data in het seriële programma om deze naar de Arduino te sturen via Serial.
  • Als het gelukt is, ziet u de echo van de data op de TCP server software.
  • Typ data in het TCP-serverprogramma om deze naar de Arduino te sturen via TCP.
  • Als het gelukt is, ziet u de echo van de data in het seriële programma.
Arduino Serial naar TCP

Als u een commerciële RS232-naar-Ethernet converter wilt gebruiken, kunt u de CSE-H53N Serial To Ethernet Converter aanschaffen.

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten in de opbouw en werking van de converter.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!