HTTPClient and successive GET to different servers

Patrick54
Posts: 9
Joined: Tue Jan 05, 2021 9:05 am

HTTPClient and successive GET to different servers

Postby Patrick54 » Wed Aug 04, 2021 9:28 am

Hi,

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:
  1. #include <Arduino.h>
  2. #include "WiFi.h"
  3. #include <HTTPClient.h>
  4.  
  5. // Wifi parameters
  6. #define WIFI_NETWORK "xxx"
  7. #define WIFI_PASSWORD "yyy"
  8. #define WIFI_TIMEOUT_MS 20000
  9. #define HOSTNAME "esp32DoorBell"
  10.  
  11. // Button is on pin 15
  12. #define BUTTON_PIN 15
  13.  
  14. // LED is on pin 18
  15. #define LED_PIN 18
  16.  
  17. HTTPClient http;
  18.  
  19. void connectToWifi() {
  20.   Serial.print("Connecting to Wifi");
  21.   WiFi.mode(WIFI_STA);
  22.   WiFi.setSleep(false);
  23.   WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
  24.   WiFi.setHostname(HOSTNAME);
  25.  
  26.   unsigned long startAttemptTime = millis();
  27.  
  28.   while (WiFi.status() != WL_CONNECTED && (millis() - startAttemptTime) < WIFI_TIMEOUT_MS) {
  29.     Serial.print(".");
  30.     delay(500);
  31.   }
  32.  
  33.   if (WiFi.status() != WL_CONNECTED) {
  34.     Serial.println(" failed!");
  35.   } else {
  36.     Serial.print(" connected with IP address ");
  37.     Serial.println(WiFi.localIP());
  38.   }
  39.  
  40. }
  41.  
  42.  
  43. void setup() {
  44.   // Init serial connection
  45.   Serial.begin(9600);
  46.   Serial.println("Started");
  47.  
  48.   // Set button pin in input and LED pin to output
  49.   pinMode(BUTTON_PIN, INPUT);
  50.   pinMode(LED_PIN, OUTPUT);
  51.  
  52.   // We turn on the LED to indicate that Wifi connection is not active
  53.   digitalWrite(LED_PIN, HIGH);
  54.  
  55.   // Allow reusing the HTTP connection
  56.   http.setReuse(true);
  57.  
  58.   // connect to Wifi
  59.   connectToWifi();
  60.  
  61. }
  62.  
  63. void loop() {
  64.  
  65.   // Check if Wifi has been disconnected, and reconnect if needed
  66.  
  67.   while (WiFi.status() != WL_CONNECTED) {
  68.  
  69.     // Turn on the LED to indicate that Wifi connection has been lost
  70.     digitalWrite(LED_PIN, HIGH);
  71.  
  72.     Serial.println("Wifi disconnected, attemting to reconnect");
  73.     connectToWifi();
  74.     if (WiFi.status() != WL_CONNECTED) {
  75.       Serial.println("Could not reconnect, wait 10 seconds");
  76.       delay(10000);
  77.     }
  78.   }
  79.  
  80.   // If we reach this point, it means that Wifi connection is OK, so we turn off the LED
  81.   digitalWrite(LED_PIN, LOW);
  82.  
  83.   // Read the state of the button
  84.   int PushButtonState = digitalRead(BUTTON_PIN);
  85.   int httpCode;
  86.   String payload;
  87.  
  88.   if (PushButtonState == HIGH ) {
  89.  
  90.     Serial.println("Button is on");
  91.  
  92.     // Call a Jeedom scenario
  93.  
  94.     http.begin("https://mydomain.com/core/api/jeeApi.php?apikey=xxx&type=scenario&id=3&action=start");
  95.     httpCode = http.GET();
  96.  
  97.     if (httpCode > 0) {
  98.  
  99.         payload = http.getString();
  100.        
  101.         Serial.println(httpCode);
  102.         Serial.println(payload);
  103.     } else {
  104.       Serial.println("Error on HTTP request");
  105.     }
  106.    
  107.    
  108.     http.end();
  109.  
  110.     Serial.println("After call to jeedom");
  111.  
  112.     // Send a notification via Telegram
  113.  
  114.     http.begin("https://api.telegram.org/bot1234:theKey/sendMessage?chat_id=-6789&text=test+esp32");
  115.     httpCode = http.GET();
  116.  
  117.     if (httpCode > 0) {
  118.  
  119.         payload = http.getString();
  120.         Serial.println(httpCode);
  121.         Serial.println(payload);
  122.     } else {
  123.       Serial.println("Error on HTTP request");
  124.     }
  125.  
  126.     http.end();
  127.  
  128.     Serial.println("After call to telegram");
  129.  
  130.     // Delay of 500 ms to avoid multiple execution
  131.  
  132.     delay(500);
  133.   }
  134.  
  135.   delay(25);
  136. }
  137.  
When the door bell is triggered, I get this output:

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
We can see that the call to my home automation system is working fine, but the second call to telegram fails.
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?

Patrick54
Posts: 9
Joined: Tue Jan 05, 2021 9:05 am

Re: HTTPClient and successive GET to different servers

Postby Patrick54 » Wed Aug 04, 2021 2:01 pm

I have found a workaround, by creating 2 distinct HTTPClient variables. But I still would like to understand what is happening 🙂

Who is online

Users browsing this forum: No registered users and 74 guests