Programming a chat server for my SEED Ethernet shield, new to C and arduino!

Hello everyone! This coming summer/semester I plan on creating a chat server with my seed Ethernet shield. I have port forwarded the telnet port on the shield and configured the network correctly, but there are a few ideas that I would like some guidance on. I have been using the demo sketch for the chat server from the arduino website to test my arduino. Firstly, input for chat. The default chat server sketch takes any input it gets from the client and spits it out to connected clients. I would like to create a way to type a full line or sentence, then press enter to send THAT to all connected clients. I thought I might just need to figure out how to take input after enter is pressed, instead of after anything is pressed. Second: Nicknames! I would really like to ask for a name before you enter the chat room, for identification for who is typing what. I would like to print the name before all of the messages. Third, maybe a character limit on incoming messages? Sounds easy enough to do. Finally: Chat logging. maybe if I can scrape up some money and buy an arduino Ethernet shield I can figure out how to log all messages to a text file on the SD card. Kind of not as necessary out of all of the others. I am very new to programming arduino, so any help for this would be appreciated. Thanks to all who help! smiley-grin Just ask for clarification if I was not clear, sorry in advance!

The default chat server sketch takes any input it gets from the client and spits it out to connected clients. I would like to create a way to type a full line or sentence, then press enter to send THAT to all connected clients.

The key there is that the server takes what it gets, and sends it to all clients, WHEN IT GETS IT. If you don't want the server to send partial sentences, don't send partial sentences to the server.

I thought I might just need to figure out how to take input after enter is pressed, instead of after anything is pressed.

You do, but the problem is on the chat client end, not the server end.

You do, but the problem is on the chat client end, not the server end.

Thank you for the reply! So, should I be using the client class to take input, like client.write()? Or am I thinking of this wrong? For example, I think this is correct. I will try it when I am home soon.

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

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();    
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }

    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.write();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}

This is the chat sever sketch but I changed "char thisChar = client.read();" into "char thisChar = client.write();"

This is the chat sever sketch but I changed "char thisChar = client.read();" into "char thisChar = client.write();"

Look up what client.write() takes as input, and what it returns, and then explain why you did this.

PaulS:

This is the chat sever sketch but I changed "char thisChar = client.read();" into "char thisChar = client.write();"

Look up what client.write() takes as input, and what it returns, and then explain why you did this.

Alright, so I looked it up while compiling the edited sketch and this is what I found here: http://arduino.cc/en/Reference/ClientWrite I did this because I figured that the following meant that I could send a string, or series of bytes, or whatever might work. Now that I think about it more, should I have put something in the parentheses in client.write()?

Write data to the server the client is connected to. This data is sent as a byte or series of bytes.

The sketch did not compile correctly and I got this

Chat_server_project.ino: In function 'void loop()': Chat_server_project:69: error: no matching function for call to 'EthernetClient::write()' C:\Program Files (x86)\Arduino\libraries\Ethernet/EthernetClient.h:17: note: candidates are: virtual size_t EthernetClient::write(uint8_t) C:\Program Files (x86)\Arduino\libraries\Ethernet/EthernetClient.h:18: note: virtual size_t EthernetClient::write(const uint8_t*, size_t) C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)

Could you explain this to me as well? Thanks so much for helping!

I did this because I figured that the following meant that I could send a string, or series of bytes, or whatever might work. Now that I think about it more, should I have put something in the parentheses in client.write()?

Yes, you should have. Whatever you expect to write() goes in the parentheses.

PaulS:

I did this because I figured that the following meant that I could send a string, or series of bytes, or whatever might work. Now that I think about it more, should I have put something in the parentheses in client.write()?

Yes, you should have. Whatever you expect to write() goes in the parentheses.

So, should I make thisChar equal to something else? And could you hint me toward what it might be? This is what I have now, but I don't have time to test it.

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

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();    
      Serial.println("We have a new client");
      client.println("Hello, client!"); 
      alreadyConnected = true;
    } 

    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      client.write(thisChar);
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}

Which Arduinu? If it's a Yun then its a hour or two of work as the code is already written (as is the code for a full on web server and FTP server (if that's the right word) and lots more. You just have to port the software if its not already done.

While it may be possible to get a limited chat server (a la undernet) going using an avr it really is just like trying to drive a nail in too stone with a soggy sponge! Its just not what an avr is designed to do!

Mark

holmes4: Which Arduinu? If it's a Yun then its a hour or two of work as the code is already written (as is the code for a full on web server and FTP server (if that's the right word) and lots more. You just have to port the software if its not already done.

Using an arduino uno, just learning some stuff about Ethernet library.

If your really going to do a chat server with e-net than cost out

A. and uno + an E-net shield

B. a Yun which includes the e-net shield

and then allow for the fact that the Yun includes a 400M(approx) processor what run linux and can run a standard ( availabe for free) chat server and a http server at the same time. Its got the power of an inter server of 10/15 years ago (but its still mone than you need.

Forceing an Uno with 2k of ram and 16M of speed to act as a chat server could be done (may be ) it it would very very hard work and the results would be very poor.

Mark