Arduino_Scuola
Published © GPL3+

Using SMS messages to control LED color

Use your GSM shield to control the output of Arduino through SMS messages.

IntermediateFull instructions provided30 minutes9,961
Using SMS messages to control LED color

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Arduino GSM Shield (integrated antenna)
×1
Tinkerkit Text LCD 16x02
×1
TinkerKit Rotary Potentiometer
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
NeoPixel Ring: WS2812 5050 RGB LED
Adafruit NeoPixel Ring: WS2812 5050 RGB LED
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code snippet #1

Arduino
#include <Adafruit_NeoPixel.h> // This includes Pixel library from ADAFRUIT. Awailable here https://github.com/adafruit/Adafruit_NeoPixel
#include <GSM.h> // This includes the GSM library, included in your Arduino IDE
#include <LiquidCrystal.h> // This includes the LCD library, included in your Arduino IDE

#define PINNUMBER "" // declaration of the constant PINNUMBER used to push the pin number of the SIM card
#define PIN 6 // declaration of the constant PIN, and setting it to output nr. 6
LiquidCrystal lcd(12, 11, 9, 8, 5, 4); // sets the pinnumbers used for the LCD display. Comment out if no LCD

Adafruit_NeoPixel LED = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ400); 
/* This sets values to the variables in the NeoPixel class function. 
First variable is the number of lights, 
then it is the output, previously defined as nr.6
Next variable sets how the pixels are wired
  NEO_GRB is for GRB bitstream (Green, Red, Blue) most neopixels
  NEO_RGB is for RGB bitstream (Red, Green, Blue)
Finally it addresses the frequency of the bitstream
  NEO_KHZ800 for 800 KHz bitstream
  NEO_KHZ400 for 400 KHz bitstream
*/


GSM gsmAccess; // opens up GSM access on the shield.
GSM_SMS sms;

char remoteNumber[20]; // Variable to store the remote number from the modem.
char c; //Variable for reading the sms messages from the SIM card
char m; //Variable for storing the first letter in each messages
int x=0; //Counter for the number of SMS messages processed
char lastm; //Variable to stop SMS replying
String lastMess; //for storing the whole message
int countR, countG, countB, countY, countW, countP, countO; //Counters for keeping information on how often each class is called

Code snippet #2

Arduino
void setup() {
  //Setup for SMS recieving. Serial setup makes it possible to monitor the status on your PC while connected
  Serial.begin(9600);
  Serial.println("SMS Recieving");
  
  // LCD setup is for the LCD screen. Comment it out if you dont set it up.
  lcd.begin(16, 2);
  lcd.print("SMS recieve");
  
  boolean notConnected = true; // this defines a variable that indicates no GSM connection if true
  while(notConnected) {  // if there is no connection, the program runs gsmAcess. gsmAcess returns GSM_READY when connected
    if(gsmAccess.begin(PINNUMBER)==GSM_READY) // If you have a PIN number on your SIM card, write it as parameters here in quotes. PINNUMBER"9876"
    notConnected = false;
    else {
      //messages printed on the serial monitor or LCD screen, then it tries again in 1000 milliseconds
      Serial.println("No connection");
 
  //Code for the LCD screen, comment out or remove if note used.
      lcd.setCursor(0, 1);
      lcd.print("No connection");
      
      delay(1000);
    }
  }
  // if connection is established
  Serial.println("GSM connected"); //GSM connected
  Serial.println("Waiting"); //Waiting for SMS
  
  //Code for the LCD screen, comment out or remove if note used.
  lcd.clear();
  lcd.print("GSM connected");
  lcd.setCursor(0,1);
  lcd.print("Waiting");
  
  //Setup for the lights
  
  LED.begin();
  LED.show();
}

Code snippet #3

