解决了从Arduino MKR1010发布到Webhook for IFTTT的问题

问题描述 投票:-1回答:1

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

代码如下,但不会触发Web挂钩。我可以在浏览器中手动粘贴网址,这确实会触发网址挂钩。发布代码后,它将返回400错误的请求错误。

在下面的代码中,键已用虚拟值替换。

有人知道为什么这不触发网络挂钩吗? /您能解释一下为什么服务器拒绝发布请求吗?只要发送了响应,我什至不需要读取服务器的响应。

谢谢

// 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);

    }
post arduino iot ifttt
1个回答
0
投票

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

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

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

所以您的post应该看起来像

client.post("/trigger/btn1press/with/key/mykeyhere", contentType, postData);
© www.soinside.com 2019 - 2024. All rights reserved.