Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: Rog2323 on May 19, 2013, 12:49 pm

Title: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: Rog2323 on May 19, 2013, 12:49 pm
Hello all Arduino users.

I have found an excellent resource for making a web server using the Arduino Ethernet shield:
http://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/ (http://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/)

This is actually a set of tutorials and covers all the technologies necessary for making a web server.
It covers:


Someone commented on Twitter that this is possibly the best Arduino web server tutorial ever. After following it I can agree.  8)

So if you want to use the Arduino Ethernet shield as a web server, but are clueless where to start, then take a look at the tutorial - highly recommended.
Enjoy!
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: liuzengqiang on May 21, 2013, 07:57 am
I only read the first few parts. So far so good. I wish I had see it before I started exploring. Thumbs up!
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: Zevna on May 23, 2013, 05:15 pm
Thanks for sharing! You made my day!

Cheers!

L
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: JayStel on May 24, 2013, 05:23 am
Went through the Ethernet tutorial. Outstanding! Bookmarked the site.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: Rog2323 on May 25, 2013, 12:19 pm
Just wondered what hardware you guys are using? Which Arduino board / Ethernet shield (or the combo one called Arduino Ethernet) and has anyone used the WiFi shield with the tutorial?

I saw a post on this forum about all sorts of problems with the WiFi shield. Anyone have any luck with it?

Rog.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: liuzengqiang on May 25, 2013, 05:55 pm
The tutorial is mostly independent from what hardware is used, ethernet or wifi. Yes, I am responsible for posting and replying a lot of these wifi shield problem threads :)

The shield is not as stable as ethernet shield so if you want to try one, buy one that says R3 on it and never upgrade firmware. Then use arduino ide 1.0.2
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: Rog2323 on May 29, 2013, 01:56 pm

The tutorial is mostly independent from what hardware is used, ethernet or wifi. Yes, I am responsible for posting and replying a lot of these wifi shield problem threads :)

The shield is not as stable as ethernet shield so if you want to try one, buy one that says R3 on it and never upgrade firmware. Then use arduino ide 1.0.2


Thanks for the info, liudr. I think I will wait a while before getting the WiFi shield.

I was asking about what hardware everyone was using out of curiosity, but good to know that the tutorial should work on most hardware. I am using a Arduino Uno before R2 revision and Ethernet shield v5. I think it is v5, the version number is not printed on the board and I am not sure how to find it. It looks like the one on shieldlist.org, so I think it must be version 5: http://shieldlist.org/arduino/ethernet-v5 (http://shieldlist.org/arduino/ethernet-v5)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: liuzengqiang on May 29, 2013, 06:37 pm
The wifi client side is relatively robust enough on the other hand. I would wait a bit before spend the $100 USD for this shield. They just released a new IDE version and firmware for the wifi shield. If they have ears, I bet they are listening to the complaints and plan for future revisions right now.
Title: How to access different network?
Post by: danilovpm on Aug 07, 2013, 07:02 pm
How to access different network?

Web servers in this tutorial are used to serve web pages that can be accessed from a web browser running on any computer connected to the same network as the Arduino.

Thanks for sharing the tutorial, very good
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: pico on Aug 12, 2013, 06:43 pm

I was asking about what hardware everyone was using out of curiosity, but good to know that the tutorial should work on most hardware.


I'm using an nRF24L01+ module for Wifi hardware, LOL.

I've got an example AJAX webserver sketch that is directly derived from the one on the startingelectronics.com. It's actually a bit simpler than the original sketch. Here it is:

http://embeddedcoolness.com/faq/00500-ajax-web-server/ (http://embeddedcoolness.com/faq/00500-ajax-web-server/)

I developed the RFX system because I thought Wifi shields were way too expensive a solution for wireless Internet for Arduinos and related devices. So I looked at how to use inexpensive radio transceivers (nRF24L01+) in order to do the same things. (The low powered versions can be had for less than $5, and the high power versions for less than $20.)

It's now affordable to give all your Arduino/Arduino-like projects wireless Internet capability, if that's what you want. (I know it's what I want!)

I've got some code snippets in the docs on the site showing how to do some common things (like send emails, post updates to an Xively feed, etc.), but I'll progressively be putting up additional complete example sketches to show how to do these things.

Hopefully I'll find time to get some new examples up in the next few days.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: patrikk on Aug 21, 2013, 01:48 pm
Has anyone got this tutorial working ? I have problems with showing the webpage
I did this tutorial:

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-IO/

I've done other web server tutorial on that site and it work ( the none ajax).

I've also done another one from another site but both are based on David A. Mellis work.


and yes I've set the correct ip. I used the code as it was except for the IP and MAC
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 21, 2013, 01:53 pm
I use this code.
http://playground.arduino.cc/Code/WebServerST
It will keep port scanners and hackers like me from locking up your sketch. PuTTY is a wonderful tool.  :)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: patrikk on Aug 21, 2013, 02:18 pm
Thanks but that solutions will not have an interactive GUI and also it will produce addtional text in the url if you want to change a port value.

I require a ajax solution to meet my needs.  so any other suggestions?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 21, 2013, 03:00 pm
Search the forum for ajax with zoomkat as the author. He has some good ideas, even tho I can lock up most of his code in less than a minute.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: patrikk on Aug 22, 2013, 11:39 am
My problem was that I removed the SD include and the SD code and added the html into the sketch ( because I couldnt find my memory card). So I just bought a new one and used the code as is( except the IP change) and it worked fine.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 22, 2013, 03:03 pm
That server code is easy to lock up. I can do that in a few seconds with PuTTY.  :)

Most of zoomkat's code is more difficult than that. I need my Mega2560/ethernet shield to lock up some of his code examples. I'm not running down zoomkat here. He is a good guy and really smart, like I said in another post. His code has a few vulnerabilities if left exposed on the internet.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 22, 2013, 10:59 pm

I use this code.
http://playground.arduino.cc/Code/WebServerST
It will keep port scanners and hackers like me from locking up your sketch. PuTTY is a wonderful tool.  :)


Tim, can you explain what these vulnerabilities are, and how "locking up" the sketch is done, and also what it is about your sketch that avoids such problems?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 22, 2013, 11:14 pm
Hackers are a bit hesitant to show vulnerabilities in code, but I am a good hacker, not a bad one. I try to prevent a hack. This code will lock up every server example I have seen, including zoomkat's. Follow the serial monitor prompts. Do not close anything until you remove the CAT5 from your Arduino. Change the network settings to suit your localnet.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(192,168,1,254);
EthernetClient client;

void setup() {
  Serial.begin(9600);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready. Enter l to lock");
}

void loop() {
  if(Serial.available()) {

    if(Serial.read() == 'l')     
    {
      if(!getPage(server)) Serial.print("Fail ");
      else Serial.print("Pass ");
    }
  }   
}

byte getPage(IPAddress ipBuf)
{
  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.print("connected");

    // send request with no \r\n
    client.print("GET / HTTP/1.1");
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

  Serial.println(" & locked. Remove CAT5 from the shield.");
 
  return 1;
}

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 22, 2013, 11:22 pm

Hello all Arduino users.

I have found an excellent resource for making a web server using the Arduino Ethernet shield:
http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/ (http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/)


When that tutorial gets to the point of doing something useful in part 5,
http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/

I believe the following lines have an error, he left out the self-closing tags, or else </input>, on the <input> elements.
Code: [Select]
   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");
   }

The code will work ok for a single input-control, but hoses for multiple controls. This problem burned me for a couple of days, until I learned what the heck " />" was.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 22, 2013, 11:44 pm

Hackers are a bit hesitant to show vulnerabilities in code, but I am a good hacker, not a bad one. I try to prevent a hack. This code will lock up every server example I have seen, including zoomkat's. Follow the serial monitor prompts. Do not close anything until you remove the CAT5 from your Arduino. Change the network settings to suit your localnet.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(192,168,1,254);
EthernetClient client;

void setup() {
  Serial.begin(9600);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready. Enter l to lock");
}

void loop() {
  if(Serial.available()) {

    if(Serial.read() == 'l')     
    {
      if(!getPage(server)) Serial.print("Fail ");
      else Serial.print("Pass ");
    }
  }   
}

