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?
WiFiClient
-.-