ESP32 Code gut?
Hi, ich würde gerne fragen ob dieser code gut ist weil ich kenne mich nicht aus und da kommt immer dieser fehlercode:
'sendDiscordNotification' was not declared in this scope
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "WLanSSID";
const char* password = "Password";
const char* webhook_url = "https://discord.com/api/webhooks/MyWebhook";
const int doorbellPin = 13;
bool doorbellState = LOW;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Verbindung zum WLAN wird hergestellt...");
}
Serial.println("WLAN verbunden!");
pinMode(doorbellPin, INPUT);
}
void loop() {
doorbellState = digitalRead(doorbellPin);
if (doorbellState == HIGH) {
sendDiscordNotification();
delay(5000);
}
delay(100);
}
void sendDiscordNotification() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(webhook_url);
http.addHeader("Content-Type", "application/json");
String payload = "{\"content\": \"Es hat geklingelt!\"}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Fehler bei der HTTP Anfrage: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Verbindung verloren");
}
}
1 Antwort
ralphdieter
bestätigt
Von
Experte
Die Funktion sendDiscordNotification ist erst nach der Verwendung deklariert. Das geht in c nicht.
Du musst die Funktion entweder nach oben schieben, oder "ankündigen", dass diese Funktion existieren wird, indem du `void sendDiscordNotification();` an den Start packst.
Sonst ist der Code okay.
Woher ich das weiß:eigene Erfahrung – Langjähriger Poweruser & praktische Programmiererfahrung