I have tried to expand one of the Ethernet examples so I can receive several characters - e.g. for sending commands to an Arduino over the net. For some reason, the client always recieves the same 27 rubbish characters when I run this program. Is there a buffer I didn't clear out or am I doing something wrong?
I have tried this with Arduino 15,16 and 17 on Linux.
The serial output shows that the myword[] array gets the 27 characters as input when the program is started.
I have also tried a different sketch that doesn't disconnect the client, this shows that it is only when the Arduino starts up that this happens.
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 170 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
// telnet defaults to port 23
Server server(23);
char c;
char myword[40];
int i;
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600); // DEBUG only
//initArray();
// start listening for clients
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
// output the content
for (int l = 0; l < sizeof(myword); l++) {
client.print(myword[l], BYTE);
Serial.print(myword[l]);
}
client.flush();
i=0;
initArray();
client.stop();
// client.print(sizeof(myword));
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != '\r') {
// we've gotten a character on the current line
myword[i++]=c;
Serial.print("Storing character\t");
Serial.print(c);
Serial.print("\tin array\t");
Serial.print(i);
Serial.print("\n");
current_line_is_blank = false;
}
}
}
//client.stop();
//initArray();
//i=0;
}
//client.stop();
//initArray();
//i=0;
}
int initArray() {
for(int n=0;n<40;n++){
myword[n]=' ';
Serial.print("Now resetting number:\t");
Serial.print(n);
Serial.print("\n");
}
}