0

Helping a friend out with an IoT project and I wanted to try having an ESP32-S3 send a JSON-formatted POST request to pushme.win, which is a super-simple Webhook push notification service. (IFTTT wanted to charge me for it). This would be triggered whenever a connected PIR motion sensor detects movement.

Assume that my unique Trigger ID is 101deadbeefcafe133769420. Using the command

curl -X POST https://pushme.win/trigger -H 'Content-Type: application/json' -d '{"triggerId": "101deadbeefcafe133769420", "text": "Hello, World!"}'

works just fine. Problem is when I try to code it with an ESP32, I consistently get a 400 Bad Request error back.

Using Wokwi.com as a simulator, this is the code I have:

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const int motion_sensor = 23;

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* trigger_id = "101deadbeefcafe133769420";
const char* greeting = "Hello, World!";

const char* pushme_url = "https://pushme.win/trigger/";

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

void setup() {
  pinMode(motion_sensor, INPUT);
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  const int motion = digitalRead(motion_sensor);
  Serial.println(motion);
  delay(100);
  if ((millis() - lastTime) > timerDelay) {
    if (WiFi.status() == WL_CONNECTED) {
      if (motion == HIGH) {
        WiFiClient client;
        HTTPClient http;

        http.begin(client, pushme_url);

        http.addHeader("Content-Type", "application/json");

        StaticJsonDocument<200> doc;
        doc["triggerId"] = trigger_id;
        doc["text"] = greeting;

        String request_body;
        serializeJson(doc, request_body);
        int http_response_code = http.POST(request_body);

        Serial.print("HTTP Response code: ");
        Serial.println(http_response_code);
        
        http.end();
      }
    } else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

How would I remedy this?

New contributor
andrew g is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • Turns out I don't need a WiFiClient -.-
    – andrew g
    Commented Sep 3 at 21:00
  • The pushme_url you provided includes an extra trailing slash (/) at the end, which might cause the server to reject the request. Make it const char* pushme_url = "pushme.win/trigger";
    – liaifat85
    Commented yesterday

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.