Программы на C++. Форум разработчиков
Anonymous
Как получить запрос API с помощью ESP32
Сообщение
Anonymous » 10 июл 2025, 10:07
Я разрабатываю сценарий, в котором ESP32 общается с API, но я не могу преобразовать его в JSON. Есть ли способ сделать это? Я искал весь день, но не нашел никаких результатов.
Код: Выделить всё
#include
#include
#include
// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// API URL (returns JSON)
const char* apiUrl = "url-api";
void setup() {
Serial.begin(115200);
delay(1000);
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(700);
Serial.print("=");
}
Serial.println("\nWi-Fi connected successfully!");
}
void loop() {
// Check if connected before making the request
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiUrl);
int responseCode = http.GET();
if (responseCode > 0) {
// If request was successful, get the response
String response = http.getString();
// Create a JSON document
StaticJsonDocument doc;
// Try to parse the JSON
DeserializationError error = deserializeJson(doc, response);
if (!error) {
// Access data from the parsed JSON
bool led2Status = doc["status"];
Serial.print(led2Status);
} else {
Serial.print("Failed to parse JSON: ");
}
} else {
Serial.print("HTTP Request failed. Code: ");
};
}
}
Большинство результатов с ESP32 не произошли, как и ожидалось, они не пришли с форматом JSON, чтобы иметь возможность обрабатывать данные>
Подробнее здесь:
https://stackoverflow.com/questions/796 ... sing-esp32
1752131246
Anonymous
Я разрабатываю сценарий, в котором ESP32 общается с API, но я не могу преобразовать его в JSON. Есть ли способ сделать это? Я искал весь день, но не нашел никаких результатов.[code]#include #include #include // Wi-Fi credentials const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; // API URL (returns JSON) const char* apiUrl = "url-api"; void setup() { Serial.begin(115200); delay(1000); Serial.print("Connecting to Wi-Fi"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(700); Serial.print("="); } Serial.println("\nWi-Fi connected successfully!"); } void loop() { // Check if connected before making the request if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(apiUrl); int responseCode = http.GET(); if (responseCode > 0) { // If request was successful, get the response String response = http.getString(); // Create a JSON document StaticJsonDocument doc; // Try to parse the JSON DeserializationError error = deserializeJson(doc, response); if (!error) { // Access data from the parsed JSON bool led2Status = doc["status"]; Serial.print(led2Status); } else { Serial.print("Failed to parse JSON: "); } } else { Serial.print("HTTP Request failed. Code: "); }; } } [/code] Большинство результатов с ESP32 не произошли, как и ожидалось, они не пришли с форматом JSON, чтобы иметь возможность обрабатывать данные> Подробнее здесь: [url]https://stackoverflow.com/questions/79696576/how-to-get-api-request-using-esp32[/url]