byte getPage(IPAddress ipBuf)
{
  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.print("connected");

    // send request with no \r\n
    client.print("GET / HTTP/1.1");
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

  Serial.println(" & locked. Remove CAT5 from the shield.");
 
  return 1;
}


Thanks. I got one serial monitor prompt and nothing at all after that. And nothing showed up at either URL on my PC [at 192.168.1.177 (my 'static ip') or 192.168.1.254].

I guess I'm too naive and confused to even understand what you're trying to do here. It seems to me you've established router connection at one IPaddress, but are trying to access the server on another,
Code: [Select]
IPAddress ip(192,168,2,2);

IPAddress server(192,168,1,254);


Hopelessly lost, so probably best to forget my question, :-(.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 22, 2013, 11:50 pm
You asked the question. I answered it. I can lock up any server example except mine. Change the server variable to the target ip you want to lock up. Mine was 192.168.1.254. With my code, it doesn't lock up. With any other example...YOU ARE DOOMED!!

edit: The reason is most Arduino server examples wait for a "\r\n" to determine End Of Header. My crash code sends a request with NO "\r\n". All other server code will lock up unless it receives a '\n'. I don't send one with the crash code. All other examples will wait FOREVER for a '\n'.


Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: zoomkat on Aug 23, 2013, 02:29 am
Just curious, what hang protection does Apache use?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 23, 2013, 05:21 am

Just curious, what hang protection does Apache use?

About the same as mine.
http://playground.arduino.cc/Code/WebServerST

Both IIS and Apache will brush off that lock code without a problem. Only examples here on the Arduino site have problems with that code. Most (except yours zoomkat) can be locked with PuTTY about the same way. Send the GET line and don't send the blank line, then break the connection.

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 23, 2013, 06:52 am

Thanks. I got one serial monitor prompt and nothing at all after that. And nothing showed up at either URL on my PC [at 192.168.1.177 (my 'static ip') or 192.168.1.254].

I guess I'm too naive and confused to even understand what you're trying to do here. It seems to me you've established router connection at one IPaddress, but are trying to access the server on another,
Code: [Select]
IPAddress ip(192,168,2,2);

IPAddress server(192,168,1,254);


Hopelessly lost, so probably best to forget my question, :-(.

The "IPAddress ip" is the ip of my Arduino that will do the lock attack. The "IPAddress server" is the ip of the victim Arduino running the server code. Mine are on different subnets.

When you start the lock sketch, you should get "Ready. Enter l to lock" on the serial monitor. Press 'l' and enter. That is a lower case L. Then disconnect the CAT5 cable from the attacking Arduino when prompted.

If you do not have two Arduinos, you can use PuTTY in your computer as the attacker. Enter the ip of the victim Arduino in the "Connect To:" box and port 80 in the "Port" box and select "Raw" as the connection type. Then click "Open" at the bottom. You should get a prompt with no response. Type "GET / HTTP/1.1" and press the enter key only once, then pull the CAT5 out of the computer's network card or out of the Arduino. Either will break the connection. It will lock at that point and will not respond until you reboot it.

PuTTY does not lock up some of zoomkat's server code. PuTTY sends a newline character when you press the enter key and zoomkat's code waits only for the first newline, not the blank line.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 23, 2013, 08:49 am
Quote
The "IPAddress ip" is the ip of my Arduino that will do the lock attack. The "IPAddress server" is the ip of the victim Arduino running the server code. Mine are on different subnets.

Well, that explains a lot, LOL - at this point, I'm happy to have anything working at all.

Ok, I added your timeout counter code from the WebServerST example to my AMI-1284 Server from the other thread. However, PUTTY won't crash anything for me.

With PUTTY, I followed your setup procedure. Then, no matter what I send, <cr>, GET, or whatever, my server dumps the web page correctly, PUTTY receives it correctly, and then a window pops up and says "Connection closed by remote host". The page is correct, since I can capture it, stick it in a file with .htm extension using Notepad, and it displays perfectly on the browser.
Quote
GET / HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html

<html><title>AMI-1284 Server4</title><body>
<TABLE BORDER=0 BGCOLOR="#DDDDDD" cellpadding=6><TR><TD><TABLE BORDER=1 cellpadding=4><TR><TD><B>AMI-1284 Server</B><BR></TD><TD><FORM action="http://192.168.1.177" ><INPUT type="submit" value="Refresh" /> </FORM></TD></TR></TABLE><TABLE BORDER=0 cellpadding=4><TR><TD><TABLE BORDER=1 bgcolor="#44EE44" width=72 cellspacing=0 cellpadding=4><TR><TD align=center><FORM action="http://192.168.1.177/" ><INPUT type="hidden" name="cmd" value="11" /><INPUT type="submit" value=" OFF " /> </FORM><font color="black"><B>LED1<br>(Red)</B></font></TD></TR></TABLE></TD><TD><TABLE BORDER=1 bgcolor="#EE4444" width=72 cellspacing=0 cellpadding=4><TR><TD align=center><FORM action="http://192.168.1.177/" ><INPUT type="hidden" name="cmd" value="12" /><INPUT type="submit" value=" ON  " /> </FORM><font color="black"><B>LED2<br>(Yellow)</B></font></TD></TR></TABLE></TD><TD><TABLE BORDER=1 bgcolor="#EE4444" width=72 cellspacing=0 cellpadding=4><TR><TD align=center><FORM action="http://192.168.1.177/" ><INPUT type="hidden" name="cmd" value="14" /><INPUT type="submit" value=" ON  " /> </FORM><font color="black"><B>LED3<br>(Green)</B></font></TD></TR></TABLE></TD><TD><TABLE BORDER=1 bgcolor="#66BBFF" width=72 cellspacing=0 cellpadding=4><TR><TD align=center><FORM action="http://192.168.1.177/" ><INPUT type="hidden" name="cmd" value="16" /><INPUT type="submit" value="TRIG" /> </FORM><font color="black"><B>Beep<br>Piezo</B></font></TD></TR></TABLE></TD><TD><TABLE BORDER=1 bgcolor="#44EE44" width=72 cellspacing=0 cellpadding=4><TR><TD align=center><FORM action="http://192.168.1.177/" ><INPUT type="hidden" name="cmd" value="19" /><INPUT type="submit" value=" OFF " /> </FORM><font color="black"><B>Night<br>Light</B></font></TD></TR></TABLE></TD><TD><TABLE BORDER=1 bgcolor="#44EE44" width=72 cellspacing=0 cellpadding=4><TR><TD align=center><FORM action="http://192.168.1.177/" ><INPUT type="hidden" name="cmd" value="21" /><INPUT type="submit" value=" OFF " /> </FORM><font color="black"><B>Fan<br>Ceiling</B></font></TD></TR></TABLE></TD><TD></TD></TR></TABLE><BR>
Vin = 7.61V
<BR>
<table border=1 bgcolor="#AAFFDD" cellspacing=0 cellpadding=4><tr><td rowspan=2 align=center><B>Digital<br>Pins</B><BR></td><td align=center>D31..D24</td><td align=center>D23..D16</td><td align=center>D15..D8</td><td align=center>D7..D0</td></tr><tr><td><font size=+1 color="#AA0000"><B>0</B></font>0001101</td><td>aaa00000</td><td>00001110</td><td>00110011</td></tr></table>
<BR>
<table border=1 bgcolor="#AAFFDD" cellspacing=0 cellpadding=4><tr><td rowspan=2 align=center><B>Analog<br>Pins</B></td><td align=center>A5 (D21)</td><td align=center>A6 (D22)</td><td align=center>A7 (D23)</td></tr><tr><td align=right>293</td>
<td align=right>0</td>
<td align=right>1023</td>
</table>
</TD></TR></TABLE></body></html>


This all happens instantly, so there is no hang at the server, no 10-sec timeout in the server-client loop, and no time to worry about pulling the CAT5 plug. I looked in all the configuration screens of PUTTY, but couldn't see anything obvious to change. "Implicit LF in every CR", and the converse, are not checked.

I'm using my adaptation of zoomkat's logical scheme from the other thread [ie wait for '\n' to dump the page], rather than your WebServerST logical scheme [ie, basically same as original WebServer, except with the timeout added]. The timeout code is in my sketch, in any case.

Thanks, between you and z-k, I seem to be making some progress, :-). I think I can live with what I have.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 23, 2013, 09:03 am
If you use my timeout code with your Arduino server, you can't crash it with the lockup script or PuTTY. PuTTY won't crash zoomkat's code either. Which code are you using?

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 23, 2013, 09:19 am
I'm using z-k's version with your timeout. I'll experiment some more tomorrow with WebServer style code and no timeout, and z-k's with no timeout. Computer is off, heading for bed now.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 23, 2013, 09:19 am
This is the part that causes the lockup in most examples. This requires two newlines in a row. You can crash this with PuTTY.
Code: [Select]
if (c == '\n' && currentLineIsBlank) {

This is what zoomkat uses. PuTTY sends that newline immediately with the request. I could not find a way to stop PuTTY from sending the newline with the GET, so I couldn't crash this with PuTTY.
Code: [Select]
if (c == '\n') {

edit: You must use the Arduino sketch code above to crash zoomkat's code. It never sends a newline with the GET. Here is the send with no newline from that code. Note it uses client.print() instead of client.println().
Code: [Select]
 if(client.connect(ipBuf,80))
 {
   Serial.print("connected");

   // send request with no \r\n
   client.print("GET / HTTP/1.1");
 }

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 23, 2013, 09:23 am
Yeah, that's the difference. Tomorrow.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 23, 2013, 02:17 pm
The only disadvantage to zoomkat's code is it does not read the entire request header, only the first line with the GET or POST. Since it doesn't read the rest of the header or the body of the request, you can't use a POST request with zoomkat's code.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 24, 2013, 12:00 am
Tim, I did some more tests today using your PUTTY scheme.

To confirm your results, for the WebServerST sketch, the server will hang if the timeout counter is disabled, but will timeout and recover if it's enabled. So, that's very good.

Also, as you know, zoomkat's scheme will pass the PUTTY test, as will my modification of it. But I'll leave the timeout code in there, in any case.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 24, 2013, 01:13 am
The only reservation I have about your/zoomkat's code is it does leave characters in the w5100 socket rx buffer. I have lost many sockets doing that. My server code empties the rx buffer (including any POST data in the request body) before sending a response. That pretty much insures a clean close without losing a socket. But that just me...  :D
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 24, 2013, 01:29 am
Thanks, knowledge is king. Now to learn about sockets, :-).
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: zoomkat on Aug 24, 2013, 04:28 am
The below meta refresh test code is for checking the turnaround time for a client/server interaction. The server code doesn't seem prone to hanging up on its own from what I can tell. The code has a counter to count the interactions.


Code: [Select]

// zoomkat's meta refresh data frame test page 8/17/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical 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
unsigned long int x=0; //set refresh counter to 0
String readString;

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

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 8/17/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c;
         }
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          //client.println("HTTP/1.1 200 OK");
          //client.println("Content-Type: text/html");
          //client.println();
         
          client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>");
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>");
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY><br>");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("<br><br>");
           
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
           
            client.print("<br>analog input1 is: ");
            client.print(analogRead(analogInPin1));
                       
            client.print("<br>analog input2 is: ");
            client.print(analogRead(analogInPin2));
           
            client.print("<br>analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                   
            client.print("<br>analog input4 is: ");
            client.print(analogRead(analogInPin4));
           
            client.print("<br>analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.print("<br></BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print(F("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>"
            "Zoomkat's Arduino frame meta refresh test 8/17/13"
            "<BR><BR>Arduino analog input data frame:<BR>"
            "&nbsp;&nbsp;<a href='/datastart' target='DataBox' title=''yy''>META-REFRESH</a>"
            "&nbsp;&nbsp;&nbsp;&nbsp;<a href='/data' target='DataBox' title=''xx''>SINGLE-STOP</a>"
            "&nbsp;&nbsp;&nbsp;&nbsp;<a href='/datafast' target='DataBox' title=''zz''>FAST-DATA</a><BR>"
            "<iframe src='/data' width='350' height='250' name='DataBox'>"
            "</iframe><BR></HTML>"));
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Aug 24, 2013, 11:03 am
I wrote a socket test sketch a few months ago. It is a web server, plus does a dns request for Google and ntp request every 30 seconds. It shows the socket status at different points during each. It shows for each socket
Socket#<number>:<status byte> <local port> D:<remote ip>(remote port)

The common status byte codes are
0x00 = available (not used)
0x14 = server waiting for client
0x17 = server with client
0x22 = udp

Insure network settings are correct, including a valid dns server.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <utility/w5100.h>
#include <Dns.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);
IPAddress gateway(192,168,2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dnServer(192,168,2,1);

IPAddress timeServer;
EthernetServer server(80);
EthernetUDP Udp;
DNSClient dnsC;

long timer = 0L;
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

void setup() {
 Serial.begin(9600);

 pinMode(4, OUTPUT);
 digitalWrite(4, HIGH);
       
 Ethernet.begin(mac, ip, dnServer, gateway, subnet);

 Serial.println("Start");
 server.begin();
 ShowSockStatus();
 timer = millis();
 dnsC.begin(dnServer);

 if(dnsC.getHostByName("pool.ntp.org",timeServer)) {
   Serial.print("\r\nNTP address = ");
   Serial.println(timeServer);            
 }  
 else Serial.println("dns fail");
 ShowSockStatus();
}

void loop()
{
 IPAddress remoteAddr;
 //Check if a web client has attached.
 checkServer();
       
 if ((millis() - timer) > 30000) {
   if(dnsC.getHostByName("www.google.com",remoteAddr)) {
     Serial.print(F("\r\nIP address = "));
     Serial.println(remoteAddr);            
   }  
   else Serial.println(F("dns fail"));
         
   ShowSockStatus();
   delay(10);          
   timer = millis();
   checkTime();
 }
}

void ShowSockStatus()
{
 for (int i = 0; i < MAX_SOCK_NUM; i++) {
   Serial.print(F("Socket#"));
   Serial.print(i);
   uint8_t s = W5100.readSnSR(i);
   Serial.print(F(":0x"));
   Serial.print(s,16);
   Serial.print(F(" "));
   Serial.print(W5100.readSnPORT(i));
   Serial.print(F(" D:"));
   uint8_t dip[4];
   W5100.readSnDIPR(i, dip);
   for (int j=0; j<4; j++) {
     Serial.print(dip[j],10);
     if (j<3) Serial.print(".");
   }
   Serial.print(F("("));
   Serial.print(W5100.readSnDPORT(i));
   Serial.println(F(")"));
 }
}

unsigned long sendNTPpacket(EthernetUDP & Udp, IPAddress& address)
{
 memset(packetBuffer, 0, NTP_PACKET_SIZE);
 packetBuffer[0] = 0b11100011;   // LI, Version, Mode
 packetBuffer[1] = 0;     // Stratum, or type of clock
 packetBuffer[2] = 6;     // Polling Interval
 packetBuffer[3] = 0xEC;  // Peer Clock Precision
 packetBuffer[12]  = 49;
 packetBuffer[13]  = 0x4E;
 packetBuffer[14]  = 49;
 packetBuffer[15]  = 52;
 Udp.beginPacket(address, 123); //NTP requests are to port 123
 Udp.write(packetBuffer,NTP_PACKET_SIZE);
 Udp.endPacket();
}

void checkTime()
{
 Udp.begin(8888);
 sendNTPpacket(Udp, timeServer); // send an NTP packet to a time server
 Serial.println("\r\nTime check");
 ShowSockStatus();
 // wait to see if a reply is available
 delay(1000);  

 if ( Udp.parsePacket() ) {  
   // We've received a packet, read the data from it
   Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

   //the timestamp starts at byte 40 of the received packet and is four bytes,
   // or two words, long. First, esxtract the two words:

   unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
   unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
   // combine the four bytes (two words) into a long integer
   // this is NTP time (seconds since Jan 1 1900):
   unsigned long secsSince1900 = highWord << 16 | lowWord;  

   // now convert NTP time into everyday time:
   Serial.print("Unix time = ");
   // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
   const unsigned long seventyYears = 2208988800UL;    
   // subtract seventy years:
   unsigned long epoch = secsSince1900 - seventyYears;  
   // print Unix time:
   Serial.println(epoch);  
 }  
 else Serial.println("No NTP packet received");

 Udp.stop();
 ShowSockStatus();
}

void checkServer()
{
 EthernetClient client = server.available();
 if(client) {
   boolean currentLineIsBlank = true;
   boolean currentLineIsGet = true;
   int tCount = 0;
   char tBuf[64];
   int r,t;
   char *pch;

   Serial.println(F("\r\nServer client"));
   ShowSockStatus();

   Serial.print(F("\r\nClient request: "));

   // this controls the timeout
   int loopCount = 0;

   while (client.connected()) {
     while(client.available()) {
       // if packet, reset loopCount
       loopCount = 0;
       char c = client.read();
       if(currentLineIsGet && tCount < 63)
       {
         tBuf[tCount] = c;
         tCount++;
         tBuf[tCount] = 0;          
       }

       if (c == '\n' && currentLineIsBlank) {
         // send a standard http response
         Serial.println(tBuf);
         Serial.print(F("POST data: "));
         while(client.available()) Serial.write(client.read());
         Serial.println();

         pch = strtok(tBuf,"?");

         while(pch != NULL)
         {
           if(strncmp(pch,"t=",2) == 0)
           {
             t = atoi(pch+2);
             Serial.print(F("t="));
             Serial.println(t,DEC);            
           }

           if(strncmp(pch,"r=",2) == 0)
           {
             r = atoi(pch+2);
             Serial.print(F("r="));              
             Serial.println(r,DEC);
           }


           pch = strtok(NULL,"& ");
         }
         Serial.println(F("Sending response"));
         client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));
         client.println(F("<body><H1>TEST</H1>"));
         client.println(F("<form method=GET>T: <input type=text name=t><br>"));
         client.println(F("R: <input type=text name=r><br><input type=submit></form>"));


         client.println(F("</body></html>"));
         client.stop();
       }
       else if (c == '\n') {
         currentLineIsBlank = true;
         currentLineIsGet = false;
       }
       else if (c != '\r') {
         currentLineIsBlank = false;
       }
     }

     loopCount++;

     // if 10000ms has passed since last packet
     if(loopCount > 10000) {
       // close connection
       client.stop();
       Serial.println(F("\r\nTimeout"));
     }

     // delay 1ms for timeout timing
     delay(1);
   }
   Serial.println(F("done"));
 }
}

edit: The timeServer ip will be assigned by dns from pool.ntp.org.
The available (0x00) and server waiting (0x14) sockets will show the remote ip and ports settings from the previous use of that socket.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Aug 28, 2013, 10:35 pm
Hi,

I use in my house an Arduino UNO with Ethernet Shield connected to the router that controls the lights of the house through the computer and smartphone by socket commands.

I am now trying to create a mini Webserver by Arduino to control the lights by Smart TV also through the browser, and I'm following the instructions in Part 5 of the tutorial.

I adapted the code from the tutorial with the code that I already used, but I have questions in some parts:

Code: [Select]
void loop() {

EthernetClient client = server.available();

// SE receber um caracter...

delay(50);
if (client) {

boolean currentLineIsBlank = true;

// guarda o caracter na string 'msg'
msg[0]=msg[1];
msg[1]=msg[2];
msg[2]=msg[3];
msg[3]=msg[4];
msg[4]= client.read();


char c = msg[4]; // Lê 1 byte (caractere) do cliente
HTTP_req += c;  // Salva o pedido HTTP 1 caractere de cada vez


if (msg[4]=='#')
{
   
   LOOKS AND EXECUTE SOCKET COMMANDS
   
} // Fim IF msg[4]
else
{
while (client.connected())
{
   if (client.available())
   {
if (c == '\n' && currentLineIsBlank)
{
// Envia um cabeçalho de resposta padrão HTTP
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// Envia a página
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino - Luzes da Casa</title>");
client.println("</head>");
client.println("<body>");
client.println("<center>");
client.println("<h1>Luzes da Casa</h1>");
client.println("<p>Clique para Acender ou Apagar a luz.</p>");
client.println("<form method=\"get\">");
ProcessButton(client);
client.println("</form>");
client.println("</center>");
client.println("</body>");
client.println("</html>");
Serial.print(HTTP_req);
HTTP_req = "";    // Termina o pedido, limpa a String
break;
}

if (c == '\n')
{
   currentLineIsBlank = true;
}
else if (c != '\r')
{
   currentLineIsBlank = false;
}
   } // Fim (client.available())
} // Fim while (client.connected())
delay(1);      // Dar tempo ao navegador para receber os dados
client.stop(); // Fecha a conexão
} // Fim ELSE
} // Fim client
} // Fim Loop

// Mudar LUZ e enviar de volta para a caixa HTML
void ProcessButton(EthernetClient cl)
{
if (HTTP_req.indexOf("luz=Quarto") > 0)
{
  if (digitalRead(4) == LOW)
  {
  digitalWrite(4, HIGH);
  }
  else
  {
  digitalWrite(4, LOW);
  }
}

if (HTTP_req.indexOf("luz=Sala") > 0)
{
  if (digitalRead(5) == LOW)
  {
  digitalWrite(5, HIGH);
  }
  else
  {
  digitalWrite(5, LOW);
  }
}

cl.println F("<input type=\"submit\" value=\"Quarto\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\">");
cl.println F("<input type=\"submit\" value=\"Sala\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\">");
}


When making a socket connection, the Arduino would not be stuck in "While" blocking of analyzing the messages to be sent? And if I delete the "While", the Arduino would not be resubmitting the page every time it goes by Loop?

And in "client.stop ();", when making a socket connection, it would also be closed?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 28, 2013, 10:58 pm
If you mean Part 5 of this tutorial, you will notice [as I mentioned a few posts ago], his <input> elements are wrong. He needs either a </input> or " />" to close them, and make them correct [at least I think so]. If you do what he does with multiple controls, and not just one, then they interact and the control logic gets mucked up.

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Aug 28, 2013, 11:17 pm

If you mean Part 5 of this tutorial, you will notice [as I mentioned a few posts ago], his <input> elements are wrong. He needs either a </input> or " />" to close them, and make them correct [at least I think so]. If you do what he does with multiple controls, and not just one, then they interact and the control logic gets mucked up.

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/


Now I put the " />".

But when making a socket connection, the Arduino would not be stuck in "While" blocking of analyzing the messages to be sent? And if I delete the "While", the Arduino would not be resubmitting the page every time it goes by Loop?

And in "client.stop ();", when making a socket connection, it would also be closed?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Aug 28, 2013, 11:28 pm
Tim will have to answer those questions. I'm just learning this stuff, like the " />", :-).
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 01, 2013, 08:21 pm
I changed the code, I wonder if will work the Webserver and Socket connection:

Code: [Select]
...

void loop() {
     
      EthernetClient client = server.available();
   
     // SE receber um caracter...
       
      delay(50);
      if (client) {
       
        boolean currentLineIsBlank = true;
        while (client.connected())
         {
           if (client.available())
           {
              // guarda o caracter na string 'msg'
              msg[0]=msg[1];
              msg[1]=msg[2];
              msg[2]=msg[3];
              msg[3]=msg[4];
              msg[4]= client.read();
             
             
              char c = client.read(); // Lê 1 byte (caractere) do cliente
              HTTP_req += c;  // Salva o pedido HTTP 1 caractere de cada vez
             
     
             if (msg[4]=='#')
             {

                LOOKS AND EXECUTE SOCKET COMMANDS
       
             } // Fim IF msg[4]
             else
             {
               if (c == '\n' && currentLineIsBlank)
               {
                  // Envia um cabeçalho de resposta padrão HTTP
                  client.println("HTTP/1.1 200 OK");
                  client.println("Content-Type: text/html");
                  client.println("Connection: close");
                  client.println();
                  // Envia a página
                  client.println("<!DOCTYPE html>");
                  client.println("<html>");
                  client.println("<head>");
                  client.println("<title>Arduino - Luzes da Casa</title>");
                  client.println("</head>");
                  client.println("<body>");
                  client.println("<center>");
                  client.println("<h1>Luzes da Casa</h1>");
                  client.println("<p>Clique para Acender ou Apagar a luz.</p>");
                  client.println("<form method=\"get\">");
                  ProcessButton(client);
                  client.println("</form>");
                  client.println("</center>");
                  client.println("</body>");
                  client.println("</html>");
                  Serial.print(HTTP_req);
                  HTTP_req = "";    // Termina o pedido, limpa a String
                  break;
               }
               
               if (c == '\n')
               {
                 currentLineIsBlank = true;
               }
               else if (c != '\r')
               {
                 currentLineIsBlank = false;
               }
             } // Fim ELSE
           } // Fim (client.available())
        } // Fim while (client.connected())
       
        delay(1);      // Dar tempo ao navegador para receber os dados
        client.stop(); // Fecha a conexão
     } // Fim client
   } // Fim Loop
   
  // Mudar LUZ e enviar de volta para a caixa HTML
  void ProcessButton(EthernetClient cl)
  {
      if (HTTP_req.indexOf("luz=Quarto") > 0)
      {
          if (digitalRead(4) == LOW)
          {
              digitalWrite(4, HIGH);
          }
          else
          {
              digitalWrite(4, LOW);
          }
      }
     
      if (HTTP_req.indexOf("luz=Sala") > 0)
      {
          if (digitalRead(5) == LOW)
          {
              digitalWrite(5, HIGH);
          }
          else
          {
              digitalWrite(5, LOW);
          }
      }
     
      cl.println F("<input type=\"submit\" value=\"Quarto\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\" />");
      cl.println F("<input type=\"submit\" value=\"Sala\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\" />");
  }

...
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 01, 2013, 09:14 pm
Quote
I changed the code, I wonder if will work the Webserver and Socket connection:

You changed it so it will fail.

Did you try this code?
http://playground.arduino.cc/Code/WebServerST

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 01, 2013, 10:08 pm
How many characters will this String store before you run out of SRAM?
Code: [Select]
              HTTP_req += c;  // Salva o pedido HTTP 1 caractere de cada vez
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 01, 2013, 10:18 pm

How many characters will this String store before you run out of SRAM?
Code: [Select]
              HTTP_req += c;  // Salva o pedido HTTP 1 caractere de cada vez



This part of the code is equal to the Tutorial:
http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/

The string "HTTP_req" will not copy the answer from "c" to examine the response?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 01, 2013, 10:23 pm
Quote
The string "HTTP_req" will not copy the answer from "c" to examine the response?

Yes, until the Arduino runs out of SRAM, at which time the SRAM will overflow (wrap around) and crash. Actually, you will probably have a stack collision before it wraps around.

My point was that is not really an excellent web server tutorial. There are some good example sections in that code, but if it doesn't work for you, it isn't excellent, is it?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 01, 2013, 10:31 pm

Quote
The string "HTTP_req" will not copy the answer from "c" to examine the response?

Yes, until the Arduino runs out of SRAM, at which time the SRAM will overflow (wrap around) and crash. Actually, you will probably have a stack collision before it wraps around.

My point was that is not really an excellent web server tutorial. There are some good example sections in that code, but if it doesn't work for you, it isn't excellent, is it?



What tutorial do you recommend?

Currently I use the Arduino with socket commands through of the smartphone and laptop, but I want also put a Webserver to control by browser of the Smart TV.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 01, 2013, 10:38 pm
They are all socket commands in a way. I posted my server code earlier. And zoomkat posted his code also. You can also get some good examples from that tutorial once you get the client request without crashing your code.

Those are the basics. Then you must modify those examples, or pay someone like zoomkat or me to complete your code. Besides being a network routing specialist, that is part of my job.  :)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 01, 2013, 11:05 pm

They are all socket commands in a way. I posted my server code earlier. And zoomkat posted his code also. You can also get some good examples from that tutorial once you get the client request without crashing your code.

Those are the basics. Then you must modify those examples, or pay someone like zoomkat or me to complete your code. Besides being a network routing specialist, that is part of my job.  :)



