SIM5320 controlling Relay using Arduino

The module I am using is a shield on top of Arduino. It requires no wiring just plug it in. Anyway I have shown the wiring diagram in case if you want to use it separately. The module includes proper power regulator, so we can connect 5V to the 5V pin. Also the TX/RX serial communication already includes necessary TTL level shifter.

SIM5320 comes in American version SIM5320A and European version SIM5320E. If any other countries, we need to check the local mobile-phone provider frequency. SIM5320A module can work with 850MHz and 1900MHz 3G frequencies while SIM5320E module can work with 900MHz and 2100MHz 3G frequencies. For example, I used SIM5320A in Australia as the mobile provider Telstra uses 850MHz as one of their 3G frequency.

AT command manual, Schematic and Datasheet can be found in the following links. https://www.tinyosshop.com/3g-gprs-gsm-shield-for-arduino-sim5320a and https://www.tinyosshop.com/3g-gprs-gsm-shield-for-arduino-sim5320e

Wiring diagram

Diagram

Picture

Arduino Fona Library

Click Sketch → Include Library → Manage Libraries...

Type Adafruit Fona in the Filter your search area

Click Install Adafruit Fona Library

Arduino code

// Arduino Code
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
#define FONA_TX 2
#define FONA_RX 3
#define FONA_RST 4
#define FONA_POWER 8
#define RELAY_PIN 9

char replybuffer[255];
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
String smsString = "";
char fonaNotificationBuffer[64];          //for notifications from the FONA
char smsBuffer[250];

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA_3G fona = Adafruit_FONA_3G(FONA_RST);

void setup() 
{
  pinMode(FONA_POWER, OUTPUT);
  digitalWrite(FONA_POWER, HIGH);
  delay(180);
  digitalWrite(FONA_POWER, LOW);
  delay(3000);
  
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  
  Serial.begin(4800);
  Serial.println(F("FONA SMS caller ID test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  // make it slow so its easy to read!
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial))
  {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));
  
  fonaSerial->print("AT+CNMI=2,1\r\n");  //set up the FONA to send a +CMTI notification when an SMS is received
  Serial.println("FONA Ready");
}

void loop() 
{
  char* bufPtr = fonaNotificationBuffer;    //handy buffer pointer
  
  if (fona.available())      //any data available from the FONA?
  {
    int slot = 0;            //this will be the slot number of the SMS
    int charCount = 0;
    //Read the notification into fonaInBuffer
    do
    {
      *bufPtr = fona.read();
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaNotificationBuffer)-1)));

    //Add a terminal NULL to the notification string
    *bufPtr = 0;

    //Scan the notification string for an SMS received notification.
    //  If it's an SMS message, we'll get the slot number in 'slot'
    if (1 == sscanf(fonaNotificationBuffer, "+CMTI: " FONA_PREF_SMS_STORAGE ",%d", &slot)) 
    {
      Serial.print("slot: "); Serial.println(slot);

      char callerIDbuffer[32];  //we'll store the SMS sender number in here

      // Retrieve SMS sender address/phone number.
      if (! fona.getSMSSender(slot, callerIDbuffer, 31)) 
      {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);

      // Retrieve SMS value.
      uint16_t smslen;
      if (fona.readSMS(slot, smsBuffer, 250, &smslen)) // pass in buffer and max len!
      { 
        smsString = String(smsBuffer);
        Serial.println(smsString);
      }

      if (smsString == "On")  //Change "On" to something secret like "On$@8765"
      {
        digitalWrite(RELAY_PIN, HIGH);
        Serial.println("Relay is ON.");
        delay(100);
        fona.sendSMS(callerIDbuffer,"Relay is ON." );
     
      }
      else if(smsString == "Off") //Change "On" to something secret like "Off&%4235"
      {
        digitalWrite(RELAY_PIN, LOW);
        Serial.println("Relay is OFF.");
        delay(100);
        fona.sendSMS(callerIDbuffer, "Relay is OFF.");
      }
       
      if (fona.deleteSMS(slot)) {
        Serial.println(F("OK!"));
      } else {
        Serial.print(F("Couldn't delete SMS in slot ")); Serial.println(slot);
        fona.print(F("AT+CMGD=?\r\n"));
      }
    }  
  }
}
// End of Arduino Code

Demonstration

Advanced setup

When someone send "On" or "Off" message to the phone number, it switch on/off the relay. To make it secure, use a secret word instead of simple "On" or "Off"
      //               HERE
      if (smsString == "On")  //Change "On" to something secret like "On$@8765"
      {
        digitalWrite(RELAY_PIN, HIGH);
        Serial.println("Relay is ON.");
        delay(100);
        fona.sendSMS(callerIDbuffer,"Relay is ON." );
     
      }
      //                   HERE
      else if(smsString == "Off") //Change "On" to something secret like "Off&%4235"
      {
        digitalWrite(RELAY_PIN, LOW);
        Serial.println("Relay is OFF.");
        delay(100);
        fona.sendSMS(callerIDbuffer, "Relay is OFF.");
      }

If you don't want to receive confirmation message from the SIM5320 Module. you can delete the following two lines of code. This can save SMS cost

    if (smsString == "On")  //Change "On" to something secret like "On$@8675"
      {
        digitalWrite(RELAY_PIN, HIGH);
        Serial.println("Relay is ON.");
        delay(100);
        fona.sendSMS(callerIDbuffer,"Relay is ON." );
     
      }

      else if(smsString == "Off") //Change "On" to something secret like "Off&%4235"
      {
        digitalWrite(RELAY_PIN, LOW);
        Serial.println("Relay is OFF.");
        delay(100);
        fona.sendSMS(callerIDbuffer, "Relay is OFF.");
      }