I am stuck with something that seems pretty simple. I have an ESP32 linked to my door bell. When someone rings at the door, I want it to post a message in a telegram group, and to trigger a scenario on my home automation system. These 2 actions are performed via the HTTPClient library, with the GET command.
Here is the code:
- #include <Arduino.h>
- #include "WiFi.h"
- #include <HTTPClient.h>
- // Wifi parameters
- #define WIFI_NETWORK "xxx"
- #define WIFI_PASSWORD "yyy"
- #define WIFI_TIMEOUT_MS 20000
- #define HOSTNAME "esp32DoorBell"
- // Button is on pin 15
- #define BUTTON_PIN 15
- // LED is on pin 18
- #define LED_PIN 18
- HTTPClient http;
- void connectToWifi() {
- Serial.print("Connecting to Wifi");
- WiFi.mode(WIFI_STA);
- WiFi.setSleep(false);
- WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
- WiFi.setHostname(HOSTNAME);
- unsigned long startAttemptTime = millis();
- while (WiFi.status() != WL_CONNECTED && (millis() - startAttemptTime) < WIFI_TIMEOUT_MS) {
- Serial.print(".");
- delay(500);
- }
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println(" failed!");
- } else {
- Serial.print(" connected with IP address ");
- Serial.println(WiFi.localIP());
- }
- }
- void setup() {
- // Init serial connection
- Serial.begin(9600);
- Serial.println("Started");
- // Set button pin in input and LED pin to output
- pinMode(BUTTON_PIN, INPUT);
- pinMode(LED_PIN, OUTPUT);
- // We turn on the LED to indicate that Wifi connection is not active
- digitalWrite(LED_PIN, HIGH);
- // Allow reusing the HTTP connection
- http.setReuse(true);
- // connect to Wifi
- connectToWifi();
- }
- void loop() {
- // Check if Wifi has been disconnected, and reconnect if needed
- while (WiFi.status() != WL_CONNECTED) {
- // Turn on the LED to indicate that Wifi connection has been lost
- digitalWrite(LED_PIN, HIGH);
- Serial.println("Wifi disconnected, attemting to reconnect");
- connectToWifi();
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println("Could not reconnect, wait 10 seconds");
- delay(10000);
- }
- }
- // If we reach this point, it means that Wifi connection is OK, so we turn off the LED
- digitalWrite(LED_PIN, LOW);
- // Read the state of the button
- int PushButtonState = digitalRead(BUTTON_PIN);
- int httpCode;
- String payload;
- if (PushButtonState == HIGH ) {
- Serial.println("Button is on");
- // Call a Jeedom scenario
- http.begin("https://mydomain.com/core/api/jeeApi.php?apikey=xxx&type=scenario&id=3&action=start");
- httpCode = http.GET();
- if (httpCode > 0) {
- payload = http.getString();
- Serial.println(httpCode);
- Serial.println(payload);
- } else {
- Serial.println("Error on HTTP request");
- }
- http.end();
- Serial.println("After call to jeedom");
- // Send a notification via Telegram
- http.begin("https://api.telegram.org/bot1234:theKey/sendMessage?chat_id=-6789&text=test+esp32");
- httpCode = http.GET();
- if (httpCode > 0) {
- payload = http.getString();
- Serial.println(httpCode);
- Serial.println(payload);
- } else {
- Serial.println("Error on HTTP request");
- }
- http.end();
- Serial.println("After call to telegram");
- // Delay of 500 ms to avoid multiple execution
- delay(500);
- }
- delay(25);
- }
Code: Select all
Started
Connecting to Wifi. connected with IP address 192.168.107.10
HTTP server started
Button is on
200
ok
After call to jeedom
404
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>
After call to telegram
If I change the order of the calls, I can successfully post the message on telegram, but the call to my home automation system fails with an HTTP code 302.
What am I doing wrong here?