esp32_client_rest_api_webserver_dual_core | Last modified: 1751441923 | Edit | Home

#include <WiFi.h>
#include <HTTPClient.h>
#include <WebServer.h>
#include <ArduinoJson.h>

TaskHandle_t request_btc;

WebServer server(80);
float btc;

void handleRoot() {
  String html = "<!DOCTYPE html>";
  html += "<html>\n";
  html += "<head>\n";
  html += "<title>Server ESP32</title>\n";
  html += "</head>\n";
  html += "<body>\n";
  html += "<p>" + String(btc) + "</p>";
  html += "</body>\n";
  html += "</html>";
  server.send(200, "text/html", html);
}

void handleNotFound() {
  server.send(404, "text/plain", "File Not Found");
}

void setup() {

  Serial.begin(115200);
  while (!Serial) delay(100);

  WiFi.begin("ssid", "passwd");

  Serial.print("Connecting to WiFi... ");
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println("Ok.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");

  // Create a task that will be executed in the Task1code() function
  // with priority 1 and executed on core 0

  xTaskCreatePinnedToCore(
    request_btc,   /* Task function. */
    "Task1",     /* name of task. */
    10000,       /* Stack size of task */
    NULL,        /* parameter of the task */
    1,           /* priority of the task */
    &Task1,      /* Task handle to keep track of created task */
    0);          /* pin task to core 0 */
}

void request_btc(void *pvParameters) {

  for(;;) {
    if (WiFi.status() == WL_CONNECTED) {

      HTTPClient http;

      // https://docs.kraken.com/api/docs/rest-api/get-ticker-information
      http.begin("https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR");
      Serial.print("Send request... ");

      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {

        Serial.println(httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK) {
          String response = http.getString();
          Serial.println(response);

          // Use arduinojson.org/v6/assistant to compute the capacity.
          //DynamicJsonDocument doc(2048);
          JsonDocument doc;
          DeserializationError error = deserializeJson(doc, response);
          if (error) {
            // F -> https://www.locoduino.org/spip.php?article131 [FR]
            Serial.print(F("deserializeJson() failed: "));
            Serial.println(error.f_str());
          } else {
            btc = doc["result"]["XXBTZEUR"]["c"][0].as<float>();
            Serial.print("BTC: ");
            Serial.println(btc);
          }
        }
      } else {
        Serial.printf("failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
      http.end();
    } else {
      Serial.println("Network unreachable");
    }
    delay(5000);
  }
}

void loop() {

  server.handleClient();
  delay(1);

}