Receiving a HTTP POST Request on Arduino

后端 未结 3 1113
悲哀的现实
悲哀的现实 2021-01-31 12:41

Is it possible to receive a HTTP Post request with my Arduino Uno using a Ethernet shield. I want to make an Android app wich can control me Arduino and I thought the best way t

3条回答
  •  醉梦人生
    2021-01-31 13:21

    A POST looks like:

    POST /test.php HTTP/1.1
    Host: 192.168.0.55
    Content-Type: application/x-www-form-urlencoded
    Connection: close
    User-Agent: Arduino/1.0
    Content-Length: 1024
    
    data=5
    

    So you have to ignore first "newline" and read on. Here is my modified Web Server code to read posted data:

    /*
      Web Server 
    */
    
    #include 
    #include 
    
    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = {
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    };
    IPAddress ip(192, 168, 15, 177);
    
    // Initialize the Ethernet server library
    // with the IP address and port you want to use
    // (port 80 is default for HTTP):
    EthernetServer server(80);
    
    void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }
    
    
      // start the Ethernet connection and the server:
      //Ethernet.begin(mac, ip);
      Ethernet.begin(mac);
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
    }
    
    
      void writeResponse(EthernetClient client) {
        // send a standard http response header
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");  // the connection will be closed after completion of the response
        // client.println("Refresh: 5");  // refresh the page automatically every 5 sec
    
        client.println();
        client.println("");
        client.println(" ");
        // output the value of each analog input pin
        for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
          int sensorReading = analogRead(analogChannel);
          client.print("analog input ");
          client.print(analogChannel);
          client.print(" is ");
          client.print(sensorReading);
          client.println("
    "); } client.println("
    "); client.println(""); client.println(""); client.println("
    "); client.println(" "); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.print("new client ["); //Serial.print(client.getRemoteIP()); //Serial.println("]"); // an http request ends with a blank line boolean currentLineIsBlank = true; String req_str = ""; int data_length = -1; boolean skip = true; //int empty_line_count = 0; while (client.connected()) { if (client.available()) { char c = client.read(); //Serial.write(c); req_str += c; // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank && req_str.startsWith("GET")) { writeResponse(client); break; } if (c == '\n' && currentLineIsBlank && req_str.startsWith("POST") && !skip) { writeResponse(client); break; } if (c == '\n' && currentLineIsBlank && req_str.startsWith("POST") && skip) { skip = false; String temp = req_str.substring(req_str.indexOf("Content-Length:") + 15); temp.trim(); //Serial.print("Content-Length="); data_length = temp.toInt(); /*Serial.println(data_length); writeResponse(client); break;*/ while(data_length-- > 0) { c = client.read(); req_str += c; } writeResponse(client); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } Serial.println(req_str); // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }

提交回复
热议问题