Alright, so I bought a book to help teach me about some Arduino programming. My Dad bought me an ethernet shield when I got my R3 and I never really knew what to do with it until I got the book, which dedicates a whole chapter to using Arduino with an ethernet shield.
I’m trying to hook this up to my train set and be able to turn things on and off from my upstairs computer (train set is in the basement). We took our old router and made it into a bridge to communicate to our current router to communicate to our computer. Pinging my Arduino’s IP address works, so I know the connection is fine.
In the book it included code to be able to turn digital pin 8 on and off through a webpage. This is the code it had (that I copied into an Arduino Sketch):
#include <SPI.h>
#include <Ethernet.h>
byte mac] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip] = { 192,168,1,130 };
const int MAX_PAGENAME_LEN = 8;
char buffer[MAX_PAGENAME_LEN+1];
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
delay(2000);
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
int type = 0;
while (client.connected())
{
if (client.available())
{
memset(buffer,0, sizeof(buffer));
if(client.find("/"))
if(client.readBytesUntil('/', buffer,sizeof(buffer)))
{
Serial.println(buffer);
if(strcmp(buffer,"POST ") == 0)
{
client.find("
\r");
while(client.findUntil("PinD", "
\r"))
{
int pin = client.parseInt();
int val = client.parseInt();
pinMode(pin, OUTPUT);
digitalWrite(pin, val);
}
}
sendHeader(client,"Post example");
client.println("<h2>Click buttons to turn pin 8 on or off</h2>");
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
client.println(" value='0'><input type='submit' value='Off'/></form>");
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
client.println(" value='1'><input type='submit' value='On'/></form>");
client.println("</body></html>");
client.stop();
}
break;
}
}
delay(1);
client.stop();
}
}
void sendHeader(EthernetClient client, char *title)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head><title>");
client.print(title);
client.println("</title><body>");
}
I understand that the 2 HTML buttons assign the value of on or off (1 or 0) to the function digitalWrite(pin, val). However when I click either of the buttons on the webpage generated on the local IP address 130 nothing changes to the LED I have on digital pin 8. (and there are no electrical issues, I checked)
Can anyone help me? I’ve been stuck on this pretty much all day.
Thanks in advance.