Created
May 13, 2020 20:30
-
-
Save polluxlabs/adc2676bd6ebebbb356a5182082cf0b5 to your computer and use it in GitHub Desktop.
A newsticker with an ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoJson.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
// WLAN-Daten | |
const char* ssid = "WLAN-NETZWERK"; | |
const char* password = "PASSWORT"; | |
void apiCall(){ | |
if ((WiFi.status() == WL_CONNECTED)) { | |
HTTPClient http; | |
http.begin("https://newsapi.org/v2/top-headlines?country=de&apiKey=API_KEY"); | |
int httpCode = http.GET(); | |
if (httpCode == 200) { | |
Serial.println(httpCode); | |
String payload = http.getString(); | |
const size_t capacity = JSON_ARRAY_SIZE(20) + 20*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 20*JSON_OBJECT_SIZE(8) + 19020; | |
DynamicJsonDocument doc(capacity); | |
DeserializationError error = deserializeJson(doc, payload); | |
if (error) { | |
Serial.print(F("deserializeJson() hat nicht funktioniert: ")); | |
Serial.println(error.c_str()); | |
return; | |
} | |
JsonArray articles = doc["articles"]; | |
JsonObject articles_0 = articles[0]; | |
const char* articles_0_title = articles_0["title"]; | |
Serial.println(articles_0_title); | |
} | |
else { | |
Serial.println("Fehlerhafter HTTP-Request"); | |
} | |
http.end(); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Ich verbinde mich mit dem Netzwerk..."); | |
} | |
Serial.println("Ich bin mit dem Netzwerk verbunden!"); | |
} | |
void loop() { | |
apiCall(); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment