-1

My project is automatic room light controller with visitor counter using Arduino.

My project was running correct upto this, but the problem started when we connected GSM module to our project because we have given a limit to the room, so when after limit exceeded using GSM a msg must be sent to the admin.

Actual problem is when a limit exceeds,code is not coming out of loop and when we are trying to increment or decrement no. of persons using IR sensor but its not happening.

Help me which loop i sholud use to send msg automatically

#include<LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 5);
char msg;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
#define in 14
#define out 19
#define relay 2
int count = 0;
void IN()
{
  count++;
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0, 1);
  lcd.print(count);
  delay(1000);
}
void OUT()
{
  count--;
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0, 1);
  lcd.print(count);
  delay(1000);
}
void setup()
{
  lcd.begin(16, 2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0, 1);
  lcd.print(count);

  mySerial.begin(9600);   // Setting the baud rate of GSM Module
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  Serial.println("GSM SIM900A BEGIN");
  Serial.println("Enter character for control option:");
  Serial.println("s : to send message");
  Serial.println();
  delay(100);
}

void loop()
{
  if (digitalRead(in))
    IN();
  if (digitalRead(out))
    OUT();

  if (count <= 0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0, 1);
    lcd.print("Light Is Off");
    delay(200);
  }
  else
    digitalWrite(relay, HIGH);

  if (count >= 3)
  {
    SendMessage();

    digitalWrite(relay, HIGH);
    delay(100);
    lcd.clear();
    lcd.print("Limit exceeded");
    lcd.setCursor(0, 1);
    lcd.print(count);
    return;
  }
  //else
  //  digitalWrite(relay, LOW);

  if (Serial.available() > 0)
    switch (Serial.read())
    {
      case 's':
        SendMessage();
        break;
      case 'r':
        ReceiveMessage();
        break;
    }
  if (mySerial.available() > 0)
    Serial.write(mySerial.read());
}

void SendMessage()
{
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+918419906897\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("GSM TEST:Limit of the room exceeded");// The SMS text you want to send
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  //Serial.end();
}

void ReceiveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);
  if (mySerial.available() > 0)
  {
    msg = mySerial.read();
    Serial.print(msg);
  }
}

void Response()
{
  int count = 0;
  Serial.println();
  while (1)
  {
    if (Serial.available())
    {
      char data = Serial.read();
      if (data == 'K') {
        Serial.println("OK");
        break;
      }
      if (data == 'R') {
        Serial.println("GSM Not Working");
        break;
      }
    }
    count++;
    delay(10);
    if (count == 1000) {
      Serial.println("GSM not Found");
      break;
    }
  }
}
  • your description of the problem is unclear. i am unable to understand which part works and which part does not work. – jsotola Jan 27 '18 at 10:24
  • Why do you use so many long delays? You wait for 1 second everytime a person comes in or out. This blocks all other code, that could be running – chrisl Jan 27 '18 at 23:20
  • Actual problm is when the limit of persons exceeds a msg must b sent to admin so i gave it inside loop a if condition bt the prblm is once the limit exceeds msg is sent bt i cant increment or decrement no. Of peraons manually. It is not coming out of if condition @jsotola – Nag Raju Jan 28 '18 at 12:37
0

Check that the GSM module is wired correctly, is setup correctly and that you get a ringing tone when you call that number from a different number. If that is all okay, write / copy a sketch from one of the many online tutorials that sends an SMS taking all input from the serial monitor of the Arduino IDE. When that is working, transfer that code into the sketch above. The return statement in your code will exit the loop() function within the sketch, but the C code running your sketch will just invoke your sketch again. Also, good practise says it's best not to include a function that is not actually used in the sketch - examples being the ReceiveMessage() and Response() functions.

  • Can u plzz send me the right code – Nag Raju Jan 28 '18 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.