I'm having trouble programming an Arduino Uno with this sketch, the sketch compiles and I get full serial output and an IP address, but no web browser can connect to it.
I've tried this Arduino example (https://www.arduino.cc/en/Tutorial/WebServer) and it works right out of the box, I don't understand why my code therefore doesn't work.
My code is below:
#include <SPI.h>
#include <Ethernet.h>
// Bootup millisecond counter
const unsigned long started = millis();
// Serial settings
const int SERIAL_BAUDRATE = 9600;
// Non-DHCP ethernet settings:
// Device MAC address: DE:AD:BE:EF:FE:ED
const byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(
192, 168, 1, 177
);
// Pins
const int P_RELAY = 7;
const int P_ON_LED = 8;
const int P_OFF_LED = 9;
const int P_SW_MAN = 10;
// Relay power states
const char B_RELAY_ON = HIGH;
const char B_RELAY_OFF = LOW;
// Relay switching state
boolean RELAY_STATE = false;
// Network/web data
EthernetServer server(80);
String readString;
String localIp;
// Testing switches
const boolean connectToEthernet = true;
const boolean hostWebServer = true;
const boolean useDHCP = false;
const boolean testRelay = false;
// Testing settings
const int testRelayDelay = 500;
// Formats an IPAddress nicely as a string for printing
String displayAddress(IPAddress address) {
return String(address[0]) + "." +
String(address[1]) + "." +
String(address[2]) + "." +
String(address[3]);
}
void setup() {
const unsigned long started = millis();
delay(50); // allow some time (50 ms) after powerup and sketch start, for the Wiznet W5100 Reset IC to release and come out of reset.
Serial.begin(SERIAL_BAUDRATE);
Serial.println("HTTP Web Remote Power-Point Controller.");
Serial.println();
if (connectToEthernet) {
Serial.println("Connecting to Ethernet...");
if (useDHCP) {
Ethernet.begin(mac);
Serial.println("Obtaining IP address...");
localIp = displayAddress(Ethernet.localIP());
} else {
Ethernet.begin(mac, ip);
localIp = displayAddress(ip);
}
if (localIp != "0.0.0.0") {
Serial.println("Connected. IP: " + localIp);
if (hostWebServer) {
Serial.println("Starting Ethernet server...");
server.begin();
Serial.println("Started.");
}
} else {
Serial.println("Error: No address provided via DHCP.");
Serial.println("Please check your network connection and settings, and try again.");
while (1);
}
}
Serial.println("Configuring pins...");
pinMode(P_RELAY, OUTPUT);
pinMode(P_ON_LED, OUTPUT);
pinMode(P_OFF_LED, OUTPUT);
pinMode(P_SW_MAN, INPUT_PULLUP);
Serial.println("Pins configured.");
Serial.println("Pin Information: \n Relay: " + String(P_RELAY) +
"\n On LED: " + String(P_ON_LED) +
"\n Off LED: " + String(P_OFF_LED) +
"\n Manual Switch: " + String(P_SW_MAN));
if (hostWebServer && connectToEthernet) {
Serial.println("Remote controller interface is running at http://" + localIp + "/");
}
if (testRelay) {
Serial.println("Testing relay mode enabled.");
Serial.println("Toggling relay every " + String(testRelayDelay) + " milliseconds...");
}
const unsigned long starttime = (millis() - started);
Serial.println("Startup took " + String(starttime) + " milliseconds.");
}
void loop() {
if (testRelay) {
delay(testRelayDelay);
if (RELAY_STATE) {
toggleRelay(false);
} else {
toggleRelay(true);
}
}
if (useDHCP) {
byte dhcpRenew = Ethernet.maintain();
if (dhcpRenew == 1) {
Serial.println("Dynamic DHCP lease renewal failed.");
} else if (dhcpRenew == 2) {
Serial.println("Dynamic DHCP lease was renewed.");
} else if (dhcpRenew == 3) {
Serial.println("Dynamic DHCP lease rebind failed.");
} else if (dhcpRenew == 4) {
Serial.println("Dynamic DHCP lease was rebound.");
}
}
EthernetClient client = server.available();
if (client) {
Serial.println("Client connected to web server.");
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (String(c) == "\n") {
Serial.println("Data received from HTTP client: " + String(readString));
// HTTP header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// HTTP body
if (readString.indexOf("?state") > 0) {
if (RELAY_STATE) {
client.print("1");
} else {
client.print("0");
}
} else {
client.println("<!DOCTYPE HTML>");
client.println("<html lang=\"en\">");
client.println("<head>");
client.println("<title>Power-Point Controller</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>hello world</h1>");
client.println("</body>");
client.println("</html>");
}
delay(10);
client.stop();
Serial.println("Client disconnected from web server.");
if (readString.indexOf("?relay=1") > 0) {
toggleRelay(true);
} else if (readString.indexOf("?relay=0") > 0) {
toggleRelay(false);
}
readString = "";
}
}
}
}
}
void toggleRelay(bool state) {
if (state == true) {
digitalWrite(P_OFF_LED, LOW);
digitalWrite(P_RELAY, B_RELAY_ON);
digitalWrite(P_ON_LED, HIGH);
RELAY_STATE = true;
Serial.println("Relay State: ON");
} else if (state == false) {
digitalWrite(P_ON_LED, LOW);
digitalWrite(P_RELAY, B_RELAY_OFF);
digitalWrite(P_OFF_LED, HIGH);
RELAY_STATE = false;
Serial.println("Relay State: OFF");
}
}
useDHCP
andtestRelay
are false, so test your source without that superfluous code).