Shelly 1 Plus mit eigenem Code geflasht erkennt die Schalter betätigung nicht.

  • Habe einen Shelly 1 Plus an 24V mit eigenem Code mit VSC und PlatformIO geflasht, was auch gut geklappt hat, leider wird der Schalter auf Sw nicht erkannt.

    Jetzt meine Frage: was habe ich falsch gemacht, laut Datenblatt vom SHelly 1 Plus sind es die richtigen Pins, zeihe auch Pin 4 auf GND.

    Falls noch weitere Infos zu klärung gebraucht werde kann ich gerne nachreichen

    Hier mein Code:

    Main.cpp:

    #include <WiFi.h>
    #include <WebServer.h>
    #include <WebSocketsServer.h>

    const char* ssid = "shellyAP";
    const char* password = "12345678";

    WebServer server(80);
    WebSocketsServer webSocket = WebSocketsServer(81);

    const int relayPin = 26; // GPIO26 für das Relais
    const int buttonPin = 4; // GPIO4 für den Schalter

    int threshold = 5;
    int counter = 0;
    int relayOnTimeSeconds = 420; // Zeit in Sekunden, die das Relais eingeschaltet bleibt (initial 7 Minuten)
    bool buttonPressed = false;
    bool lastButtonState = HIGH;
    const unsigned long debounceDelay = 50; // Millisekunden
    unsigned long lastDebounceTime = 0;

    void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
    String msg = (char*)data;
    if (msg == "getStatus") {
    String status = "Zählerstand: " + String(counter);
    webSocket.broadcastTXT(status);
    }
    }

    void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
    switch (type) {
    case WStype_TEXT:
    handleWebSocketMessage(num, payload, length);
    break;
    default:
    break;
    }
    }

    void handleRoot() {
    int relayOnTimeMinutes = relayOnTimeSeconds / 60;
    String html = "<!DOCTYPE html><html><head>";
    html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
    html += "<style>";
    html += "body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }";
    html += "form { max-width: 400px; margin: auto; padding: 20px; border: 1px solid #ccc; border-radius: 10px; }";
    html += "input[type='number'] { width: 100%; padding: 10px; margin: 5px 0 20px 0; border: 1px solid #ccc; border-radius: 5px; }";
    html += "input[type='submit'] { width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; }";
    html += "input[type='submit']:hover { background-color: #45a049; }";
    html += ".counter { text-align: center; margin-top: 20px; font-size: 20px; }";
    html += ".log { text-align: center; margin-top: 20px; font-size: 14px; white-space: pre-wrap; background-color: #f4f4f4; padding: 10px; border-radius: 5px; border: 1px solid #ccc; }";
    html += "</style>";
    html += "<script>";
    html += "var socket = new WebSocket('ws://' + window.location.hostname + ':81/');";
    html += "socket.onmessage = function(event) { document.getElementById('log').innerText += event.data + '\\n'; };";
    html += "function getStatus() { socket.send('getStatus'); }";
    html += "setInterval(getStatus, 1000);";
    html += "</script>";
    html += "</head><body>";
    html += "<h2>Konfiguration</h2>";
    html += "<form action=\"/set\" method=\"POST\">";
    html += "<label for=\"threshold\">Bei:</label>";
    html += "<input type=\"number\" id=\"threshold\" name=\"threshold\" value=\"" + String(threshold) + "\"><br>";
    html += "<label for=\"relay_on_time_minutes\">Einschaltzeit (Minuten):</label>";
    html += "<input type=\"number\" id=\"relay_on_time_minutes\" name=\"relay_on_time_minutes\" value=\"" + String(relayOnTimeMinutes) + "\"><br>";
    html += "<input type=\"submit\" value=\"Einstellung uebernehmen\">";
    html += "</form>";
    html += "<div class=\"counter\">Zaehlerstand: " + String(counter) + "</div>";
    html += "<div id=\"log\" class=\"log\"></div>";
    html += "</body></html>";
    server.send(200, "text/html", html);
    }

    void handleSet() {
    if (server.hasArg("threshold")) {
    threshold = server.arg("threshold").toInt();
    }
    if (server.hasArg("relay_on_time_minutes")) {
    relayOnTimeSeconds = server.arg("relay_on_time_minutes").toInt() * 60;
    }
    server.sendHeader("Location", "/");
    server.send(303);
    }

    void setup() {
    Serial.begin(115200);
    Serial.println("Setup started");

    pinMode(relayPin, OUTPUT);
    pinMode(buttonPin, INPUT_PULLUP);

    WiFi.softAP(ssid, password);

    server.on("/", handleRoot);
    server.on("/set", HTTP_POST, handleSet);
    server.begin();

    webSocket.begin();
    webSocket.onEvent(onWebSocketEvent);

    Serial.println("Setup completed");
    }

    void loop() {
    server.handleClient();
    webSocket.loop();

    int reading = digitalRead(buttonPin);

    if (reading != lastButtonState) {
    lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading == LOW && !buttonPressed) {
    buttonPressed = true;
    } else if (reading == HIGH && buttonPressed) {
    buttonPressed = false;
    counter++;
    Serial.println("Button pressed, counter: " + String(counter));
    webSocket.broadcastTXT("Button pressed, counter: " + String(counter));
    if (counter >= threshold) {
    digitalWrite(relayPin, HIGH);
    Serial.println("Relay turned on");
    webSocket.broadcastTXT("Relay turned on");
    delay(relayOnTimeSeconds * 1000);
    digitalWrite(relayPin, LOW);
    Serial.println("Relay turned off");
    webSocket.broadcastTXT("Relay turned off");
    counter = 0;
    }
    }
    }

    lastButtonState = reading;
    }

    Hier PlatformIO.ini:

    [env:esp32dev]

    platform = espressif32

    board = esp32dev

    framework = arduino

    lib_deps =

    arduinoWebSockets

    HTTPClient

    links2004/WebSockets@^2.4.1

    monitor_speed = 115200Bitte

    BBitte

    B

  • Dieses Thema enthält 33 weitere Beiträge, die nur für registrierte Benutzer sichtbar sind, bitte registrieren Sie sich oder melden Sie sich an um diese lesen zu können.