Post 解决从ArduinoMKr010发布到iftt webhook的问题

Post 解决从ArduinoMKr010发布到iftt webhook的问题,post,arduino,iot,ifttt,Post,Arduino,Iot,Ifttt,我的目标是发出post请求以触发IFTTWebhook操作。我正在使用MKR1010板。我能够使用云集成连接到网络并打开和关闭连接的LED 代码如下,但不会触发web钩子。我可以在浏览器中手动粘贴网址,这会触发web钩子。当代码被发布时,它返回一个400错误请求错误 以下代码中的键已替换为伪值 有人知道为什么这不会触发web钩子吗?/您能解释一下服务器拒绝post请求的原因吗?我甚至不需要从服务器读取响应,只要它被发送 多谢各位 // ArduinoHttpClient - Version: L

我的目标是发出post请求以触发IFTTWebhook操作。我正在使用MKR1010板。我能够使用云集成连接到网络并打开和关闭连接的LED

代码如下,但不会触发web钩子。我可以在浏览器中手动粘贴网址,这会触发web钩子。当代码被发布时,它返回一个400错误请求错误

以下代码中的键已替换为伪值

有人知道为什么这不会触发web钩子吗?/您能解释一下服务器拒绝post请求的原因吗?我甚至不需要从服务器读取响应,只要它被发送

多谢各位

// ArduinoHttpClient - Version: Latest 
#include <ArduinoHttpClient.h>


#include "thingProperties.h"


#define LED_PIN 13
#define BTN1 6

char serverAddress[] = "maker.ifttt.com";  // server address
int port = 443;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);


// variables will change:
int btnState = 0;         // variable for reading the pushbutton status
int btnPrevState = 0;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  // setup the board devices
  pinMode(LED_PIN, OUTPUT);
  pinMode(BTN1, INPUT);


}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  // read the state of the pushbutton value:

btnState = digitalRead(BTN1);
if (btnPrevState == 0 && btnState == 1) {
 led2 = !led2;
 postrequest();
}
  digitalWrite(LED_PIN, led2);

btnPrevState = btnState;

}



void onLed1Change() {
  // Do something

   digitalWrite(LED_PIN, led1);
    //Serial.print("The light is ");
    if (led1) {
        Serial.println("The light is ON");
    } else {
    //    Serial.println("OFF");
    }


}

void onLed2Change() {
  // Do something

  digitalWrite(LED_PIN, led2);
}


void postrequest() {
  //    String("POST /trigger/btn1press/with/key/mykeyhere")
 Serial.println("making POST request");
  String contentType = "/trigger/btn1press/with/key";
  String postData = "mykeyhere";

  client.post("/", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);

    }
//ArduinoHttpClient-版本:最新
#包括
#包括“thingProperties.h”
#定义LED_引脚13
#定义BTN1 6
char serverAddress[]=“maker.ifttt.com”;//服务器地址
int端口=443;
WiFiClient-wifi;
HttpClient=HttpClient(wifi、服务器地址、端口);
//变量将发生变化:
int btnState=0;//用于读取按钮状态的变量
int btnPrevState=0;
无效设置(){
//初始化串行端口并等待端口打开:
Serial.begin(9600);
//如果没有找到串行监视器,则此延迟提供了在不阻塞的情况下等待串行监视器的机会
延迟(1500);
//在thingProperties.h中定义
initProperties();
//连接到Arduino物联网云
ArduinoCloud.begin(ArduinoIoPreferredConnection);
/*
以下功能允许您获取更多信息
与网络和物联网云连接状态和错误相关
数字越大,获得的信息越细粒度。
默认值为0(仅错误)。
最大值为4
*/
设置调试消息级别(2);
ArduinoCloud.printDebugInfo();
//设置板设备
引脚模式(LED_引脚,输出);
pinMode(BTN1,输入);
}
void循环(){
uduinocloud.update();
//你的代码在这里
//读取按钮值的状态:
btnState=数字读取(BTN1);
如果(btnPrevState==0&&btnState==1){
led2=!led2;
postrequest();
}
数字写入(LED_引脚,led2);
btnPrevState=btnState;
}
仅作废1更改(){
//做点什么
数字写入(LED_引脚,led1);
//连续打印(“灯是”);
如果(led1){
Serial.println(“灯亮”);
}否则{
//序列号。打印号(“关闭”);
}
}
仅作废2更改(){
//做点什么
数字写入(LED_引脚,led2);
}
void postrequest(){
//字符串(“POST/trigger/btn1press/with/key/mykeyhere”)
序列号。println(“提出邮寄请求”);
字符串contentType=“/trigger/btn1press/with/key”;
字符串postData=“mykeyhere”;
client.post(“/”,contentType,postData);
//阅读响应的状态代码和正文
int statusCode=client.responseStatusCode();
字符串响应=client.responseBody();
串行打印(“状态代码:”);
Serial.println(状态码);
连续打印(“回复:”);
序列号println(应答);
Serial.println(“等待五秒钟”);
延迟(5000);
}

为什么要发出POST请求并在POST正文中发送密钥?浏览器发送GET请求。是的

client.get("/trigger/btn1press/with/key/mykeyhere");

在HttpClient
post()
中,第一个参数是“path”,第二个参数是contentType(例如“text/plain”),第三个参数是HTTP post请求的主体

所以你的
帖子应该是这样的

client.post("/trigger/btn1press/with/key/mykeyhere", contentType, postData);

你不是已经问过这个问题了吗?我删除并重新格式化了这个问题,以便它反映出修改后的代码,以便获得答案/减少混淆。你应该编辑现有的问题,而不是删除并重新提问。嘿,谢谢你的回复,如果我理解你的意思,我应该修改代码来执行GET请求来触发webhook,而不是POST(我对它的工作原理有误解!),如果是这样,我将给出一个go as IFTTT,它只按照你在GET示例中概述的那样格式化webhook字符串,而不使用其他两个参数。或者它在POST示例中仍然可行吗?@user2916488,如果它作为来自浏览器的GET请求工作,那么它应该是一个GET请求。不要被“得到”弄糊涂。GET是一个没有正文的请求,POST是一个带有正文内容的请求,感谢更改仍然给出了400个错误,但是这次很明显服务器端口必须是80而不是443。这两个变化的结合使它工作了,我现在可以从不同的按钮启动多个请求-因此可以通过按钮控制Alexa:)我想我已经为这个答案申请了赏金,但如果不让我知道!