I'm using a freetronics EtherTen. The issues I'm seeing seem to be commonly commented on about bugs in the Ethernet code library. And my debugging so far indicates that the Ethernet library is just not giving the results you expect. I'll explain the debugging later. The symptoms are that after anywhere from 5 minutes - 5 days, the Ethernet refuses to make a successful connection to a server until the board is reset. I'm using Arduino 1.0 + Ethernet patches from git. I'm running on Ubuntu 10.04. My first question is, what are other running for software to keep their ethernet running un-interrupted. I know Jon has light switches running using EtherTens, I can't believe he resets them every now and again to make sure they stay running.
Now what appears to be happening in code;
I've reduced MAX_SOCK_NUM to 1 as I believe it's related to re-use of sockets. I've reduced my default delay from 60seconds to 0. And I've adjusted the return values of client.connect() to be -1 to -4 for each of the possible return locations that send a failure.
We successfully connect a number of times. Could be thousands. Then we attempt to connect and we get -4 returned from the client.connect() code;
- Code: Select all
while (status() != SnSR::ESTABLISHED) {
delay(1);
if (status() == SnSR::CLOSED) {
_sock = MAX_SOCK_NUM;
return -4;
}
}
So it looks like we are getting a connection that's not completely closing for one reason or another. It's probably in time_wait or the like; The code there supposedly sets us in the CLOSED state before returning.
Next time through the loop() function, we get a -2 returned;
- Code: Select all
if (_sock == MAX_SOCK_NUM) {
for (int i = 0; i < MAX_SOCK_NUM; i++) {
Serial.println(W5100.readSnSR(i), DEC);
}
return -2;
}
This is the state the code is now stuck in. Every call to client.connect() will result in a -2 returned.
I've also got the code printing what connection status code the wiznet is seeing. For about 20seconds after the first failure I see 0x17 (ESTABLISHED). And after about 20 seconds, I start seeing 0x1C (CLOSE_WAIT) which doesn't seem to ever end.
Does anybody have any ideas what's going on here?
My main sketch code without the DHT reading library;
- Code: Select all
#define MAXTIMINGS 85
#include <SPI.h>
#include <Ethernet.h>
// Ethernet setup
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(172,17,73,202); // Bob
// initialize the library instance:
EthernetClient client;
class DHT {
private:
uint8_t data[5];
uint8_t _pin;
boolean read(void);
unsigned long _lastreadtime;
public:
DHT(uint8_t pin);
uint16_t readTemperature(void);
uint16_t readHumidity(void);
};
DHT dht(7);
void setup() {
IPAddress myIp(172,17,73,150);
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(A0, INPUT);
Serial.begin(9600);
Ethernet.begin(mac, myIp);
// give the ethernet module time to boot up:
delay(1000);
}
void loop() {
// Read the temperature and Humidity
uint16_t h = dht.readHumidity();
uint16_t t = dht.readTemperature();
int16_t status = 0;
Serial.println("Looping...");
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
// It failed, dont send any results to the server
Serial.println("Failed to collected data");
} else {
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
status = client.connect(server, 80);
Serial.print("Status: ");
Serial.println(status, DEC);
if (status > 0) {
unsigned long timeout = millis() + 60000;
Serial.println("connecting...");
// send the HTTP PUT request.
// fill in your feed address here:
client.print("GET /log.php?t=");
client.print(t, DEC);
client.print("&h=");
client.print(h, DEC);
client.println(" HTTP/1.0\n");
// Get all client data
// How long to wait for it all to arrive?
while (millis() - timeout >= 60000 && client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
Serial.println();
Serial.print(millis());
Serial.print(" disconnecting.");
client.stop();
Serial.println(millis());
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
// Only complete one read per minutes
delay(0);
}