在 Arduino 上接收 HTTP POST 请求 [英] Receiving a HTTP POST Request on Arduino

查看:74
本文介绍了在 Arduino 上接收 HTTP POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用以太网屏蔽通过我的 Arduino Uno 接收 HTTP Post 请求.我想制作一个可以控制我 Arduino 的 Android 应用程序,我认为最好的方法是使用 HTTP Post 请求.

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 to do it is with a HTTP Post Request.

关于1的帖子很多],但我找不到任何关于如何接收 HTTP Post 请求的帖子.我刚开始为 Arduino 编程,但我已经为 Android 制作了一些应用程序(我已经完成了 Android 的邮政编码).

There are many posts about 1[Sending a POST Request], but i couldn't found any posts of how to receive HTTP Post request. I just started programming for Arduino but I already made a few apps for Android (I already have the post code for Android done).

推荐答案

我想像您一样阅读 POST,而不是使用 GET.我是这样做的:

I wanted to read a POST like you instead of using a GET. I did it like this:

/*
 A simple Arduino Ethernet web server. 
 by John Harrison
 */

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

// You can change the MAC and IP addresses to suit your network:

byte mac[] = { 0X52, 0X64, 0X75, 0X69, 0X6E, 0X6F };
IPAddress ip( 192,168,0,97 );

EthernetServer server(80); // Port 80 is HTTP port
char new_state[1024];

void setup()
{
  Serial.begin(9600);
  // Start the Ethernet server:
  Ethernet.begin(mac, ip);

  server.begin();

  // Set the digital pins ready to write to
  for (int pin = 2; pin <= 9; pin++) {
    pinMode(pin, OUTPUT);
  }

  Serial.print("Serving on http://");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();

  if (client) {

    // Serial.println("Client connected");

    while (client.connected()) {

      int i = 0;
      int head = 1;
      int body = 0;

      while(client.available()) {
        char c = client.read();
        if (c == '\n') {

          if ( i <= 2 ) {

            // an http request ends with a blank line

            sendPage(client);
            if ( head == 1 ) {
              body = 1;
              head = 0;
            }

          }

          i = -1;

        }
        if ( body == 1 ) {
          new_state[i] = c;
        }
        i++;
        new_state[i] = '\0';
      }
      i = 0;
    }

    // Serial.println("Disconnected");
    /*
    if ( strlen(new_state) > 0 ){
      Serial.print ("[");
      Serial.print(new_state);
      Serial.println ("]");
    }
    */
    // Post data looks like pinD2=On
    if ( strncmp( new_state, "pinD", 4) == 0 ) {
      int pin = new_state[4] - 48; // Convert ascii to int
      // Serial.println(pin);
      if ( strncmp( new_state+5, "=On", 3) == 0 ) {
        digitalWrite(pin, 1);
      } 
      else if ( strncmp( new_state+5, "=Off", 4) == 0 ) {
        digitalWrite(pin, 0);
      }
    }

  }

}

void sendPage(EthernetClient client)
{

  // Serial.println("Sending response");

  // send a standard http response header
  client.println("HTTP/1.0 200 OK\Content-Type: text/html\n\n<html>\n<head>");
  client.println("<link rel='icon' href='data:;base64,iVBORw0KGgo='>");
  client.println("<title>POST Pin controller</title>\n</head>\n<body>\n");
  client.println("<h2>Buttons turn pins on or off</h2>");
  client.println("<form method='post' action='/' name='pins'>");

  char line[1024];
  int pin;

  for ( pin=2; pin<=9; pin++ ) {
    sprintf(line, "<input name='pinD%d' type='submit' value='On' />\n", pin);
    client.print(line);
    sprintf(line, "<input name='pinD%d' type='submit' value='Off' /> %d<br />\n", pin, pin);
    client.print(line);
  }

  client.println("</form>\n</body>\n</html>");
  client.stop();

}

有一些更简单、更小的方法可以做到这一点,但我发现它们非常滞后,所以一直试图尽快完成.

There are ways to do it which are simpler and smaller, but I found them quite laggy so have been trying to get it as fast as possible.

我已经用它来控制 Mega 2560 上针脚 2-9 上的 8 个 LED.我还没有在 Uno 上测试过,但我希望它也能正常工作.

I have used this to control 8 LEDs on pins 2-9 on a Mega 2560. I haven't tested it on a Uno yet, but I expect it would work the same.

这篇关于在 Arduino 上接收 HTTP POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