Arduino RS422 naar WiFi

In deze handleiding gaan we aan de slag met de Arduino Uno R4 WiFi om een seriële RS422-naar-WiFi-converter te bouwen. Met deze opstelling leest de Arduino data vanaf een seriële RS422-interface en verzendt deze naar een TCP-server, zowel binnen hetzelfde lokale netwerk (LAN) als op afstand via internet. Ook kan de Arduino data ontvangen van de TCP-server en deze terugsturen via de seriële RS422-interface.

Met deze stappen stelt u veelzijdige communicatiebruggen op tussen seriële RS422-apparaten en een TCP/IP-server met behulp van Arduino.

Arduino RS422 naar WiFi converter

Over RS422 en TCP

Als u nog niet bekend bent met het gebruik van RS422 en TCP-communicatie met Arduino, bekijk dan de volgende tutorials:

Hoe de RS422 naar WiFi converter werkt

  • De Arduino maakt verbinding met een serieel apparaat via de seriële RS422-interface
  • De Arduino werkt als TCP-client en maakt verbinding met een TCP-server (dit kan software zijn op uw PC of een andere Arduino die als TCP-server is geprogrammeerd)
  • De Arduino leest data van de seriële RS422-interface en stuurt deze naar de TCP-server
  • De Arduino leest data van de TCP-verbinding en stuurt deze naar de seriële RS422-interface

Bedradingsschema

  • Bedradingsschema bij gebruik van hardware serial
Arduino TTL naar RS422 bedradingsschema

This image is created using Fritzing. Click to enlarge image

  • Bedradingsschema bij gebruik van software serial
Arduino RS422 naar TTL bedradingsschema

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-rs422-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 RS422 interface if (TCP_client.available()) { char c = TCP_client.read(); Serial.write(c); } // read data from RS422 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-rs422-to-wifi */ #include <WiFiS3.h> #include <SoftwareSerial.h> // define the SoftwareSerial object and their pins SoftwareSerial rs422(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); rs422.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 RS422 interface if (TCP_client.available()) { char c = TCP_client.read(); rs422.write(c); } // read data from RS422 interface and send them to TCP if (rs422.available()) { char c = rs422.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 testen door data te sturen in de volgende richtingen:

  • Serial Software (op uw PC) → RS422 → Arduino → WiFi → TCP Server Software (op uw PC)
  • TCP Server Software (op uw PC) → WiFi → Arduino → RS422 → Serial Software (op uw PC)
Arduino RS422 naar PC communicatie

Volg daarvoor de onderstaande stappen:

  • Als u voor het eerst met Arduino Uno R4 werkt, zie dan hoe u de omgeving voor Arduino Uno R4 instelt in de Arduino IDE.
  • Verbind de Arduino Uno R4 WiFi met uw PC via de TTL-naar-RS422 module en de RS422-naar-USB-kabel volgens het bovenstaande bedradingsschema
  • Installeer een seriële terminalprogramma zoals Tera Term of PuTTY
  • Installeer een TCP-serverprogramma zoals ezTerm
  • Open het seriële programma en configureer de seriële parameters (COM-poort, baudrate, enz.)
  • Open het TCP-serverprogramma en configureer het als TCP-server, klik vervolgens op de knop Listen
ezTerm TCP Server
  • Open de Opdrachtprompt (Command Prompt) op uw PC.
  • Vind het IP-adres van uw PC door onderstaande opdracht uit te voeren:
ipconfig
  • De uitvoer ziet er ongeveer als volgt 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 . . . . . . . . . :
  • Werk het IP-adres van de TCP-server (uw PC) bij in de Arduino-code. In bovenstaand voorbeeld: 192.168.0.26
  • Compileer en upload de code naar het Arduino-bord door te klikken op de Upload knop in de Arduino IDE
  • Typ wat gegevens in het seriële programma om deze via serial naar de Arduino te sturen.
  • Bij succesvolle verbinding ziet u de echo data in het TCP-serverprogramma.
  • Typ wat gegevens in het TCP-serverprogramma om deze via TCP naar de Arduino te sturen.
  • Bij succesvolle verbinding ziet u de echo data in het seriële programma.
Arduino Serieel naar TCP

Als u een commerciële RS422-naar-Ethernet converter wilt gebruiken, kunt u de CSE-H55N2 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 en helpt u bij elke stap.

※ 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!