Search code examples
postarduinoiotifttt

Solve issue POSTING to webhook for IFTTT from Arduino MKR1010


I am aiming to make a post request to trigger a IFTTT webhook action. I am using the MKR1010 board. I am able to connect to the network and turn the connected LED on and off using the cloud integration.

The code is as follows, but doesn't trigger the web hook. I can manually paste the web address in a browser and this does trigger the web hook. When the code is posted it returns a 400 bad request error.

The key has been replaced in the below code with a dummy value.

Does anybody know why this is not triggering the web hook? / Can you explain why the post request is being rejected by the server? I don't even really need to read the response from the server as long as it is sent.

Thank you

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

    }

Solution

  • Why do you want to make a POST request and send the key in the POST body? The browser sends a GET request. It would be

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

    In HttpClient post() the first parameter is 'path', the second parameter is contentType (for example "text/plain") and the third parameter is the body of the HTTP POST request.

    So your post should look like

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