Go Down

Topic: how to use POST on arduino webserver? (Read 2257 times) previous topic - next topic

guimas

I'm trying to do a webserver on arduino mega 2560 + ethernet shield. When I use HTTP GET it works but when I switch to POST it stop working.
I stored the html files on the SD card, here is the code and the SD files. thanks

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

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

File webFile;

String HTTP_req = "";     // stores the received HTTP request

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

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;          // save HTTP request character
                Serial.print(c);    // print HTTP request character to serial monitor
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    client.println();
                    // send web page
                    if ((HTTP_req.indexOf("POST / ") > -1)
                                 || (HTTP_req.indexOf("POST /loginPost.htm") > -1)) {
                        webFile = SD.open("loginPost.htm");        // open web page file
                    }
                    else if (HTTP_req.indexOf("POST /opcoesPost.htm") > -1) {
                        webFile = SD.open("opcoesPost.htm");        // open web page file
                    }
                    else if (HTTP_req.indexOf("POST /abrirPost.htm") > -1) {
                        webFile = SD.open("abrirPost.htm");        // open web page file
                       
                        digitalWrite(11,HIGH);
                        delay(2000);
                        digitalWrite(11, LOW);
                       
                    }
                   
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    HTTP_req = "";  // empty the string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

SurferTim

Technically, you must parse the header for the "Content-Length:" variable and read that many characters after the header (blank line). This will normally show the POST data.  The POST data could arrive in a separate packet, so it may or may not show it or maybe not all of it.
Code: [Select]
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {

                   // here is where the POST data is
                   while(client.available()) {
                      Serial.write(client.read());
                    }

                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    client.println();
                    // send web page


guimas

thanks but when I try to enter the IP on the browser it don't open the page, only if I put get on the method =//

SurferTim


thanks but when I try to enter the IP on the browser it don't open the page, only if I put get on the method =//

That is correct. If you enter the ip in your browser address bar, that is a GET request. A POST request can only be sent from a form on the currently loaded page.

guimas

So i have to treat GET and POST, do you some examples of how can i do this ?
thaks.

SurferTim

The sketch in this link displays the POST data if it is sent in the same packet as the request:
http://playground.arduino.cc/Code/WebServerST
Code: [Select]
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print(F("POST data: "));
          while(client.available()) Serial.write(client.read());

The recommended way is to parse the request for the "Content-Length:" variable and read that many characters after the blank line. The POST data may not arrive in the same packet, especially if it is a device like an Arduino sending the POST request.

guimas

thaks the POST worked. How can I do to call another page ?

rappt

Completed code to comunicate with arduino form internet, and internet to arduino.
This code on arduino mega and ethernet shield controls and monitorize my reef aquarium  :) :)
Link: http://q.gs/4tkFt
Feel free to ask questions you have  :)

mananpbansal

I'm trying to do a webserver on arduino mega 2560 + ethernet shield. When I use HTTP GET it works but when I switch to POST it stop working.
I stored the html files on the SD card, here is the code and the SD files. thanks

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

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

File webFile;

String HTTP_req = "";     // stores the received HTTP request

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

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;          // save HTTP request character
                Serial.print(c);    // print HTTP request character to serial monitor
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    client.println();
                    // send web page
                    if ((HTTP_req.indexOf("POST / ") > -1)
                                 || (HTTP_req.indexOf("POST /loginPost.htm") > -1)) {
                        webFile = SD.open("loginPost.htm");        // open web page file
                    }
                    else if (HTTP_req.indexOf("POST /opcoesPost.htm") > -1) {
                        webFile = SD.open("opcoesPost.htm");        // open web page file
                    }
                    else if (HTTP_req.indexOf("POST /abrirPost.htm") > -1) {
                        webFile = SD.open("abrirPost.htm");        // open web page file
                       
                        digitalWrite(11,HIGH);
                        delay(2000);
                        digitalWrite(11, LOW);
                       
                    }
                   
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    HTTP_req = "";  // empty the string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}


Go Up