element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino Tutorials
  • Products
  • Arduino
  • Arduino Tutorials
  • More
  • Cancel
Arduino Tutorials
Blog SMS to light an LED!
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino Tutorials to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: markvenn
  • Date Created: 7 Apr 2015 9:12 PM Date Created
  • Views 3209 views
  • Likes 5 likes
  • Comments 3 comments
  • rgb
  • uno
  • arduino
  • gsm_shield
Related
Recommended

SMS to light an LED!

markvenn
markvenn
7 Apr 2015

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.

image

(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

  • Sign in to reply
  • Former Member
    Former Member over 9 years ago

    Hi Markvenn, I have tried your code using a different setup. I used a SIM800L based GSM board with Arduino duemilanove.It worked as expected, if pins 2 and 3 are maintained as the serial communication lines. The problem is if you send an sms text other than red, green, blue, the setup hangs ie subsequent sms with red, green or blue will not work anymore until power is switched off and on.

    I want to find out if you experienced that and what could be the possible solution. Thanks

     

    Kobby

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 10 years ago

    markvenn - Mark, thanks for that; very useful. I've often wondered what could be done with an SMS shield and now I have a nice example that can easily be developed. Thanks again

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • markvenn
    markvenn over 10 years ago

    I forgot to put in the resistors on the fritzing breadboard :-(

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube