0% found this document useful (0 votes)
21 views6 pages

542 Program

Roport

Uploaded by

khankashib955
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

542 Program

Roport

Uploaded by

khankashib955
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <LiquidCrystal.

h>
LiquidCrystal lcd(4, A4, A3, A2, A1, A0);
const int voltage = 5;
const int current = 6;
int tv = 0, tc = 0, lag = 0, ams = 0, cls = 0;
float ilag = 0, ilead = 0, pf = 0, ipf = 0, d = 0, angle = 0, ar = 0;
float i = 0, w = 0 ;
int i1, i2, i3, i4;
#define relay 10
int temp = 0, ii = 0;
int led = 13;
const int buz=8;
float ut = 0, ut1 = 0;
float unit = 0;
float tempunit = 0;
float tempunit1 = 0;
float unitrate = 7.5;
float bill = 0.0;
char str[15];
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(voltage, INPUT);
pinMode(current, INPUT);
pinMode(relay, OUTPUT);
pinMode(buz, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("WELCOME");
lcd.setCursor(0, 1);
lcd.print("SMART METER");
delay(7000);
digitalWrite(relay, HIGH);
Serial.println("AT");
delay(500);
Serial.println("AT+CNMI=2,2,0,0,0");
delay(500);
Serial.println("AT+CMGF=1");
delay(1000);
lcd.clear();
}
//---------------------------------------------------------------------
void check()
{
if (!(strncmp(str, "LOAD=OFF", 8)))
{
digitalWrite(relay, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SMS received");
lcd.setCursor(0, 1);
lcd.print("LOAD IS OFF");
sendsms(2);
delay(200);
lcd.clear();
}
else if (!(strncmp(str, "LOAD=ON", 7)))
{
digitalWrite(relay, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SMS received");
lcd.setCursor(0, 1);
lcd.print("LOAD IS ON");
sendsms(1);
delay(200);
lcd.clear();
}
else if (!(strncmp(str, "UNIT=SMS", 8)))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SMS received");
lcd.setCursor(0, 1);
lcd.print("sending Bill");
sendsms(3);
delay(1000);
lcd.clear();
}
}
//-------------------------------------------------------------------
void sendsms(int n)
{
Serial.print("AT+CMGF=1\r");
delay(200);
Serial.print("AT+CNMI=2,2,0,0,0\r");
delay(200);
Serial.print("AT+CMGS=");
delay(200);
Serial.print('"');
delay(200);
Serial.print("+917760144362");
delay(200);
Serial.print('"');
delay(200);
Serial.write(0x0D);
delay(200);
if (n == 1)
{
Serial.print(" LINE IS ON ");
}
else if (n == 2)
{
Serial.print(" LINE OFF DUE TO SOME REASON");
}
else if (n == 3)
{
Serial.print("UNIT=");
Serial.print(unit);
Serial.print(" BILL=");
Serial.print(bill);
}
delay(500);
Serial.write(0x1A);
delay(5000);
// sending on another number
Serial.print("AT+CMGF=1\r");
delay(200);
Serial.print("AT+CNMI=2,2,0,0,0\r");
delay(200);
Serial.print("AT+CMGS=");
delay(200);
Serial.print('"');
delay(200);
Serial.print("+917022159719");
delay(200);
Serial.print('"');
delay(200);
Serial.write(0x0D);
delay(200);
if (n == 1)
{
Serial.print(" LINE IS ON ");
}
else if (n == 2)
{
Serial.print(" LINE OFF DUE TO SOME REASON");
}
else if (n == 3)
{
Serial.print("UNIT=");
Serial.print(unit);
Serial.print(" BILL=");
Serial.print(bill);
}
delay(500);
Serial.write(0x1A);
delay(200);
}
//--------------------------------------------------------------------------
void loop()
{
// POWER FACTOR CALCULATION..........................
tv = digitalRead(voltage);
tc = digitalRead(current);
while (tv == LOW)
{
tv = digitalRead(voltage);
}
while (tv == HIGH)
{
tv = digitalRead(voltage);
}
while (tv == LOW)
{
tv = digitalRead(voltage);
}
tv = micros();
while (tc == LOW)
{
tc = digitalRead(current);
}
tc = micros();
lag = tc - tv;
ilag = lag * 0.001;
angle = ilag * 18;
ar = angle * 0.0174533;
pf = cos(ar);
if (ilag < 5)
{ //lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IL=");
lcd.print(ilag, 2);
}
else
{ ilead = 10 - ilag;
angle = ilead * 18;
ar = angle * 0.0174533;
pf = cos(ar);
lcd.setCursor(0, 0);
lcd.print("ID=");
lcd.print(ilead, 2);
}
lcd.print(" PF=");
lcd.print(pf);
lcd.print(" ");
// CURRENT READING..........................................
i3 = 0;
for (i1 = 0; i1 < 9000; i1++)
{
i2 = analogRead(A5);
if (i2 > i3)
{
i3 = i2;
}
}
i = i3 / 512.9;
w = 230 * i;
ut = millis() - ut1;
ut1 = millis();
ut = ut / 1000;
ut = ut / 360;
tempunit = w * ut;
tempunit = tempunit / 100;
unit = unit + tempunit;
bill = unit * unitrate;
lcd.setCursor(0, 1);
lcd.print("W=");
lcd.print(w);
lcd.print(" ");
lcd.print("U=");
lcd.print(unit);
lcd.print(" B=");
lcd.print(bill);
//SMS READ AND CONTROL
if (temp == 1)
{
check();
temp = 0;
ii = 0;
delay(1000);
}
delay(1000);
}
//Serial Event..................................
void serialEvent()
{
while (Serial.available())
{
if (Serial.find("###"))
{
digitalWrite(buz, HIGH);
delay(1000);
digitalWrite(buz, LOW);
while (Serial.available())
{
char inChar = Serial.read();
str[ii++] = inChar;
if (inChar == '*')
{
temp = 1;
return;
}
}
}
}
}

You might also like