230V AUTOMATIC OVER VOLTAGE REGULATOR

SMART SOCKET ARDUINO PROGRAMMING CODE

 ////////////////////////////////////////////////////////////////////////

//       Part of this code is from RemoteXY include library          ///

//       Editted by AYODEJI KAREEM ayodejikareem.m@gmail.com        ///

//////////////////////////////////////////////////////////////////////


// RemoteXY select connection mode and include library

#define REMOTEXY_MODE__SOFTSERIAL

#include <SoftwareSerial.h>

#include <RemoteXY.h>

#include <LiquidCrystal_I2C.h>  /*include LCD I2C Library*/

LiquidCrystal_I2C lcd(0x27, 16, 2); /*I2C scanned address defined + I2C screen size*/

//Voltage measurement

#include <Filters.h> //Easy library to do the calculations

#include <SPI.h>     //Libraries for display

#include <Wire.h>


// RemoteXY connection settings

#define REMOTEXY_SERIAL_RX 2

#define REMOTEXY_SERIAL_TX 3

#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate

#pragma pack(push, 1)

uint8_t RemoteXY_CONF[] =   // 117 bytes

{ 255, 5, 0, 0, 0, 110, 0, 16, 11, 1, 10, 48, 35, 6, 21, 21, 4, 1, 31, 83,

  50, 95, 79, 78, 0, 31, 79, 70, 70, 0, 10, 48, 6, 7, 20, 20, 4, 1, 31, 83,

  49, 95, 79, 78, 0, 31, 79, 70, 70, 0, 10, 48, 36, 38, 21, 21, 4, 1, 31, 83,

  52, 95, 79, 78, 0, 31, 79, 70, 70, 0, 10, 48, 8, 39, 20, 20, 4, 1, 31, 83,

  51, 95, 79, 78, 0, 31, 79, 70, 70, 0, 1, 1, 18, 77, 30, 12, 2, 31, 68, 105,

  115, 112, 108, 97, 121, 32, 80, 97, 114, 97, 109, 101, 116, 101, 114, 115, 0

};


// this structure defines all the variables and events of your control interface

struct {


  // input variables

  uint8_t pushSwitch_1; // =1 if state is ON, else =0

  uint8_t pushSwitch_2; // =1 if state is ON, else =0

  uint8_t pushSwitch_3; // =1 if state is ON, else =0

  uint8_t pushSwitch_4; // =1 if state is ON, else =0

  uint8_t button_1; // =1 if button pressed, else =0


  // other variable

  uint8_t connect_flag;  // =1 if wire connected, else =0


} RemoteXY;

#pragma pack(pop)


/////////////////////////////////////////////

//           END RemoteXY include          //

/////////////////////////////////////////////


#define PIN_PUSHSWITCH_1 5

#define PIN_PUSHSWITCH_2 4

#define PIN_PUSHSWITCH_3 7

#define PIN_PUSHSWITCH_4 6

#define PIN_BUTTON_1 8


//CURRENT MEASUREMENT

float  Sensitivity = 0.66 ;  // sensitivity in Volts / Amp for 30A sensor


//VOLTAGE MEASUREMENT SETTINGS

float testFrequency = 50;   // test signal frequency (Hz)

float windowLength = 40.0 / testFrequency; // how long to average the signal, for statistist


int Sensor1 = 0; //Sensor analog input, here it's A0


float intercept = -0.04; // to be adjusted based on calibration testing

float slope = 0.0405; // to be adjusted based on calibration testing

float current_Volts1; // Voltage1


unsigned long printPeriod = 2000; //Refresh rate

unsigned long previousMillis = 0;


void setup()

{

  RemoteXY_Init ();


  pinMode (PIN_PUSHSWITCH_1, OUTPUT);

  pinMode (PIN_PUSHSWITCH_2, OUTPUT);

  pinMode (PIN_PUSHSWITCH_3, OUTPUT);

  pinMode (PIN_PUSHSWITCH_4, OUTPUT);

  pinMode (PIN_BUTTON_1, OUTPUT);


  // TODO you setup code

  lcd.init();  /*LCD display initialized*/

  lcd.clear();     /*Clear LCD Display*/

  lcd.backlight();      /*Turn ON LCD Backlight*/

  lcd.setCursor(2, 0);  /*Set cursor to Row 1*/

  lcd.print("4 SMART SOCKET"); /*print text on LCD*/

  lcd.setCursor(2, 1);  /*set cursor on row 2*/

  lcd.print("FINAL ND PROJECT"); /*print message on LCD*/

}


void loop()

{

  RemoteXY_Handler ();


  digitalWrite(PIN_PUSHSWITCH_1, (RemoteXY.pushSwitch_1 == 0) ? LOW : HIGH);

  digitalWrite(PIN_PUSHSWITCH_2, (RemoteXY.pushSwitch_2 == 0) ? LOW : HIGH);

  digitalWrite(PIN_PUSHSWITCH_3, (RemoteXY.pushSwitch_3 == 0) ? LOW : HIGH);

  digitalWrite(PIN_PUSHSWITCH_4, (RemoteXY.pushSwitch_4 == 0) ? LOW : HIGH);

  digitalWrite(PIN_BUTTON_1, (RemoteXY.button_1 == 0) ? LOW : HIGH);


  // TODO you loop code

  // use the RemoteXY structure for data transfer

  // do not call delay()


  //VOLTAGE MEASUREMENT LOOP

  lcd.init();

  lcd.clear();     /*Clear LCD Display*/

  lcd.backlight();

  lcd.setCursor(0, 0);

  delay(100);


  lcd.setCursor(0, 0);

  lcd.print("SMART SOCKET");

  lcd.setCursor(0, 1);

  lcd.print("VOLT_PHASE 1:");


  RunningStatistics inputStats1;                //Easy life lines, actual calculation of the RMS requires a load of coding

  inputStats1.setWindowSecs( windowLength );


  while ( true ) {

    {

      Sensor1 = analogRead(A0);  // read the analog in value in phase 1:

      inputStats1.input(Sensor1);

    }  // log to Stats function



    if ((unsigned long)(millis() - previousMillis) >= printPeriod) {

      previousMillis = millis();   // update time every second

      Serial.print( "\n" );


      current_Volts1 = intercept + slope * inputStats1.sigma(); //Calibartions for offset and amplitude

      current_Volts1 = current_Volts1 * (78.30);             //Further calibrations for the amplitude


      Serial.print( "\tVoltage1: " );

      Serial.print( current_Volts1 ); //Calculation and Value display is done the rest is if you're using an OLED display


      lcd.setCursor(14, 1);

      lcd.print(current_Volts1);


      //CURRENT MEASUREMENT CALCULATION

      float  sensorVoltage =  analogRead ( A2 ) * ( 5.0  /  1023.0 ) ;  //


      float  sensorReading_I = ( sensorVoltage - 2.5 ) / Sensitivity ;  // Equation to obtain the


      Serial.print ( "Current:" );


      Serial . println ( sensorReading_I , 3 ) ;

      lcd.setCursor(14, 2);

      lcd.print(sensorReading_I);

      delay ( 200 ) ;

    }

  }

}


CONTACT ME on ayodejikareem.m@gmail.com for more explanation

Comments