I'm looking this example you showed:
http://playground.arduino.cc/Code/WebServerST

But I confess that I do not understanding what part I can change so that the page stay more or less this:
Code: [Select]
<!DOCTYPE html>
<html>
<head>
<title>Arduino - Luzes da Casa</title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" bgcolor="#CCCCCC">
<center>
<div style="background-color:#333333; color:#FFFFFF; font-size: 39px; font-weight: bold">Luzes da Casa</div>
<br>
<div style="font-size: 21px; font-weight: bold">Clique para Acender ou Apagar a luz.</div>
<br>
<form method="get">
<input type="submit" value="Quarto" onclick="submit();" name="luz" style="height:300px; width:300px; font-size:50px" />
<input type="submit" value="Sala" onclick="submit();" name="luz" style="height:300px; width:300px; font-size:50px" />
</form>
</center>
</body>
</html>


The buttons will turn on or off of the light of the room and the bedroom:
Code: [Select]
if (digitalRead(4) == LOW)
          {
              digitalWrite(4, HIGH);
          }
          else
          {
              digitalWrite(4, LOW);
          }
     
      if (digitalRead(5) == LOW)
          {
              digitalWrite(5, HIGH);
          }
          else
          {
              digitalWrite(5, LOW);
          }
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 01, 2013, 11:10 pm
You need to replace this with your web page.
Code: [Select]
          client.println(F("<head><script type=\"text/javascript\">"));
          client.println(F("function show_alert() {alert(\"This is an alert\");}"));
          client.println(F("</script></head>"));
          client.println(F("<body><H1>TEST</H1>"));
          client.println(F("<form method=GET onSubmit=\"show_alert()\">T: <input type=text name=t><br>"));
          client.println(F("R: <input type=text name=r><br><input type=submit></form>"));
          client.println(F("</body></html>"));
 
