Arduino + LCD + Telnet = good fun

So I have been playing with my arduino with a LCD I got ages ago. First off I had to solder some pin headers to the LCD so that I can plug it into a breadboard. I could have soldered the pins straight to the LCD but I used some ribbon cable in the middle, and some veroboard (Note to self: Get a dremel for birthday, that stuff is a pain to cut though!) to add flexibility and some give if/when I drop it…

Once that was all soldered together I started to play with the sample code. Simple enough stuff, although my 1×16 LCD actually thinks it is a 2×8 LCD which complicates things slightly. Luckily there is a library (just make sure you download the right version) that you can replace the default LCD library with to get things like text wrapping to work properly (Another quirk of LCDs (Line 1 wraps to line 3, line 2 to 4…)). I had to completely replace the old library with this new one (well renamed the old directory) to get it to work. There’s probably a way to import extra libraries but my way works too 😀

Once the LCD was working I went on to play with the network shield. Used it before so simple enough, and I used the ChatServer code as a base for my code.

Show me the codes!

/*
LCD Telnet server

 A simple server that distributes any incoming messages to all
 connected clients and a connected LCD.
 To use telnet to your device's IP address and type.
 You can see the client's input in the serial monitor as well.
 Using an Arduino Ethernet shield. 

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * LCD connected to pins 9,8 and data to 2,3,4,5

 Created 24 Jan 2012
 by Chris Rogers

 Based on the ChatServer
 http://arduino.cc/en/Tutorial/ChatServer
(created 18 Dec 2009
 by David A. Mellis
 modified 10 August 2010
 by Tom Igoe)

 */

//Include Ethernet and Liquid Crystal(440) libraries
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
int incomingByte = 0;

void setup() {
   // set up the lcd's number of columns and rows:
  lcd.begin(8, 2);
  //Default text to start with
  lcd.print("Running!");
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
  // open the serial port
  Serial.begin(9600);
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!gotAMessage) {
      Serial.println("We have a new client");
      client.println("Welcome to Mordor!");
      gotAMessage = true;
    }

    // read the bytes incoming from the client:
    char thisChar = client.read();
    // echo the bytes back to the client:
    server.write(thisChar);
    // write the bytes to the lcd.
    lcd.write(thisChar);
    // print the bytes to serial.
    Serial.print(thisChar);
  }  
}

So pretty simple really.

A couple of things to note however:

  • The network shield uses pins 10,11,12 & 13 , so don’t plug the LCD into those otherwise the LCD will just display random squiggles and you will spend hours debugging your code trying to work out why it doesn’t work. Not that I would know…
  • Upon first connection a bunch of random data is sent down the telnet session. Not to sure if this is just my computer or what, more investigation is needed
  • Serial uses pins 0 & 1 so avoid them to.

So on to the next thing! I was thinking a HTTP server with a pretty box to enter text into or custom character designer. Or maybe a tweet display.


Leave a Reply

Your email address will not be published. Required fields are marked *