Arduino Code
#include "ACS712.h"
ACS712 sensor1(ACS712_30A, A0); /// current sensor attached to A0 of arduino nano
ACS712 sensor2(ACS712_30A, A1); /// current sensor attached to 1 of arduino nano
#include <LiquidCrystal.h> ////////lcd screen
#include <SoftwareSerial.h> /////Sim 800l communication
SoftwareSerial sim(11, 12); //tx,rx
String number = "9080154392"; //message number
String alert = "ALERT ! Theft Identified"; ///alert message
int i = 0;
int senttime = 0;
int currenttime = 0;
float I1 = 0.00;
float I2 = 0.00;
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600); //baud rate of serial communication between computer and arduino
lcd.begin(16, 2);
sim.begin(115200); //baud rate of serial communication between arduino and sim 800l
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" POWER THEFT ");
lcd.setCursor(0, 1);
lcd.print(" IDENTIFICATION ");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("STARTING DEVICE");
delay(250);
lcd.setCursor(0, 1);
lcd.print(" in secs ");
Serial.println("Starting Device wait 20 secs");
for (i = 20; i > 0; i--)
{
lcd.setCursor(6, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(i);
delay(1000);
}
////////////////////////////////////////GSM Initialization/////////////////////////////////////////////
sim.println("AT");
delay(200);
sim.println("AT");
delay(200);
if (sim.find("OK"))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" GSM module ");
lcd.setCursor(0, 1);
lcd.print(" ready! ");
Serial.println("GSM Module Ready");
delay(1000);
}
else
{
while (1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" !ERROR! ");
lcd.setCursor(0, 1);
lcd.print("check GSM module");
Serial.println("GSM Module error check wires or restart device to fix");
delay(1000);
}
}
/////////////////////////////////////////////Sensor calibrartion/////////////////////////////////////////////////
Serial.println("Calibrating... Ensure that no current flows through the sensor at this moment");
sensor1.calibrate();
sensor2.calibrate();
Serial.println("Done!");
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" Calibrating ");
lcd.setCursor(0, 1);
lcd.print(" sensors ");
delay(2000);
}
void loop()
{
get_sensor_data();
if ((I1 - I2) > 0.16)
{
delay(1000);
get_sensor_data();
theftanalysis();
delay(1000);
get_sensor_data();
if ((I1 - I2) > 0.16)
{
get_sensor_data();
theftscreen(); ///////////theft confirm
currenttime = millis();
Serial.print("current time : ");
Serial.println(currenttime);
Serial.print("sent time : ");
Serial.println(senttime);
if ((currenttime - senttime) > 20000)
{
send_alert(); ////mesge sending
}
digitalWrite(13, HIGH);
}
}
else if ((I1 - I2) < 0.10)
{
get_sensor_data();
normal();
digitalWrite(13, LOW);
}
}
/////////////////////////////////sensor Function///////////////////////////////////////////
void normal()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dist meter A");
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(I1);
lcd.setCursor(0, 1);
lcd.print("Home meter A");
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(I2);
Serial.println("Normal");
Serial.print("Distribution Meter: ");
Serial.print(I1);
Serial.println("A");
Serial.print("Home Meter: ");
Serial.print(I2);
Serial.println("A");
delay(1000);
}
////////////////////////////////gsm functions//////////////////////////////////////////////
void send_alert()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sending Message");
lcd.setCursor(3, 1);
lcd.print(number);
sim.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(100);
sim.println("AT+CMGS=\"" + number + "\"\r"); //specifys the number to send message
delay(100);
String SMS = String(alert);
sim.println(SMS);
delay(100);
sim.println((char)26); //exits from sms mode
delay(1000);
senttime = millis();
}
//////////////////////////////////////Theft Screen///////////////////////////////////////
void theftscreen()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Alert! Theft ");
lcd.setCursor(0, 1);
lcd.print("D: A");
lcd.setCursor(2, 1);
lcd.print(" ");
lcd.setCursor(2, 1);
lcd.print(I1);
lcd.setCursor(9, 1);
lcd.print("H: A");
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(I2);
Serial.println("ALERT! Theft");
Serial.print("Distribution Meter: ");
Serial.print(I1);
Serial.println("A");
Serial.print("Home Meter: ");
Serial.print(I2);
Serial.println("A");
}
/////////////////////////////////Theft Analyse///////////////////////////////////////////////
void theftanalysis()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Analysing Theft");
lcd.setCursor(0, 1);
lcd.print("D: A");
lcd.setCursor(2, 1);
lcd.print(" ");
lcd.setCursor(2, 1);
lcd.print(I1);
lcd.setCursor(9, 1);
lcd.print("H: A");
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(I2);
Serial.println(" Analysing Theft");
Serial.print("Distribution Meter: ");
Serial.print(I1);
Serial.println("A");
Serial.print("Home Meter: ");
Serial.print(I2);
Serial.println("A");
}
///////////////////////////////////////////get sensor data////////////////////////
void get_sensor_data()
{
I1 = sensor1.getCurrentAC();
I2 = sensor2.getCurrentAC();
}
No comments:
Post a Comment