Telnet Client in LUA

Last night I started looking more seriously, as promised, into implementing a telnet client in LUA for the ESP8266 Wifi Module. I have been using an excellent development environment called ESPlorer – this runs on linux, Windows & OS/X. I tried reading the RTC for the telnet protocol but that was, predictably, dry, so I for the most part resorted to an example of a simple telnet client written in C for embedded systems and, later, the source for linux telnet located in the GNU inetutils package.

Telnet is a fairly simple, somewhat extensible protocol. The initial conversation before data transfer starts involves a two and frowing between client and server side to determine who supports what and to what extent. The basic options are taken care of with either statements or responses:

* Please do use
* Please don’t use
* I will support
* I won’t support

These conversation commands all start with byte 0xff and in their simple form are three bytes:

IAC – 0xff ‘Interpret as command’
DO 0xfd/DONT 0xfe/WILL 0xfb/WONT 0xfc

In the simplest scenario the client can listen to the ‘please do/don’t’ requests coming from the server and respond ‘I won’t to all. This can be done trivially by replacing the second byte in the request and sending it back.

Eventually the server gets bored and starts sending data, typically the welcome message and login prompt.

For command with parameters the structure is a little more complex and can include arbitrary data, for example the terminal type or the X-Display environment variable setting.

It would be useful for my client to set the window size automatically. Future enhancement.

After some several hours of development early yesterday morning and no so early I have a telnet client implemented with following features:

* display a list of WiFi access points in range
* connect to an access point with supplied password
* telnet to an ip address
* show ip, gateway and netmask

I have code to do DNS resolution but that isn’t working correctly, I anticipate it is something stupid.

Later on today I transferred the programmed module on to the end of a franken- rs232 cable and was able to communicate but there were clearly issues. I think I was going through two null modem adaptors and there were clearly issues with this – I kept getting repeated DC1 characters I wasn’t seeing when plugged into Linux.

No amount of searching found a suitable donor cable so I’m going to have to order some DB25/DB9 connectors and make a custom cable. This is all a little moot as eventually I plan to install the module directly in a Tandy portable, but for development purposes I can’t live without it.

Somewhat exhausted now I am encouraged that I managed to cobble together enough lua knowledge to get this far. There’s nothing like a practical problem to get coding and learning.

In the post there are some uber-rechargable batteries on the way. Thinking about how to power the wifi module (lipos, micro chargers, etc) I came to the conclusion that buying higher capacity AA batteries was bar far the simplest solution. If the WiFi module is powered at the same time as the Tandy portable the drain won’t be constant and I believe there are facilities to power down the WiFi part when not required.

Normal life resumes tomorrow so progress will now slow somewhat. It’s been an educational journey so far however!

Today’s photos