randomport.com

Serial to ethernet

The serial to ethernet module adds TCP connectivity to your project. I took on this project after testing several cheap modules from eBay, that did not perform as expected.

One of the modules I've tested had no way of closing a TCP connection. Not a big deal by itself, but it turned out it always used the same source port as well. These two things coupled together produced some unexpected behaviour. Everything seemed to work the first time around, but the next connection did not take. After a lot of thought and investigation with Wireshark, it dawned on me that the server I was connecting to, already had a connection to the same port from the same IP and source port. It thought the connection was alive and did not accept another one. Other modules had other problems so I decided to make my own. This is the result.

ser2eth

The ENC28J60 module is from eBay, the only addition is the STM32F100 ARM microcontroller, making this a lot more a software project than hardware. The module works @3.3V. Since the ENC module makes 6.25 MHz clock available on one of its pins, the MCU uses that for system clock so an extra crystal is not required. The module draws about 90mA.

The module is driven by AT commands. Starting baud rate is 38400.

Currently:

  • IP configuration is available by DHCP only. Static configuration is not supported.
  • UDP is not available.
  • Only a single simultaneous TCP connection is possible.
  • No higher level protocols (http,ftp,smtp,...) are implemented. I prefer doing that in application using the module.
  • MAC is set using the MCU's unique identifier

General commands:

Command Description
AT Returns OK
ATE1
ATE0
Echo on/off
ATI Returns ID string
AT+IPR=baud Change baud rate

Configuration commands (pre TCP):

Command Description
AT+LWIPINIT Init lwIP and hw interface
AT+LWIPMAC=? Returns hw interface MAC
AT+LWIPDHCP Starts automatic IP configuration via DHCP
AT+LWIPIP=? Returns currently configured own IP
AT+LWIPNM=? Returns currently configured netmask
AT+LWIPGW=? Returns currently configured gateway
AT+LWIPDNS=? Returns currently configured DNS server IP

TCP commands:

Command Description
AT+LWIPCONNECT=dst_ip,dst_port,src_port TCP connect to dst_ip:dst_port from src_port
AT+LWIPLISTEN=port Start TCP listening on port
AT+LWIPSEND Send data over established TCP connection
AT+LWIPCLOSE Close TCP connection
AT+LWIPDNS=hostname Resolves hostname to IP using the configured DNS server

Data sending and reception

I've started this project to provide another project, which used SIM900 GSM module, with an alternative communications interface where ethernet was available. Sending and receiving data is therefore similar to SIM900 and I'll only give a brief description here and you can look at the SIM900 AT commands datasheet or refer to the source for details. After receiving AT+LWIPSEND the module outputs > and waits for data. Character 0x1a (Ctrl-Z) is data terminator. The protocol is really designed to transfer ASCII data and not binary. This isn't a problem for most of the standard protocols like http,ftp,smtp,etc. but could be inadequate if you're planning to use a binary protocol. All received TCP data is sent over the serial interface immediately.

The AT+LWIPCONNECT command accepts only IP and not host names as destination parameter. If you need host name connectivity, you have to do the resolution manually using AT+LWIPDNS.

Usage example

Command Reponse Comment
AT
OK
ATI
MK ser2eth v1.2
OK
AT+LWIPINIT
OK
AT+LWIPDHCP
OK
+LWIP: IFSTAT Sent after DHCP completes
AT+LWIPIP=?
+LWIPIP: 192.168.1.18
OK
AT+LWIPDNS=www.google.com
OK
+LWIPDNS: 74.125.136.103 Sent after DNS resolution completes
AT+LWIPCONNECT=74.125.136.103,80,1234
OK
+LWIP: CONNECT Sent after connection is established
AT+LWIPSEND
512> Number before > is bytes of buffer free
GET / HTTP/1.1
Host: google.com
(send 0x1a to end data entry)
OK
HTTP/1.1 302 Found
Location: http://www.google.si/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
(removed the rest of reply)
Server response is sent immediately upon reception
AT+LWIPCLOSE
OK
+LWIP: CLOSE Sent after connection is closed

Bill of materials

ser2eth board ser2eth schematics

Qty Value / Farnell code Device Size Parts
1 1838512 STM32F100 LQFP48 IC1
4 100n multilayer 0805 C1, C2, C3, C4

Files

Gerbers and HEX | Source on GitHub

To compile the sources, my STM32F10x library is required.