2

I am using following code to send message using Arduino Uno & SIM800 module. But I need to send same message on multiple numbers then what changes should I make in my code.

const int buttonPin = 8;   // Main gate sensor 1
const int buttonPin1 = 7;  // Main gate sensor 2
const int buttonPin2 = 9;  // Store room sensor 1
const int buttonPin3 = 10; // Store room sensor 2

bool i = false;
bool j = false;
bool k = false;
bool l = false;

void setup()
{
  Serial.begin(9600);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);

  digitalWrite(buttonPin, LOW);
  digitalWrite(buttonPin1, LOW);
  digitalWrite(buttonPin2, LOW);
  digitalWrite(buttonPin3, LOW);

  delay(1000);
}
void loop()

{
  sensor1();
  sensor2();
}
  void sensor1()
  {
  if (digitalRead(buttonPin1)== LOW && digitalRead(buttonPin) == HIGH  && i == false)

  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91...................");
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Main Gate is Closed");
    Serial.write(0x1A);
    delay(1000);
    i = true;
    j = false;

  }

  if (digitalRead(buttonPin)== LOW && digitalRead(buttonPin1) == HIGH &&  j == false)
  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91........................");
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Main Gate is open");
    Serial.write(0x1A);
    delay(1000);
    j = true;
    i = false;
  }
  }
  void sensor2()
  {
  if (digitalRead(buttonPin2) == HIGH && digitalRead(buttonPin3)==LOW && k == false)
  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91....................");
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Store room is Closed");
    Serial.write(0x1A);
    delay(1000);
    k = true;
    l = false;
  }
  if (digitalRead(buttonPin3) == HIGH &&digitalRead(buttonPin2)== LOW && l == false)
  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91....................");
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Store room is Open");
    Serial.write(0x1A);
    delay(1000);
    l = true;
    k = false;
  }
}
1
  • 1
    Write a function that sends any message to any number and call it numerous times
    – Majenko
    Mar 18 '17 at 7:57
1

@nilesh just add some delay between two massege

I modified your code check it

const int Main1 = 8;
const int Main2 = 9;
const int Store1 = 7;
const int Store2 = 10;
bool i = false;
bool j = false;
bool k = false;
bool l = false;

void setup()
{
  Serial.begin(9600);

  pinMode(Main1, INPUT);
  pinMode(Main2, INPUT);
  pinMode(Store1, INPUT);
  pinMode(Store2, INPUT);

  digitalWrite(Main1, LOW);
  digitalWrite(Main2, LOW);
  digitalWrite(Store1, LOW);
  digitalWrite(Store2, LOW);

  delay(1000);
}
void loop()

{
  sensor1();
  sensor2();
}
void sensor1()
{
  if (digitalRead(Main1) == HIGH && digitalRead(Main2) == LOW && i == false)

  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91-----------");// first number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Main gate closed");
    Serial.write(0x1A);
    delay(5000);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91-----------");// Second number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Main gate closed");
    Serial.write(0x1A);
    delay(1000);
    i = true;
    j = false;

  }
  if (digitalRead(Main2) == HIGH && digitalRead(Main1) == LOW && j == false)
  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91---------------");//first number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Main gate Open");
    Serial.write(0x1A);
    delay(5000);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91--------------"); second number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Main gate Open");
    Serial.write(0x1A);
    delay(1000);
    j = true;
    i = false;
  }
}
void sensor2()
{
  if (digitalRead(Store1) == HIGH && digitalRead(Store2) == LOW && k == false)
  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
   Serial.print("+91-----------");// first number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Store room is closed");
    Serial.write(0x1A);
    delay(10000);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91-----------");// second number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Store room is Closed");
    Serial.write(0x1A);
    delay(1000);
    k = true;
    l = false;
  }
  if (digitalRead(Store2) == HIGH && digitalRead(Store1) == LOW && l == false)
  {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
   Serial.print("+91-----------");// first number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Store room is Open");
    Serial.write(0x1A);
    delay(5000);
    Serial.print("AT+CMGS=\"");
   Serial.print("+91-----------");// Second number
    Serial.print("\"\r\n");
    delay(2000);
    Serial.print("Store room is Open");
    Serial.write(0x1A);
    delay(1000);
    l = true;
    k = false;
  }
}

