Prepaid Kilowatt Hour Meter using GSM and Arduino. How to DIY?

Programming, Tutorial, Coding, New Design, and/or any project for your Arduino and Raspberry Pi can post your topic /suggestion here.
User avatar
Mitch
Posts: 36
Joined: Mon Jan 30, 2017 4:58 am

Prepaid Kilowatt Hour Meter using GSM and Arduino. How to DIY?

Wed Oct 03, 2018 11:56 pm

Prepaid Kilowatt Hour Meter is a good concept in which you can recharge its balance, like we do in our mobile phone. In this project we are building a automated system by using Arduino and GSM module. You can recharge the electricity balance through this system, just by sending a SMS. It can also disconnect the home power connection, if there is low or zero balance in the system this can be read by the electric meter and automatically send some updates to user’s mobile phone like low balance alert, cut off alert, resume alert and recharge alert.

The Circuit Diagram
Prepaid-Kilowatt-Hour Meter-Diagram.png
Prepaid-Kilowatt-Hour Meter-Diagram.png (38.16 KiB) Viewed 1515 times
Here we have interfaced electricity energy meter with Arduino using the pulse LED (Calibration or Cal) of electricity Energy meter. We only need to connect tis CAL LED to Arduino through an Optocoupler IC.

Components and Parts we need:
Arduino UNO
GSM Module
16x2 LCD
Analogue Kilowatt Hour Meter
Optocoupler 4n35
Resistors
POT
Connecting wires
Bulb and holder
SIM card
Power supply
Mobile Phone

When we power up the system then it reads previous values of dollars stored in EEPROM and restores them into the variables then checks the available balance with the predefined value and take action, like if available balance is greater than 15 Dollar the Arduino turns On the electricity by using relay. And if balance is less than 15 Dollar the Arduino will send SMS to the users phone about low balance alert and requesting to recharge soon. And if the balance is less than 5 Dollar the Arduino will cut off the electricity connection and then send SMS to user’s phone for ‘Light Cut Off’ alert and requesting to recharge soon. (*use your prepare currency).

Now when we need to recharge our system, we can recharge it simply by sending an SMS to the system, through Cell Phone. Like if we want to recharge by 45 bucks then we will send #45*, here # and * are prefix and suffix to the recharge amount. When the System received this message and extract recharge amount and update the balance of the system then the electricity will turn on again...

The Circuit connections for this Wireless Electricity Meter, are shown in the diagram; we have used a Arduino UNO for processing all the things used in project. A liquid crystal display is used for displaying the status of Units and remaining balance. Data pins of LCD namely RS, EN, D4, D5, D6, D7 are connected to Arduino digital pin number 7, 6, 5, 4, 3, 2. And Rx and Tx pins of GSM module are directly connected to the Tx and Rx pins of Arduino respectively. And GSM module is powered by using a 12 volt adaptor. A relay is used for switching electricity connection which is connected at pin 12 of Arduino though ULN2003 relay driver.

Calculation of Pulses and Units:

Before proceeding for the calculations, first we have to keep in mind the pulse rate of energy meter. There are two pulse rates of energy meter first is 1600 imp/kwh and second is 3200 imp/kwh. So here we are using 3200 imp/kwh pulse rate energy meter.

So first we need to calculate the Pulses for 100 watt, means how many times Pulse LED will blink in a minute, for the load of 100 watts.

Pulse= (Pluse_rate*watt*time)/ (1000*3600)

So pulses for 100 watt bulb in 60 seconds, with energy meter of 3200 imp/kwh pulse rate can be calculated as below:

Pulses=3200*100*60/1000*3600

Pulses = ~5.33 pulse per minute

Now we need to calculate Power factor of a single pulse, means how much electricity will be consumed in one pulse:

PF= watt/(hour*Pulse)

PF=100/60*5.33

PF=0.3125 watt in a single pulse

Units= PF*Total pulse/1000

Total pulses in an hour is around 5.33*60=320

Units = 0.3125*320/1000

Units = 0.1 per hour

If a 100 watt bulb is lighting for a day then it will consume

Units =0.1*24

Units = 2.4 Units

And suppose unit rate is at your region is 5 dollars per unit then

You have to pay for 2.4 Units Rs:

Dollar= 2.4*5 = 12 dollar

Programing

