无法将Arduino的POST请求发送到Google表单

时间:2014-11-07 05:33:50

标签: arduino google-docs http-post

我使用WiFi CC3000 shield和Adafruit_CC3000.h库。我能够连接到我的wifi网络并且所有示例都能正常工作。但我的草图并没有:(
这是我的loop()函数:

void loop()
{
  Serial.println("In-loop");
  unsigned long ip;
  cc3000.getHostByName("docs.google.com", &ip);
  client = cc3000.connectTCP(ip, 80);
  if (client.connected())
  {
    Serial.println("Connected!!!");

    String data = "entry.1820200471=46";
    data += "&submit=Submit";

    client.println(F("POST docs.google.com/forms/d/1YABi7F1ViEqVknN74dQhNLLJqq0RJLRr1QjohRk0qhc/formResponse"));
    // client.println(F("HTTP/1.1\r\n"));
    client.println(F("Host: docs.google.com\r\n"));
    client.println(F("Accept: */*\r\n"));
    client.println(F("Accept-Encoding: gzip, deflate\r\n"));
    client.println(F("User-Agent: runscope/0.1\r\n"));
    client.println(F("Content-Type: application/x-www-form-urlencoded\r\n"));
    client.println(F("Connection: close\r\n"));
    client.print(F("Content-Length: \r\n"));
    client.println(data.length());
    client.println();
    client.print(data);
    client.println();

    Serial.println("POST docs.google.com/forms/d/1YABi7F1ViEqVknN74dQhNLLJqq0RJLRr1QjohRk0qhc/formResponse");
    // Serial.println("HTTP/1.1");
    Serial.println("Host: docs.google.com");
    Serial.println("Accept: */*");
    Serial.println("Accept-Encoding: gzip, deflate");
    Serial.println("User-Agent: runscope/0.1");
    Serial.println("Content-Type: application/x-www-form-urlencoded");
    Serial.println("Connection: close");
    Serial.print("Content-Length: ");
    Serial.println(data.length());
    Serial.println();
    Serial.print(data);
    Serial.println();

    Serial.println("----------");
    unsigned long lastRead = millis();
    while(client.connected() && (millis() - lastRead < 3000)) {
      while (client.available()) {
        char c = client.read();
        Serial.print(c);
        lastRead = millis();
      }
    }
    Serial.println("----------");
  } else {
    Serial.println("Not connected");
  }
  delay(15000L);
}

我得到的输出是:

In-loop
Connected!!!
POST docs.google.com/forms/d/1YABi7F1ViEqVknN74dQhNLLJqq0RJLRr1QjohRk0qhc/formResponse
Host: docs.google.com
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: runscope/0.1
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 33

entry.1820200471=46&submit=Submit
----------
----------

我已经尝试了我的请求here并且它有效(实际上我从那里拿了一些标题)。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我已经尝试了很多次,但我还没有找到一种直接使用POST的好方法。我读过这是因为谷歌需要https进行所有通信。

要使其工作,需要像pushinhbox.com这样的中继服务。

这是一个很好的指南: http://m.instructables.com/id/Post-to-Google-Docs-with-Arduino/

Arduino不能,因为它没有编程空间或ram来轻松地或以可用的方式运行SSL。这是一个很好的快速讨论: Arduino Due HTTPS Support

你可以使用另一个平台,比如Intel Galileo,这是一个运行arduino代码的Linux主板 http://arduino.cc/en/ArduinoCertified/IntelGalileo

或者另一种像树莓派或beaglebone黑色。