DIY – GSM Relay Control Using Smart Switch Board

john October 29, 2017 No Comments

The main objective of this project is to control the ON/OFF status of electrical appliances at home or company remotely using a cell phone and relay switch. The appliances may include lights, fans, pump, and air conditioners and so on. Though all the household items can be automated to change their ON/OFF status, our prototype model GSM remote control circuit uses 8 electrical appliances.

The project includes multiple users to control the appliances and uses unique SMS formats for each application. The switch board can be operated by SMS feature of the mobile phones to ON/OFF the electrical appliances in home or office etc.., only by the registered users. The system validates the users and also the message sent by the user. The list of options served by the system is given below.

  • Get ON/OFF status of the Loads
  • Get Users List
  • Add an User
  • Remove an User
  • Get Account Balance of the SIM
  • Reset Message Counter

GSM Relay Control Board – Circuit Diagram

GSM Relay Control Board - Circuit Diagram

GSM Relay Control Board – Circuit Diagram

Features of the Smart Switch Board

 What happens if an unregistered user operates?

If an unregistered user tries to operate, the system replies a template “Unregistered User. Please contact admin.” The system also sends an SMS to the first user in the users list or the programmed user (if no user is added) a template “Tried to operate the switch board.” and the contact number of that user. Below is the real-time simulation output.

 

The Program/Code

Download Program

How to add a user to the system?

To add or register a new user to the system, any of the previously registered users or the programmed user should send number ‘3’ as a message to the system. Then the user will receive a message template having the user name and the contact number. By editing and sending the received template with the details of the user to be added, the system will add the user. Below is the code snippet for adding a user.

GSM Relay Control - Code for Adding a User

GSM Relay Control – Code for Adding a User

The above code adds the user details in the EEPROM as well as the user’s list. Edit the user name up to 10 characters and the 10-digit contact number within the same lines of the received message and send the same to the system. The user will be added to the system. Below is the simulation output for adding a user. Now, let’s add the previously shown unregistered user.

 

 

How to know the ON/OFF status of the loads?

The user can request to send the ON/OFF status of the loads through option 1. The relay states are read from the output port which is dedicated to operate the relays. This data is used to frame the template and an SMS is sent to the requested user. The logic in framing the message is to use the ‘for loop’ to each load and then if the array index reaches the ON/OFF state, the state of that load is placed.

GSM Relay Control - Loads ON OFF Status template

Loads ON OFF Status template

 

The message that contains the ON/OFF status of the loads is framed such that, the symbol ‘=’ is used as an identifier and the character after this ‘=’ symbol is read/written as the state of the load. The ON state is represented by numeric ‘1’ and the OFF state is represented by numeric ‘0’. In order to ON or OFF the load, this numeric character needs to be edited and the edited message is sent to the system.  The number of ‘=’ symbols and the character after this symbol are verified whenever this template is received by the system. If the number of ‘=’ symbols and the number of loads are not equal, or if there is a character other than 0 or 1 after any of the ‘=’ symbols then the message is considered as an invalid template and a template “Invalid message template” along with the list of options is sent to the user. This feature is useful to a new user until the user gets habituated to the system.

GSM Relay Control - Invalid message template notification

Invalid message template notification

 

The names of the load are defined in the program and the verification logic doesn’t consider the name but the message framing considers the number of characters of the load. Whenever a load ON/OFF request is received and found it as a valid template, the program logic searches for the ‘=’ symbol and the next character is read as the respective load state and it is stored in the loads status array. This is done until 8 loads are read and the relay states are modified according to the request. Below is the code snippet for processing the message from a valid user.

GSM Relay Control - Reading the load control request and message validation

Reading the load control request and message validation

How to turn ON/OFF the loads?

The same template which is received in response to the loads status request is used to turn ON/OFF the load. The user has to edit the 1’s and 0’s in this template and send to the system. The ON state is represented by numeric ‘1’ and the OFF state is represented by numeric ‘0’. Below is the real-time simulation output for requesting the load status and switching the loads ON/OFF.

 

 

How to Get the Users List?

