#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiClientSecure.h>
// WiFi und API-Konfiguration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* weatherAPIKey = "d6530034d4435c642cfe4c23cc163666";
const char* weatherAPIURL = "http://api.openweathermap.org/data/2.5/weather?q=";
// CallMeBot Konfiguration
const char* callMeBotPhoneNumber = "+491781860860"; // Mit eigener Telefonnummer austauschen
const char* callMeBotAPIKey = "8551936"; // Mit eigenem API-Key austauschen
const char* callMeBotAPIURL = "https://api.callmebot.com/whatsapp.php";
// LED-Pins
const int LED_GREEN = 15;
const int LED_YELLOW = 2;
const int LED_RED = 0;
// OneWire und DallasTemperature Konfiguration
const int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// Standort
String location = "Mengen";
bool messageSent = false;
int loopCounter = 0;
void setup() { // Main Setup
Serial.begin(115200);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
connectToWiFi();
sensors.begin();
}
void loop() { // Loop (Dauerwiederholung)
if (WiFi.status() != WL_CONNECTED) { // Wenn WiFi connection fehlgeschlagen
Serial.println("WiFi Verbindung fehlgeschlagen!");
connectToWiFi();
} else {
String weatherData = getWeatherData(location);
if (!weatherData.isEmpty()) {
printWeatherData(weatherData);
loopCounter++;
if (!messageSent && loopCounter >= 2) { // Überprüft ob laufgang 2 und sendet dann an CallMeBot
sendWeatherDataToCallMeBot(weatherData);
messageSent = true;
}
} else {
Serial.println("Fehler: Keine Wetterdaten erhalten");
}
readLocalTemperature();
}
delay(10000);
}
void connectToWiFi() { // Wifi connection
Serial.println("Verbinde mit WiFi...");
WiFi.begin(ssid, password);
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
delay(1000);
Serial.println("Verbindung zum WiFi...");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Verbunden mit WiFi");
} else {
Serial.println("Fehler: Verbindung zum WiFi fehlgeschlagen!");
}
}
String getWeatherData(String location) { //Aufruf der Wetterdaten
HTTPClient http;
String weatherData;
http.begin(weatherAPIURL + location + "&appid=" + weatherAPIKey); // Zusammefnügung OpenWeather API, Ort und API-Key
int httpResponseCode = http.GET();
if (httpResponseCode > 0) { //Wetterdaten aufruf Statusmelung
weatherData = http.getString();
Serial.println("Wetterdaten erfolgreich abgerufen");
} else {
Serial.print("Fehler beim Abrufen der Wetterdaten. HTTP Fehlercode: ");
Serial.println(httpResponseCode);
}
http.end();
return weatherData;
}
void printWeatherData(String weatherData) { // Auslesung der Wetterdaten mit umrechnung (Kelvin in Celsius)
DynamicJsonDocument doc(1024);
deserializeJson(doc, weatherData);
const char* description = doc["weather"][0]["description"];
float tempInKelvin = doc["main"]["temp"];
float tempInCelsius = tempInKelvin - 273.15;
float feels_likeInKelvin = doc["main"]["feels_like"];
float feels_likeInCelsius = feels_likeInKelvin - 273.15;
float humidity = doc["main"]["humidity"];
Serial.println("Aktuelles Wetter für " + location + ":");
Serial.print("Beschreibung: ");
Serial.println(description);
Serial.print("Temperatur: ");
Serial.print(tempInCelsius);
Serial.println(" °C");
Serial.print("Gefühlte Temperatur: ");
Serial.print(feels_likeInCelsius);
Serial.println(" °C");
Serial.print("Luftfeuchtigkeit: ");
Serial.print(humidity);
Serial.println(" %");
// Zeigt wetterstatus mit den LED's mithilfe der Standortangabe an (ließt weatherData aus)
if (String(description).indexOf("clear") >= 0) {
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
} else if (String(description).indexOf("cloud") >= 0 || String(description).indexOf("mist") >= 0) {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
} else if (String(description).indexOf("rain") >= 0 || String(description).indexOf("storm") >= 0 || String(description).indexOf("snow") >= 0) {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, HIGH);
} else {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
}
}
void readLocalTemperature() { // Auslesung Lokaler DS10B20 Temperatur Sensor
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("");
} else {
Serial.print("Lokale Temperatur: ");
Serial.print(tempC);
Serial.println(" °C");
}
}
void sendWeatherDataToCallMeBot(String weatherData) { //Sendet die Daten an CallMeBot
DynamicJsonDocument doc(1024);
deserializeJson(doc, weatherData);
const char* description = doc["weather"][0]["description"];
float tempInKelvin = doc["main"]["temp"];
float tempInCelsius = tempInKelvin - 273.15;
float feels_likeInKelvin = doc["main"]["feels_like"];
float feels_likeInCelsius = feels_likeInKelvin - 273.15;
float humidity = doc["main"]["humidity"];
float localTempC = sensors.getTempCByIndex(0);
String message = "Aktuelles Wetter für " + location + ":\n" +
"Beschreibung: " + String(description) + "\n" +
"Temperatur: " + String(tempInCelsius) + " °C\n" +
"Gefühlte Temperatur: " + String(feels_likeInCelsius) + " °C\n" +
"Luftfeuchtigkeit: " + String(humidity) + " %\n\n" +
"Lokale Temperatur: " + String(localTempC) + " °C";
// Encoded URL
message.replace(" ", "%20");
message.replace("\n", "%0A");
message.replace(":", "%3A");
String url = String(callMeBotAPIURL) + "?phone=" + callMeBotPhoneNumber + "&text=" + message + "&apikey=" + callMeBotAPIKey; // Zusammenstellung der CallMeBot sendungs URL (Besteht aus Nummer, Nachricht und API-Key)
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
http.begin(client, url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Nachricht erfolgreich an CallMeBot API gesendet");
} else {
Serial.print("Fehler beim Senden der Nachricht an CallMeBot API. HTTP Fehlercode: ");
Serial.println(httpResponseCode);
}
http.end();
}
Loading
ds18b20
ds18b20