Hi

I have been playing with my Uno and GSM shield to try to see if I can get the real world to react to a good old fashioned SMS text. In these days of 3G and 4G data rates, I wanted to show that we don't have to have the latest toys to interact with the real world.

So, to start I am using a common cathode RGB LED with a first gen Uno with an R3 GSM shield wired up as below.

sms_rgb_bb.jpg

(Please imagine an Uno underneath this shield :-))

So, the idea is to react to the contents of an incoming text message and light the LED with the colour that is sent to the Arduino. (A this point the choice is red, green or blue).

 

At the moment I am working on IF statements to check the contents of the message as it arrives but I am probably going to change this to either a case statement or with the message passed as a parameter into a new function. Anyway, the code as it stands at the moment:

 

/*

This sketch, for the Arduino GSM shield, waits for a SMS message
and displays it through the Serial port. Then returns message to sender
letting them know it has been received on Arduino.
dependent on the message certain colours will be displayed by led

Circuit:
* GSM shield attached to and Arduino
* SIM card that can receive SMS messages
* common cathode rgb led

created 7 Apr 2015
based on code by Javier Zorzano / TD & Tom Igoe

*/

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

// PIN Number for the SIM (set if sim pin code protected)
#define PINNUMBER ""

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

// Array to hold the number a SMS is retreived from
char senderNumber[20];
char outGoing[16];

// variables to define pins to be set for each colour
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
// define String variable to hold incoming message
String message;

void setup()
{
  // configure pins to connect to led
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  setColour(0, 0, 0); // initialise led to off

  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // configure and initialise GSM connection
  Serial.println("SMS Messages Receiver");

  // connection state
  boolean notConnected = true;

  // Start GSM connection
  while (notConnected)
  {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop()
{
  char c;

  // If there are any SMSs available()
  if (sms.available())
  {
    Serial.println("Message received from:");

    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

    // Read message bytes and print them
    while (c = sms.read())
      message += c; // build the message from the sms.read() function

    //Serial.print(c);
    Serial.println(message); // display message on serial for testing
    message.toLowerCase(); // convert to lower case to make sure the comparision works later

    Serial.println("\nEND OF MESSAGE");

    /* read the contents of message variable. see if it is blue, green or red
     light appropriate colour on LED and send message back to let sender know
     that the message has been received.
     */
    if (message == "blue")
    {
      Serial.println("received message to set BLUE");
      Serial.println("\nSENDING REPLY");
      sms.beginSMS(senderNumber);
      sms.print("SET LED TO BLUE. (Message was blue");
      sms.endSMS();
      Serial.println("\nCOMPLETE");
      setColour(0, 0, 255);
      message = "";
    }

    if (message == "green")
    {
      Serial.println("received message to set GREEN");
      Serial.println("\nSENDING REPLY");
      sms.beginSMS(senderNumber);
      sms.print("SET LED TO GREEN.");
      sms.endSMS();
      Serial.println("\nCOMPLETE");
      setColour(0, 255, 0);
      message = "";
    }

    if (message == "red")
    {
      Serial.println("received message to set RED");
      Serial.println("\nSENDING REPLY");
      sms.beginSMS(senderNumber);
      sms.print("SET LED TO RED.");
      sms.endSMS();
      Serial.println("\nCOMPLETE");
      setColour(255, 0, 0);
      message = "";
    }



    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}
/* function that sets colour
takes three values to set the levels of each colour. Can be set as hex values by
prefixing with 0x. For example 0x03 = decimal 3, 0x0d = decimal 13
*/
void setColour(int red, int green, int blue)
{

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

 

So, there you go, a good old fashioned text message that says red, (or ReD due to the toLowerCase() function), will light the led a nice red colour. I will make some changes to the code, it comes in at 17,056 bytes of program storage space with 1,196 bytes of global variables so I thank that I want to tweak it a bit to see how efficient I can make it.

 

Shopping list

=========

 

Arduino GSM ShieldArduino GSM Shield

RGB LED (such as Kingbright L154A4SURKQBDZGWRGB LED (such as Kingbright L154A4SURKQBDZGW

Arduino UnoArduino Uno