So I have been dealing with this for the past few days. I just got my arduino uno R3 and a ebay ethernet shield W5100. Everything works for about 30min to 1hr then things go wrong.
I have it set up with the DHCP Server example but modified so that I can connect to it via putty telnet, I can send messages to it which can turn on or off relays.
After 30min - 1hr I can no longer connect to it. The uno is still working and can process code and I can ping the shield and all the lights on the shield are functioning just like before. It is set up with a static IP and I confirmed this looking at the attached devices on my network. The only way that I know of getting it to work again is to reset it.
I really don't know what else to do. I have been on google for the past few days and I have tried many things and it still freezes up. I don't know if its a bad shield or I just have not tried the correct code.
Any help is appreciated.
Pics when I cannot connect to it.
Edit: Sorry I had some old code. I have updated to use my current code. Not much changed tho.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
int relays[] = {2, 3, 4, 5, 6, 7, 8, 9}; //Pins that relays are on in order from 1 - 8
String relayName[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"}; //Relay Names in order from 1 - 8
int randomNumber = 0;
int input = 0;
int val = 0;
int welcomeFlag = 0;
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; // Enter a MAC address and IP address for your controller.
IPAddress ip(192, 168, 1, 177); // The IP address will be dependent on your local network.
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 1); // gateway and subnet are optional:
IPAddress subnet(255, 255, 255, 0); // gateway and subnet are optional:
EthernetServer server(23); // telnet defaults to port 23
void setup() {
Serial.begin(9600); // Open serial communications and wait for port to open:
Ethernet.begin(mac, ip, myDns, gateway, subnet); //Starts Ethernet Connection
delay(1000);
Serial.print("My IP address: ");
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) { // print your local IP address:
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(".");
}
Serial.println();
server.begin(); // start listening for clients
//Pin Setups and modes;
for(int z = 0; z < 8; z++){
pinMode(relays[z], OUTPUT);
digitalWrite(relays[z], HIGH);
}
}
void loop() {
EthernetClient client = server.available(); // wait for a new client:
while (client.connected()) {
if(welcomeFlag == 0){
client.println("Connected...");
welcomeFlag = 1;
}
delay(100);
//client.print("hello");
input = client.read(); // read the bytes incoming from the client:
if(input == 104){
helpme(client); //Calls helpme fuction
}
else if(input == 113){
Serial.println("Disconnection");
client.print("Disconnected");
delay(1000);
client.stop();
}
else if(input == 100){
for (int r = 0; r < 100; r++){
randomNumber = random(10);
relay(randomNumber, client);
delay(100);
}
turnAllOff(client);
}
else{
input -= 49;
if(input >= 0 && input <= 7){
relay(input, client); //Calls for a single relay to turn on or off
}
else if(input == -1){
turnAllOff(client); //Calls fuction to turn all relays off
}
else if(input == 8){
turnAllOn(client); //Calls fuction to turn all relays on
}
}
}
client.stop();
if(welcomeFlag == 1){
welcomeFlag = 0;
}
}
void relay(int x, EthernetClient client){
client.println("--------------");
val = digitalRead(relays[x]);
if(val == 1){
digitalWrite(relays[x], LOW);
client.print("turning Relay ");
client.print(relays[x] - 1);
client.println(" on");
}
else{
digitalWrite(relays[x], HIGH);
client.print("turning Relay ");
client.print(relays[x] - 1);
client.println(" off");
}
}
void turnAllOff(EthernetClient client){
client.println("--------------");
for(int i = 0; i < 8; i++){
digitalWrite(relays[i], HIGH);
client.print("turning Relay ");
client.print(relays[i] - 1);
client.println(" off");
}
}
void turnAllOn(EthernetClient client){
client.println("--------------");
for(int i = 0; i < 8; i++){
digitalWrite(relays[i], LOW);
client.print("turning Relay ");
client.print(relays[i] - 1);
client.println(" on");
}
}
void helpme(EthernetClient client){
client.println("--------------");
int oneornone = 0;
for(int y = 0; y < 8; y++){
client.print("relay ");
client.print(y + 1);
client.print(" - ");
client.print(relayName[y]);
oneornone = digitalRead(relays[y]);
if(oneornone == 1){
client.print("\t\t");
client.println("(-off-)");
}
else{
client.print("\t\t");
client.println("(on)");
}
}
}