Receiving a HTTP POST Request on Arduino

后端 未结 3 1121
悲哀的现实
悲哀的现实 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:27

    Not sure about POST, but GET definitely works. Here's an AJAX example that I've been using that works. It just controls an RGB LED.

     xmlhttp.open("GET", "http://ipAddressOfArduino?r=" + redVal + "&g=" + greenVal + "&b=" + blueVal + "&e", true);
    

    Then on the Arduino side, I just parse the data.

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    
    
    #include 
    #include 
    boolean reading = false;
    String myStr;
    int redVal, greenVal, blueVal;
    
    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
      //byte ip[] = { 192, 168, 0, 1 };   //Manual setup only
      //byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
      //byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
    
      // if need to change the MAC address (Very Rare)
      byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    
      EthernetServer server = EthernetServer(80); //port 80
    ////////////////////////////////////////////////////////////////////////
    
    void setup(){
      Serial.begin(9600);
    
      pinMode(3, OUTPUT);
      pinMode(5, OUTPUT);
      pinMode(6, OUTPUT);
    
      Ethernet.begin(mac);
      //Ethernet.begin(mac, ip); //for manual setup
    
      server.begin();
      Serial.println(Ethernet.localIP());
    
    }
    
    void loop(){
    
      // listen for incoming clients, and process qequest.
      checkForClient();
    
    }
    
    void checkForClient(){
    
      EthernetClient client = server.available();
    
      if (client) {
    
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        boolean sentHeader = false;
        myStr = "";
        while (client.connected()) {
          if (client.available()) {
    
            char c = client.read();
    
            if(reading && c == ' ') reading = false;
            if(c == '?') reading = true; //found the ?, begin reading the info
    
            if(reading){
              //Serial.print(c);
              if (c!='?') {
                myStr += c;
              }
    
            }
    
            if (c == '\n' && currentLineIsBlank)  break;
    
            if (c == '\n') {
              currentLineIsBlank = true;
            }else if (c != '\r') {
              currentLineIsBlank = false;
            }
          }
        }
    
        parseThangs(myStr);
        analogWrite(3, redVal);
        analogWrite(5, greenVal);
        analogWrite(6, blueVal);    
        delay(100); // give the web browser time to receive the data
        client.stop(); // close the connection:    
      } 
    }
    
    void parseThangs(String str) {
      int startIndex = str.indexOf("r");
      int endIndex = str.indexOf("g");
      String redStr = str.substring(startIndex + 2, endIndex - 1);
      char tempRed[4];
      redStr.toCharArray(tempRed, sizeof(tempRed));
      redVal = atoi(tempRed);
      startIndex = str.indexOf("g");
      endIndex = str.indexOf("b");
      String greenStr = str.substring(startIndex + 2, endIndex -1);
      char tempGreen[4];
      greenStr.toCharArray(tempGreen, sizeof(tempGreen));
      greenVal = atoi(tempGreen);
      startIndex = str.indexOf("b");
      endIndex = str.indexOf("e");
      String blueStr = str.substring(startIndex + 2, endIndex -1);
      char tempBlue[4];
      blueStr.toCharArray(tempBlue, sizeof(tempBlue));
      blueVal = atoi(tempBlue);
      Serial.println(redStr + " " + greenStr + " " + blueStr);
    }
    

    Probably a little sloppy, but it works.

提交回复
热议问题