I am looking for some basic help with the ethernet shield.
I have got it up and running and all the demo Web server apps are running.
What I want to do is getting analog values and control outputs via telnet or any remote access method.
What I want to know is how the remote access works when sending some strings.
For example, this simple code found in the Ethernet shield library;
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte gateway[] = { 10, 0, 0, 1 };
byte subnet[] = { 255, 255, 0, 0 };
// telnet defaults to port 23
Server server = Server(23);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
server.write(client.read());
}
}
I can Telnet and I get:
m26k9@desktop:~$ telnet 192.168.0.20
Trying 192.168.0.20...
Connected to 192.168.0.20.
Escape character is '^]'.
^]
telnet>test
?invalid command
But I am having trouble after this.
What does the server.write(client.read()); code actually do?
I was thinking it will echo what I write but all I get is ?invalid command.
This is the code that produces it , it has electricty consumption (all the stuff with 'blinks'), Dallas digital thermometer and analogue light level reading. It should give you some clues and you'll probably spot the code I lifted from the web server example......
/*
* Web Server
*
* A simple web server that shows the value of the analog input pins.
*/
#include <Ethernet.h>
#include <OneWire.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 201 };
int ledPin = 0; // Pin 13 guzzled by ethernet
int dallasPin = 1;
int litePin = 0;
unsigned long time = 0;
unsigned long time1;
unsigned long mins = 60000;
unsigned long blinktime = 0;
unsigned long totalblinks = 0; // blinks since start
unsigned long blinksmin = 0; // blinks this minute
unsigned long blinkshour = 0; // blinks this Hour
unsigned long lastblink = 0;
unsigned long prevblink = 0;
unsigned long secs = 0;
unsigned long hrs = 3600000;
unsigned long elaphrs = 0;
unsigned long elapmin = 0;
byte dallasadd1[8];
byte data[8];
int i;
int dallas1;
float light;
int lite;
boolean lighton = false;
float watts1 = 0; // on elapsed time since last blink
float rollavwatts = 100;
float blinktimefloat = 0;
float temp1 = 0;
float temp = 0;
Server server(80);
OneWire ds(dallasPin);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(ledPin, OUTPUT);
attachInterrupt(0, blink, RISING);
ds.reset();
ds.reset_search();
ds.search(dallasadd1);
ds.select(dallasadd1);
ds.write(0x44,1);
}
void loop()
{
time = millis();
elaphrs = int(time / 3600000);
elapmin = elaphrs * 3600000;
elapmin = time - elapmin;
elapmin = elapmin / 60000;
if (time >= secs){ // 1 second loop
secs = time + 1000;
blinktime = max(lastblink - prevblink,time - lastblink);
blinktimefloat = float(blinktime) / 5;
watts1 = 3600000 / blinktimefloat;
ds.reset();
ds.select(dallasadd1);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { data[i] = ds.read(); };
dallas1 = (data[1] << 8) + data[0];
temp = float(dallas1);
temp1 = temp/16;
ds.reset();
ds.select(dallasadd1);
ds.write(0x44,1);
lite = analogRead(0);
light = float(light * 0.8 + lite * 0.2);
};
if (time >= time1){ // 0.2 second loop
time1 = time + 200;
if (lighton){ digitalWrite(ledPin, HIGH); lighton = false; }
else { digitalWrite(ledPin, LOW);};
};
if (time >= mins){ // 1 minute loop
mins = time + 60000;
blinksmin = 0;
rollavwatts = rollavwatts * 0.98 + watts1 * 0.02;
};
if (time >= hrs){ // 1 Hour loop
hrs = time + 3600000;
blinkshour = 0;
};
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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("Welcome to the Majestic Monitor");
client.println("
");
client.print("Milliseconds since start : ");
client.print(time);
client.println("
");
client.print("Hours , Minutes since Start : ");
client.print(elaphrs);
client.print(" ");
client.print(elapmin);
client.println("
");
client.print("Blinks Total , Last Min , Last Hour : ");
client.print(totalblinks);
client.print(" ");
client.print(blinksmin);
client.print(" ");
client.print(blinkshour);
client.println("
");
client.print("Last Blink , Prev Blink, Last - Prev: ");
client.print(lastblink);
client.print(" ");
client.print(prevblink);
client.print(" ");
client.print(lastblink-prevblink);
client.println("
");
client.print("Watts , Rolling Average Watts: ");
client.print(watts1);
client.print(" ");
client.print(rollavwatts);
client.println("
");
client.print("Light Level: ");
client.print(int(light));
client.println("
");
client.print("Digital Thermometer : ");
client.print(temp1);
client.print(" Deg C ");
client.print(dallas1);
client.print(" ");
for( i = 0; i < 8; i++) {
client.print(dallasadd1[i], HEX); client.print(" "); }
;
client.println("
");
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
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
void blink()
{
if (lastblink - time > 500){
lighton = true;
prevblink = lastblink;
lastblink = time;
totalblinks ++;
blinksmin ++;
blinkshour ++;
}
}
One of the problems with using the telnet protocol is that there is handshaking that goes on between the client and server. The handshaking involves strings of bytes sent back and forth.
You should be able to work around this by using a different port than 23. Most telnet clients suppress the handshaking when using a port other than 23.