Arduino Automatic Meter reading
Arduino Code
// include the library code:
include
const int buttonPin = 7; // the number of the pushbutton pin
char inchar;
const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int ctrr = 0;
int bal = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin, OUTPUT);
Serial.print(“pin state low\n”);
delay(3000); // give time for GSM module to register on network.
Serial.println(“AT+CMGF=1”); // set SMS mode to text
delay(200);
Serial.println(“AT+CNMI=2,2,0,0,0”); // set module to send SMS data to serial out upon receipt
delay(200);
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
lcd.clear();
lcd.setCursor(0, 0);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
bal=100;
lcd.print (bal);
delay(2000);
}
if (buttonState == HIGH) {
// turn LED on:
while(1){
//
if (buttonState == HIGH)
{
lcd.clear();
ctrr++;
bal–;
lcd.print(” RD “);
lcd.print (ctrr);
lcd.print(” bal “);
lcd.print (bal);
delay(2000);
digitalWrite(ledPin, HIGH);
}
if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
Serial.print(“\r”);
delay(1000);
Serial.print(“AT+CMGF=1\r”);
delay(1000);
Serial.print(“AT+CMGS=\”+919888708401\”\r”); //Number
delay(1000);
Serial.print(ctrr); //The text of the message to be sent
delay(1000);
Serial.write(0x1A);
delay(1000);
}
test12();
if (ctrr>0) {
digitalWrite(ledPin, HIGH);
}
if (bal==0) {
digitalWrite(ledPin, LOW);
while(1){}
}
else{}}
}
/* if ((ctrr==0)&&(ctrr<0)) {
digitalWrite(ledPin, LOW);
}*/
else {
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 0); // top left
lcd.print(“Prepaid system”);
// delay(200);
// turn LED off:
// // top left
//
}
}
void test12()
//If #a1b1c1d1 comes as sms, all led’s should light up.
{ if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar==’#’)
{
delay(10);
inchar=Serial.read();
//first led
if (inchar==’a’)//closed
{
delay(10);
inchar=Serial.read();
if (inchar==’0′)//closed
{
// digitalWrite(led1, LOW);
delay(1000);
// digitalWrite(relay1,HIGH);
Serial.print(“Relay 1 LOW\n”);
// delay(1000);
// Serial.println(“AT+CMGD=1,4”); //Delete all message
delay(500);
Serial.println(“AT+CMGF=1”);
delay(500);
Serial.println(“AT+CMGS=\”9888708401\””);//Change the receiver phone number
delay(500);
Serial.print(“Power OFF”); //the message you want to send
delay(500);
Serial.write(26);
delay(500);
}
else if (inchar==’1′)
{
// digitalWrite(led1, HIGH);
delay(1000);
// digitalWrite(relay1, LOW);
Serial.print(“Relay 1 HIGH\n”);
// delay(1000);
// Serial.println(“AT+CMGD=1,4”); //Delete all message
delay(500);
Serial.println(“AT+CMGF=1”);
delay(500);
Serial.println(“AT+CMGS=\”9888708401\””);//Change the receiver phone number
delay(500);
Serial.print(“Power ON”); //the message you want to send
delay(500);
Serial.write(26);
delay(500);
}
delay(10);
inchar=Serial.read();
if(inchar=='b')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
//digitalWrite(led2, LOW);
delay(500);
// digitalWrite(relay2,HIGH);
Serial.print("Relay 2 LOW\n");
//Serial.println("AT+CMGD=1,4"); //Delete all message
}
else if (inchar=='1')
{
//digitalWrite(led2, HIGH);
delay(500);
// digitalWrite(relay2, LOW);
Serial.print("Relay 2 HIGH\n");
// Serial.println("AT+CMGD=1,4"); //Delete all message
// Serial.print("Light ON");
}
}
}
Serial.println("AT+CMGD=1,4");
}
}}