Browse Source

add 13 api fetch

master
Jakub Škrabánek 4 weeks ago
parent
commit
b5afc873aa
  1. 5
      13_dadjokez/.gitignore
  2. 10
      13_dadjokez/.vscode/extensions.json
  3. 17
      13_dadjokez/platformio.ini
  4. 76
      13_dadjokez/src/main.cpp
  5. 11
      13_dadjokez/test/README

5
13_dadjokez/.gitignore

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
13_dadjokez/.vscode/extensions.json

@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

17
13_dadjokez/platformio.ini

@ -0,0 +1,17 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
bblanchon/ArduinoJson@^7.3.1 ;^-> ALT 94 (držím celou dobu alt a čísla stisknu na numericke klavesnici a pak pustím alt)

76
13_dadjokez/src/main.cpp

@ -0,0 +1,76 @@
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "12XKM";
const char* password = "ProsimTeNevim";
HTTPClient http;
const char* apiUrl = "https://icanhazdadjoke.com";
const int fetchInterval = 15000; //1000ms=1s
void fetchAndPrintDadJoke() {
Serial.println("---Zacinam stahovat vtip---");
http.begin(apiUrl);
http.addHeader("Accept","application/json");
http.addHeader("User-Agent","ESP32 student project for API testing");
int httpCode = http.GET();
if(httpCode > 0) {
Serial.printf("Server odpověděl HTTP Kodem: %d\n",httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
JsonDocument doc;
DeserializationError error = deserializeJson(doc,payload);
if(error) {
Serial.print("Chyb pri parsovani JSONu:");
Serial.println(error.c_str());
} else {
const char* joke = doc["joke"];
if(joke){
Serial.println("-------JOKE-------");
Serial.println(joke);
Serial.println("--------------");
} else {
Serial.println("Chyba: klič 'joke' nenalezen JSON odpovědi");
Serial.println("Prijaty JSON (pro kontrolu):");
Serial.println(payload);
}
}
} else {
//třeba při 404 nebo 500
Serial.printf("Server vratil chybny HTTP kod: %d\n", httpCode);
String errorPayload = http.getString();
Serial.println("Telo chybove hlasky:");
Serial.println(errorPayload);
}
} else {
Serial.printf("CHYBA pri odesilani: %s\n",http.errorToString(httpCode).c_str());
}
}
void setup() {
Serial.begin(115200);
Serial.println("ESP32 Dad Joke Fetcher");
Serial.print("Pripojovaní k Wifi:");
Serial.println(ssid);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("WiFi připojena");
Serial.print("IP adresa:");
Serial.println(WiFi.localIP());
}
void loop() {
fetchAndPrintDadJoke();
Serial.println("\nCekam" + String(fetchInterval/1000)+ "vterin pred stazenim dalsiho vtipu...");
delay(fetchInterval);
}

11
13_dadjokez/test/README

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
Loading…
Cancel
Save