Wireless Lamp Control using SIM800C GSM Module, Arduino Uno and DC Relay Module

Overview

GSM, or Global System for Mobile Communications, is the most popular cell phone standard in the world. This system operates in almost 200 countries. It is considered as the default global standard. In this project, the SIM800C is used to control the state of the lamp. The SIM800L is a cheap and compact GSM module. This module can perform the processes of larger GSM modules, with minimal space and requirements. On the other hand, a relay module is a type of switch that operates both electrically and mechanically. A relay module can switch the mains voltage.

Hardware Used

Software Used

Libraries Used

  • SoftwareSerial.h

Application Description

The SIM800C module uses GSM and GPRS (General Packet Radio Service) to send and receive data from mobile devices. In this project, the SIM800C uses GSM to transmit data through texting or SMS. The data is then used by the Arduino to control the switch of the relay, which turns the lamp on or off.

Set-up the Hardware

Figure 1 – Schematic Diagram

 

Figure 2 – SIM800C front view

Figure 3 – SIM800C back view

This project follows the schematic diagram shown in Figure 1. The Arduino pins 9 and 10 both represent the RX and TX pins needed for data transmission to the SIM800C module. Note that the connections of the RX and TX pins of the Arduino Uno and the SIM800C module are interconnected. The RX pin of the Arduino and the TX pin of the SIM800C module are connected, and the TX pin of the Arduino and the RX pin of the SIM800C module are also connected.

Also, this project uses a 5V DC relay module. Connecting the relay to the lamp is achieved by soldering the wires. Soldering the connections is a must since the breadboard cannot handle the mains voltage of 220V AC. However, jumper wires may be used to connect the pins of the relay module (VCC, GND, IN) to the Arduino Uno.

Set-up the Software

 

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10); 

char incomingByte; 
String incomingData;
bool atCommand = true;

int relay = 7;
int index = 0;
String number = "";
String message = "";

void setup(){
    Serial.begin(9600);
    mySerial.begin(9600);
    pinMode(relay, OUTPUT); 
    
    // Check if you're currently connected to SIM800C 
    while(!mySerial.available()){
      mySerial.println("AT");
      delay(1000); 
      Serial.println("connecting....");
    }
    Serial.println("Connected..");  
    mySerial.println("AT+CMGF=1");  //Set SMS Text Mode 
    delay(1000);  
    mySerial.println("AT+CNMI=1,2,0,0,0");  //procedure, how to receive messages from the network
    delay(1000);
    mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read unread messages
    Serial.println("Ready to receive Commands..");  
 }

void loop(){  
  if(mySerial.available()){
      delay(100);
      // Serial buffer
      while(mySerial.available()){
        incomingByte = mySerial.read();
        incomingData += incomingByte;
       }
        delay(10); 
        if(atCommand == false){
          receivedMessage(incomingData);
        }
        else{
          atCommand = false;
        }        
        //delete messages to save memory
        if (incomingData.indexOf("OK") == -1){
          mySerial.println("AT+CMGDA=\"DEL ALL\"");
          delay(1000);
          atCommand = true;
        }        
        incomingData = "";
  }
}

void receivedMessage(String inputString){
   
  //Get The number of the sender
  index = inputString.indexOf('"')+1;
  inputString = inputString.substring(index);
  index = inputString.indexOf('"');
  number = inputString.substring(0,index);
  Serial.println("Number: " + number);

  //Get The Message of the sender
  index = inputString.indexOf("\n")+1;
  message = inputString.substring(index);
  message.trim();
  Serial.println("Message: " + message);
        
  message.toUpperCase(); // uppercase the message received

  //turn LampON or OFF
  if (message.indexOf("ON") > -1){
      digitalWrite(relay, HIGH);
      Serial.println("Command: Lamp Turn On.");
   }          
  if (message.indexOf("OFF") > -1){
        digitalWrite(relay, LOW);
        Serial.println("Command: Lamp Turn Off.");
   }       
   delay(50);
  }

The code used in this project is shown above. To further understand the code shown above, this tutorial will explain the processes of the code per part, starting with the declaration of variables.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10); 

char incomingByte; 
String incomingData;
bool atCommand = true;

int relay = 7;
int index = 0;
String number = "";
String message = "";

The code shown above is its first part, which is the declaration and initialization of variables. Also, this is where the library SoftwareSerial is included in the code. The SoftwareSerial library is used to incorporate serial communication onto the different pins of the Arduino Uno rather than using pins 0 and 1 for serial communication. Serial communication is a type of communication wherein data is transmitted and received using a communication channel one bit at a time.

