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 13:18] mirco.triner |
lehrkraefte:blc:informatik:ffprg2-2020:api [2020/11/06 17:05] (current) mirco.triner |
||
---|---|---|---|
Line 8: | Line 8: | ||
<WRAP info> | <WRAP info> | ||
- | JSON (JavaScript Object Notation) bietet einen einfachen Standard für die strukturierte Kodierung von Daten in Form von menschenlesbarem Text. Dies bietet Vorteile bei einer automatisierten Weiterverarbeitung, | + | Folgendes Video erklärt API in wenigen Minuten: https:// |
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | Bei vielen Web-APIs werden die Daten mit JSON (JavaScript Object Notation) | ||
< | < | ||
- | { name: " | + | {"name":" |
</ | </ | ||
</ | </ | ||
+ | |||
+ | <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); | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | </ | ||