I'm working with an ESP32 and running a very simple sketch that sends a GET request using HTTPClient. Everything works, WiFi connects, the countdown displays, and the GET request goes through. Here's my code:
//just note, this is totally custom.
#include "arduino_wifi_secrets.h"
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
void setup() {
Serial.begin(115200);
Serial.println("\n\n\n\n");
WiFi.mode(WIFI_STA);
WiFi.begin(SECRET_SSID, SECRET_PASS);
Serial.print("connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("starting in...");
for (uint8_t i = 10; i > 0; i--) {
Serial.printf(" %d...", i);
Serial.flush();
delay(1000);
}
Serial.println();
}
void loop() {
// wait for WiFi connection
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Serial.println("building GET...");
http.begin("http://httpbin.org/get");
http.addHeader("Accept", "*/*");
http.addHeader("Accept-Language", "en-US,en;q=0.9");
http.addHeader("Host", "httpbin.org");
http.addHeader("User-Agent", "ESP32");
Serial.println("sending GET...");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on internet error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("http stat code: %d\n", httpCode);
// file found at server
if (httpCode == 200 || httpCode == 203 || httpCode == 206) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("failed due to an internet error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
Serial.print("retrying in...");
for (uint8_t i = 10; i > 0; i--) {
Serial.printf(" %d...", i);
Serial.flush();
delay(1000);
}
Serial.println();
}
But when I use "http://httpbin.org/get", I always get "204". And when I try "http://3.233.51.125/get", I get a "connection refused" error, even though it worked fine on my browser with no redirects.
The rest of the code works perfectly, WiFi, serial output, countdowns, and retries, all good!
I'll just use the ESP8266 module and see how that goes for now. If that works, maybe I can have the ESP32 communicate with the ESP8266. That would be the most inefficient way to communicate, ever, to an HTTP web server.
I'm just replying as I go, and apparently I got a code of 200 and a successful response on the ESP8266. But that doesn't quite mean the conversation is quite done. I want to see if I could get this to work on the ESP32.
Okay, so I'm just going to use the ESP8266 and connect it to the ESP32 via serial. And the ESP8266 should act as the Wi-Fi module, which should help free up space in the ESP32. And also the ESP8266 I know works for GET requests, and possibly POST requests as well. I don't know why the ESP32 isn't cooperating with me, but it is not. And this is a temporary solution, but not quite the solution. So this topic is still up for discussion. And a big thanks to everyone who participated. (@kenb4)
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address:
and
www.yahoo.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.yahoo.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.yahoo.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.yahoo.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.yahoo.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address:
You also said plain IPs in the URL did not work either (different problem: connection refused). The hostByName code hasn't changed recently. Starts with
int NetworkManager::hostByName(const char *aHostname, IPAddress &aResult) {
static bool hasGlobalV6 = false;
static bool hasGlobalV4 = false;
err_t err = ERR_OK;
const char *servname = "0";
struct addrinfo *res;
aResult = static_cast<uint32_t>(0);
// First check if the host parses as a literal address
if (aResult.fromString(aHostname)) {
return 1;
}
So minor addition to the resolver test
void loop() {
Serial.print("Enter host name or dotted-IP-address: ");
while (Serial.available() <= 0);
String host = Serial.readStringUntil('\n');
host.trim();
Serial.println(host);
IPAddress srv {};
if (srv.fromString(host)) {
Serial.print("literal address: "); Serial.println(srv);
}
if (!WiFi.hostByName(host.c_str(), srv)) {
Serial.println("Lookup failed");
return;
}
Serial.print("resolved: "); Serial.println(srv);
}
And as expected
Enter host name or dotted-IP-address: httpbin.org
resolved: 3.229.116.23
Enter host name or dotted-IP-address: 1.2.3.4
literal address: 1.2.3.4
resolved: 1.2.3.4
Enter host name or dotted-IP-address: 3.233.51.125
literal address: 3.233.51.125
resolved: 3.233.51.125
Enter host name or dotted-IP-address: www.google.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: httpbin.org
resolved: 18.208.113.193
Enter host name or dotted-IP-address: www.yahoo.com
resolved: 18.208.113.193
Enter host name or dotted-IP-address: