esp32_client_wifi_client_http_client_rest_api | Last modified: 1744481085 | Edit | Home

Client Wifi


#include <WiFi.h>

void setup() {

  Serial.begin(115200);
  while (!Serial) continue;

  WiFi.begin("SSID", "P4sswOrd");
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.print("IP address:");
  Serial.println(WiFi.localIP());

}

void loop() {

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to Wifi.");
  } else {
    Serial.println("WiFi connection lost.");
  }
  delay(1000);
}

Client HTTP


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

void setup() {

  Serial.begin(115200);
  while (!Serial) continue;

  WiFi.begin("SSID", "P4ssw0rd");

  while (WiFi.status() != WL_CONNECTED) delay(500);

  Serial.println("Connected to Wifi.");

  HTTPClient http;

  Serial.print("Get https://example.com... ");
  http.begin("https://example.com");
  int httpCode = http.GET();

  if (httpCode == HTTP_CODE_OK) {

    Serial.println(http.getString());

  } else {

    Serial.printf("failed, error: %s\n", http.errorToString(httpCode).c_str());

  }

  http.end();
}

void loop() {
}

Client REST API


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

void setup() {

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

  WiFi.begin("SSID", "P4ssw0rd");
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println("Connected.");
}

void loop() {

  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");

    int httpCode = http.GET();

    if (httpCode == HTTP_CODE_OK) {

      String response = http.getString();

      JsonDocument doc;
      DeserializationError error = deserializeJson(doc, response);
      if (error) {
        Serial.print(F("deserializeJson() failed: "));
        Serial.println(error.f_str());
      } else {
        Serial.print("BTC: ");
        Serial.println(doc["result"]["XXBTZEUR"]["c"][0].as<double>());
      }

    } else {

      Serial.printf("failed, error: %i %s\n", httpCode, http.errorToString(httpCode).c_str());

    }

    http.end();

  } else {

    Serial.println("Network unreachable");

  }

  delay(5000);
}