Showing posts with label sms to control led. Show all posts
Showing posts with label sms to control led. Show all posts

Monday, 13 October 2014

Arduino GSM Shield to control led

This project is an attempt to control an led (likewise a relay properly connected) to on or off. The code below is well commented in order to be self explanatory.

/*

 This sketch, for the Arduino GSM shield, waits for a SMS message
 and displays it through the Serial port and turn on/off a led.

 Circuit:
 * GSM shield attached to an Arduino
 * SIM card that can receive SMS messages

*/

// include the GSM library
#include <GSM.h>

// PIN Number for the SIM.If your SIM has no security PIN, you can leave it blank
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is received from
char senderNumber[20];
//led is connected to pin13
int led=13;

void setup()
{
// initialize serial communications and send a message that the sketch has started:
  Serial.begin(9600);
// initialize pin 13 as output
  pinMode(led,OUTPUT);
// start with the led in off state
  digitalWrite(led,LOW);

  Serial.println("SMS Messages Receiver");
 
  // variable to track the connection state
  boolean notConnected = true;

  /* Connect to the network by calling gsmAccess.begin(). It takes the SIM card's PIN as an argument.
  By placing this inside a while() loop, you can continually check the status of the connection.
  When the modem does connect, gsmAccess() will return GSM_READY.
  Use this as a flag to set the notConnected variable to true or false. Once connected, the remainder of setup will run.*/
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

//Finish setup with some information to the serial monitor.
  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

//SMS messages are received by the modem. In loop(), create a variable of type char to temporarily hold characters from
//any SMS received. Use sms.available() to check for the presence of any messages on the SIM :
void loop()
{
  char c;
//create an array to save the values read from the sms.
  char smsData[80];//Make it bigger if needed
//create an index into the array
  byte smsIndex = 0;
  if (sms.available())

//If a SMS is available, retrieve the remote sender's number by calling sms.remoteNumber(remoteNumber, 20).
//the remoteNumber argument is the char array you declared in the beginning of the sketch.
//Send this number to the serial monitor.
  {
    Serial.println("Message received from:");
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

//To read a message, use sms.read(). Here, you'll store each character from the message into the variable c
//and print it out as it gets read.
    while(c=sms.read())
    {
      Serial.print(c);//ie what happens here is to read a byte one at a time and then discard
//as you read the data you store it in the array by incrementing the index(position in the array)
      smsData[smsIndex++] = c;
      smsData[smsIndex] = '\0';  // Keep string NULL terminated. To indicate end of character string
    }
      if (strcmp(smsData, "On")==0) //function to compare two strings. If contents of both strings are equal it returns a 0
        {digitalWrite (led,HIGH);}
      if (strcmp(smsData, "Off")==0)
        {digitalWrite (led,LOW);}
     
//Indicate the message is complete and remove it from memory with sms.flush().
    Serial.println("\nEND OF MESSAGE");
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

//Add a brief delay and close the loop.
  delay(1000);
}