Then search the request for your variables when it is submitted like mine does for "r" and "t".
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: pico on Sep 02, 2013, 05:25 am

If you mean Part 5 of this tutorial, you will notice [as I mentioned a few posts ago], his <input> elements are wrong. He needs either a </input> or " />" to close them, and make them correct [at least I think so].

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/


Actually, Dan, the code is perfectly valid HTML -- <input ...> is one of those tags that do not require a closing tag, usually called "self-closing tags" or sometimes "unpaired tags" or even "void elements" (<img ...> being another common one).

Have a google!
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 02, 2013, 03:38 pm
Then search the request for your variables when it is submitted like mine does for "r" and "t".


My question is exactly this, because I do not understanding how can I do to become more or less this:
Code: [Select]
void ProcessButton(EthernetClient cl)
{
if (HTTP_req.indexOf("luz=Quarto") > 0)
{
if (digitalRead(4) == LOW)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
}
}

if (HTTP_req.indexOf("luz=Sala") > 0)
{
if (digitalRead(5) == LOW)
{
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
}

cl.println F("<input type=\"submit\" value=\"Quarto\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\">");
cl.println F("<input type=\"submit\" value=\"Sala\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\">");
}
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 02, 2013, 03:45 pm
Is "HTTP_req" a String type? If so, I can't help you. zoomkat says that data type works ok, but I have not found that to be correct. Just my experience.

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 02, 2013, 05:40 pm

