Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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> <WRAP info> Folgendes Video erklärt API in wenigen Minuten: https://www.youtube.com/watch?v=G2sWDHz3gmk </WRAP> <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, macht sie aber auch einer manuellen Inspektion und Überarbeitung besser zugänglich. Ein Beispiel eines JSON-Files sieht wie folgt aus: <code> {"name":"John", "age":31, "city":"New York"} </code> </WRAP> <WRAP todo> Erstellen Sie einen kostenlosen Account auf https://openweathermap.org </WRAP> <WRAP todo> Lesen Sie die Seite https://randomnerdtutorials.com/decoding-and-encoding-json-with-arduino-or-esp8266/ durch und probieren Sie anschliessend die aktuellen Wetterdaten für Zürich zu holen. ACHTUNG: Das vorige Tutorial verwendet teilweise die alte Bibliothek mit ArduinoJson5. Folgende Seite hat ein Beispiel zur aktuellen Version: https://arduinojson.org/v6/example/parser/ * http://api.openweathermap.org/data/2.5/weather?q=Zurich&APPID={your API Key} <hidden Key> </hidden> </WRAP> <WRAP info> Mithilfe des Assistenten (https://arduinojson.org/v6/assistant/) kann der benötigte Speicherplatz zum Parsen eines Textes berechnet werden. Das Festlegen des Speicherplatzes durch den Benutzer erlaubt eine schnellere und ressourcenschonendere Ausführung. (https://arduinojson.org/v6/how-to/determine-the-capacity-of-the-jsondocument/) </WRAP> === Lösungsvorschlag === <hidden Lösungsvorschlag> <code> #include <WiFi.h> #include <HTTPClient.h> #include "ArduinoJson.h" const char* ssid = "SSID"; const char* password = "PASSWORT"; const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=Zurich&units=metric&APPID="; const String key = "API_KEY"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } Serial.println("Connected to the WiFi network"); } 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(); //Make the request if (httpCode > 0) { //Check for the returning code String payload = http.getString(); //Serial.println(httpCode); //Serial.println(payload); 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, payload); JsonArray weather = doc["weather"]; JsonObject main = doc["main"]; float main_temp = main["temp"]; // 281.87 int main_pressure = main["pressure"]; // 1032 int main_humidity = main["humidity"]; // 100 float main_temp_min = main["temp_min"]; // 281.15 float main_temp_max = main["temp_max"]; // 283.15 Serial.println(main_temp); Serial.println(main_pressure); Serial.println(main_humidity); } else { Serial.println("Error on HTTP request"); } http.end(); //Free the resources } delay(30000); } </code> </hidden> lehrkraefte/blc/informatik/ffprg2-2020/api.txt Last modified: 2020/11/06 17:05by mirco.triner