{"id":43696,"date":"2017-10-12T13:24:36","date_gmt":"2017-10-12T13:24:36","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=43696"},"modified":"2019-04-02T10:23:26","modified_gmt":"2019-04-02T10:23:26","slug":"control-a-12v-lamp-via-sms-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/control-a-12v-lamp-via-sms-with-arduino\/","title":{"rendered":"Control a 12V Lamp via SMS with Arduino"},"content":{"rendered":"<p>In this tutorial we&#8217;re going to show you how you can control a 12V lamp via SMS using an Arduino UNO, a relay module, and the SIM900 GSM shield.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-43803\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-thumbnail.jpg?resize=1000%2C560&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1000\" height=\"560\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-thumbnail.jpg?w=1000&amp;quality=100&amp;strip=all&amp;ssl=1 1000w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-thumbnail.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-thumbnail.jpg?resize=768%2C430&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p>Before proceeding with this tutorial we recommend the following resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Guide for Relay Module with Arduino<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/sim900-gsm-gprs-shield-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Guide to SIM900 GSM GPRS Shield with Arduino<\/a><\/li>\n<\/ul>\n<h2>First, watch the video demonstration<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/xtUG3LKriAE?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Project overview<\/h2>\n<p>This project uses the SIM900 GSM shield to receive and send SMS with the Arduino. This projects aims to:<\/p>\n<ul>\n<li>turn a 12V lamp on when you send an SMS to the Arduino with the text &#8220;ON&#8221;<\/li>\n<li>turn a 12V lamp off when you send an SMS to the Arduino with the text &#8220;OFF&#8221;<\/li>\n<li>you can request the lamp state by sending an SMS to the Arduino with the text &#8220;STATE&#8221;, the Arduino should reply back with the text &#8220;lamp is on&#8221; or &#8220;lamp is off&#8221;<\/li>\n<\/ul>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-43745 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-Arduino.jpg?resize=700%2C470&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"470\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-Arduino.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-Arduino.jpg?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>Parts required<\/h2>\n<p>In this project you need to connect the Arduino to the GSM shield and to a relay module connected to a 12V lamp. Here&#8217;s a complete list of the parts required for this project (click the links below to find the best price at\u00a0<a href=\"https:\/\/makeradvisor.com\/tools\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maker Advisor<\/a>):<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/sim900-gsm-gprs-shield\/\" target=\"_blank\" rel=\"noopener noreferrer\">SIM900 GSM Shield<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/5v-2a-power-adaptor-dc-jack\/\" target=\"_blank\" rel=\"noopener noreferrer\">5V 2A power adaptor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/5v-2-channel-relay-module-optocoupler\/\" target=\"_blank\" rel=\"noopener noreferrer\">Relay module<\/a><\/li>\n<li>12V lamp<\/li>\n<li>12V lamp holder<\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/dc-power-male-plug-jack-adapter-2-1mm\/\" target=\"_blank\" rel=\"noopener noreferrer\">Male DC barrel jack 2.1mm<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/12v-2a-power-adaptor-dc-jack\/\" target=\"_blank\" rel=\"noopener noreferrer\">12V power adaptor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">Breadboard<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\">Jumper Wires<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/24v12v5v-power-supply\/\" target=\"_blank\" rel=\"noopener noreferrer\">Optional &#8211; 12V\/5V power supply<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Preparing the GSM Shield<\/h2>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-43806\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/SIM900-GSM-shield_f.jpg?resize=700%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"421\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/SIM900-GSM-shield_f.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/SIM900-GSM-shield_f.jpg?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>To use the SIM900 GSM shield:<\/p>\n<p><strong>1)<\/strong>\u00a0You need an active SIM card. We recommend using a SIM card with a prepaid plan for testing purposes.<\/p>\n<p><strong>2)<\/strong> You have to turn off the SIM card pin lock. Insert the SIM card in your smartphone and turn off the pin lock in the phone security settings.<\/p>\n<p><strong>3)\u00a0<\/strong>The GSM shield should be powered using an external 5V power supply that can provide 2A, or 9V 1A, or 12V 1A.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-adapter.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-42383\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-adapter.jpg?resize=700%2C640&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"640\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-adapter.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-adapter.jpg?resize=300%2C274&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><strong>4)\u00a0<\/strong>We&#8217;re going to use software serial to communicate with the shield. So, on the serial port select, make sure the jumper cap is connected as shown in figure below to use software serial.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/software-serial.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-42415\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/software-serial.jpg?resize=700%2C296&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"296\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/software-serial.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/software-serial.jpg?resize=300%2C127&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><strong>5)\u00a0<\/strong>You can turn on\/off the shield by pressing the power key, or you can turn it on automatically with the Arduino via the D9 pin. To automatically turn the shield on and off, you need to solder the R13 connections as shown in the figure below.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-43808\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/R13.jpg?resize=685%2C338&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"685\" height=\"338\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/R13.jpg?w=685&amp;quality=100&amp;strip=all&amp;ssl=1 685w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/R13.jpg?resize=300%2C148&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/p>\n<p><strong>6)\u00a0<\/strong>If this is your first time dealing with the GSM shield, we recommend you reading the Preliminary Steps in our<a href=\"https:\/\/randomnerdtutorials.com\/sim900-gsm-gprs-shield-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\"> GSM shield guide here<\/a>.<\/p>\n<p><strong>7)\u00a0<\/strong>Insert the SIM card into the SIM card holder.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/insert-SIM.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-42389\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/insert-SIM.jpg?resize=700%2C245&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"245\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/insert-SIM.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/insert-SIM.jpg?resize=300%2C105&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><strong>8)\u00a0<\/strong>Finally, wire the SIM900 GSM shield to the Arduino by following the schematic below.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/GSM-Arduino-circuit.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-43811\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/GSM-Arduino-circuit.jpg?resize=1200%2C738&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"738\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/GSM-Arduino-circuit.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/GSM-Arduino-circuit.jpg?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/GSM-Arduino-circuit.jpg?resize=768%2C472&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/GSM-Arduino-circuit.jpg?resize=1024%2C630&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p><strong>9) <\/strong>You should power up the SIM900 GSM shield using the external 5V power supply. Make sure you select the external power source with the toggle switch next to the DC jack.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-42416 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-select.png?resize=338%2C227&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"338\" height=\"227\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-select.png?w=338&amp;quality=100&amp;strip=all&amp;ssl=1 338w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/power-select.png?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 338px) 100vw, 338px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong> You can test it the shield is working properly by sending AT comments from the Arduino IDE with an FTDI programmer &#8211; <a href=\"https:\/\/randomnerdtutorials.com\/sim900-gsm-gprs-shield-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">check\u00a0Guide to SIM900 GSM GPRS Shield with Arduino<\/a><\/p>\n<h2>Wiring the Relay Module<\/h2>\n<p>In this project we control a 12V lamp. However, this project can be easily adapted to control mains voltage or other electronics appliances. Check our <a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Guide for Relay Module with Arduino<\/a> to learn how to control mains voltage with a relay.<\/p>\n<p>We are using the following 12V lamp and its corresponding lamp holder.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-43801\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/12-V-lamp-and-lamp-holder.jpg?resize=700%2C420&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"420\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/12-V-lamp-and-lamp-holder.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/12-V-lamp-and-lamp-holder.jpg?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>Add the relay module and the lamp holder to the previous circuit.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/connect-relay-12-V-lamp.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-43739 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/connect-relay-12-V-lamp.jpg?resize=1200%2C715&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"715\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/connect-relay-12-V-lamp.jpg?w=1880&amp;quality=100&amp;strip=all&amp;ssl=1 1880w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/connect-relay-12-V-lamp.jpg?resize=300%2C179&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/connect-relay-12-V-lamp.jpg?resize=768%2C458&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/connect-relay-12-V-lamp.jpg?resize=1024%2C610&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>For safety purposes, we&#8217;re placing our relay inside a plastic box enclosure.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-43807\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/relay-inside-case.jpg?resize=700%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"422\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/relay-inside-case.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/relay-inside-case.jpg?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>Code<\/h2>\n<p>Below you can find the code for this project. <strong>Make sure you edit the code with the phone number the Arduino should reply to<\/strong>. We recommend you reading the code explanation before uploading the code.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Complete Project Details http:\/\/randomnerdtutorials.com\n *\/\n\n\/\/ Include Software Serial library to communicate with GSM\n#include &lt;SoftwareSerial.h&gt;\n\n\/\/ Configure software serial port\nSoftwareSerial SIM900(7, 8);\n\n\/\/ Variable to store text message\nString textMessage;\n\n\/\/ Create a variable to store Lamp state\nString lampState = &quot;HIGH&quot;;\n\n\/\/ Relay connected to pin 12\nconst int relay = 12;\n\nvoid setup() {\n  \/\/ Automatically turn on the shield\n  digitalWrite(9, HIGH);\n  delay(1000);\n  digitalWrite(9, LOW);\n  delay(5000);\n  \n  \/\/ Set relay as OUTPUT\n  pinMode(relay, OUTPUT);\n\n  \/\/ By default the relay is off\n  digitalWrite(relay, HIGH);\n  \n  \/\/ Initializing serial commmunication\n  Serial.begin(19200); \n  SIM900.begin(19200);\n\n  \/\/ Give time to your GSM shield log on to network\n  delay(20000);\n  Serial.print(&quot;SIM900 ready...&quot;);\n\n  \/\/ AT command to set SIM900 to SMS mode\n  SIM900.print(&quot;AT+CMGF=1\\r&quot;); \n  delay(100);\n  \/\/ Set module to send SMS data to serial out upon receipt \n  SIM900.print(&quot;AT+CNMI=2,2,0,0,0\\r&quot;);\n  delay(100);\n}\n\nvoid loop(){\n  if(SIM900.available()&gt;0){\n    textMessage = SIM900.readString();\n    Serial.print(textMessage);    \n    delay(10);\n  } \n  if(textMessage.indexOf(&quot;ON&quot;)&gt;=0){\n    \/\/ Turn on relay and save current state\n    digitalWrite(relay, LOW);\n    lampState = &quot;on&quot;;\n    Serial.println(&quot;Relay set to ON&quot;);  \n    textMessage = &quot;&quot;;   \n  }\n  if(textMessage.indexOf(&quot;OFF&quot;)&gt;=0){\n    \/\/ Turn off relay and save current state\n    digitalWrite(relay, HIGH);\n    lampState = &quot;off&quot;; \n    Serial.println(&quot;Relay set to OFF&quot;);\n    textMessage = &quot;&quot;; \n  }\n  if(textMessage.indexOf(&quot;STATE&quot;)&gt;=0){\n    String message = &quot;Lamp is &quot; + lampState;\n    sendSMS(message);\n    Serial.println(&quot;Lamp state resquest&quot;);\n    textMessage = &quot;&quot;;\n  }\n}  \n\n\/\/ Function that sends SMS\nvoid sendSMS(String message){\n  \/\/ AT command to set SIM900 to SMS mode\n  SIM900.print(&quot;AT+CMGF=1\\r&quot;); \n  delay(100);\n\n  \/\/ REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER\n  \/\/ USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS\n  SIM900.println(&quot;AT + CMGS = \\&quot;XXXXXXXXXX\\&quot;&quot;); \n  delay(100);\n  \/\/ Send the SMS\n  SIM900.println(message); \n  delay(100);\n\n  \/\/ End AT command with a ^Z, ASCII code 26\n  SIM900.println((char)26); \n  delay(100);\n  SIM900.println();\n  \/\/ Give module time to send SMS\n  delay(5000);  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Arduino_Control_a_12V_Lamp_via_SMS.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<p>The code is pretty straightforward to understand. Continue reading to understand what each section of code aims to do.<\/p>\n<h3>Import library<\/h3>\n<p>The Arduino communicates with the shield using software serial communication. So, you start by including the\u00a0<strong>SoftwareSerial.h<\/strong>\u00a0library.<\/p>\n<pre>\/\/ Include Software Serial library to communicate with GSM\r\n<strong>#include &lt;SoftwareSerial.h&gt;<\/strong><\/pre>\n<h3>Software serial port<\/h3>\n<p>Then, you create a software serial port on pins 7 and 8 (pin 7 is set as RX and 8 as TX).<\/p>\n<pre>\/\/ Configure software serial port\r\n<strong>SoftwareSerial SIM900(7, 8);<\/strong><\/pre>\n<h3>Creating variables<\/h3>\n<p>You create a <strong>textMessage<\/strong> variable that will hold the text message received by the Arduino; and another variable to store the current lamp state (relay state), called <strong>lampState<\/strong>. The lampState variable is set to HIGH by default, because we want the relay off by default. The relay works with inverted logic, so you need to set it to HIGH to turn it off.<\/p>\n<pre>\/\/ Variable to store text message\r\n<strong>String textMessage;<\/strong>\r\n\r\n\/\/ Create a variable to store Lamp state\r\n<strong>String lampState = \"HIGH\";<\/strong><\/pre>\n<p>You also assign the pin number the relay is connected to, pin 12.<\/p>\n<pre>\/\/ Relay connected to pin 12\r\n<strong>const int relay = 12;<\/strong><\/pre>\n<h3>setup()<\/h3>\n<p>In the setup() function, you start by turning on the GSM shield. The following lines are the equivalent of pressing the shield &#8220;power&#8221; button.<\/p>\n<pre><strong>digitalWrite(9, HIGH);\r\ndelay(1000);\r\ndigitalWrite(9, LOW);\r\ndelay(5000);<\/strong><\/pre>\n<p>The relay is set as an output, and its state is off by default.<\/p>\n<pre>\/\/ Set relay as OUTPUT\r\n<strong>pinMode(relay, OUTPUT);<\/strong>\r\n\r\n\/\/ By default the relay is off\r\n<strong>digitalWrite(relay, HIGH);<\/strong><\/pre>\n<p>Then, you initialize serial communication with the shield, and give it some time to log on to the network.<\/p>\n<pre>\/\/ Initializing serial commmunication\r\n<strong>Serial.begin(19200); \r\nSIM900.begin(19200);\r\n<\/strong>\r\n\/\/ Give time to your GSM shield log on to network\r\n<strong>delay(20000);\r\nSerial.print(\"SIM900 ready...\");<\/strong><\/pre>\n<p>The shield is set to SMS mode with the following:<\/p>\n<pre>\/\/ AT command to set SIM900 to SMS mode\r\n<strong>SIM900.print(\"AT+CMGF=1\\r\"); \r\ndelay(100);<\/strong>\r\n\/\/ Set module to send SMS data to serial out upon receipt \r\n<strong>SIM900.print(\"AT+CNMI=2,2,0,0,0\\r\");\r\ndelay(100);<\/strong><\/pre>\n<h3>loop()<\/h3>\n<p>In the loop(), you read the incoming messages and compare if those messages contain the text ON, OFF or STATE. Accordingly to the message content, the Arduino will perform different tasks.<\/p>\n<p>So, you start by checking whether there are messages to read. If there are, the message will be saved on the <strong>textMessage<\/strong> variable. For debugging purposes, the message is displayed on the serial monitor.<\/p>\n<pre>if(SIM900.available() &gt;0) {\r\n textMessage = SIM900.readString();\r\n Serial.print(textMessage); \r\n delay(10);\r\n}<\/pre>\n<h4>Turn the lamp on<\/h4>\n<p>If the message received contains the text &#8220;ON&#8221;, then the relay will be turned on, turning on the lamp. Then, you save the current lamp state on the <strong>lampState<\/strong> variable. Finally, you clean the <strong>textMessage<\/strong> variable, so that it is empty to receive new SMS.<\/p>\n<pre>if (textMessage.indexOf(\"ON\") &gt;= 0){\r\n\/\/ Turn on relay and save current state\r\ndigitalWrite(relay, LOW);\r\nlampState = \"on\";\r\nSerial.println(\"Relay set to ON\"); \r\ntextMessage = \"\";<\/pre>\n<h4>Turn the lamp off<\/h4>\n<p>On\u00a0 the other side, if the SMS received contains the text &#8220;OFF&#8221;, the relay will be turned off, the new state will be saved, and the textMessage is cleaned.<\/p>\n<pre>if (textMessage.indexOf(\"OFF\") &gt;= 0){\r\n\/\/ Turn off relay and save current state\r\ndigitalWrite(relay, HIGH);\r\nlampState = \"off\"; \r\nSerial.println(\"Relay set to OFF\");\r\ntextMessage = \"\";<\/pre>\n<h4>Returning the lamp state<\/h4>\n<p>Finally, if the incoming SMS contains the text &#8220;STATE&#8221;, the Arduino should send a message to a predefined phone number saying whether the lamp is currently on or off.<\/p>\n<pre>if (textMessage.indexOf(\"STATE\") &gt;= 0){\r\n  String message = \"Lamp is \" + lampState;\r\n  sendSMS(message);\r\n  Serial.println(\"Lamp state resquest\");\r\n  textMessage = \"\";<\/pre>\n<p>The message is sent using the sendSMS() function defined at the bottom of the code.<\/p>\n<h4>Sending an SMS<\/h4>\n<p>The <strong>sendSMS()<\/strong> function created at the end of the code, accepts a string as an argument. That string should be the message to be sent.<\/p>\n<p>The number the Arduino answers to is set at the following line:<\/p>\n<pre>SIM900.println(\"AT + CMGS = \\\"<span style=\"color: #ff0000;\"><strong>XXXXXXXXXXXX<\/strong><\/span>\\\"\");<\/pre>\n<p>Replace the <strong><span style=\"color: #ff0000;\">XXXXXXXXXXXX<\/span><\/strong> with the recipient\u2019s phone number.<\/p>\n<p><strong>Note:<\/strong> you must add the number according to the international phone number format. For example, in Portugal the number is preceded by <span style=\"color: #ff0000;\"><strong>+351<\/strong><\/span>XXXXXXXXX.<\/p>\n<p>After adding the phone number the Arduino should reply to, you can copy the full code to your Arduino IDE and upload it to your Arduino board.<\/p>\n<h2>Wrapping up<\/h2>\n<p>In this project we&#8217;ve shown you how you can use the SIM900 GSM shield to control a 12V lamp and to request data (the lamp state) to the Arduino.<\/p>\n<p>The concepts learned in this project can be applied to control any electronics appliances you like. You can also use what you&#8217;ve learned here to request the Arduino about sensor data &#8211; for example, request the latest readings of your weather station.<\/p>\n<p>You can also build a surveillance system using a PIR motion sensor and a GSM shield that sends you an SMS when unexpected motion is detected.<\/p>\n<p>We hope you&#8217;ve found this project useful. If you like this project, make sure you check our Arduino course: <a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino Step-by-step Projects with 25 Projects<\/a>.<\/p>\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we&#8217;re going to show you how you can control a 12V lamp via SMS using an Arduino UNO, a relay module, and the SIM900 GSM shield.<\/p>\n","protected":false},"author":5,"featured_media":43803,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,2,267,225,264,10],"tags":[],"class_list":["post-43696","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-arduino-project","category-home-automation","category-project","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Control-lamp-via-SMS-thumbnail.jpg?fit=1000%2C560&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/43696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=43696"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/43696\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/43803"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=43696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=43696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=43696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}