Is "HTTP_req" a String type? If so, I can't help you. zoomkat says that data type works ok, but I have not found that to be correct. Just my experience.


"HTTP_req" is a String, but no need to use it, I just need to understand how do I read the GET requests in the code you indicated.

The request "luz=Quarto" should do this:
Code: [Select]
if (digitalRead(4) == LOW)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
}


And the request "luz=Sala" should do this:
Code: [Select]
if (digitalRead(5) == LOW)
{
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: oric_dan on Sep 02, 2013, 05:58 pm


If you mean Part 5 of this tutorial, you will notice [as I mentioned a few posts ago], his <input> elements are wrong. He needs either a </input> or " />" to close them, and make them correct [at least I think so].

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/


Actually, Dan, the code is perfectly valid HTML -- <input ...> is one of those tags that do not require a closing tag, usually called "self-closing tags" or sometimes "unpaired tags" or even "void elements" (<img ...> being another common one).

Have a google!

Sheesh, too many darn rules and exceptions. I am pretty sure that adding " />" is what fixed my problem with there being interactions between buttons when I had multiple buttons, but maybe it was something else again. Will have to test it some more later - am out of town now.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: zoomkat on Sep 02, 2013, 06:19 pm
Simple server test code for turning an arduino pin high/low via a web page.

Code: [Select]

//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//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 }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // 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(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // 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");
               client.println();
               client.println(); 
             }
             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("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          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("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: neo3 on Sep 04, 2013, 05:09 pm

Simple server test code for turning an arduino pin high/low via a web page.

Code: [Select]

//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//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 }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // 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(5, OUTPUT); //pin selected to control
 //start Ethernet
 Ethernet.begin(mac, ip, gateway, subnet);
 server.begin();

 //enable serial data print
 Serial.begin(9600);
 Serial.println("server LED test 1.0"); // 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");
              client.println();
              client.println();  
            }
            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("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
         client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

         //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
         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("on") >0)//checks for on
         {
           digitalWrite(5, HIGH);    // set pin 4 high
           Serial.println("Led On");
         }
         if(readString.indexOf("off") >0)//checks for off
         {
           digitalWrite(5, LOW);    // set pin 4 low
           Serial.println("Led Off");
         }
         //clearing string for next read
         readString="";

       }
     }
   }
 }
}




