Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| lehrkraefte:blc:informatik:ffprg2-2020:api [2020/11/04 12:23] – mirco.triner | lehrkraefte:blc:informatik:ffprg2-2020:api [2020/11/06 16:05] (current) – mirco.triner | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== API ====== | ||
| + | |||
| + | Die Abkürzung API steht für Application Programming Interface und bezeichnet eine Programmierschnittstelle. Die Anbindung erfolgt auf Quelltext-Ebene. APIs kommen in vielen Anwendungen zum Einsatz und werden im Webumfeld in Form von Web-APIs genutzt. | ||
| + | |||
| + | <WRAP info> | ||
| + | Eine Programmierschnittstelle dient dazu, Informationen zwischen einer Anwendung und einzelnen Programmteilen standardisiert auszutauschen. Die Übergabe von Daten und Befehlen erfolgt strukturiert nach einer zuvor definierten Syntax. | ||
| + | </ | ||
| + | |||
| + | <WRAP info> | ||
| + | Folgendes Video erklärt API in wenigen Minuten: https:// | ||
| + | </ | ||
| + | |||
| + | <WRAP info> | ||
| + | Bei vielen Web-APIs werden die Daten mit JSON (JavaScript Object Notation) kodiert. JSON bietet einen einfachen Standard für die strukturierte Kodierung von Daten in Form von menschenlesbarem Text. Dies bietet Vorteile bei einer automatisierten Weiterverarbeitung, | ||
| + | < | ||
| + | {" | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <WRAP todo> | ||
| + | Erstellen Sie einen kostenlosen Account auf https:// | ||
| + | </ | ||
| + | |||
| + | <WRAP todo> | ||
| + | Lesen Sie die Seite https:// | ||
| + | |||
| + | * http:// | ||
| + | <hidden Key> | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <WRAP info> | ||
| + | Mithilfe des Assistenten (https:// | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Lösungsvorschlag === | ||
| + | <hidden Lösungsvorschlag> | ||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | const char* ssid = " | ||
| + | const char* password = " | ||
| + | |||
| + | const String endpoint = " | ||
| + | const String key = " | ||
| + | |||
| + | void setup() { | ||
| + | |||
| + | Serial.begin(115200); | ||
| + | |||
| + | WiFi.begin(ssid, | ||
| + | |||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(1000); | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | Serial.println(" | ||
| + | |||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | |||
| + | if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status | ||
| + | |||
| + | HTTPClient http; | ||
| + | |||
| + | http.begin(endpoint + key); //Specify the URL | ||
| + | int httpCode = http.GET(); | ||
| + | |||
| + | if (httpCode > 0) { //Check for the returning code | ||
| + | |||
| + | String payload = http.getString(); | ||
| + | // | ||
| + | // | ||
| + | |||
| + | const size_t capacity = JSON_ARRAY_SIZE(3) + 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(12) + 270; | ||
| + | DynamicJsonDocument doc(capacity); | ||
| + | | ||
| + | deserializeJson(doc, | ||
| + | |||
| + | |||
| + | JsonArray weather = doc[" | ||
| + | | ||
| + | JsonObject main = doc[" | ||
| + | float main_temp = main[" | ||
| + | int main_pressure = main[" | ||
| + | int main_humidity = main[" | ||
| + | float main_temp_min = main[" | ||
| + | float main_temp_max = main[" | ||
| + | | ||
| + | Serial.println(main_temp); | ||
| + | Serial.println(main_pressure); | ||
| + | Serial.println(main_humidity); | ||
| + | |||
| + | | ||
| + | } | ||
| + | |||
| + | else { | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | http.end(); //Free the resources | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | delay(30000); | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | |||