#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* apiEndpoint = "http://api.openweathermap.org/data/2.5/weather?q=munich&appid=1e3c1094c33839191f343c88bf847cbc&units=metric";
const int buttonPin = 2; // GPIO2 als Eingang für den Taster
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
pinMode(buttonPin, INPUT_PULLUP); // Initialisiere den Taster-Pin als Eingang mit internem Pull-up-Widerstand
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Bereit");
display.display();
}
void loop() {
int buttonState = digitalRead(buttonPin);
if(buttonState == LOW) { // Wenn der Taster gedrückt ist (niedrige Logikstufe)
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(apiEndpoint);
int httpCode = http.GET();
if(httpCode > 0){
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
float temp = doc["main"]["temp"];
String weather = doc["weather"][0]["main"];
display.clearDisplay();
display.setCursor(0,0);
display.println("Temp: " + String(temp) + "C");
display.println("Wetter: " + weather);
display.display();
} else {
Serial.println("Error on HTTP request");
}
http.end();
} else {
display.clearDisplay();
display.setCursor(0,0);
display.println("WLAN nicht verbunden");
display.display();
}
delay(500); // Entprellverzögerung
while(digitalRead(buttonPin) == LOW); // Warte, bis der Taster losgelassen wird
}
}