To use the different pins of the Arduino Uno as the RX and TX pins of the serial communication, the Arduino pins must be declared. The second line of the code shown above states pins 9 and 10 as the RX and TX pins.

The next portion of the code is the declaration of variables needed. Declaring variables is a vital task when programming since this is where the data type of the variables are first indicated.

Lastly, the initialization of variables was created. This is where the variables first acquired their initial values. Their values may change as the code progresses.

void setup(){
    Serial.begin(9600);
    mySerial.begin(9600);
    pinMode(relay, OUTPUT); 
    
    // Check if you're currently connected to SIM800C
    while(!mySerial.available()){
      mySerial.println("AT");
      delay(1000); 
      Serial.println("connecting....");
    }
    Serial.println("Connected..");  
    mySerial.println("AT+CMGF=1");  //Set SMS Text Mode 
    delay(1000);  
    mySerial.println("AT+CNMI=1,2,0,0,0");  //procedure, how to receive messages from the network
    delay(1000);
    mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read unread messages
    Serial.println("Ready to receive Commands..");  
 }

The second part of the code is the setup function. This is where the different Arduino pins needed are assigned as either inputs or outputs. Also, this is where the baud rate is defined in both the hardware and software serial communication. Baud rate is defined as the rate of transfer of information in a communication channel. In this project, a baud rate of 9600 is used. This means that the serial port is capable of sending and receiving a maximum of 9600 bits per second.

Also, this project uses the AT commands of the SIM800C module for it to work as it is expected to. AT commands are instructions or directions needed to control a modem or module to perform a set of operations. In this case, the module should read messages, receive messages, and set its mode to SMS text.

void loop()
{  
  if(mySerial.available()){
  
      delay(100);
      
      // Serial buffer
      while(mySerial.available()){
        incomingByte = mySerial.read();
        incomingData += incomingByte;
        
       }
        
        delay(10); 
        if(atCommand == false){
          receivedMessage(incomingData);
        
        }else{
          atCommand = false;
        }
        
        //delete messages to save memory
        if (incomingData.indexOf("OK") == -1){
          mySerial.println("AT+CMGDA=\"DEL ALL\"");
          delay(1000);
          atCommand = true;
        }
        
        incomingData = "";
  }
}

The third part of the code is the loop function. This is where the Arduino does its processes and repeats it continuously. The first main portion of the loop function is the if-statement which checks the availability of the connections between the SIM800C and the Arduino Uno.  Once the connection is available, the loop function uses another if-else statement to receive the message. Lastly, another if-statement and an AT command are used to delete messages to save memory.

void receivedMessage(String inputString){
  
 
  //Get The number of the sender
  index = inputString.indexOf('"')+1;
  inputString = inputString.substring(index);
  index = inputString.indexOf('"');
  number = inputString.substring(0,index);
  Serial.println("Number: " + number);

  //Get The Message of the sender
  index = inputString.indexOf("\n")+1;
  message = inputString.substring(index);
  message.trim();
  Serial.println("Message: " + message);
        
  message.toUpperCase(); // uppercase the message received

  //turn LampON or OFF
  if (message.indexOf("ON") > -1){
      digitalWrite(relay, HIGH);
      Serial.println("Command: Lamp Turn On.");
   }
          
  if (message.indexOf("OFF") > -1){
        digitalWrite(relay, LOW);
        Serial.println("Command: Lamp Turn Off.");
   }
          
   delay(50);
  }

The fourth and last part of the code is the receivedMessage() function. This is where the Arduino compares the received SMS to perform a task. In this case, the function gets the number and the message. The message is then compared using if-statements.

When the message contains the word “ON,” the Arduino gives a high signal to the relay. This, in turn, allows the relay to switch and allows current to flow through, which turns on the lamp. On the other hand, the Arduino gives a low signal to the relay module once the compared message contains the word “OFF.” This turns off the lamp.

Conclusion

The SIM800C GSM module is not only limited to turning off lamps or switching relays. This is just an introduction to the capabilities of the said module. It is always up to you to mess around. The sky is the limit!

References

The post Wireless Lamp Control using SIM800C GSM Module, Arduino Uno and DC Relay Module appeared first on CreateLabz.

ArduinoAutomationCellularDc relayGprsGsmKnowledgebaseRelaySim800cSoftwareserial

Leave a comment

All comments are moderated before being published