As the system is operated only by the registered users, it is required to create a list of users. This list is stored in the EEPROM and is read after every restart. The data read from the EEPROM is stored in the RAM of the microcontroller and is used for the user validity verification. The mobile numbers are 10-digits in size and are stored in two-dimensional arrays along with the contact names. Whenever this option is requested by the user, the list of users containing the contact name and the 10-digit mobile number is sent to the user. The system is programmed to store 5 users and this can be easily modified. In addition to these 5 users stored in the EEPROM, it is better to feed a contact within the program code to operate the system prior to adding the users in real-time. This contact cannot be removed after programming the microcontroller. Here, the zero index of the user’s list is programmed with a permanent contact number and this is not shown in the user’s list when requested by the user. The same contact can be added again to the user’s list.

GSM Relay Control - Users List

GSM Relay Control – Users List

How is the SMS processed?

The systems of this kind are designed to operate based on some pre-defined templates. Hence, the message needs to be read as per the design. When an SMS is received from a user, the SMS is processed to fetch the useful details like the contact number, contact name and finally the message. Below is the code snippet for SMS processing. For a detailed explanation, please go through our article on Interfacing GSM Module – Interfacing GSM Module Using Proteus

GSM Relay Control - Program - Reading the data from an SMS

Program – Reading the Data from an SMS

 

After extracting the data from the received SMS, the user is verified whether valid or not. If valid then, the message is processed and respective action is performed. If the user is not registered then, the notifications are sent.

How is the User verified?

For every received SMS, the contact number of the user is verified, whether it is in the user’s list or not. If the contact number exists in the list then, the user is considered as a valid user and the message is processed. In the other case, if the contact number doesn’t exist in the list then, an SMS is sent to the user stating about the invalidity. The code snippet for the user validity verification is given below.

GSM Relay Control - Program - User Validity Verification

Program – User Validity Verification

How to remove a registered user?

The system can hold 5 users in its memory and this can be increased by modifying the program. Any registered user can remove the user from the memory. The option number 4 is dedicated for this purpose. By sending 4 to the system as a message, the system replies the user’s list along with their indices. Then the user has to remember the index of the user’s contact number that should be removed and send that index as a template.

For example, send “RU1” to remove the user 1. The system on receiving the message with this RU template will read the index of the user to be removed and removes the respective user permanently from the memory including the EEPROM. Then the user’s list is adjusted such that the list has no empty indices i.e. if a user with index 2 is removed from the list containing 4 users, then the fourth user is shifted to the second place. The address locations of the fourth user are filled with 255. Thus, a registered user can be removed.

If there is only one registered user in the list then, it is reminded to the user, because if this user is removed then the system can be operated only by the programmed user. After removing the user, the number of users is decremented and the resultant is stored in the zero location of the EEPROM for future usage. The code snippet for the user removal is provided below.

GSM Relay Control - Program - Removing a registered user

Program – Removing a registered user

How to Get SIM account balance?

The dialing feature of the GSM module is used for this purpose. By sending ‘5’ as a message to the system, the system understands that the account balance is requested and it dials *123# and stores the received account statement. Then after, the system will forward the same account statement to the requested user. The system counts the number of messages sent by it. A variable named ‘message count’ is incremented after sending an SMS. This message count is sent along with the account balance so that the user can recharge the SMS plan (if necessary) on beforehand.

GSM Relay Control - SIM account statement

GSM Relay Control – SIM account statement

How to Reset the message count?

The message count variable is incremented after sending an SMS. This is a two digit number and resets to zero after 99. This variable can be reset to zero intentionally by sending ‘6’ as a message. This feature is useful when an SMS plan is used. After every recharge, this can be reset to know the number of messages sent by the system then after. The message count is updated in the EEPROM after every sent message and also after the message count reset operation.

 

How is the EEPROM updated?

The EEPROM data of the used locations will be a decimal value 255 in most of the cases, but it can be an unknown value also. It is better to load the details of one user at the time of loading the program into the microcontroller or write decimal value 255 in the locations where the user’s list is stored. The EEPROM data is read every time when the microcontroller is powered ON or reset. The user’s name and the contact number are given a size of 10 address locations and the unused locations within the contacts list are filled with the decimal value 255. The address locations are user-defined. The code snippet for reading and writing the EEPROM data is shown below.

GSM Relay Control - Code to Read Write of EEPROM Data

Code to Read Write of EEPROM Data

 

Below is the simulation that shows how the EEPROM is updated.

 

Leave a Reply

Your email address will not be published. Required fields are marked *