ESP-IDF-HTTP-Webserver minimales ArduinoJson-Serialisierungsbeispiel

esp_http_handler.cpp
static const httpd_uri_t valueHandler = {
    .uri       = "/api/value",
    .method    = HTTP_GET,
    .handler   = [](httpd_req_t *req) {
        httpd_resp_set_type(req, "application/json");
        // JSON-Dokument erstellen
        DynamicJsonDocument json(1024);
        json["value"] = 1.0;
        // Serialize JSON to string
        std::string buf;
        serializeJson(json, buf);
        // Antwort senden
        httpd_resp_send(req, buf.c_str(), buf.length());
        return ESP_OK;
    }
};

Um ArduinoJson zu PlatformIO hinzuzufügen, füge die folgenden lib_deps zu platformio.ini hinzu:

platformio.ini
lib_deps =
    ArduinoJSON@6.17.2

Ähnliche Beiträge nach Kategorie: Arduino ESP8266/ESP32