MainGeneratorMonitorAlert_v4 — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 circuit simulation you can run free in your browser on Velxio, by catchrajnishkapur.

Components

Sketch code

/*
  Mains / Generator Monitor — ESP32 (Arduino core)
  --------------------------------------------------
  Wiring assumption (pulled-up inputs):
    - Mains sense pin:     GPIO 19
    - Generator sense pin: GPIO 18
    - OFF  = contact open  -> pin reads HIGH (internal pull-up)
    - ON   = contact closed to GND -> pin reads LOW

  NOTE: GPIO34-39 on ESP32 are input-only and have NO internal pull-up/down.
  If you must use one of those pins, add an external 10k pull-up resistor.

  Design:
    1. Mains pin change -> hardware GPIO interrupt (IRAM_ATTR ISR).
       ISR does the absolute minimum: timestamps the event and sets a flag.
    2. loop() runs a retriggerable software debounce (default 300 ms).
       Once the pin has been stable for the debounce window AND the state
       actually differs from the last confirmed state, it's treated as a
       real transition -> "Mains-ON" / "Mains-OFF" printed immediately.
    3. Independently of Mains, a PERIODIC esp_timer fires every 2 minutes
       (starting at boot, continuing forever) and samples the Generator
       pin (digitalRead) regardless of what Mains is doing.
    4. To avoid printing a combined line every 2 minutes even when nothing
       changed, the periodic check only prints when the Generator's state
       is DIFFERENT from what was last reported. When it does report, it
       prints CURRENT Mains state + Generator state together.
    5. esp_timer callbacks run in the esp_timer service task (not real ISR
       context), so it's safe to do digitalRead()/Serial.println() there.
    6. ALARM: if Mains-ON and Generator-ON hold true continuously for more
       than 4 minutes, an alarm line is printed once. The 4-minute window
       is tracked with a plain millis() timestamp and checked every loop()
       pass (not tied to the 2-minute Generator sampling), and resets the
       moment either Mains or Generator leaves the ON state.
*/

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Werror=unused-value"

#include <WiFi.h>
#include <Arduino.h>
#include <Wire.h>
#include "esp_timer.h"

/*
 * Start of the OTA Drive block
 */

#include <otadrive_esp.h>
// Product Key is used for RK's Generator On Alarm Module Product on OTADrive.com
#define OTA_PRODUCT_KEY "865c9958-976d-465e-801b-f4c33ddd12ea"
// OTA_FIRMWARE_VERSION - will need to be incremented for each update via OTADrive
#define OTA_FIRMWARE_VERSION "[email protected]"
#define OTA_CHECK_INTERVAL 250 // in seconds

uint32_t chipId = 0;
void     otaUpdate();
void     onUpdateProgress(int progress, int totalt);
String   getChipId();

/*
 * End of the OTA Drive block
 */

/*
 * End of Error Display block
 */
 
/*
 * Start of Application Specific Code
 */

/*
 * Start of LCD Display Block
 */
#include <LiquidCrystal_I2C.h>
#include <Ticker.h>

#define LCD_I2C_ADDRESS 0x27
#define LCD_COLUMNS     20
#define LCD_ROWS        4
#define LCD_I2C_ADDRESS 0x27
#define LCD_BLINK_INTERVAL 1 // second

// Level 1: Weakest signal (Just the base/

More projects by catchrajnishkapur