Homer
Published © MIT

Arduino - Make a Wi-Fi/Ethernet Serial Converter

Make a converter that transform data from Serial to Wi-Fi/Ethernet and vice versa, by using Arduino.

BeginnerFull instructions provided1 hour20,030
Arduino - Make a Wi-Fi/Ethernet Serial Converter

Things used in this project

Story

Read more

Code

ethernet2RS232

Arduino
Arduino source code
#include <Phpoc.h>
#include <PhpocExpansion.h>

#define BUFFER_SIZE 100
#define TCP_PEER_ADDR "192.168.0.110"   // enter your tcp server addr here
#define TCP_PEER_PORT 1470    // enter your tcp server port here

char peer_addr[] = TCP_PEER_ADDR;
int peer_port = TCP_PEER_PORT;
PhpocClient tcp_client;

byte expansionId = 1; // based on the DIP switches you set
// https://www.phpoc.com/support/manual/pes-2607_user_manual/contents.php?id=layout
ExpansionSerial rs232(expansionId);

byte tcp_buf[BUFFER_SIZE];
byte serial_buf[BUFFER_SIZE];
int rwlen;

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ;

  // initialize PHPoC [WiFi] Shield:
  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);

  // connect to TCP server
  if(tcp_client.connect(peer_addr, peer_port))
  {
    // if connected
    Serial.println("Connected to server");
  }
  else // if not connected
    Serial.println("connection failed");

  Expansion.begin();
  // set baudrate, parity, data bit, stop bit and flow control to  115200, none,
  // 8 bit, 1 bit and no flow control, respectively.
  rs232.begin(F("115200N81N"));

}

void loop() {


  int tcp_rxlen = tcp_client.available();
  int serial_txfree = rs232.availableForWrite();
  if (tcp_rxlen) {
    if (serial_txfree > tcp_rxlen)
  {
    rwlen = (tcp_rxlen <= BUFFER_SIZE) ? tcp_rxlen : BUFFER_SIZE;
  
    // read the incoming bytes from TCP communication
    tcp_client.readBytes(tcp_buf, rwlen);
    // send these received data via serial interface
    rs232.write(tcp_buf, rwlen);
  }
  }

  int serial_rxlen = rs232.available();
  int tcp_txfree = tcp_client.availableForWrite();
  if(serial_rxlen) {
  if (tcp_txfree > serial_rxlen)
  {
  rwlen = (serial_rxlen <= BUFFER_SIZE) ? serial_rxlen : BUFFER_SIZE;
  // read the incoming bytes from serial communication
  rs232.readBytes(serial_buf, rwlen);
  // send these received data via TCP interface
    tcp_client.write(serial_buf, rwlen);
  }
  }

  if(!tcp_client.connected())
  {
    // if the server's disconnected, stop the tcp_client:
    Serial.println("disconnected");
    tcp_client.stop();
    // do nothing forevermore:
    while(true)
      ;
  }
}

Credits

Homer
17 projects • 46 followers
Contact

Comments

Please log in or sign up to comment.