After using surferTims patch for w5100.h, I got my keep-alive sensor value server up and running. And right away stumple into the next issue. The time it takes.
First I suspected the analogRead.
Then I suspected the ethernet shield and the many single "client.print()"
Then I suspected the String library by itself.
But I just cannot nail it down to any of this.
Here is my code (PLEASE NOTE THE TIME MEASURES IN THE COMMENTS)
/*
Keep Alive Server socket
Receives one-char-instructions what to send back next
Closes the socket when receiving a "X"
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 2011 (based on the Webserver example)
by Thomas Schuett
*/
#define FASTADC 1
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 111 };
//byte gateway[] = {192,168,0,1};
//byte subnet[] = { 255, 255, 255, 0 };
//String responseStr; // then nothing works any more !?!?
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void setup()
{
// from the forum:
#if FASTADC
// set prescale to 16
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
#endif
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
delay(1000);
blink();
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
//blink();
while (client.connected()) {
if (client.available()) {
//blink2();
char c = client.read();
client.println(c);
if (c == 'A') { // needs 14 ms
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(analogChannel)); // takes around 3 ms each ??
client.println();
}
}
if (c == 'B') { // needs 33 ms (!!!!!!)
String response111 = String("");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
response111 += "analog input ";
response111 += analogChannel;
response111 += " is ";
response111 += analogRead(analogChannel);
response111 += "\n";
}
client.println(response111);
}
if (c == 'C') { // needs 5 ms
String response111 = String("");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
response111 += "analog input ";
response111 += analogChannel;
response111 += " is ";
response111 += analogRead(analogChannel);
response111 += "\n";
}
client.println("xxx");
}
if (c == 'D') { // 33 ms
String response111 = String("");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
response111 += "analog input ";
response111 += analogChannel;
response111 += " is ";
response111 += "xxx";
response111 += "\n";
}
client.println(response111);
}
if (c == 'E') { // 33 ms
String response111 = String("");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
response111 += "analog input ";
response111 += analogChannel;
response111 += " is ";
response111 += "xxx";
response111 += " ";
}
client.println(response111);
}
if (c == 'F') { // needs 3 to 4 ms
String response111 = String("");
for (int analogChannel = 0; analogChannel < 1 ; analogChannel++) {
response111 += analogRead(analogChannel);
response111 += " ";
}
client.println(response111);
}
if (c == 'G') { // needs 8 ms
String response111 = String("");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
response111 += "---";
response111 += " ";
}
client.println(response111);
}
if (c == 'H') { // needs 2 ms
client.println("a");
}
if (c == 'I') { // needs 3 to 4 ms
client.println("a b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b c");
}
if (c == 'J') { // needs 2 ms
client.println("a\nb\nb\nb\nb\nb\nb\nc\n");
}
client.println("END.");
if (c == 'X') {
break;
}
}
}
// give the other side time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
And in case you want to test it by yourself, here is the client side, written in Java:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class LoopThread extends Thread {
public boolean stop = false;
// public String sendChars = "AIIIA";
public String sendChars = "J";
int whichChar = 0;
public void run() {
Socket s = null;
OutputStreamWriter sout =null;
try {
s = new Socket("192.168.0.111",80);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
sout = new OutputStreamWriter(out);
BufferedInputStream bin = new BufferedInputStream(in);
int cnt = 0;
do {
cnt++;
System.out.println("======================= "+cnt);
boolean ende = false;
String c = sendChars.substring(whichChar,whichChar+1);
whichChar++;
if (whichChar == sendChars.length()) whichChar = 0;
long startTime = System.currentTimeMillis();
sout.write(c);
sout.flush();
while (! ende && ! stop) {
String line = readLine(bin);
System.out.print(line);
if (line.indexOf("END.") > -1) {
ende = true;
}
}
long stopTime = System.currentTimeMillis();
System.out.println("Time used: "+(stopTime-startTime)+ " ms");
try { Thread.sleep(20); } catch (InterruptedException e) { }
} while (! stop);
sout.write("X");
sout.flush();
} catch (Throwable e) {
e.printStackTrace();
}
try {
try { Thread.sleep(20); } catch (InterruptedException e) { }
s.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("\n\n==== ENDE ====");
}
private String readLine(BufferedInputStream bin) throws IOException {
StringBuffer buf = new StringBuffer();
int l = 0;
do {
l = bin.read();
if (l > -1) {
buf.append((char) l);
}
else {
break;
}
} while ((char) l != '\n');
if (buf.length() > 0) return buf.toString();
else throw new IOException("input stream closed");
}
}
and
import java.io.IOException;
public class Loop {
static boolean stop = false;
public static void main(String[] args) {
LoopThread t = new LoopThread();
t.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
t.stop = true;
}
}
Does anyone understand the measured timings?
(BTW: Yes, I saw the thread "Arduino Ethernet Shield Speed", but that turned out to be due to serial debug output delays.)