Hi I'm having problem with the code from the book of Simon Monk with the project ''web relay'', i have a ethernet shield 2 with poe, with an arduino uno R3 and the code wont work. I added the library ''ethernet2'' for it to work but still wont. I tried other example scketech from the ethernet 2 library from the IDE and they worked so I know it's not my shield that is broken.
That is the exact same code from the book exept I changed it to my local ip and changed the #include <Ethernet.h> to #include <Ethernet2.h>
Is there a way to make it work with my shield because it seems like this code wasn't intended for this shield.
// web_relay
// Arduino Uno or (Leonardo with Ethernet SHield R3)
#include <SPI.h>
#include <Ethernet2.h>
// MAC address just has to be unique. This should work
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address will be dependent on your local network:
IPAddress ip(192, 168, 1, 7);
EthernetServer server(80);
int relayPin = A0;
char line1[100];
void setup()
{
pinMode(relayPin, OUTPUT);
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
readHeader(client);
if (! pageNameIs("/"))
{
client.stop();
return;
}
digitalWrite(relayPin, valueOfParam('a'));
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// send the body
client.println("<html><body>");
client.println("<h1>Relay Remote</h1>");
client.println("<h2><a href='?a=1'/>On</a></h2>");
client.println("<h2><a href='?a=0'/>Off</a></h2>");
client.println("</body></html>");
client.stop();
}
}
}
void readHeader(EthernetClient client)
{
// read first line of header
char ch;
int i = 0;
while (ch != '\n')
{
if (client.available())
{
ch = client.read();
line1[i] = ch;
i ++;
}
}
line1[i] = '\0';
Serial.println(line1);
}
boolean pageNameIs(char* name)
{
// page name starts at char pos 4
// ends with space
int i = 4;
char ch = line1[i];
while (ch != ' ' && ch != '\n' && ch != '?')
{
if (name[i-4] != line1[i])
{
return false;
}
i++;
ch = line1[i];
}
return true;
}
int valueOfParam(char param)
{
for (int i = 0; i < strlen(line1); i++)
{
if (line1[i] == param && line1[i+1] == '=')
{
return (line1[i+2] - '0');
}
}
return 0;
}