First of all we include required library and Define pins & variables that are required in our project. This can be seen in first few lines of our program code below.
After it we initialize the LCD, serial communication, GSM and display some message message. After this in loop function we read serial received data if any. And reads pulse from energy meter and show units and balance on LCD.

Code: Select all

void setup() 
{
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  .. ...
  ... ....

lcd.print("Dohangout.Com");
  lcd.setCursor(0,1);
  delay(2000);
  lcd.print("GSM Initializing...");
  gsm_init();
  .. ...
  ... ....
  
After this in loop function we read serial received data if any. And reads pulse from energy meter and show units and balance on LCD.

Code: Select all

void loop()    
{
  serialEvent();
  rupees=EEPROM.read(1);
  units=dollars/5.0;
  lcd.setCursor(0,0);
  lcd.print("Units:");
  .. ...
  ... ....
  
void init_sms(),void send_data(String message), and void send_sms() functions have been used to send SMS.

gsm_init() function is used for initializing the GSM module for get ready to operate with the system. In this we first sends AT command to know whether GSM module is connected or not. After it we turned off the echo and then check the network.

Code: Select all

void gsm_init()
{
  lcd.clear();
  lcd.print("Finding Module..");
  boolean at_flag=1;
  while(at_flag)
  .. ...
  ... ...
In check_status() function system reads connection and Balance conditions; like whether electricity balance is greater than the defined limit. If balance is less than 15 , then it alerts the user by sending the SMS alert of ‘Low Balance’ and if balance is less than 5 rupees then system will cut the electricity and inform the user by sending SMS using GSM module.

Code: Select all

void check_status()
{
  if(dollars>15)
   {
    digitalWrite(relay, HIGH);
    flag1=0;
    .. ...
    ... ....
send_confirmaiton_sms() function is used for sending confirmation message to the user if recharge has been done and it also update the balance in the system.
decode_message() function is used for decoding the amount figure from the SMS message, by using the # and * as starting and ending character. read_pulse() function is used for reading pulse from the Energy meter through optocoupler IC. And update the unit and balance.

serialEvent() function is used for serial communication and receiving the message.

Now here is The CODE:

Code: Select all

#include<EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2);

int led=13;
#define pulsein 8
#define relay 12
unsigned int pusle_count=0;
float units=0;
unsigned int dollars=0;

 float watt_factor=0.3125;
unsigned int temp=0,i=0,x=0,k=0;
char str[70],flag1=0,flag2=0;

String bal="";

void setup() 
{
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(pulsein, INPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(pulsein, HIGH);
  lcd.setCursor(0,0);
  lcd.print("Auto Electric");
  lcd.setCursor(0,1);
  lcd.print("      Meter    ");
  delay(2000);
  lcd.clear();
  lcd.print("Dohangout.Com");
  delay(2000);
  lcd.clear();
  lcd.print("GSM Initializing...");
  gsm_init();
  lcd.clear();
  lcd.print("System Ready");
  Serial.println("AT+CNMI=2,2,0,0,0");
  init_sms();
  send_data("System Ready");
  send_sms();
  delay(1000);
  digitalWrite(led, LOW);
  lcd.clear();
//  EEPROM.write(1,0);
 // rupees=EEPROM.read(1);
}

void loop()    
{
    serialEvent();
    rupees=EEPROM.read(1);
    units=dollars/5.0;
    lcd.setCursor(0,0);
    lcd.print("Units:");
    lcd.print(units);
    lcd.print("      ");
    lcd.setCursor(0,1);
    if(dollars<15)
    lcd.print("LOW Balance:");
    else
    lcd.print("Balance:");
    lcd.print(dollars);
    lcd.print("      ");
    read_pulse();
    check_status();
    if(temp==1)
    {
     decode_message();
     send_confirmation_sms();
    }
}
void serialEvent()
{
  while(Serial.available())
  {
    char ch=(char)Serial.read();
    str[i++]=ch;
    if(ch == '*')
    {
      temp=1;
      lcd.clear();
      lcd.print("Message Received");
      delay(500);
      break;
    }
  }
}

void init_sms()
{
   Serial.println("AT+CMGF=1");
   delay(200);
   Serial.println("AT+CMGS=\"+971506539860\""); //enter your phone number here
   delay(200);
}

void send_data(String message)
{
  Serial.println(message);
  delay(200);
}

void send_sms()
{
  Serial.write(26);
}

void read_pulse()
{
    if(!digitalRead(pulsein))
    {
      digitalWrite(led, HIGH);
      //count++;
      //units=watt_factor*count/1000;
      if(units<1){}
      else
      units--;
      rupees=units*5;
      EEPROM.write(1,dollars);
      while(!digitalRead(pulsein));
      digitalWrite(led,LOW);
     // delay(2000);
    }
}

void check_status()
{
      if(dollars>15)
      {
        digitalWrite(relay, HIGH);
        flag1=0;
        flag2=0;
      }
      if(dollars<15 && flag1==0)
      {
       lcd.setCursor(0,1);
       lcd.print("LOW Balance       ");
       init_sms();
       send_data("Energy Meter Balance Alert:");
       send_data("Low Balance\n");
       Serial.println(dollars);
       delay(200);
       send_data("Please recharge your meter soon.\n Thank you");
       send_sms();
       message_sent();
       flag1=1;
      }
      if(dollars<5 && flag2==0)
     {
      digitalWrite(relay, LOW);
      lcd.clear();
      lcd.print("Light Cut Due to");
      lcd.setCursor(0,1);
      lcd.print("Low Balance");
      delay(2000);
      lcd.clear();
      lcd.print("Please Recharge ");
      lcd.setCursor(0,1);
      lcd.print("Electric Meter ");
      init_sms();
      send_data("Electric Balance Alert:\nLights will cut due to low Balance\nPlease recharge your meter soon.\n Thank you");
      send_sms();
      message_sent();
      flag2=1;
    }
}

void decode_message()
{
  x=0,k=0,temp=0;
     while(x<i)
     {
      while(str[x]=='#')
      {
        x++;
        bal="";
        while(str[x]!='*')
        {
          bal+=str[x++];
        }
      }
      x++;
    }
    bal+='\0';
}

void send_confirmation_sms()
{
    int recharge_amount=bal.toInt();
    dollars+=recharge_amount;
    EEPROM.write(1, dollars);
    lcd.clear();
    lcd.print("Energy Meter ");
    lcd.setCursor(0,1);
    lcd.print("Recharged:");
    lcd.print(recharge_amount);
    init_sms();
    send_data("Energy Meter Balance Alert:\nYour energy meter has been recharged Rs:");
    send_data(bal);
    send_data("Total Balance:");
    Serial.println(dollars);
    delay(200);
    send_data("Electricity have been connected\nThank you");
    send_sms();
    temp=0;
    i=0;
    x=0;
    k=0;
    delay(1000);
    message_sent();
}

void message_sent()
{
  lcd.clear();
  lcd.print("Message Sent.");
  delay(1000);
}

void gsm_init()
{
  lcd.clear();
  lcd.print("Finding Module..");
  boolean at_flag=1;
  while(at_flag)
  {
    Serial.println("AT");
    while(Serial.available()>0)
    {
      if(Serial.find("OK"))
      at_flag=0;
    }
    delay(1000);
  }

  lcd.clear();
  lcd.print("Module Connected..");
  delay(1000);
  lcd.clear();
  lcd.print("Disabling ECHO");
  boolean echo_flag=1;
  while(echo_flag)
  {
    Serial.println("ATE0");
    while(Serial.available()>0)
    {
      if(Serial.find("OK"))
      echo_flag=0;
    }
    delay(1000);
  }

  lcd.clear();
  lcd.print("Echo OFF");
  delay(1000);
  lcd.clear();
  lcd.print("Finding Network..");
  boolean net_flag=1;
  while(net_flag)
  {
    Serial.println("AT+CPIN?");
    while(Serial.available()>0)
    {
      if(Serial.find("+CPIN: READY"))
      net_flag=0;
    }
    delay(1000);
  }
  lcd.clear();
  lcd.print("Network Found..");
  delay(1000);
  lcd.clear();
}

I hope you like this cool project that can be use in your neighborhood when they asking for electric connection. You can create one of this so you don't need to ask every time. obscene/drinkingdrunk


Return to “ARDUINO AND RASPBERRY PI”

Links

In total there are 9 users online :: 1 registered, 0 hidden and 8 guests
Registered users: Google [Bot]
Most users ever online was 156 on Sun Jun 17, 2018 7:42 am
Total posts 446
Total topics 325
Total members 62
Our newest member anonymous-kali
No birthdays today