ESP-IDF-HTTP-Webserver minimales ArduinoJson-Serialisierungsbeispiel

English Deutsch
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");
        // create json docuemnt
        DynamicJsonDocument json(1024);
        json["value"] = 1.0;
        // Serialize JSON to string
        std::string buf;
        serializeJson(json, buf);
        // Send response
        httpd_resp_send(req, buf.c_str(), buf.length());
        return ESP_OK;
    }
};

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

platformio.ini

Check out similar posts by category: Arduino, ESP8266/ESP32