Hello all,
I am new to the forum here at Arduino, so I apologize ahead of time if my post doesn't thoroughly-explain my problem. I was basically interested in doing this instructables project that required use of the HanRun ethernet shield, so I acquired one from a local retailer whom sells almost any piece of Arduino hardware you can think of. I was testing the example library sketches to explore some basic features like IP addresses, web servers, etc. The first thing I tried was viewing my IP address, as my project required me to obtain one for locational accuracy, so I used the example sketch "DhcpAddressPrinter" to print a custom IP address. The code is as follows:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
printIPAddress();
}
void loop() {
switch (Ethernet.maintain())
{
case 1:
//renewed fail
Serial.println("Error: renewed fail");
break;
case 2:
//renewed success
Serial.println("Renewed success");
//print your local IP address:
printIPAddress();
break;
case 3:
//rebind fail
Serial.println("Error: rebind fail");
break;
case 4:
//rebind success
Serial.println("Rebind success");
//print your local IP address:
printIPAddress();
break;
default:
//nothing happened
break;
}
}
void printIPAddress()
{
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
Now, it is to my understanding that the newer HanRun boards have their IP address printed on a sticker on the back. However, mine does not have that, (so I wrote my own custom one), and it also has a slightly-different layout from the other ones I have seen in different users' topics. It too has an SD card slot, which I have verified is empty, so it shouldn't be influencing my code. When I am running the sketch, nothing is being printed in my serial monitor. Even after leaving it for five minutes or more, nothing appears (not even a failure message). Baudrate is 9600 as it should be, so it can't be that. I have verified all my connections are good, including my one to the router. I suspected that I might have the newer W5500 board, as I read on other posts that people solve their issue when they switch to the "ethernet2" library, but I tried it too and had no luck. So I decided to switch to the other example, "WebServer", which allows to view the I/O and analog input statuses on a web page. The sketch is below:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 167);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
I used the same custom IP address but even though the code uploaded to the board correctly, I kept on getting the "page could not be loaded" error message on my browser. Again, even through repeated resets and checking that all my pins were plugged in and my ethernet cable is connected properly, nothing seemed to work. I feel that its worth mentioning that I have no circuits wired to my board currently, I am just trying to test it and understand how it all works before implementing it into my project. The fact that neither of these examples worked (nor did those in "ethernet2" library) leads me to wonder if this particular board is defective, though I believe that it comes from Arduino itself as it bears the brand logo and name on it. I even tried switching to another one of my UNO's to see if it was my UNO board itself (which is not an Arduino-made board) causing the issue, but had no luck.
I apologize if this is a very drawn-out question, but I have viewed my YouTube videos and existing forums and tried to use them to fix my issue with no luck. Should I just try exchanging the HanRun board or is there a possible different issue that I am overlooking? Thank you in advance.