#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned long previousMillis = 0; // Store the previous time
const long interval = 4000; // Interval between updates in milliseconds
static const unsigned char PROGMEM image_operation_warning_bits[] = {0x01,0x80,0x02,0x40,0x02,0x40,0x04,0x20,0x09,0x90,0x09,0x90,0x11,0x88,0x11,0x88,0x21,0x84,0x40,0x02,0x41,0x82,0x81,0x81,0x80,0x01,0x7f,0xfe};
static const unsigned char PROGMEM image_device_power_button_bits[] = {0x01,0x00,0x01,0x00,0x19,0x30,0x25,0x48,0x49,0x24,0x51,0x14,0xa1,0x0a,0xa0,0x0a,0xa0,0x0a,0xa0,0x0a,0xa0,0x0a,0x50,0x14,0x48,0x24,0x27,0xc8,0x18,0x30,0x07,0xc0};
float Current;
int Voltage;
int Relay1Status; // Example relay status (1 means ON, 0 means OFF)
void DisplayVoltage() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setTextSize(2);
display.setTextWrap(false);
display.setCursor(19, 8);
display.print("VOLTAGE:");
display.setTextSize(3);
display.setCursor(25, 30);
display.print(Voltage);
display.setCursor(89, 32);
display.print("V");
display.display();
}
void DisplayCurrent() {
display.clearDisplay();
display.setTextSize(2);
display.setTextWrap(false);
display.setCursor(16, 7);
display.print("CURRENT:");
display.setTextSize(3);
display.setCursor(13, 30);
display.print(Current);
display.setCursor(92, 28);
display.print("A");
display.display();
}
void DisplayOFF() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.drawBitmap(26, 11, image_operation_warning_bits, 16, 14, SSD1306_WHITE);
display.drawBitmap(91, 32, image_device_power_button_bits, 15, 16, SSD1306_WHITE);
display.setTextSize(1);
display.setTextWrap(false);
display.setCursor(55, 16);
display.print("System");
display.setCursor(28, 36);
display.print("Power OFF");
display.display();
}
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) ; // Don't proceed, loop forever
}
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last time we updated the display
// Toggle between displaying voltage, current, or OFF state every 4 seconds
static int displayState = 0; // 0: voltage, 1: current, 2: OFF
if (displayState == 0) {
DisplayVoltage();
} else if (displayState == 1) {
DisplayCurrent();
} else if (displayState == 2 && Relay1Status == 0) { // Check if Relay1Status is 0
DisplayOFF();
}
// Increment displayState and reset to 0 after reaching 2
displayState = (displayState + 1) % 3;
}
// Update Voltage and Current values for testing (replace with actual readings)
Voltage = random(100, 250);
Current = random(1.0, 4.9);
Relay1Status = random(0, 2);
}
//------------------------------------------------------
/*
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Preferences.h>
#include <ZMPT101B.h>
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
EnergyMonitor emon2;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int CT_pin1 = 33; // set the pin for the current transformer sensor
const int CT_pin2 = 32;
const int VoltageSensor_pin = 35;
// Constants for Current sensor calibration
const float currentCalibration = 4.7;
// Voltage sensor sensitivity for calibration
#define SENSITIVITY 296.0f
// ZMPT101B sensor output connected to analog pin
// and the voltage source frequency is 50 Hz.
ZMPT101B voltageSensor(VoltageSensor_pin, 50.0);
const float V_in = 3.3; // set the input voltage for the ADC
//Relay Pins
#define RelayPin1 23
float setcurrent = 4.0;
float sensor1current = 0;
float sensor2current = 0;
int voltage;
float Current;
int Relay1Status;
float hysteresis = 0.05; // Hysteresis value
unsigned long previousMillis = 0; // Store the previous time
const long interval = 5000; // Interval between updates in milliseconds
static const unsigned char PROGMEM image_operation_warning_bits[] = { 0x01, 0x80, 0x02, 0x40, 0x02, 0x40, 0x04, 0x20, 0x09, 0x90, 0x09, 0x90, 0x11, 0x88, 0x11, 0x88, 0x21, 0x84, 0x40, 0x02, 0x41, 0x82, 0x81, 0x81, 0x80, 0x01, 0x7f, 0xfe };
static const unsigned char PROGMEM image_device_power_button_bits[] = { 0x01, 0x00, 0x01, 0x00, 0x19, 0x30, 0x25, 0x48, 0x49, 0x24, 0x51, 0x14, 0xa1, 0x0a, 0xa0, 0x0a, 0xa0, 0x0a, 0xa0, 0x0a, 0xa0, 0x0a, 0x50, 0x14, 0x48, 0x24, 0x27, 0xc8, 0x18, 0x30, 0x07, 0xc0 };
void setup() {
Serial.begin(115200); // initialize serial communication
// Change the sensitivity value based on value you got from the calibrate
voltageSensor.setSensitivity(SENSITIVITY);
emon1.current(CT_pin1, currentCalibration); // Current: input pin, calibration.
emon2.current(CT_pin2, currentCalibration); // Current: input pin, calibration.
pinMode(RelayPin1, OUTPUT); // set the pin mode to OUTPUT for Relay
digitalWrite(RelayPin1, LOW);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.clearDisplay();
}
void DisplayVoltage() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setTextSize(2);
display.setTextWrap(false);
display.setCursor(19, 8);
display.print("VOLTAGE:");
display.setTextSize(3);
display.setCursor(25, 30);
display.print(voltage);
display.setCursor(89, 32);
display.print("V");
display.display();
}
void DisplayCurrent() {
display.clearDisplay();
display.setTextSize(2);
display.setTextWrap(false);
display.setCursor(16, 7);
display.print("CURRENT:");
display.setTextSize(3);
display.setCursor(13, 30);
display.print(sensor1current);
display.setCursor(92, 28);
display.print("A");
display.display();
}
void SystemPowerOFF() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.drawBitmap(26, 11, image_operation_warning_bits, 16, 14, SSD1306_WHITE);
display.drawBitmap(91, 32, image_device_power_button_bits, 15, 16, SSD1306_WHITE);
display.setTextSize(1);
display.setTextWrap(false);
display.setCursor(55, 16);
display.print("System");
display.setCursor(28, 36);
display.print("Power OFF");
display.display();
}
void RelayOnOff() {
// Update relay status based on sensor readings and hysteresis
if (sensor1current > setcurrent) {
Relay1Status = 0;
} else if (abs(sensor1current - sensor2current) <= hysteresis && sensor1current + hysteresis <= setcurrent) {
Relay1Status = 1;
} else {
Relay1Status = 0;
}
digitalWrite(RelayPin1, Relay1Status); // Set relay based on Relay1Status
// Print relay status for debugging
if (Relay1Status == 0) {
Serial.println("Relay1 is OFF");
} else {
Serial.println("Relay1 is ON");
}
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last time we updated the display
// Toggle between displaying voltage, current, or OFF state every 4 seconds
static int displayState = 0; // 0: voltage, 1: current, 2: OFF
if (displayState == 0) {
DisplayVoltage();
} else if (displayState == 1) {
DisplayCurrent();
} else if (displayState == 2 && Relay1Status == 0) { // Check if Relay1Status is 0
SystemPowerOFF();
}
// Increment displayState and reset to 0 after reaching 2
displayState = (displayState + 1) % 3;
}
// Read current for each sensor
sensor1current = emon1.calcIrms(1480);
sensor2current = emon2.calcIrms(1480);
// Read the voltage
voltage = voltageSensor.getRmsVoltage();
RelayOnOff();
Serial.print("Sensor 1 AC Current = ");
Serial.print(sensor1current);
Serial.println(" A");
Serial.print("Sensor 2 AC Current = ");
Serial.print(sensor2current);
Serial.println(" A");
Serial.print("Supply Voltage= ");
Serial.print(voltage);
Serial.println(" V");
Serial.println(" ************************** ");
delay(1000);
}
*/