ESP32-automatic-timer-switch — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by kshyhoo.

Components

Sketch code

/*===============================================================================
 *  ESP32S3 16-Channel Automatic Relay Timer Switch w/ Full MQTT 
 *  Author: Raff Alds
 *  Github: https://www.github.com/xiv3r
 *  License: GPLv3
===============================================================================*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <Preferences.h>
#include <ArduinoJson.h>
#include <ESPmDNS.h>
#include <Wire.h>
#include <RTClib.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <sys/time.h>
#include <strings.h>
//  Preferences (NVS)
Preferences preferences;
#define NVS_NAMESPACE "relay16"
#define EEPROM_MAGIC 0x1234
#define EEPROM_VERSION 14
#define EXT_CFG_MAGIC 0xEC
#define RELAY_CONFIG_MAGIC 0x1CE5
//  Year 2106+ Support
#define MIN_UNIX_TIME_64 1577836800ULL
#define MAX_UNIX_TIME_64 4294967295ULL
#define VALID_UNIX_TIME_64(epoch) ((epoch) >= MIN_UNIX_TIME_64 && (epoch) <= MAX_UNIX_TIME_64)
//  Day-of-Week / Month-of-Year Masks
#define DAY_ALL 0x7F
#define MONTH_ALL 0x0FFF
//  Timing Constants
static const unsigned long NTP_RETRY_INTERVAL = 30000UL;
static const unsigned long WIFI_CHECK_INTERVAL = 10000UL;
static const unsigned long WIFI_CONNECT_TIMEOUT = 20000UL;
static const unsigned long SCHEDULE_PROCESS_INTERVAL = 250UL;
static const unsigned long RTC_REBASE_INTERVAL = 300000UL;
static const unsigned long DS3231_SYNC_INTERVAL = 3600000UL;
//  DST Cache Timing
static const unsigned long DST_CACHE_TTL_SECONDS = 5ULL;
static const unsigned long DST_CACHE_INVALIDATE_INTERVAL = 5000UL;
//  MQTT Constants
#define MQTT_CONFIG_MAGIC 0x4D51
#define MQTT_CONFIG_VERSION 1
#define MQTT_BUFFER_SIZE 2048
#define MQTT_KEEPALIVE_SEC 30
#define MQTT_MAX_CA_LEN 4096
#define MQTT_MAX_CMD_LEN 512
static const unsigned long MQTT_RECONNECT_MIN = 5000UL;
static const unsigned long MQTT_RECONNECT_MAX = 300000UL;
static const unsigned long MQTT_PUBLISH_POLL = 250UL;
static const unsigned long MQTT_DISCOVERY_REFRESH = 300000UL;
static const unsigned long MQTT_DISCOVERY_SETTLE = 3000UL;
//  NTP Async State Machine Constants
#define NTP_STATE_IDLE 0
#define NTP_STATE_CONNECTING 1
//  64-bit NTP Constants
#define NTP_PACKET_SIZE 48
#define NTP_PORT 123
#define NTP_TIMEOUT 3000
#define NTP_EPOCH_OFFSET 2208988800ULL
//  Memory Management
static const unsigned long MEMORY_CLEANUP_INTERVAL = 30000UL;
//  Boot Button Factory Reset
#define BOOT_BUTTON_PIN 0
#define FACTORY_RESET_HOLD 5000
static unsigned long bootButtonPressStart = 0;
static bool bootButtonPressed = false;
static bool factoryResetTriggered = false;
//  mDNS Settings
#define MDNS_HOSTNAME_DEFAULT "esp32s3"
//  NTP Fallback Pool
static const char* NTP_SERVERS[] = {
  "time.google.com",
  "time.windows.com",
  "time.cloudflare.com",
  "time.facebook.com"
};
static const uint8_t NUM_NTP_SERVERS = 4;
//  Time Source Tracking
#define TIME_SOURCE_NONE 0
#define TIME_SOURCE_NTP 1
#define TIME_SOURCE_BROWSER 2
#define TIME_SOURC

More projects by kshyhoo