I adapted your code with the code that I already use of sockets commands, but want to know if will still work the commands that use the socket connection made by my applications:
Code: [Select]
...

void loop() {
     
     EthernetClient client = server.available();
   
    // SE receber um caracter...
       
     delay(50);
     if (client) {
       while (client.connected()) {
         if (client.available()) {
           
           char c = client.read();
       
           // guarda o caracter na string 'msg'
           msg[0]=msg[1];
           msg[1]=msg[2];
           msg[2]=msg[3];
           msg[3]=msg[4];
           msg[4]= c;
           
           if (msg[4]=='#') {
             Serial.println(msg);

             LOOKS AND EXECUTE SOCKET COMMANDS
          } // Fim IF msg[4]
          else {
           //ler caractere por caractere do pedido HTTP
           if (readString.length() < 100) {
   
             //armazena os caracteres na string
             readString += c;
             //Serial.print(c);
           }
           
           //if pedido HTTP acabou
           if (c == '\n') {
   
             ///////////////
             Serial.println(readString); //imprimir no serial para debuging
   
             //saída de cabeçalho dos dados HTML
             if(readString.indexOf('?') >=0) { //não envia a página
                client.println(F("HTTP/1.1 204 OK"));
                client.println();
                client.println();  
             }
             else {
               client.println(F("HTTP/1.1 200 OK")); //envia a página
               client.println(F("Content-Type: text/html"));
               client.println();
     
               client.println(F("<html>"));
               client.println(F("<head>"));
               client.println(F("<title>Arduino - Luzes da Casa</title>"));
               client.println(F("</head>"));
               client.println(F("<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" bgcolor=\"#CCCCCC\">"));
               client.println(F("<center>"));
               client.println(F("<div style=\"background-color:#333333; color:#FFFFFF; font-size: 39px; font-weight: bold\">Luzes da Casa</div>"));
               client.println(F("<br>"));
               client.println(F("<div style=\"font-size: 21px; font-weight: bold\">Clique para Acender ou Apagar a luz.</div>"));          
               client.println(F("<br>"));
               client.println(F("<form method=\"get\">"));
               client.println(F("<input type=\"submit\" value=\"Quarto\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\">"));
               client.println(F("<input type=\"submit\" value=\"Sala\" onclick=\"submit();\" name=\"luz\" style=\"height:300px; width:300px; font-size:50px\">"));
               client.println(F("</form>"));
               client.println(F("</center>"));
               client.println(F("</body>"));
               client.println(F("</html>"));
             }
   
             delay(1);
             //parar client
             client.stop();
   
             ///////////////////// controlar iluminação
             if(readString.indexOf("luz=Quarto") >0)//checar Quarto
             {
               if (digitalRead(4) == LOW)
               {
                 digitalWrite(4, HIGH);
               }
               else
               {
                 digitalWrite(4, LOW);
               }
               Serial.println("Web Quarto");
             }
             if(readString.indexOf("luz=Sala") >0)//checar Sala
             {
               if (digitalRead(5) == LOW)
               {
                 digitalWrite(5, HIGH);
               }
               else
               {
                 digitalWrite(5, LOW);
               }
               Serial.println("Web Sala");
             }
             //limpar string para a próxima leitura
             readString="";
           }
          } // Fim ELSE
         } // Fim client.available
       } // Fim client.connected
     }// Fim client
  }// Fim Loop

...
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: jonnygreenwood on Sep 04, 2013, 07:22 pm
wow .. good reference for me to learn arduino wider  :D

thank you for sharing

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: arniep on Sep 10, 2013, 04:25 pm
Can anyone give this newbie a suggestion for how to incorporate login and pw verification into a webserver on arduino?
Thanks...arniep
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: rappt on Oct 11, 2013, 07:38 pm
Hi,
Here http://q.gs/4tkFt (http://q.gs/4tkFt) have arduino completed code to control and monitorize my reef aquarium over internet, with arduino and ethernet shield :) :) :P :)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: sakugava on Sep 21, 2014, 08:47 am
I tried the http://playground.arduino.cc/Code/WebServerST with the example http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-gauge/
and it did not work. The gauge only shows 0 (zero)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Sep 21, 2014, 02:58 pm
My code examples in the playground are not designed for a persistent connection like Ajax uses. Did you modify the code to leave the connection open for the specific file request?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: mido_1986 on Sep 29, 2014, 06:29 pm
I have a question about the web server,  actually am interested in controlling my lights via internet. the problem  is that the security cameras device is connected on port 80 and it seems that we cant use the same port for arduino ( am not sure about it but a guy from the internet company told me that)
the question is: can i use other ports like 2500 or 3000 or any other port with the web server ??
thanks  8)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: Hanneman on Oct 20, 2014, 06:16 pm
I love the tutorials on the starting electronics website!

There is one thing I am trying to do, but does not work the way I want it to work.

According to the AJAX tutorials on the startingelectronics website it's possible to create a html button
and turn on a device (Relay, led, buzzer whatever).
This absolutely works.

But what about increasing or decreasing a value of a variable by 1 (only when button is pushed once)?

If you make a button with a link in it, the url will be something like http://192.168.1.177/?Button.
And as long as this url is working and the page is refreshing or kept alive, the links is working.
This is very good when you are turning on a led, relay or something like that.

But when you want to increase a value of a variable by 1 at the push of a button, the value of the variable keeps on going up as the page refreshes or is being "kept alive".

Does anyone know how to do this properly or a good tutorial for this?

Thanks in advance!
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: franco190453 on Oct 23, 2014, 01:38 am
It is an Excellent tutorial!.
However, nothing has been said about the risks involved in setting up a web server
without proper Internet security.
Regards
Franco
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: panpira on Dec 15, 2014, 01:04 pm
Hi (or bette this timer: Ho, Ho, Ho  :D  ) folks.

the tutorial site is really a good work, but the code from SurferTim (greetings and thx a lot 4 ur work) is much better!!!

i tried to make it a little bit clearer for my unprofessional eyes  :smiley-eek:
hope its ok for u Tim if i post it here.

a word to ajax: it is possible to modify Tims code also for ajax use. have tried it out and it works. but i used the original Tims-code for it. if i have make it work for the new one i will post it here for u if anyone think that it helps him (or her).

a question to SurferTim (hope u will read this):
afer the sd file is written there comes a return. in my code i saw, thet it breaks the rotine (so i think) too early. is the return necessary at this place or make i a little mistake?

my code part:
Code: [Select]
while(client.available()) client.read();

          int scanCount = sscanf(tBuf,"%7s %47s %8s",methodBuffer,requestBuffer,protocolBuffer);
         
          if(!checkRequest(&scanCount,client,tBuf,methodBuffer,requestBuffer,paramBuffer,protocolBuffer,fileName,fileType))return;
          if(!checkFile(client, fileName, fileType, tBuf,requestBuffer,methodBuffer)) return;
          else {
            openFile(client, fileName, fileType, tBuf,methodBuffer);
//fragliches return?????????????????????????????????????????????????????????????????????
            return;
          }
          serialPrintTundR(paramBuffer);
         


ur code part:
Code: [Select]
myFile.close();             
#ifdef ServerDEBUG
                Serial.println(F("closed"));
#endif
                client.stop();               
#ifdef ServerDEBUG
                Serial.println(F("disconnected"));
