I have a Ruby on Rails API with CORS configured, and I can successfully test the endpoint using Postman, receiving JSON responses as expected. I need to perform a GET request on my ESP32, so I created a tunnel using NGROK. I am calling my endpoint at https://4bbc-2804-d59-aa99-7300-608c-83d3-efdf-302c.ngrok-free.app/api/v1/information_locker?serial=Z2CGPYV5OW. However, every time I attempt to make this GET request, I encounter error -1 in the serial output of my ESP32.
I'm looking for guidance on where I might be going wrong. Are there alternative methods for creating a tunnel for my application, or other software solutions that could help establish a successful connection? Any insights would be greatly appreciated!
#include <WiFiManager.h>
#include <HTTPClient.h>
const char* catURL = "https://catfact.ninja/fact"; // URL para testar a conexão com o Google
const char* apiURL = "https://4bbc-2804-d59-aa99-7300-608c-83d3-efdf-302c.ngrok-free.app/api/v1/information_locker?serial=Z2CGPYV5OW"; // URL da sua API
void setup() {
Serial.begin(2400);
// Inicializa o WiFiManager
WiFiManager wifiManager;
wifiManager.autoConnect("ESP32"); // Conecta automaticamente com o nome "ESP32"
Serial.println("Conectado ao WiFi!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { // Verifica se está conectado ao WiFi
HTTPClient http;
// Primeira requisição GET (Google)
Serial.println("Iniciando requisição GET para o Cat Fact...");
http.begin(catURL); // Inicializa a requisição com a URL do Google
Serial.println(catURL);
int httpCode = http.GET(); // Realiza a requisição GET
if (httpCode > 0) { // Verifica se a requisição foi bem-sucedida
String payload = http.getString(); // Obtém a resposta como string
Serial.println("Código HTTP: " + String(httpCode));
Serial.println("Resposta (Cat Fact): " + payload);
} else {
Serial.println("Erro ao fazer GET para o Cat Fact: " + String(httpCode));
}
http.end();
delay(5000);
Serial.println("Iniciando requisição GET para a API do ngrok...");
Serial.println(apiURL);
http.begin(apiURL);
int httpCode2 = http.GET();
if (httpCode2 > 0) {
String payload2 = http.getString();
Serial.println("Código HTTP: " + String(httpCode2));
Serial.println("Resposta (dados da API): " + payload2);
} else {
Serial.println("Erro ao fazer GET para a API: " + String(httpCode2));
}
http.end();
delay(10000);
} else {
Serial.println("WiFi não conectado. Tentando reconectar...");
}
}
Serial ESP 32 Return:
Iniciando requisição GET para o Cat Fact...
"https://catfact.ninja/fact"
Código HTTP: 200
Resposta (Cat Fact): {"fact":"Cats' hearing is much more sensitive than humans and dogs.","length":58}
Iniciando requisição GET para a API do ngrok...
https://4bbc-2804-d59-aa99-7300-608c-83d3-efdf-302c.ngrok-free.app/api/v1/information_locker?serial=Z2CGPYV5OW
Erro ao fazer GET para a API: -1
Log NGROK:
Setting NGROK:
Test Code in Wokwi
Test NGROK: esp32 http GET NGROK.ino - Wokwi ESP32, STM32, Arduino Simulator
Test CATS: esp32 http GET CATS.ino - Wokwi ESP32, STM32, Arduino Simulator


