GSM Smart Socket
Have control of your appliance while you are away from home
Components and supplies
1
Arduino UNO
1
SIM800H IoT module
1
4-CHANNEL RELAY CONTROLLER FOR I2C
Tools and machines
1
Premium Male/Male Jumper Wires, 40 x 3" (75mm)
Apps and platforms
1
Arduino IDE
Project description
Code
GSM Smart Socket
c_cpp
1/* 2 * Date : 6 March 2021 3 * Author : Erick H Moshi 4 * Description: GSM based switch 5 */ 6 7#include <SoftwareSerial.h> // Library for using serial communication 8SoftwareSerial SIM800(7, 8); // Pins 7, 8 are used as used as software serial pins 9 10String incomingData; // for storing incoming serial data 11String message = ""; // A String for storing the message 12String lineState = "IDLE"; // To initially set the state to idle once booted or in cases of power cuts 13int relay_pin = 13; // Initialized a pin for relay module 14 15void setup() 16{ 17 Serial.begin(115200); // baudrate for serial monitor 18 SIM800.begin(19200); // baudrate for GSM shield 19 20 pinMode(relay_pin, OUTPUT); // Setting erlay pin as output pin 21 digitalWrite(relay_pin, HIGH); // Making relay pin initailly low 22 23 delay(30000); 24 25 Serial.println("Initializing ..."); 26 delay(1000); 27 28 // set SMS mode to text mode 29 SIM800.print("AT+CMGF=1\ "); 30 delay(5000); 31 32 SIM800.print("AT+CMGD=1,4\ "); // delete all sms on sim card 33 delay(5000); 34 35 // set gsm module to show the output on serial out 36 SIM800.print("AT+CNMI=2,2,0,0,0\ "); 37 delay(5000); 38 39 // Retrieve own mobile number from SIM 40 Serial.println(F("Attempting to retrieve own MSISDN number from sim")); 41 SIM800.print("AT+CNUM\ "); 42 delay(5000); 43 44 // Enable caller ID on incoming calls 45 Serial.println(F("Enabling Caller ID on incoming calls")); 46 SIM800.print("AT+CLIP=1\ "); 47 Serial.println("Setup complete!"); 48 delay(5000); 49 50 // Send text to alert that system is back online after a power cut/ when switched on 51 SIM800.println("AT+CMGF=1"); 52 delay(100); 53 SIM800.println("AT+CMGS=\\"+ZZZxxxxxxxxx\\""); // Send SMS to the phone number, this will alert you that the system is on 54 delay(100); 55 SIM800.println("SYSTEM ONLINE"); // The content of the SMS 56 delay(100); 57 SIM800.println((char)26); // ASCII code of CTRL+Z 58 delay(100); 59 SIM800.println(); 60 delay(1000); 61 62} 63 64void loop() 65{ 66 //Function for receiving sms 67 receive_message(); 68 69 // if received command is to turn on relay 70 if(incomingData.indexOf("LINE_ON")>=0) 71 { 72 digitalWrite(relay_pin, HIGH);// set to high since we are using the Normarlly Closed side of the relay 73 message = "Line is turned ON"; 74 lineState = "ON"; 75 // Send a sms back to confirm that the relay is turned on 76 send_message(message); 77 } 78 79 // if received command is to turn off relay 80 if(incomingData.indexOf("LINE_OFF")>=0) 81 { 82 digitalWrite(relay_pin, LOW);//Set to low since we are using the Normally Closed side of the relay and Low will activate the relay switching the line off 83 message = "Line is turned OFF"; 84 lineState = "OFF"; 85 // Send a sms back to confirm that the relay is turned off 86 send_message(message); 87 } 88 if(incomingData.indexOf("STATE")>=0) 89 { 90 message = "Line state is " + lineState; 91 send_message(message); 92 Serial.println("Line state resquest"); 93 message = ""; 94 } 95} 96 97void receive_message() 98{ 99 if (SIM800.available() > 0) 100 { 101 incomingData = SIM800.readString(); // Get the data from the serial port. 102 Serial.print(incomingData); 103 delay(10); 104 } 105} 106 107void send_message(String message) 108{ 109 SIM800.println("AT+CMGF=1"); //Set the GSM Module in Text Mode 110 delay(100); 111 SIM800.println("AT+CMGS=\\"+ZZZxxxxxxxxx\\""); // Sends feedback text to the phone number 112 delay(100); 113 SIM800.println(message); // The SMS text you want to send 114 delay(100); 115 SIM800.println((char)26); // ASCII code of CTRL+Z 116 delay(100); 117 SIM800.println(); 118 delay(1000); 119}
Downloadable files
gsm_module_bb_oe842iZqce.png
gsm_module_bb_oe842iZqce.png

gsm_module_bb_oe842iZqce.png
gsm_module_bb_oe842iZqce.png

Comments
Only logged in users can leave comments