Hello,
I tried to find similar scenarios like we have, but found nearly none.
However many do complain of the similar issue of lockups/freezes.
I read some interesting posts from esteemed SurferTim and Pert and other kind contributors/helpers/savers.
We need to send data (several lines of strings of ca 100 characters) once every 15, 30, 45 or 60 minutes.
We must also very much save power and thus are powering on the Ethernet Shield2 only for sending this data.
Attached please kindly review the program we have.
I tried my best to reduce it to the main routines we use, really hope it compiles for you, it uses just standard libraries as is.
The rest of the program is just assembling the string and checking when to send etc. and has nothing to do with the problem we have.
We do HW reset the Ethernet Shield2 before sending each line, that helped a bit.
The problem we are still facing are random lockups when sending.
Somewhere when trying to send the program times out, get's stuck.
So we implemented the watchdog to catch such a lockup and reboot the Arduino.
What are we doing wrong?
How could we make the code more stable, i.e. prevent lockups please?
Also, does the constant client.connect, client.stop and Ethernet.begin we have to use cause some other issues like wearing our the EEPROM or filling up the stack or so?
I tried to dive into the underlying libraries but got lost, my programming days have passed nearly 4 decades ago unfortunately.
Thank you for your kind help with this!
/* Arduino MEGA with Ethernet Shield2.
* Ethernet Shield2 is powered on/off via pin ETHPOWER
* It can also be reset individually via pin ETHRESET
*/
#include <EEPROM.h> // read/write to EEPROM
#include <Wire.h> // I2C peripherial interface, for communication with GPS
#include <SPI.h> // Serial peripherial interface, for communication with Ethernet Shield2 and memory
#include <Ethernet.h> // Ethernet Shield2 library
#include <EthernetUdp.h> // for remote access
#include <utility/W5100.h> // Ethernet Shield2 functions
EthernetClient client; // For outgoing connections, upload to webserver
IPAddress ip(192, 168, 173, 244); // from tower
IPAddress dnsServer(8, 8, 8, 8);
IPAddress gateway(192, 168, 173, 254); // Must be 254 for tower
IPAddress subnet(255, 255, 255, 0);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char cmDserver[] = "www.test.com"; // Web server to connect to
char charPut[200]; // maximum line length
String strPut; // this string is sent to the server
int clientConnectStatus;
// pins and states
const byte ETHPOWER = 16; // Ethernet power control; on=LOW, off=HIGH
const byte ETHOFF = HIGH; // power Ethernet Shield2 only when needed
const byte ETHON = LOW; // (default)= Power to Eth is connected
const byte ETHRESET = 19; // Resets ONLY Ethernet Shield2 board
const byte ETHnoRESET = HIGH; // (default)= Reset pin of Ethernet Shield2 is high
const byte ETHdoRESET = LOW; // Low resets Ethernet Shield2 only
const byte ETHSS = 53; // pin used for Ethernet Shield2 SS (instead of SS10)
void setup() {
Serial.begin(115200);
pinMode(ETHPOWER, OUTPUT); // activtes Ethernet Shield2 power control pin
pinMode(ETHRESET, OUTPUT); // activtes Ethernet Shield2 reset control pin
}
void loop() {
enableEthernet();
client.flush();
// w="test" "test" is the string sent to the webserver
strPut = "PUT /wx/saveme.php?sub=subdir2&w=test HTTP/1.1\r\nHost: www.test.com\r\nConnection: close\r\n\r\n";
strPut.toCharArray(charPut, strPut.length());
client.setTimeout(600);
clientConnectStatus = client.connect(cmDserver, 80);
Serial.print("Client connect status:");
Serial.println(clientConnectStatus);
client.write(charPut, strPut.length()); //use a single packet with char[].
client.flush();
disableEthernet();
delay(5000);
}
void enableEthernet() {
digitalWrite(ETHPOWER, ETHON); // turns Ethernet Shield2 on
delay(100); // capacitors might delay correct power, give it time to settle
digitalWrite(ETHRESET, ETHnoRESET); // turn off reset, let Ethernet Shield2 boot up
delay(100); // 1ms per datasheet but POR capacitor might delay it
Ethernet.init (ETHSS) ; // This pin selects the Ethernet Shield2 on the SPI bus
Ethernet.begin(mac, ip, dnsServer, gateway, subnet); // Eth must be initialized after each power up
delay(1000);
W5100.setRetransmissionTime(0x07D0); // don't wait too long
W5100.setRetransmissionCount(4);
}
void disableEthernet() {
Serial.print("Executing disableEthernet()...");
client.stop();
digitalWrite(ETHPOWER, ETHOFF);
Serial.println("Done.");
}