2018-10-11

Esp32 Tageszeitschaltuhr als Arduino Tab.

Zeitschaltuhr.ino

// ****************************************************************
// Sketch Esp32 Zeitschaltuhr Modular(Tab)
// created: Jens Fleischer, 2018-10-11
// last mod: Jens Fleischer, 2018-10-11
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp32, Relais Modul
// GND an GND
// IN an T3 = 15
// VCC an VIN -> je nach verwendeten Esp.. möglich
// Jumper JD-VCC VCC
// alternativ ext. 5V Netzteil verwenden
// Getestet auf: ESP32 NodeMCU-32s
/******************************************************************
  Copyright (c) 2018 Jens Fleischer. All rights reserved.

  This file is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This file is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
*******************************************************************/
// Diese Version von Zeitschaltuhr sollte als Tab eingebunden werden.
// #include <WebServer.h> muss im Haupttab aufgerufen werden
// Die Funktionalität des ESP32 Webservers ist erforderlich.
// Der Lokalzeit Tab ist zum ausführen der Zeitschaltuhr einzubinden.
// Die Funktion "schaltuhr();" muss im Setup aufgerufen werden.
// Zum schalten muss die Funktion "zeitschaltuhr();" im loop(); aufgerufen werden.
/**************************************************************************************/

const uint8_t RelPin = T3;     // Pin für Relais einstellen
const uint8_t item = 0x0A;
struct schaltzeit {
  char schaltZeit[6];
} sz[item];

void schaltuhr() {
  pinMode(RelPin, OUTPUT);
  digitalWrite(RelPin, HIGH);
  File file = SPIFFS.open("/szeit.txt", "r");
  if (file) {
    for (uint8_t i = 0; i < item; i++) {
      file.readBytesUntil('\n', sz[i].schaltZeit, sizeof(sz[i].schaltZeit));
    }
    file.close();
  }
  server.on("/schaltuhr", HTTP_POST, []() {
    if (server.hasArg("sz0")) {
      File file = SPIFFS.open("/szeit.txt", "w");
      if (file) {
        for (uint8_t i = 0; i < server.args(); i++) {
          strcpy (sz[i].schaltZeit, server.arg(i).c_str());
          file.printf("%s\n", server.arg(i).c_str());
        }
        file.close();
      }
      server.send(204, "");
    }
    String temp = "[";
    for (uint8_t i = 0; i < item; i++) {
      if (temp != "[") temp += ',';
      temp += (String)"\"" + sz[i].schaltZeit + "\"";
    }
    temp += "]";
    server.send(200, "application/json", temp);
  });
}

void zeitschaltuhr() {
  static bool relState = HIGH, oldrelState = HIGH;
  char buf[6];
  static unsigned long letzteMillis = 0;
  unsigned long aktuelleMillis = millis();
  if (aktuelleMillis - letzteMillis >= 1000) {
    letzteMillis = aktuelleMillis;
    snprintf(buf, sizeof(buf), "%0.2d:%0.2d", tm.tm_hour, tm.tm_min);
    for (uint8_t i = 0; i < item; i++) {
      if (strcmp(sz[i].schaltZeit, buf) == 0) i % 2 == 0 ? relState = LOW : relState = HIGH;
    }
    if (relState != oldrelState) {
      oldrelState = relState;
      digitalWrite(RelPin, relState);    // Relais umschalten
    }
  }
}

Der Lokalzeit Tab ist zum ausführen der Zeitschaltuhr einzubinden.

Zurück