thanks

bhupendra.sinkar727@gmail.com

1

I have same question but minor different.

sim.println("AT+CMGS="+91xxxxxxxxxx"\r");

i dont want to write this. i want to write mobile number on serial monitor in arduino and on that number, i want to send message. how can i do?

0
0

Try out this one. Here, I made the code for two number. Just simply add a new function for every new number. I hope it will help you.

const int buttonPin = 8;   // Main gate sensor 1
const int buttonPin1 = 7;  // Main gate sensor 2
const int buttonPin2 = 9;  // Store room sensor 1
const int buttonPin3 = 10; // Store room sensor 2

bool i = false;
bool j = false;
bool k = false;
bool l = false;

void setup()
{
  Serial.begin(9600);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);

  digitalWrite(buttonPin, LOW);
  digitalWrite(buttonPin1, LOW);
  digitalWrite(buttonPin2, LOW);
  digitalWrite(buttonPin3, LOW);

  delay(1000);
}
void loop()

{
  sensor1();
  sensor2();
}

void sensor1()
{
  if (digitalRead(buttonPin1)== LOW && digitalRead(buttonPin) == HIGH  && i == false)
  {
    numOneGateClose();
    numTwoGateClose();
    i = true;
    j = false;
  }

  if (digitalRead(buttonPin)== LOW && digitalRead(buttonPin1) == HIGH &&  j == false)
  {
   numOneGateOpen();
   numTwoGateOpen();
    j = true;
    i = false;
  }
}

void sensor2()
{
  if (digitalRead(buttonPin2) == HIGH && digitalRead(buttonPin3)==LOW && k == false)
  {
    numOneRoomClose();
    numTwoRoomClose();
    k = true;
    l = false;
  }

  if (digitalRead(buttonPin3) == HIGH &&digitalRead(buttonPin2)== LOW && l == false)
  {
    numOneRoomOpen();
    numTwoRoomOpen();
    l = true;
    k = false;
  }
}

void numOneGateClose()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91...................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is Closed");
  Serial.write(0x1A);
  delay(1000);
}

void numTwoGateClose()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91...................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is Closed");
  Serial.write(0x1A);
  delay(1000);
}

void numOneGateOpen()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91........................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is open");
  Serial.write(0x1A);
  delay(1000);
}

void numTwoGateOpen()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91........................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is open");
  Serial.write(0x1A);
  delay(1000);
}

void numOneRoomClose()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91...................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is Closed");
  Serial.write(0x1A);
  delay(1000);
}

void numTwoRoomClose()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91...................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is Closed");
  Serial.write(0x1A);
  delay(1000);
}

void numOneRoomOpen()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91........................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is open");
  Serial.write(0x1A);
  delay(1000);
}

void numTwoRoomOpen()
{
  Serial.println("OK");
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");
  delay(500);
  Serial.print("AT+CMGS=\"");
  Serial.print("+91........................");
  Serial.print("\"\r\n");
  delay(2000);
  Serial.print("Main Gate is open");
  Serial.write(0x1A);
  delay(1000);
}
7
  • 3
    I would like to point out, that there really should be SEND MESSAGE method that would make the whole script shorter and easier to debug. Basic REUSE principle in programming.
    – Divisadero
    Mar 18 '17 at 9:25
  • it send message to only first number Mar 18 '17 at 11:27
  • 1
    Lead by example. That code isn't a good example. As @Divisadero says, write a SEND MESSAGE function and reuse it many times.
    – Majenko
    Mar 19 '17 at 8:28
  • 1
    @NileshSurwade Ok, let me check and update my code again.
    – Hasan
    Mar 20 '17 at 6:12
  • 1
    @NileshSurwade I update my code. I hope it will help.
    – Hasan
    Mar 20 '17 at 11:07

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.