#endif
                return;//<---------- this return i mean
              }
              else {


this version of modified Tims code is the first one (finished today). there is much more to do to make it shorter and theres a lot of parameters that i give to functions that not necessary.
hope i helps the one or other to come further withhis server-projekt.
it would be nice if others help me to make it better than it is. my next step is to implement the ajax thing.

PS: please excused my broken english. am not used to comunikate in this language  :)

special thanks and greetings to SurferTim and for all of u a nice and happy chrismas.

last but not least my code from today (edit: in the atachment of the next post): (hope it would be with a slider on the side and no worm over several sites. its my first time that i post here)

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: panpira on Dec 15, 2014, 01:18 pm
posting 4 dummies :)
overseen that i can make an atachment

here it is
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Dec 15, 2014, 01:47 pm
Thanks! Happy holidays to you and yours. :)

Why are you using a return there? My server code doesn't return there. It returns only on a fail or completion.

If you make my code shorter, you must remove something that provides error checking or fault tolerance.
Just saying...
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: panpira on Dec 15, 2014, 03:24 pm
Hi Tim, nice to meet you and thanks 4 the fast reply :)

i dont want to make it shorter, in my 2560mega is enough memory left, thats not my ambition. i only need to seperate it in different funktions because it was too complex for me to understand what happens at what part of the code at which time.
for the future i want to modify it for ajax use and some other stuff. and so was it easyer for me to understand. am not really professional in programming and a newbie whith the arduino.
your code works great :) , no need to make it shorter (nice trick whith the "#ifdef"), on the contrary (i want to make it longer :) ). i only adapt it for me for the wifi shield and so i must take out the sock-things which ar not implementet in the wifi library (i think so / dont found it).

"Why are you using a return there? "
thats the question :) i think it can be removed too.

but the return is still there in ur script and prevent the code to come to the r and t thing (what is the meaning of this part?)
i run your original code (with the wifi mods - no other changes) and the result was the same - no serial printing of r and t. but this part comes after the "if(strcmp(requestBuffer,"/MYTEST.PHP") == 0)... else" part


greetings :)

...
pch = strtok(paramBuffer,"&");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("t=");
              Serial.println(t,DEC);           
#endif
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("r=");             
              Serial.println(r,DEC);
#endif
            }
...
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Dec 15, 2014, 03:42 pm
The only places in my code that have a return is after the sendBadRequest() and sendFileNotFound() calls, and after the successful completion of the send.

Please post the code a couple lines before and after the return in my code that you have a question about. I don't see it.
http://playground.arduino.cc/Code/WebServerST (http://playground.arduino.cc/Code/WebServerST)
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: panpira on Dec 15, 2014, 05:36 pm
sure. the questioned return is in the middle (in ur original code in line 322)
but the return looks correct. have just try to remove it and then the serial log shows this:

Client request #4: GET /seite2.htm HTTP/1.1
file = /SEITE2.HTM
file type = HTM
method = GET
params =
protocol = HTTP/1.1
SD file
filename format ok
SRAM = 6141
file found..opened..send..closed
disconnected
Sending response   <--------------------this-------------

Timeout                   <---------------------is--------------
disconnected          <--------------------new-------------

think the return is correct and i dont really understand it :) (till now)
can u explain me this part (after the return) please in a few words?
i want to understand and not only a functional code. am happy if i can learn something.
a little chrismas present if u want :)

Code: [Select]

                else if(strcmp(fileType,"PNG") == 0) strcat_P(tBuf,PSTR("image/png"));
                else if(strcmp(fileType,"PDF") == 0) strcat_P(tBuf,PSTR("application/pdf"));
                else if(strcmp(fileType,"ZIP") == 0) strcat_P(tBuf,PSTR("application/zip"));
                else strcat_P(tBuf,PSTR("text/plain"));

                strcat_P(tBuf,PSTR("\r\nConnection: close\r\n\r\n"));
                client.write(tBuf);

                if(strcmp(methodBuffer,"GET") == 0)  {
#ifdef ServerDEBUG
                  Serial.print(F("send.."));
#endif

                  while(myFile.available()) {
                    tBuf[clientCount] = myFile.read();
                    clientCount++;
                    tBuf[clientCount] = 0;

                    if(clientCount > 63) {
                      client.write((byte*)tBuf,64);
                      clientCount = 0;
                    }

                  }
                  if(clientCount > 0) {
                    client.write((byte*)tBuf,clientCount);
                  }
                }

                myFile.close();             
#ifdef ServerDEBUG
                Serial.println(F("closed"));
#endif
                client.stop();               
#ifdef ServerDEBUG
                Serial.println(F("disconnected"));
#endif
                return;//<------------------------------------------------------here it is---------------
              }
              else {
#ifdef ServerDEBUG
                Serial.println(F("File not found"));
#endif
                sendFileNotFound(client);
                return;
              }

            }
          }

          pch = strtok(paramBuffer,"&");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("t=");
              Serial.println(t,DEC);           
#endif
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("r=");             
              Serial.println(r,DEC);
#endif
            }


            pch = strtok(NULL,"& ");
          }
#ifdef ServerDEBUG
          Serial.println(F("Sending response"));
#endif


[edit] i had say good bye to my wireless bord and hello to my new ethernetshield. for an application in the garden the house wlan is to weak and the wifi shield to slow. it make more sence to plug the ether with a short cable into a repeater. the wifi shield is overpriced anyway and so i have a superfast wlan and can hold it on the newest standards easily.
Title: SD card read success but.web page saved in SD card not shown
Post by: harith on Jan 06, 2015, 12:33 pm
i used ethernet shield and SD card...saved a web page on SD card and tried to access it..but it won't work..in serial monitor it says..SD card read successful.web page found..but when i trying to access with firefox it is only being busy and then no connection...what can be the reason..help me please..
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Jan 06, 2015, 01:45 pm
@panpira: Sorry I missed your post. The part after the return calls a function that sends an "Error 404 File not found" message.

@harith: What did you name the file, and what is the contents of that file?
edit: Also include the output of the serial monitor.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: YusifNurizade on Jan 06, 2015, 11:14 pm
Thanks for posting!

This is a great tutorial - especially the part about controlling the Arduino from the webpage. Other tutorials I looked at explained how to get data from the MCU but not the other way around.

I'm curious whether modifying the address is the only way to control the Arduino, for example, in toggling an LED. It works for basic communication but it seems like it wouldn't be able to handle it if things got more complex. I'm new to Web Servers on the Arduino and I would really appreciate any advice.

Thanks again,
Yusif Nurizade
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: chrisapollon on Jan 10, 2015, 10:28 am
Hello I am a new developer and new in arduino. I would like to create a WiFi server using arduino WiFi shield. I would like to control devices such as switch them on and of with a relay and also view or store some sensor data on it. How can I implement this?
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: myggle on Feb 09, 2015, 03:30 pm
Seems this might be a work in progress from the original posting till now.  I initially found this thread when trying to learn how servers work from an Arduino's perspective and how I might also connect my Mega to the web via this Ethernet shield, then when getting into SurferTim's material has opened my eyes to a broader spectrum of what actually applies.

I want to build a remotely viewed/controlled arduino project that includes use of the Mega 2560 R3, Ethernet Shield, Data Logging Shield with SD slot and Real Time Clock.  The project will control an 8 channel relay and 2 temp/humidity sensors.  So I wanted to read this thread to see if the (SPI?/I2C?) conflicts could be worked out by using a different shield with SD so that the SD and Ethernet can be utilized at the same time.

Tim, you mentioned something called "TimeServer" and I followed your link to further learn.  By chance, could this be used to replace the RTC for triggering events after the MCU like kicking on a relay at 12:00 everyday and off again at another exact time respective to a 24 hour day? 
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: SurferTim on Feb 09, 2015, 03:36 pm
I don't use a RTC. A modified version of the UdpNtpClient example sketch works good for me. I use a RPi for the NTP server.
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: CatweazleNZ on Feb 10, 2015, 09:22 am
All

I have released the source code for my Arduino home automation system web server which runs at http://www.2wg.co.nz. There is 34 pages of documentation and 11,000 line of source code.

