Dear all, I am using the example program from arduino ide to rewrite the code to ping my router at home. I have it ping and connect from the beginning, but then it will show fail connection if I restart the board by reset the reset pin to ground, or if I unplug the power and plug it it back into the Arduino Uno board.
Would there be a code to fix this problem? Please help. Thank you in advance!
#include <Ethernet.h>
#include <SPI.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial LCD(2,3); //communicate on pin 3
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = { 0x91, 0xE2, 0xMA, 0x0D, 0x25, 0x4K };
byte ip = { 192, 168, 1, 105 };
byte gateway = { 192, 168, 1, 1 }; // internet access via router
byte subnet = { 255, 255, 255, 0 };
IPAddress ping(192, 168, 1, 1);
EthernetClient client;
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
LCD.begin(9600);
// give the Ethernet shield a second to initialize:
delay(500);
LCD.println(“CONNECTING…”);
delay(4000);
LCD.write(0xFE);
LCD.write(0x01);
// if you get a connection, report back via serial:
if (client.connect(ping,80)) {
LCD.println(“CONNECT”);
delay(4000);
LCD.write(0xFE);
LCD.write(0x01);
}
else {
// if you didn’t get a connection to the server:
LCD.println(“CONNECTION FAIL”);
delay(4000);
LCD.write(0xFE);
LCD.write(0x01);
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
LCD.print(c);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it’s open:
while (LCD.available() > 0) {
char inChar = LCD.read();
if (client.connected()) {
client.print(inChar);
}
}
// if the server’s disconnected, stop the client:
if (!client.connected()) {
LCD.println();
LCD.println(“DONE”);
delay(5000);
LCD.write(0xFE);
LCD.write(0x01);
client.stop();
// do nothing:
//while (true);
for(;;);
}
}