Ethernet Web server

Greetings to all members of this forum... I'm working on a Web server and I was wondering how to control things on the Arduino, for example through an Android application or browser. I watched this tutorial: http://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/

I understand everything but in this tutorial it is only one LED and I would like to control more outputs. My question is how do I wrote to the Arduino to read the http post sent to it. For example, if I push a button on the Android app or browser that sends an http post to the web server (192.168.0.87/LedOn) and the Arduino check what http post is sent to it and turns on the Led, if I send (192.168.1.87/SensorOff) he will switch off the sensor.I hope you understand. Thanks in advance. :D

I would do something like 192.168.0.87/deviceOn=1 192.168.0.87/deviceOn=2 192.168.0.87/deviceOn=3

Or another would be to have a different string for each device 192.168.0.87/deviceOne 192.168.0.87/deviceTwo 192.168.0.87/deviceThree

Okay, you can put the name that you'll, I understand that but I do not know to write to the Arduino to check the http post that it has been sent to it. For example: if(//check what http post is sent and if http post is 192.168.1.87/deviceOne) { digitalWrite(ledPin, HIGH); }

TheHeavyMan: I understand everything but in this tutorial it is only one LED and I would like to control more outputs. My question is how do I wrote to the Arduino to read the http post sent to it. For example, if I push a button on the Android app or browser that sends an http post to the web server (192.168.0.87/LedOn) and the Arduino check what http post is sent to it and turns on the Led, if I send (192.168.1.87/SensorOff) he will switch off the sensor.I hope you understand.

A couple of days ago I posted a webserver example on request. That example provides relay switching for several output pins. Runs in every webbrowser, whether Android smartphone or other device like PC, tablet or smartphone.

The sketch is included as a file attachment to this posting: http://forum.arduino.cc/index.php?topic=322364.msg2247138#msg2247138

But I never got any single comment after posting the code.

Currently only GET requests are supported (no POST requests). There is a "dumb" (plain text) interface and a HTML interface that can be used with any webbrowser. And there is an easy to fill data structure for "active HIGH" and "active LOW" relays.

Perhaps you'd like to give it a try and possibly send a comment what you think about.

Simple three button server code.

//zoomkat 5-30-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
  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, 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("multibutton server test 5-30-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("<a href='/?on4' target='inlineframe'>ON 4</a>"); 
            client.println("<a href='/?off4' target='inlineframe'>OFF 4</a>"); 

            client.println("<a href='/?on5' target='inlineframe'>ON 5</a>"); 
            client.println("<a href='/?off5' target='inlineframe'>OFF 5</a>"); 

            client.println("<a href='/?on6' target='inlineframe'>ON 6</a>"); 
            client.println("<a href='/?off6' target='inlineframe'>OFF 6</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("on4") >0)//checks for on4
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off4
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          if(readString.indexOf("on5") >0)//checks for on5
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf("off5") >0)//checks for off5
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }          
          if(readString.indexOf("on6") >0)//checks for on6
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf("off6") >0)//checks for off6
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

TheHeavyMan: Okay, you can put the name that you'll, I understand that but I do not know to write to the Arduino to check the http post that it has been sent to it. For example: if(//check what http post is sent and if http post is 192.168.1.87/deviceOne) { digitalWrite(ledPin, HIGH); }

I thought you said you understood everything on startingelectronics. Also, why would you want to use POST when GET is a lot easier to work with.

mistergreen: Also, why would you want to use POST when GET is a lot easier to work with.

There is basically NO difference in the way GET and POST get processed on the server. The ONLY difference is the POST data comes one line later i the request. The WebServer already handles both, and sends only the "payload" to the processing function, so it neither knows nor cares which submit method was used.

On the Due forum is a modified WebServer I posted probably 6 months ago, which extracts all the HTML content, and request processesing, into a separate file. It also adds a simple symbol table capability that allows the HTML data fields to be directly connected to the data within the application, so a GET or POST automatically updates the data when the page is submitted, and the updated data can be delivered back to the client IN the HTML, with no additional programming required. The symbol table does it all for you. I use this to access and update hundreds of variables in a real-time application using a browser.

Regards, Ray L.

Thanks guys for the quick responses... You said that there is no difference in GET and POST so I will use GET because I have already see an example a few posts above and It seems easy. Thanksss.... :D

TheHeavyMan: Thanks guys for the quick responses... You said that there is no difference in GET and POST so I will use GET because I have already see an example a few posts above and It seems easy. Thanksss.... :D

There are differences between GET and POST, but they don't matter in many cases. First, GET requests are visible - they should up on the URL line in the browser, where anyone can see all the data. Second, they are limited in size, by the length of the line buffer in the server. Finally, POST data comes later in the request, starting on the first line AFTER the blank line that follows the request itself. But, as I said, in terms of processing the request itself, there is no need for the code doing the request parsing to know or care which method is used. That knowledge should be limited to the internals of the server code that receives the request.

Regards, Ray L.

POST will cost you more memory since you have to store all characters in the http header which could be up to 400 characters.

GET will only cost you the first line of the http header, maybe 70 max characters.

mistergreen: POST will cost you more memory since you have to store all characters in the http header which could be up to 400 characters.

GET will only cost you the first line of the http header, maybe 70 max characters.

There is no limit to the size of a GET payload. They are typically small, but there is no requirement for them to be small. If you assume no more than 70 bytes, at some point you'll get into trouble.

Regards, Ray L.

RayLivingston: There is no limit to the size of a GET payload. They are typically small, but there is no requirement for them to be small. If you assume no more than 70 bytes, at some point you'll get into trouble.

Regards, Ray L.

For his purpose, I'd say 30 characters.

There is no limit to the size of a GET payload.

You sure about that? Seems like back in the day there was something like a 128 byte limit on "query_strings" per the http specs.

zoomkat: You sure about that? Seems like back in the day there was something like a 128 byte limit on "query_strings" per the http specs.

I'm not sure if there is a stated limit in the spec, but if there is, it's certainly WAY above the 70 characters someone cited. I have generated GET requests that were many hundreds of characters long. Not the best way to do it, but it does work. The only limitation I'm aware of is the buffer size on the server side, but I'm no expert. There's really no good reason for it to be limited on the client side.

Regards, Ray L.

zoomkat: You sure about that? Seems like back in the day there was something like a 128 byte limit on "query_strings" per the http specs.

Back in the day, there was a 255 character limit to url query string. It's not the case anymore, it's up to the thousands.

But realistically for the arduino, less than 100 bytes is fine for GET. You get to define your query string! No need to write 300bytes strings. That's beyond stupid.