My web server implementation (which is quite substantial) is my own and manifests as a set of individual purpose specific procedures (functional decomposition) - each of which should be easy enough to understand. The source code includes quite a lot of additional technical documentation using comments.

You can download the source code and documentation from the application website. Access the SD card web page and browse to the PUBLIC folder/subdirectory. A message on the dashboard/main web page will tell you what files to download.

You may have to be a little patient - the system is single threaded and will ignore you for a few seconds if it is serving up a big file to someone else.

Cheers

Catweazle NZ
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: thaslemanasreen on Feb 21, 2015, 10:40 am
this is very useful..thanks for your share..
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: lucas10880 on Jan 10, 2017, 12:13 pm
Oi pessoal então , eu uma ideia de um projeto mais não sei muito como posso desenvolver gostaria que se um de você pudesse me ajudar eu ia agradecer , o projeto é o seguinte é uma pagina que terá um campo  de texto e o usuário ira digitar alguma coisa e o site ira responder sendo assim os dois iram efetuar uma conversa homem vs maquina mas esse robô (pagina) não poderá enviar a mesma resposta sempre então eu imaginei o cérebro dele como se fosse uma roleta porque quando o usuário digitar alguma coisa ai vai automaticamente vai girar uma roleta invisível  e vai dar uma resposta e se ele perguntar a mesma coisa de novo a roleta vai girar outra vez e vai cair em outra resposta
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: mega-hz on Feb 21, 2017, 07:45 pm
Hello,

i am making a housecontrol, the arduino webserver is based on the starting-electronics server.
But there are many changes:

Because of selfdestroying an mem-hungry sd-cards i removed this and let website run of my home-linux server.
The arduino only gets und puts the IO-Datas, like Inputs, Outputs,Can-Bus and analog values.
On the 1. Arduino (there are 2!) there is a 32xOutputSHield (74595) and a 32xInput-Shield(74165), an MCP2515 CAN-Bus Shield and a Ethernet.
The 2nd Arduino has only a EthernetShield and measures the voltage/current/Power of my PVs.
Both Arduinos are polled from my Webserver, the Website with JS.
This works fine sofar.

I tryed to put all 3 (server, arduin1, arduino2) to access on internet with a dyndns sort of service.
That runs too, but every 2-3seconds, when polling, my FritzBox Router (also for IP-Phone) makes a audio-blank on the phone for half a sec. So that seems to be to much traffic for back & forwarding....

So i renamed the Connections to the local IPs, runs well, but only local.

The Website is not very nice, it is just a test for the functional, i will make a nice one soon.

My Problem is:
The System is working getting and putting datas to/from arduinos and display them on the website.
BUT, THAT is only one Part, the main part is missing yet:
The Part of making some actions when there are some states of the data! The brain!
(If sun = true, turn off outside light...)
On the Arduinos is not enough memory to do that, so i had some ideas to do that and would like to hear,
what suggestions you may have!

1. Another Arduino connected with I2C gets data , makes actions and rules, sends data.
2. Make a Python programm on my debian server for sending the html and js and making actions and rules
3. Make a C++ programm on my debian server for s..........
4. ???

any ideas?

The website is here: http://mega-hz.dnshome.de:84/ (http://mega-hz.dnshome.de:84/)
(But ithink you won't be able to see the data, because of local arduino-ip's.

And The Sketch is here: http://www.mega-hz.de/Arduino/WEB-IO/_home-sys_arduino_no_sd.ino (http://www.mega-hz.de/Arduino/WEB-IO/_home-sys_arduino_no_sd.ino)
 
Wolfram.


Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: rockwallaby on Feb 22, 2017, 02:27 am
I can see this thread has gone off topic, please excuse me also.  :smiley-confuse:
Wolfram, I looked at your website but as you say, I did not see any real time data.

To give you an idea of what I do for my own off grid system, which was to use an Arduino to do all the low level I/O control and to develop an application for a small GNU/Linux board to be the web server.

The GNU/Linux board communicates to the Arduino using Modbus protocol to get and set data.
The GNU/Linux system logs all data to SQLite3 database for historical purposes.

The Arduino controls MOSFETs for each solar PV panel connected to the LiFeYPO4 battery system.
The Arduino also communicates with a BMS (Battery Management System) via CanBus.

My project started initially with a Freetronics Ether-Mega, with onboard Ethernet, but now I use an Arduino DUE, using the inbuilt CanBus controllers to talk to the BMS.

The Arduino is a Modbus slave, being polled by the GNU/Linux board acting as Modbus master and web server, all coded up in C, and being translated across to C++.

The web application is a single page application using a framework to make application navigation very fast, with all real time data being ask for via AJAX calls.

The key points are, use the Arduino as a low levl I/O controller and leave the more complex stuff like internet connections and long term historical data storage to a small GNU/Linux system. Use a good data protocol like Modbus between Arduino and GNU/Linux system.

My signature is my GNU/Linux system web server as mentioned above. It points to my renewable energy system still being developed when I have time.

So, my suggestion for you is to go with choice number 2 or 3.  :)
_____________
Paul - VK7KPA
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: mega-hz on Feb 24, 2017, 10:42 am
that sounds very nice!
I have great interest to see more of it!

>The GNU/Linux system logs all data to SQLite3 database for historical purposes.

do you have a link for that?
datalogging for long run is also my goal!
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: rockwallaby on Feb 24, 2017, 01:33 pm
Wolfram wrote:
Quote
The GNU/Linux system logs all data to SQLite3 database for historical purposes.
Unfortunately, not really, I just programmed it up into my application, QuadlogSCADA.

But for SQLite3, here is an entry point to the SQLite3 (https://www.sqlite.org/whentouse.html) site which describes some things about where it using it might be appropriate.

To give you an idea of how I went about things:
In terms of SQLite3, data that is configured to be logged will be stored to SQLite3 database each log period, say every one second for example. The logged data can then be requested via a connected HTTP client through the application for a start period and number of records, which is then sent back through to the HTTP client as CSV as a AJAX request reply to be stored in a json model in the client side JavaScript SPA, which then can then be displayed using HighCharts JavaScript charting library.

SQLite3 is a nice easy to use and lightweight library that is fairly easy to integrate into many different coding platforms, whether that be C or C++ or Python or in fact, many others.
_____________
Paul - VK7KPA
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: Hazza on Mar 05, 2018, 06:29 am
Hi, As usual like most pots here I am new to Arduino .  I am trying to use two UNO with Ethernet boards to communicate.  to test my config I am using the Ethernet example UDPSendReceiveString and Ethernet lib 2.  When I compile the Send part it fails  on line "import hypermedia.net.*";

Also there seems to be code missing from this example like IP address assignment etc.

can anyone shed any light where I am going wrong.
/*

  Processing sketch to run with this example
 =====================================================

 // Processing UDP example to send and receive string data from Arduino
 // press any key to send the "Hello Arduino" message


 import hypermedia.net.*;

 UDP udp;  // define the UDP object

 void setup() {
 udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
 //udp.log( true );       // <-- printout the connection activity
 udp.listen( true );           // and wait for incoming message
 }

 void draw()
 {
 }

 void keyPressed() {
 String ip       = "192.168.1.177";   // the remote IP address
 int port        = 8888;      // the destination port

 udp.send("Hello World", ip, port );   // the message to send

 }

 void receive( byte[] data ) {          // <-- default handler
 //void receive( byte[] data, String ip, int port ) {   // <-- extended handler

 for(int i=0; i < data.length; i++)
 print(char(data));
 println();
 }
 */

 
Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: josemigu on Mar 05, 2018, 06:52 am

A simple question:

If my ISP (Internet Service Provider) as Movistar or another in your country, change constantly mi public IP, so how can i connect to the web server? In a wide area network.

Example: if the web server is in my house in Chile, and i go to  holyday in Brazil.. how i can connect?

Saludos desde Concepción, Chile.!

Title: Re: Excellent Arduino Ethernet Shield Web Server Tutorial
Post by: kazkaslengvo on Mar 29, 2019, 02:24 pm
A simple question:

If my ISP (Internet Service Provider) as Movistar or another in your country, change constantly mi public IP, so how can i connect to the web server? In a wide area network.

Example: if the web server is in my house in Chile, and i go to  holyday in Brazil.. how i can connect?

Saludos desde Concepción, Chile.!


dynamic dns