Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
deleted 654 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

I recently bought ana W5100 ethernetEthernet shield and I'm trying to run this simple example, which is a little improvement of the basic WebServer sketch provided by Arduino.cc

#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress DnsServer (192, 168, 1, 6);
IPAddress GateWay (192, 168, 1, 1);
IPAddress SubNetMask(255, 255, 255, 0);
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default

void setup()
  {
    Ethernet.begin(mac, ip, DnsServer, GateWay, SubNetMask);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    pinMode(2, OUTPUT);       // LED on pin 2
}

void loop()
  {
    EthernetClient client = server.available();  // try to get client
 
    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino LED Control</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>LED</h1>");
                    client.println("<p>Click to switch LED on and off.</p>");
                    client.println("<form method=\"get\">");
                    ProcessCheckbox(client);
                    client.println("</form>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
  {
    if (HTTP_req.indexOf("LED2=2") > -1) {
    // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }

    if (LED_status) {    // switch LED on
        digitalWrite(2, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
    }
    else {              // switch LED off
        digitalWrite(2, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
    }
}

So I proceed onwith connecting my shield via a LAN cable connected to my router (Gateway 192.168.1.1), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESSaddress or IP conflicts,conflict. I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6 because I'm running PI-Hole (Isit's an AD-BLOCKERad blocker) on a Raspberry, by the wayand I don't think this is the problem because all device connected and new devicedevices connected to it are working fine.

I tried also to useusing DHCP connection in order to get an IP from the router by doing a:

I recently bought an W5100 ethernet shield and I'm trying to run this simple example which is a little improvement of the basic WebServer sketch provided by Arduino.cc

#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress DnsServer (192, 168, 1, 6);
IPAddress GateWay (192, 168, 1, 1);
IPAddress SubNetMask(255, 255, 255, 0);
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default

void setup()
 {
    Ethernet.begin(mac, ip, DnsServer, GateWay, SubNetMask);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    pinMode(2, OUTPUT);       // LED on pin 2
}

void loop()
 {
    EthernetClient client = server.available();  // try to get client
 
    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino LED Control</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>LED</h1>");
                    client.println("<p>Click to switch LED on and off.</p>");
                    client.println("<form method=\"get\">");
                    ProcessCheckbox(client);
                    client.println("</form>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
 {
    if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }

    if (LED_status) {    // switch LED on
        digitalWrite(2, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
    }
    else {              // switch LED off
        digitalWrite(2, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
    }
}

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.1), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6 because I'm running PI-Hole (Is an AD-BLOCKER) on a Raspberry, by the way I don't think this is the problem because all device connected and new device connected are working fine.

I tried also to use DHCP connection in order to get an IP from the router by doing a:

I recently bought a W5100 Ethernet shield and I'm trying to run this simple example, which is a little improvement of the basic WebServer sketch provided by Arduino.cc

#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress DnsServer (192, 168, 1, 6);
IPAddress GateWay (192, 168, 1, 1);
IPAddress SubNetMask(255, 255, 255, 0);
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80

String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default

void setup() {
  Ethernet.begin(mac, ip, DnsServer, GateWay, SubNetMask); // initialize Ethernet device
  server.begin(); // start to listen for clients
  Serial.begin(9600); // for diagnostics
  pinMode(2, OUTPUT); // LED on pin 2
}

void loop() {
  EthernetClient client = server.available(); // try to get client
  if (client) { // got client?
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) { // client data available to read
        char c = client.read(); // read 1 byte (character) from client
        HTTP_req += c; // save the HTTP request 1 char at a time
 // last line of client request is blank and ends with \n
 // respond to client only after last line received
        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");
          client.println();
          // send web page
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>Arduino LED Control</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<h1>LED</h1>");
          client.println("<p>Click to switch LED on and off.</p>");
          client.println("<form method=\"get\">");
          ProcessCheckbox(client);
          client.println("</form>");
          client.println("</body>");
          client.println("</html>");
          Serial.print(HTTP_req);
          HTTP_req = ""; // finished with request, empty string
          break;
        }
 // every line of text received from the client ends with \r\n
        if (c == '\n') {
 // last character on line of received text
 // starting new line with next character read
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
        }
      } // end if (client.available())
    } // end while (client.connected())
    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection
  } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl) {
  if (HTTP_req.indexOf("LED2=2") > -1) {
    // see if checkbox was clicked
 // the checkbox was clicked, toggle the LED
    if (LED_status) {
      LED_status = 0;
    } else {
      LED_status = 1;
    }
  }

  if (LED_status) { // switch LED on
    digitalWrite(2, HIGH);
    // checkbox is checked
    cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
  } else { // switch LED off
    digitalWrite(2, LOW);
    // checkbox is unchecked
    cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
  }
}

So I proceed with connecting my shield via a LAN cable to my router (Gateway 192.168.1.1), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC address or IP conflict. I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6 because I'm running PI-Hole (it's an ad blocker) on a Raspberry, and I don't think this is the problem because all devices connected to it are working fine.

I tried also using DHCP connection in order to get an IP from the router by doing a:

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited body
Source Link
FabioEnne
  • 233
  • 7
  • 19

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.61), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.6), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.1), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

improved formatting and spelling
Source Link

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.6192.168.1.6), but if I ping 192.168.1.20 192.168.1.20 (my arduinoArduino) it says "Host Unreachable", my ruouterrouter use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6192.168.1.6 because I'm running PI-Hole (Is an AD-BLOCKER) on a Raspberry, by the way I don't think this is the problem because all device connected and new device connected are working fine.

Thanks.

EDIT: I tryiedtried also to use DHCP connection in order to get an IP from the router by doing a:

Ethernet.begin(mac);

Ethernet.begin(mac);

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.6), but if I ping 192.168.1.20 (my arduino) it says "Host Unreachable", my ruouter use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6 because I'm running PI-Hole (Is an AD-BLOCKER) on a Raspberry, by the way I don't think this is the problem because all device connected and new device connected are working fine.

Thanks.

EDIT: I tryied also to use DHCP connection in order to get an IP from the router by doing a:

Ethernet.begin(mac);

So I proceed on connecting my shield via LAN cable connected to my router (Gateway 192.168.1.6), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC ADDRESS or IP conflicts, I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6 because I'm running PI-Hole (Is an AD-BLOCKER) on a Raspberry, by the way I don't think this is the problem because all device connected and new device connected are working fine.

I tried also to use DHCP connection in order to get an IP from the router by doing a:

Ethernet.begin(mac);
Source Link
FabioEnne
  • 233
  • 7
  • 19
Loading