Sketch works OK until disconnect/reconnect USB (power) to Arduino UNO.
SETUP: Windows 7, Arduino Uno, W5200 Ethernet Shield, Relay Shield
What do I have to do to make this sketch work stand alone without having to reload sketch after each power up?
#include <SPI.h>
#include <EthernetV2_0.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,200, 2);
IPAddress gateway(192,168,200, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(23);
#define W5200_CS 10
#define SDCARD_CS 4
// int Relay1 = 4; Relay1 (pin 4) conflicts with W5200 Ethernet Shield)
int Relay2 = 5;
int Relay3 = 6;
int Relay4 = 7;
String commandString;
void setup() {
delay(2000);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(SDCARD_CS,OUTPUT);
digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
while (client.available()){
char newChar = client.read();
if (newChar == 0x0D)
{
processCommand(commandString);
// gotAMessage = false;
client.stop();
} else
{
commandString += newChar;
}
}
}
void processCommand(String command)
{
if (command.indexOf("RelayOn") > -1){
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,HIGH);
commandString = "";
return;
}
if (command.indexOf("RelayOff") > -1){
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,LOW);
commandString = "";
return;
}
commandString = "";
}