Javascript - Arduino - POST请求 - WiFi101 - 连接问题

时间:2018-03-28 22:57:58

标签: javascript post arduino http-post wifi

在我的代码中,我正在尝试向IFTTT服务webhooks(制造商)发送POST请求。

我正在使用几个库,主要是http://blog.netgloo.com/2014/11/13/run-code-at-spring-boot-startup/

我使用的是Arduino MKR1000。

我已更新固件,并为WiFi101添加了证书。

在以下代码中,我调用sslClient.connect(host, 443);它无法建立连接。我试图绕过这个并尝试将数据打印到主机,但是这也没有用。

函数返回false大约需要10-20秒,如果我将主机更改为不正确的变量,则立即返回false。我认为这是一个好兆头,因为arduino试图连接?

wifiSetup()运行良好,连接建立得相当快。

我所指的代码如下:

全球定义

//WiFi router setup
char ssid[] = "-----";   //network SSID (aka WiFi name)
char pass[] = "-----"; //network password
int status = WL_IDLE_STATUS;
const char* host = "https://maker.ifttt.com";
WiFiSSLClient sslClient;

Wifi设置程序:运行没有问题

void wifiSetup() {
  // Check for the presence of the shield
  Serial.print("WiFi101 shield: ");
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("NOT PRESENT");
    return; // don't continue
  }
  Serial.println("DETECTED");
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  printWifiStatus();                        // you're connected now, so print out the status
}

以下代码是造成问题的代码

void sendMessage() {
  if (sslClient.connect(host, 443)) {
    //change this to your Maker setting from https://ifttt.com/services/maker/settings
    String  data = "randomdata";
    sslClient.println("POST /trigger/tank_empty/with/key/bxa");
    sslClient.println("Host: https://maker.ifttt.com");
    sslClient.println("Content-Type: application/json");
    sslClient.print("Content-Length: ");
    sslClient.println(data.length());
    sslClient.println();
    sslClient.print(data);
    sslClient.stop();
    Serial.println("IFTTT request Sucessful");
  }
  else {
    Serial.println("IFTTT request failed");
  }
  delay(20000000);
} 

有没有人有任何解决方案或要解决的问题?

感谢您的帮助,

如果您需要任何额外信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

https://maker.ifttt.com不是有效的主机。有效主机是IP地址或域。 https://不是域的一部分,而是URL。

您还缺少HTTP协议版本(HTTP/1.1),这可能会导致问题。

const char* host = "maker.ifttt.com";
sslClient.println("POST /trigger/tank_empty/with/key/bxa HTTP/1.1");
sslClient.print("Host: ");
sslClient.println(host); // non hardcoded host header
sslClient.println("Content-Type: application/json");
sslClient.print("Content-Length: ");
sslClient.println(data.length());
sslClient.println();
sslClient.print(data);
sslClient.stop();