Arduino Gsm module connect ,control sent sms

GSM Module to Arduino:


  • Connect VCC of GSM module to 5V of Arduino.
  • Connect GND of GSM module to GND of Arduino.
  • Connect TX of GSM module to RX of Arduino (digital pin zero).
  • Connect RX of GSM module to TX of Arduino (digital pin 1).

Relay Module to Arduino:


  • Connect the VCC and GND of the relay module to the 5V and GND pins of the Arduino.
  • Connect IN1 to IN8 of the relay module to digital pins 2 to 9 of the Arduino, respectively....
  • Arduino Code
  • Here's a sample code to control 8 relays the use of SMS commands:




#include <SoftwareSerial.H>


SoftwareSerial mySerial(7, eight); // RX, TX


const int relayPins[8] = 2, 3, 4, 5, 6, 7, 8, 9;


void setup() 

  // Initialize the relay pins as output

  for (int i = 0; i < 8; i  ) 

    pinMode(relayPins[i], OUTPUT);

    digitalWrite(relayPins[i], LOW); // Start with all relays off

  

  

  // Initialize serial communications

  Serial.Begin(9600);

  mySerial.Begin(9600);

  

  // Wait for the GSM module to initialize

  delay(1000);

  mySerial.Println("AT");

  delay(1000);

  mySerial.Println("AT CMGF=1");  // Set SMS to text mode

  delay(1000);

  mySerial.Println("AT CNMI=1,2,0,0,0");  // Set module to send SMS data to serial out

  delay(1000);



void loop() 

  if (mySerial.Available() > 0) 

    String smsContent = mySerial.ReadString();

    Serial.Println(smsContent);

    controlRelay(smsContent);

  



void controlRelay(String sms) 

  if (sms.IndexOf("ON1") >= 0) 

    digitalWrite(relayPins[0], HIGH);

   else if (sms.IndexOf("OFF1") >= 0) 

    digitalWrite(relayPins[0], LOW);

  

  if (sms.IndexOf("ON2") >= 0) 

    digitalWrite(relayPins[1], HIGH);

   else if (sms.IndexOf("OFF2") >= 0) 

    digitalWrite(relayPins[1], LOW);

  

  if (sms.IndexOf("ON3") >= 0) 

    digitalWrite(relayPins[2], HIGH);

   else if (sms.IndexOf("OFF3") >= 0) 

    digitalWrite(relayPins[2], LOW);

  

  if (sms.IndexOf("ON4") >= 0) 

    digitalWrite(relayPins[3], HIGH);

   else if (sms.IndexOf("OFF4") >= 0) 

    digitalWrite(relayPins[3], LOW);

  

  if (sms.IndexOf("ON5") >= 0) 

    digitalWrite(relayPins[4], HIGH);

   else if (sms.IndexOf("OFF5") >= 0) 

    digitalWrite(relayPins[4], LOW);

  

  if (sms.IndexOf("ON6") >= 0) 

    digitalWrite(relayPins[5], HIGH);

   else if (sms.IndexOf("OFF6") >= 0) 

    digitalWrite(relayPins[5], LOW);

  

  if (sms.IndexOf("ON7") >= 0) 

    digitalWrite(relayPins[6], HIGH);

   else if (sms.IndexOf("OFF7") >= 0) 

    digitalWrite(relayPins[6], LOW);

  

  if (sms.IndexOf("ON8") >= 0) 

    digitalWrite(relayPins[7], HIGH);

   else if (sms.IndexOf("OFF8") >=0) 

    digitalWrite(relayPins[7], LOW);

  


Explanation of the Code

Library and Software Serial Initialization:


The SoftwareSerial library is used to create a serial verbal exchange on virtual pins 7 (RX) and 8 (TX), as the default serial pins (0 and 1) are used for verbal exchange with the laptop......

Relay Pin Initialization:


All relay pins (2-9) are initialized as output pins and are set to LOW initially all relays became off.

GSM Module Initialization:


Serial verbal exchange is started with each the Arduino and the GSM module.

The GSM module is about to textual content mode (AT CMGF=1) and to send SMS statistics to the serial output (AT CNMI=1,2,0,zero,zero).

Reading and Processing SMS:


In the loop(), the code exams if any facts is available from the GSM module.

If data is available, it reads the SMS content and passes it to the controlRelay function.

Control Relay Function:


The controlRelay characteristic exams for precise commands within the SMS content to turn on or off each relay.

For instance, if the SMS consists of "ON1", the first relay (relay 0) is turned on. If the SMS carries "OFF1", the primary relay is turned off.

Post a Comment

Previous Post Next Post