[英]Javascript - Arduino - POST request - WiFi101 - Connection issue
In my code I am trying to send a POST request to the IFTTT service webhooks (maker). 在我的代码中,我尝试将POST请求发送到IFTTT服务webhooks(制造商)。
I'm using a couple of libraries, mainly WiFi101 我正在使用几个图书馆,主要是WiFi101
I am using an Arduino MKR1000. 我正在使用Arduino MKR1000。
I have updated the firmware, and added a certificate for https://maker.ifttt.com:443 . 我已经更新了固件,并为https://maker.ifttt.com:443添加了证书。
When in the following code I call sslClient.connect(host, 443);
在以下代码中,我调用
sslClient.connect(host, 443);
It fails to make the connection. 无法建立连接。 I have tried bypassing this and just trying to print data to the host, however this also didn't work.
我尝试绕过此方法,只是尝试将数据打印到主机,但是这也没有用。
It takes about 10-20 seconds for the function to return as false, if I change the host to an incorrect variable, then it returns as false immediately. 函数返回假值大约需要10到20秒,如果我将主机更改为错误的变量,则它立即返回假值。 I'm assuming this is a good sign since the arduino is trying to connect?
我认为这是一个好兆头,因为arduino试图连接?
wifiSetup()
Runs well, connection is established reasonably quickly. wifiSetup()
运行良好,可以很快建立连接。
The code I am refering to is below: 我要引用的代码如下:
Globally defined 全局定义
//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 setup procedure: This runs without problems 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
}
The code below is the one causing problems 下面的代码是导致问题的代码之一
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);
}
Does anyone have any solutions, or things to troubleshoot? 有没有人有任何解决方案或要解决的事情?
Thanks for your help all, 谢谢大家的帮助,
Let me know if you need any extra information. 让我知道您是否需要任何其他信息。
https://maker.ifttt.com
is not a valid host. https://maker.ifttt.com
不是有效的主机。 A valid host is either an IP address or a domain. 有效主机是IP地址或域。
https://
is not a part of the domain, but an URL. https://
不是域的一部分,而是URL。
You are also missing the HTTP protocol version ( HTTP/1.1
), which could potentially cause problems. 您还缺少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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.