3

I'm trying to make an HTTP request with an ESP8266 and the ESP8266HTTPClient library. I have a request in the loop() function that makes a request every 5 seconds which works 100% flawlessly. However, I also have an interrupt setup like so:

void interrupt() {
  if(WiFiMulti.run() == WL_CONNECTED) {
    Serial.println("Knock!");
    HTTPClient http;

    knockhttp.begin(http_address + "/knock");
    int httpCode = http.GET();

    if(httpCode > 0) {
      Serial.println(http.getString());
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
  }
}

This fails. I get a "connection refused" Error every time the interrupt triggers. It's connecting to the same server as the request in the loop, just a different path.

2
  • What's firing up your interrupt? Commented Jul 30, 2017 at 20:39
  • Oops, sorry. Should've been clearer on that. It's an external interrupt from a digital IO pin that triggers on a falling edge. I've also tried debouncing which didn't help. Commented Jul 30, 2017 at 20:40

1 Answer 1

6

Okay, I fixed it.

For some reason, HTTP requests with the ESP8266HTTPClient library do not work within interrupts, so here's the solution I came up with that worked:

  1. Set some boolean flag in the interrupt.
  2. Check that flag in the main loop
  3. Perform the HTTP request in the main loop when the flag matches
  4. Reset the flag.

Hope this helps anyone else that comes across the same strange problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Happy to find your solution. I just ran into the same issue. This solved it for me :-)
Apparently the interrupt stops the WiFi code execution.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.