For those trying to avoid the pain of what I just went through here is what I found:
PaulS:
Please explain why those are global variables (that is why they are created twice), rather than class fields.
You could define the variables in the sketch, and add extern in front of the declaration statements in the header. That way, the header file would simply affirm that the variables are defined somewhere else, and would allow access to them, wherever they are defined.
I didn't necessarily want them to be global variables. After seeing that comment I took another look at the library tutorial and noticed that I should be declaring the variables below with the private declarations for the class:
#include "Arduino.h"
#include "eth.h"
void Eth::init(){
mac[0] = 0xDE;
mac[1] = 0xAD;
mac[2] = 0xBE;
mac[3] = 0xEF;
mac[4] = 0xFE;
mac[5] = 0xED;
IPAddress ip(192,168,1,177);
extern EthernetServer server;
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void Eth::servClient(){
Serial.print("Server is at: ");
Serial.println(Ethernet.localIP());
//Serial.println("servClient called");
extern EthernetServer server;
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>Lorens Page</title></head>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
//client.print("Transport is currently: ");
//client.print(stat);
client.println("
");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Clients are unable to connect to the server. I have a feeling this has something to do with the way that I have declared "EthernetServer server(80)" and the way I'm using extern.
Are the functions "init" and "serveClient" in the Eth class creating their own instance of server?