esp32_client_resp_api_webserver
Differences
This shows you the differences between two versions of the page.
esp32_client_resp_api_webserver [2025/03/19 09:31] – created bruno | esp32_client_resp_api_webserver [2025/03/19 09:48] (current) – bruno | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <code c++> | ||
+ | /* | ||
+ | Get the price of bitcoin from the Kraken API to an ESP32 platform and webserve it | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | WebServer server(80); | ||
+ | |||
+ | void handleRoot() { | ||
+ | server.send(200, | ||
+ | } | ||
+ | |||
+ | void handleNotFound() { | ||
+ | server.send(404, | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | |||
+ | Serial.begin(115200); | ||
+ | while (!Serial) delay(100); | ||
+ | |||
+ | WiFi.begin(" | ||
+ | |||
+ | Serial.print(" | ||
+ | while (WiFi.status() != WL_CONNECTED) delay(500); | ||
+ | Serial.println(" | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | server.on("/", | ||
+ | |||
+ | server.onNotFound(handleNotFound); | ||
+ | |||
+ | server.begin(); | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | |||
+ | /* | ||
+ | freely inspired by: | ||
+ | - BasicHTTPClient.ino from Espressif ESP32 Dev Module | ||
+ | - https:// | ||
+ | */ | ||
+ | | ||
+ | if (WiFi.status() == WL_CONNECTED) { | ||
+ | | ||
+ | HTTPClient http; | ||
+ | |||
+ | // https:// | ||
+ | http.begin(" | ||
+ | Serial.print(" | ||
+ | |||
+ | // 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/ | ||
+ | // | ||
+ | JsonDocument doc; | ||
+ | DeserializationError error = deserializeJson(doc, | ||
+ | if (error) { | ||
+ | // F -> https:// | ||
+ | Serial.print(F(" | ||
+ | Serial.println(error.f_str()); | ||
+ | } else { | ||
+ | Serial.print(" | ||
+ | Serial.println(doc[" | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | Serial.printf(" | ||
+ | } | ||
+ | http.end(); | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | server.handleClient(); | ||
+ | delay(5000); | ||
+ | } | ||
+ | </ |