Hello and very happy new year guys!
This is the very first techmagister tutorial in 2017. In this
tutorial we are going to make a step forward along our journey in
home automation. If you missed the first tutorial:
Today we are going to make a home
automation system to control your domestic switches using a simple
sms. You just type “ON” and send it to the system from anywhere
in the world. Then the switch will turn itself ON, simple as that. To
do the project we need some stuffs:
- SIM 900 GSM module.
This is the main module for the
project. SIM 900 is also one of the most popular module for IOT based
products. .Now there are lots of models available in the market for
cheaper price tags.SIM 900 module is a GSM device which can be
operated through GSM signals. We can input a SIM to the module and
using simple text message commands we can make some outputs. SIM 900
module can configure using “AT” commands easily. Its schematic
can be shown as follows.
- Arduino module
This device is used to communicate with
SIM 900 module and print its outputs in terminal. You can use any
kind of arduino device for this.(UNO R3., MEGA, MINI etc.).
- Wires ,1 LED,relay DC 5V relay module, Wall adapter, bulb and a holder.
Project Procedure.
1. Take the SIM 900 module and Arduino
device as follows
When
connecting GSM module with the Arduino it is necessary to use an
external DC power supply for the Arduino because when GSM module is
at its top performance the current supply from the USB (500 mA) is
not enough to powerup the modules. When only using the USB power,
module was tend to misbehave.
2. Connect
the arduino to the PC and configure connected port in arduino App
(com port).
3. Insert
a working SIM to SIM 900 module.
4. Now
go back to the arduino software and paste following code in a new
project and deploy it into the arduino.
#include <SoftwareSerial.h>
char incoming_char=0;
String data;
char pos = 0;
SoftwareSerial gsm (10, 11); // RX, TX
void setup()
{
delay(3000);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(8, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(8, LOW);
// turn the LED off by making the voltage LOW
delay(5000);
// wait for 10 seconds
Serial.begin(9600);
gsm.begin(9600);
delay(10000);
Serial.println();
gsm.println("ATE0");
gsm.write((byte)34);
//ASCII equivalent of "ENTER"
gsm.println("AT"); // set SMS mode to text
gsm.write((byte)34);
gsm.println("AT+CMGF=1");
// set SMS mode to text
gsm.write((byte)34);
gsm.print("AT+CNMI=2,2,0,0,0");
/* blurt out contents of
new SMS upon receipt to
the GSM shield's serial out*/
gsm.write((byte)34);
while(gsm.available())
Serial.write((char)gsm.read());
delay(500);
while(gsm.available())
Serial.write((char)gsm.read());
delay(500);
gsm.println();
while(gsm.available())
delay(500);
Serial.println();
delay(1000);
// The GSM module needs to return to an OK status
}
void loop() {
while(gsm.available() >0){
while(gsm.available() >0)
{
incoming_char=gsm.read();
//Get the character from the cellular serial port.
delay(20);
data = data + incoming_char;
//store all data to an array
} delay(12);
Serial.println("printing data");
Serial.println(data);
pos = data.indexOf('O');
// searching for letter "o"
if (data.charAt(pos + 1) == 'N') {
//search for the letter "N" after the letter "O"
Serial.println("ON recieved switching ON");
digitalWrite(13, HIGH);
gsm.println("AT+CMGD=1,4"); // delete all SMS
gsm.write((byte)34);
delay(2000);
Serial.println("memory cleared");}
if (data.charAt(pos + 1) == 'F') {
//search for the letter "F" after the letter "O"
Serial.println("OFF recieved switching OFF");
digitalWrite(13, LOW);
gsm.println("AT+CMGD=1,4"); // delete all SMS
gsm.write((byte)34);
delay(2000);
Serial.println("memory cleared");
}
delay(1000);}}
char incoming_char=0;
String data;
char pos = 0;
SoftwareSerial gsm (10, 11); // RX, TX
void setup()
{
delay(3000);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(8, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(8, LOW);
// turn the LED off by making the voltage LOW
delay(5000);
// wait for 10 seconds
Serial.begin(9600);
gsm.begin(9600);
delay(10000);
Serial.println();
gsm.println("ATE0");
gsm.write((byte)34);
//ASCII equivalent of "ENTER"
gsm.println("AT"); // set SMS mode to text
gsm.write((byte)34);
gsm.println("AT+CMGF=1");
// set SMS mode to text
gsm.write((byte)34);
gsm.print("AT+CNMI=2,2,0,0,0");
/* blurt out contents of
new SMS upon receipt to
the GSM shield's serial out*/
gsm.write((byte)34);
while(gsm.available())
Serial.write((char)gsm.read());
delay(500);
while(gsm.available())
Serial.write((char)gsm.read());
delay(500);
gsm.println();
while(gsm.available())
delay(500);
Serial.println();
delay(1000);
// The GSM module needs to return to an OK status
}
void loop() {
while(gsm.available() >0){
while(gsm.available() >0)
{
incoming_char=gsm.read();
//Get the character from the cellular serial port.
delay(20);
data = data + incoming_char;
//store all data to an array
} delay(12);
Serial.println("printing data");
Serial.println(data);
pos = data.indexOf('O');
// searching for letter "o"
if (data.charAt(pos + 1) == 'N') {
//search for the letter "N" after the letter "O"
Serial.println("ON recieved switching ON");
digitalWrite(13, HIGH);
gsm.println("AT+CMGD=1,4"); // delete all SMS
gsm.write((byte)34);
delay(2000);
Serial.println("memory cleared");}
if (data.charAt(pos + 1) == 'F') {
//search for the letter "F" after the letter "O"
Serial.println("OFF recieved switching OFF");
digitalWrite(13, LOW);
gsm.println("AT+CMGD=1,4"); // delete all SMS
gsm.write((byte)34);
delay(2000);
Serial.println("memory cleared");
}
delay(1000);}}
About
the code:
This code has been made using AT commands . What the code
does is if it receives an sms saying “ON” it checks the character
after “O” & if the next character is “N”, it simply put
the output pin to “HIGH”(5V) state. If the next character is “F”
it makes the output “LOW”(0V).
5. Now
send an SMS to the SIM which has inserted into the SIM900 module with
“ON”. The connected LED should turn on.
Now
you can extend this project to operate household lights as follows.
6. Connect your relay module with an AC
adapter and an AC bulb holder as follows
7. Then connect your output signal pin to
DC 5V input in your relay module.
8. Then plug the adapter to an AC 240V
outlet .
Make sure both arduino and SIM900 i=has
powered up correctly and then send a SMS with “ON” message. Bulb
should be turned “ON”.
If not, refresh the Arduino using reset
button and try again. Make a comment below with your results. Like
our Facebook Page to stay up to date with techmagister's awesome
tutorials. See you again in another tutorial.
1 Comments
thank you, a lot of thank you very very much, you are great, its really very good tutorial, i saw many websites and many videos but i failed every time and code was not working, after one month i saw your website and finally long time i made gsm based home automation with text message, your code is working with one time , but i am facing a problem, please solve it, when i send message ON then relay work off but when i send OFF command then its not working, how can i off this you dont write about this ,
ReplyDeleteand i want to use 4 channel relay, so please tell me how can off relay and what is command to do off and please send me code for 4 channel relay,
please sir solve this problem