Go Down

Topic: Getting a static IP for Arduino Ethernet Shield (Read 6700 times) previous topic - next topic

debrouillez40

I've spent a few days trying to figure out how to get basic Ethernet sketches-say the WebServer sketch (attached) to get beyond the LAN.  I have no real experience with the inner workings of the net.  I've learned a bit about port forwarding, and set my cisco linksys to an 8085 port, but I can't find a thread or tutorial that walks one through the method of getting your server tied to a static address.  I know my MAC and the shield's IP, etc. I can get any of the basic Ethernet sketches to work LAN-wide, but that's it. In short, assuming that I've set the port forwarding up correctly:

1. How do I go about getting a static IP or some other method that gets to the server from anywhere? (I'e seen some references to where you add the port # to the end of the IP, such as xx.xxx.xx.:port number. At least that's how I understand it.)
2.  How do I integrate it into the sketch? 


Also, I've read that the SD reader on these shields are crap.  I've had no luck with any sketches that involve pulling data up from an SD card, so it appears that's the case with mine. The serial port acknowledges the card being initialized, but can't find the file.


Here's the server sketch: 


 Program:      eth_websrv_page

  Description:  Arduino web server that serves up a basic web
                page. Does not use the SD card.
 
  Hardware:     Arduino Uno and official Arduino Ethernet
                shield. Should work with other Arduinos and
                compatible Ethernet shields.
               
  Software:     Developed using Arduino 1.0.3 software
                Should be compatible with Arduino 1.0 +
 
  References:   - WebServer example by David A. Mellis and
                  modified by Tom Igoe
                - Ethernet library documentation:
                  http://arduino.cc/en/Reference/Ethernet

  Date:         7 January 2013
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/

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

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

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
}

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
                // 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 Web Page</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>Hello from Arduino!  Happy Valentine's Day!!!!!!!!!!!!!!!</h1>");
                    client.println("<p>A web page from the Arduino server. I'm excited about getting on the journey to understanding how the net works.</p>");
                    client.println("</body>");
                    client.println("</html>");
                    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)

SurferTim

#1
Jul 01, 2015, 04:07 pm Last Edit: Jul 01, 2015, 04:08 pm by SurferTim
The sd card slot works good.

zoomkat

Quote
1. How do I go about getting a static IP or some other method that gets to the server from anywhere? (I'e seen some references to where you add the port # to the end of the IP, such as xx.xxx.xx.:port number. At least that's how I understand it.)
2.  How do I integrate it into the sketch? 
To get an internet static IP address, contact your ISP. You will probably have to pay extra for that. Most people use a dynamic IP service such as www.noip.com to forward traffic to your current internet IP address. You put your dynamic ip address url in your web page so the browser request get routed properly.

Quote
Also, I've read that the SD reader on these shields are crap.  I've had no luck with any sketches that involve pulling data up from an SD card, so it appears that's the case with mine. The serial port acknowledges the card being initialized, but can't find the file.
The SD card slot on the w5100 based ethernet cards work well for me. Also #7 below for posting code:

http://forum.arduino.cc/index.php/topic,148850.0.html
 
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

debrouillez40

Thanks for the reply.  Sorry I'm so ignorant of this stuff.  I took your suggestion and went to noip.com.
I got a  xxxxxx.ddns.net  host.    From there, I don't know what to do.  When I enter that, it directs me to my router settings.  I assume getting it to where the server sketch pushed to the host involve a correct port orientation.  For the port number I assigned in the sketch and in the port forwarding page of the Cisco router settings 8085.  Beyond that, I'm lost. 

zoomkat

Below is some simple arduino server code that shows how to put a no-ip URL into a web page

Code: [Select]

//zoomkat 04-10-15
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = {
  192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = {
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server test no-ip 04-10-15"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
          }
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<TITLE>Arduino GET test page</TITLE>");
            client.println("</HEAD>");
            client.println("<BODY>");

            client.println("<H1>Zoomkat's simple Arduino button</H1>");
            client.println("Arduino served LAN: <a href='/?on1' target='inlineframe'>ON</a>");
            client.println("<a href='/?off' target='inlineframe'>OFF</a><BR><BR>");

            client.println("Remote served LAN: <a href='http://192.168.1.102:84/?on1' target='inlineframe'>ON</a>");
            client.println("<a href='http://192.168.1.102:84/?off' target='inlineframe'>OFF</a><BR><BR>");

            client.println("Remote served no-ip: <a href='http://zoomkat.no-ip.org:84/?on1' target='inlineframe'>ON</a>");
            client.println("<a href='http://zoomkat.no-ip.org:84/?off' target='inlineframe'>OFF</a>");

            client.println("<IFRAME name=inlineframe style='display:none'>");         
            client.println("</IFRAME>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

debrouillez40

Great! It works!   

The only thing that's now holding me back on the shield is that the SD will initialize by the file can't be found.  It's a basic server with SD starter program.  I've looked on forums and followed advice to format the card, make sure it was FAT-32, make sure that the card was set for pin 4, etc, but the serial monitor keeps saying that it initializes but "file not found".   

The test file was a standard hello:

<!DOCTYPE html>
<html>
    <head>
        <title>Arduino SD Card Web Page</title>
    </head>
    <body>
        <h1>Hello from the Arduino SD Card!</h1>
        <p>A web page from the Arduino SD card server.</p>
    </body>
</html>


The sketch is:




/*--------------------------------------------------------------
  Program:      eth_websrv_SD

  Description:  Arduino web server that serves up a basic web
                page. The web page is stored on the SD card.
 
  Hardware:     Arduino Uno and official Arduino Ethernet
                shield. Should work with other Arduinos and
                compatible Ethernet shields.
                2Gb micro SD card formatted FAT16
               
  Software:     Developed using Arduino 1.0.3 software
                Should be compatible with Arduino 1.0 +
                SD card contains web page called index.htm
 
  References:   - WebServer example by David A. Mellis and
                  modified by Tom Igoe
                - SD card examples by David A. Mellis and
                  Tom Igoe
                - Ethernet library documentation:
                  http://arduino.cc/en/Reference/Ethernet
                - SD Card library documentation:
                  http://arduino.cc/en/Reference/SD

  Date:         10 January 2013
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA8, 0x6B };
IPAddress ip(192, 168, 1, 101); // IP address, may need to change depending on network
EthernetServer server(8081);  // create a server at port 80
File webFile;

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for debugging
   
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
}

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
                // 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
                    webFile = SD.open("index.htm");        // open web page file
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    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)
}

zoomkat

The below post has code and attach files that show how to embed SD files in a web page. I always put files on the SD card using the SD slot in my net pad.

http://forum.arduino.cc/index.php?topic=279849.msg1970392#msg1970392
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up