Arduino
void loop() {
  lastMess = "";
  //reading messages
  if (sms.available()) // if there are SMS on the SIM card
  {
    x++; //message counter adds one
    sms.remoteNumber(remoteNumber, 20);    
    
    //Show the message on monitor
    Serial.println("Messages received from");
    Serial.println(remoteNumber); //Prints senders number on the serial monitor
    Serial.println("Message:");

  //Code for the LCD screen, comment out or remove if note used.
    //Show the message on LCD
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(remoteNumber);
    lcd.setCursor(0,1);
    lcd.print("Message: ");
    lcd.setCursor(11,1);    
    
    m = sms.peek(); //sms.peek looks up the first byte in the messages. 
    //By storing it in the global char variable m, it is possible to use it later for the swich-case function

    
    while(c=sms.read()) {
      String nextChar = String (c);
      String Mess = (lastMess + nextChar);
      lastMess = Mess;
      /*sms.read collects one digit of the messages at time and stores it in the variable c.
      This while loop builds a String variable "lastMess" from each byte in the messages for displaying.
      */
    }
    
    Serial.println(lastMess); // prints the whole messages on the monitor and LCD screen
    lcd.print(lastMess);
    

    switch (m){ //Checks the value of the variable m where the sketch has stored the first letter of the SMS. Runs the case according to the value.
   
      case 'R':    //sets the lights to red with the function eittiEinu
        oneByOne(LED.Color(0, 255, 0), 50); // the parameters of the function are two, the strip color, and the delay time in milliseconds for each pixel
        countR = countR++;
        Serial.print(countR);
        delay(5000); // Sets a delay time in milliseconds. The loop will not search for a new SMS untill this time is out
        break;
    
      case 'Z':    // turn off the lights
        oneByOne(LED.Color(0, 0, 0), 50);
        delay(5000);
        break;
     
      case 'Q':     // If you send the letter Q as SMS, you will recieve an SMS in return indicating the number of SMSÃ

Code snippet #4

Arduino
void oneByOne(uint32_t l, uint8_t bid) {

  if (m != lastm)
  {
        sms.beginSMS(remoteNumber);
        String Reply ="Thank you for your message : " + lastMess;
        sms.print(Reply);
        sms.endSMS();
  }

  for(uint16_t i=0; i<LED.numPixels(); i++) {
      LED.setPixelColor(i, l);
      LED.show();
      delay(bid);
  }//end for 
  
    sms.flush(); //discards the SMS message
    lastm=m;
}

Code snippet #5

Arduino
/*
***** SHINE ON YOU (Crazy Diamond) ***** https://www.youtube.com/watch?v=R0sw2CgysWY

GSM interface of LED/GSM lightpost. 
Designed and written for the annual meeting of Innovation Center of Iceland in February 2014.

This sketch makes it possible to control the color of digital RGB LED light pixels with SMS messages. It can easily be manipulated to control 
relays, motors or other outputs of your choice. The idea behind this project is to make outdoor lightning interactive. This can be useful for example to
light up your driveway as you drive home from work or even to remotely light up your house to repell burglars when you are on holiday. This can also be used
for setting up charging posts for electric cars, where the user signs up with his GSM phone to activate the charging and gets the bill asigned to his GSM account.

Created by; Thorsteinn Tomas Broddason, Fab Lab Saudarkrokur


***** The circuit *****
For this project you need;
*Arduino Uno
*Arduino GSM shield
*LCD 16x2 display,
*WS2801 LED pixels connected to PIN 6
*Potentio meter
*5V Power source for the LED string
*12V Power source for the UNO and GSM shield

Connect the LED pixels to ground on the Arduino board and the data cable of the pixels to pin 6. Wire the LCD screen to the board as explained here:
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
LCD D4 pin to digital pin 9
LCD D5 pin to digital pin 8
LCD D6 pin to digital pin 5
LCD D7 pin to digital pin 4

Wire a 10K pot to +5V and GND, with it's output to LCD screen's VO pin (pin3).
Schematics for the circuit awailable at www.fablab.is.
*/

// **** 1  Libraries, definitions and global variables ****

#include <Adafruit_NeoPixel.h> // This includes Pixel library from ADAFRUIT. Awailable here https://github.com/adafruit/Adafruit_NeoPixel
#include <GSM.h> // This includes the GSM library, included in your Arduino IDE
#include <LiquidCrystal.h> // This includes the LCD library, included in your Arduino IDE

#define PINNUMBER "" // declaration of the constant PINNUMBER used to push the pin number of the SIM card
#define PIN 6 // declaration of the constant PIN, and setting it to output nr. 6
LiquidCrystal lcd(12, 11, 9, 8, 5, 4); // sets the pinnumbers used for the LCD display. Comment out if no LCD

Adafruit_NeoPixel LED = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ400); 
/* This sets values to the variables in the NeoPixel class function. 
First variable is the number of lights, 
then it is the output, previously defined as nr.6
Next variable sets how the pixels are wired
  NEO_GRB is for GRB bitstream (Green, Red, Blue) most neopixels
  NEO_RGB is for RGB bitstream (Red, Green, Blue)
Finally it addresses the frequency of the bitstream
  NEO_KHZ800 for 800 KHz bitstream
  NEO_KHZ400 for 400 KHz bitstream
*/


GSM gsmAccess; // opens up GSM access on the shield.
GSM_SMS sms;

char remoteNumber[20]; // Variable to store the remote number from the modem.
char c; //Variable for reading the sms messages from the SIM card
char m; //Variable for storing the first letter in each messages
int x=0; //Counter for the number of SMS messages processed
char lastm; //Variable to stop SMS replying
String lastMess; //for storing the whole message
int countR, countG, countB, countY, countW, countP, countO; //Counters for keeping information on how often each class is called


// **** 2 Setup ****

void setup() {
  //Setup for SMS recieving. Serial setup makes it possible to monitor the status on your PC while connected
  Serial.begin(9600);
  Serial.println("SMS Recieving");
  
  // LCD setup is for the LCD screen. Comment it out if you dont set it up.
  lcd.begin(16, 2);
  lcd.print("SMS recieve");
  
  boolean notConnected = true; // this defines a variable that indicates no GSM connection if true
  while(notConnected) {  // if there is no connection, the program runs gsmAcess. gsmAcess returns GSM_READY when connected
    if(gsmAccess.begin(PINNUMBER)==GSM_READY) // If you have a PIN number on your SIM card, write it as parameters here in quotes. PINNUMBER"9876"
    notConnected = false;
    else {
      //messages printed on the serial monitor or LCD screen, then it tries again in 1000 milliseconds
      Serial.println("No connection");
 
  //Code for the LCD screen, comment out or remove if note used.
      lcd.setCursor(0, 1);
      lcd.print("No connection");
      
      delay(1000);
    }
  }
  // if connection is established
  Serial.println("GSM connected"); //GSM connected
  Serial.println("Waiting"); //Waiting for SMS
  
  //Code for the LCD screen, comment out or remove if note used.
  lcd.clear();
  lcd.print("GSM connected");
  lcd.setCursor(0,1);
  lcd.print("Waiting");
  
  //Setup for the lights
  
  LED.begin();
  LED.show();
}

// **** 3 The loop ****

void loop() {
  lastMess = "";
  //reading messages
  if (sms.available()) // if there are SMS on the SIM card
  {
    x++; //message counter adds one
    sms.remoteNumber(remoteNumber, 20);    
    
    //Show the message on monitor
    Serial.println("Messages received from");
    Serial.println(remoteNumber); //Prints senders number on the serial monitor
    Serial.println("Message:");

  //Code for the LCD screen, comment out or remove if note used.
    //Show the message on LCD
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(remoteNumber);
    lcd.setCursor(0,1);
    lcd.print("Message: ");
    lcd.setCursor(11,1);    
    
    m = sms.peek(); //sms.peek looks up the first byte in the messages. 
    //By storing it in the global char variable m, it is possible to use it later for the swich-case function

    
    while(c=sms.read()) {
      String nextChar = String (c);
      String Mess = (lastMess + nextChar);
      lastMess = Mess;
      /*sms.read collects one digit of the messages at time and stores it in the variable c.
      This while loop builds a String variable "lastMess" from each byte in the messages for displaying.
      */
    }
    
    Serial.println(lastMess); // prints the whole messages on the monitor and LCD screen
    lcd.print(lastMess);
    

    switch (m){ //Checks the value of the variable m where the sketch has stored the first letter of the SMS. Runs the case according to the value.
   
      case 'R':    //sets the lights to red with the function eittiEinu
        oneByOne(LED.Color(0, 255, 0), 50); // the parameters of the function are two, the strip color, and the delay time in milliseconds for each pixel
        countR = countR++;
        Serial.print(countR);
        delay(5000); // Sets a delay time in milliseconds. The loop will not search for a new SMS untill this time is out
        break;
    
      case 'Y':    //"Yellow"
        oneByOne(LED.Color(255, 255, 0), 50);
        countY = countY++;
        Serial.print(countY);
        delay(5000);
        break;
    
      case 'G':     //"Green"
        oneByOne(LED.Color(255, 0, 0), 50);
        countG = countG++;
        Serial.print(countG);
        delay(5000);
        break;
    
      case 'B':    //"Blue"
        oneByOne(LED.Color(0, 0, 255), 50);
        countB = countB++;
        Serial.print(countB);
        delay(5000);
        break;
    
      case 'W':    // "White"
        oneByOne(LED.Color(255, 255, 255), 50);
        countW = countW++;
        Serial.print(countW);
        delay(5000);
        break;

      case 'P':     // "Pink"
        oneByOne(LED.Color(20, 255, 147), 50);
        countP = countP++;
        Serial.print(countP);
        delay(5000);
        break;

      case 'O':    //"Orange"
        oneByOne(LED.Color(80, 255, 0), 50);
        countO = countO++;
        Serial.print(countO);
        delay(5000);
        break;
     
      case 'Z':    // turn off the lights
        oneByOne(LED.Color(0, 0, 0), 50);
        delay(5000);
        break;
     
      case 'Q':     // If you send the letter Q as SMS, you will recieve an SMS in return indicating the number of SMSÃ

Github

https://github.com/adafruit/Adafruit_NeoPixel

Credits

SteiniBrodda

Posted by Arduino_Scuola

Comments