Safe LCD writes — ESP32 simulation

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

Components

Sketch code

#include <Arduino.h>
#include <Ticker.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <time.h>
#include <ESPping.h>
#include <ArduinoOTA.h>
#include "BatteryVoltageHelper.h"
#include "Configurations.h"

// Define the T-Energy-S3 I2C pins
#define I2C_SDA 21//8
#define I2C_SCL 22//9

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

constexpr unsigned long ALARM_THRESHOLD_MS  = 15UL  * 1000UL;//  2 minutes

// Level 1: Weakest signal (Just the base/dot)
// Custom 5x8 pixel bitmaps for signal bars (0 to 4 bars)
byte bar0[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b10000, 0b00000 }; // 1 dot
byte bar1[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01000, 0b11000, 0b00000 }; // 1 bar
byte bar2[8] = { 0b00000, 0b00000, 0b00000, 0b00100, 0b00100, 0b01100, 0b11100, 0b00000 }; // 2 bars
byte bar3[8] = { 0b00000, 0b00010, 0b00010, 0b00110, 0b00110, 0b01110, 0b11110, 0b00000 }; // 3 bars
byte bar4[8] = { 0b00001, 0b00011, 0b00011, 0b00111, 0b00111, 0b01111, 0b11111, 0b00000 }; // 4 bars (full)

#define OTA_FIRMWARE_VERSION "[email protected]"

LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS); 

struct LCDBlinkMessageRowStruct
{
  int col;
  int row;
  const char* message;
} params;

Ticker LCDBlinkMessageTicker;
bool gbshowAlarmMessage = true;
volatile bool gvbshowAlarmMessageFlag = false;
void LCDBlinkMessageRowcallBack(LCDBlinkMessageRowStruct* params);

QueueHandle_t lcdQueue;
// Structure for LCD Queue
struct DisplayMessage 
{
  int col;
  int row;
  int clearline;
  char text[21]; 
};

QueueHandle_t whatsappQueue;
// Structure for WhatsApp Queue
struct WhatsAppMessage 
{
    char text[50];
};

const int MAINS_PIN     = 12;
const int GENERATOR_PIN = 14;
const int ALARM_LED_PIN = 25;
volatile bool gbMains     = 0;
volatile bool gbGenerator = 0;

#define  MAINSOFF_GENSETOFF_MASK 0b00000000
#define  MAINSOFF_GENSETON_MASK  0b00000001
#define  MAINSON_GENSETOFF_MASK  0b00000010
#define  MAINSON_GENSETON_MASK   0b00000011

const    byte STATE_MASK     = 0b00000011;
volatile byte gmainsState    = 0b00000000;
volatile byte ggenState      = 0b00000000;
volatile byte glastState     = 0b00000000;

// Variables changed inside the ISR must be marked as 'volatile'
volatile bool mainsTriggerred     = false;
volatile bool generatorTriggerred = false;
volatile bool bothOnActive        = false;// true while Mains-ON && Generator-ON
volatile bool alarmRaised         = false;// latched so the alarm raised only once per episode
volatile unsigned long bothOnStartTime = 0;// millis() when that condition began

volatile unsigned long lastMainsInterruptTime = 0;
volatile unsigned long lastGeneratorInterruptTime = 0;

// Debounce time window in milliseconds
const unsigned long DEBOUNCE_DELAY = 150; 

/*
   Start of Batter Voltage Percentage Block
*/
#define BATTERY_PIN 35//3 on LilyGo T-Energy
Ba

More projects by catchrajnishkapur