stevie135s
Published © CC BY-SA

SMS Relay Control

Control 2x relays using SMS text messages. The program is for the Lilygo TTGO T-Call and requires very few parts.

BeginnerWork in progress1 hour139
SMS Relay Control

Things used in this project

Hardware components

LILYGO® TTGO T-Call
LILYGO® TTGO T-Call
×1
Buzzer
Buzzer
×1
Selectable Active H/L 5v Relay Module
×1

Story

Read more

Schematics

Active High Relay module

TTGO T-Call Pinout

Code

TTGO T-Call Relay Control

Arduino
/*
 * Relay Control Program For The TTGO T-Call
 * The Relay module i used has an active H/L jumper (i used Active High)
 * This is the only relay that will work without using a logic Level converter
 * A service SMS may take more than one text so a reset SMS may be required
 * Program by Stevie135s
 * 
 * Remember to Insert the correct mobile numbers
 * One number is to check the Incoming SMS
 * Tne second number to Send the Response SMS
 */



#include <HardwareSerial.h>
#define RX 27    // mySerial RX Pin on Sim800L
#define TX 26    // mySerial TX Pin on Sim800L
#define RLY_1 32  // Relay 1 Pin
#define RLY_2 33 // Relay 2 Pin
#define buzz 13   // Buzzer Pin
#define Sim_Power 23
String buff = "";
String Reply = "";
String R_1 = " ON"; //Relay Conditions
String R_2 = " ON";
byte S_Length;
HardwareSerial mySerial(1);

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600,SERIAL_8N1,TX,RX);
  pinMode(RLY_1, OUTPUT);   // Set Relay 1 pin
  pinMode(RLY_2, OUTPUT);   // Set Relay 2 pin
  pinMode(buzz, OUTPUT);    // Set Buzzer pin
  pinMode(Sim_Power, OUTPUT);
  digitalWrite(RLY_1, LOW); // Set Relay 1 To OFF
  digitalWrite(RLY_2, LOW); // Set Relay 2 To OFF
  digitalWrite(Sim_Power, HIGH);
  for (int f = 1; f < 13; f++) { // Startup delay loop
    digitalWrite(buzz, HIGH);     //While Sim800L initialises
    delay(250);
    digitalWrite(buzz, LOW);
    delay(750);
  }

  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Initializing...");
  delay(1000);
  mySerial.println("AT"); // Sim Handshake
  updateSerial();
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  mySerial.println("AT+CPMS=\"SM\""); // Use SIM Memory for messages
  updateSerial();
  ResetSMS();

}
void loop()
{
  WaitForSMS();        // This call is an infinite wait for incomming data
  if (buff.length() > 1) {
    if (buff.substring(0, 6) == "+CMTI:") { // Is there an SMS ?
      mySerial.println("AT+CMGR=1"); // Read SMS number one
      WaitForSMS();                         // Put the message in buff string
      Serial.println(buff.substring(33, 46)); //Phone Number Storage In String
      if (buff.substring(33, 46) == "+XXxxxxxxxxxx") { //Is it the right phone number ? <<< INSERT THE INCOMING NUMBER >>>
        Serial.println("This Is From Your Phone");  // It is your phone
        S_Length = buff.length();
        if (S_Length == 90 || S_Length == 91) { // Is the message the correct length ?
          ParseString();   // Parse the Message
        }
      }
      else {
        Reply = buff.substring(33, 46) + " " + buff.substring(75, buff.length()); // Not from right number
        sendMSG();
      }
      mySerial.println("AT+CMGD=1");        // Delete text number one
      updateSerial();
    }
  }
}
void updateSerial()
{
  delay(500);
  while (mySerial.available())
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

void WaitForSMS() {
  buff = "";
  while (mySerial.available() == 0) {
  }
  buff = mySerial.readString();
  buff.trim();
}

void ParseString() {                       // 75 to 84/85
  if (S_Length == 90) {
    if (buff.substring(75, 84) == "TurnoneON") {  // The spelling must be exactly the same
      Reply = "Power on circuit one";
      Serial.println(Reply);
      R_1 = " ON";
      sendMSG();
      digitalWrite(RLY_1, HIGH);
      digitalWrite(buzz, HIGH);
      delay(200);
      digitalWrite(buzz, LOW);
      return;
    }
    if (buff.substring(75, 84) == "TurntwoON") {  // The spelling must be exactly the same
      Reply = "Power on circuit two";
      Serial.println(Reply);
      R_2 = " ON";
      sendMSG();
      digitalWrite(RLY_2, HIGH);
      digitalWrite(buzz, HIGH);
      delay(200);
      digitalWrite(buzz, LOW);
      return;
    }
    if (buff.substring(75, 84) == "Reset SMS") {  // The spelling must be exactly the same
      ResetSMS();
      Reply = "The messages have been reset";
      sendMSG();
      return;
    }
  }
  else {
    if (buff.substring(75, 85) == "TurnoneOFF") { // The spelling must be exactly the same
      Reply = "Power off circuit one";
      Serial.println(Reply);
      R_1 = " OFF";
      sendMSG();
      digitalWrite(RLY_1, LOW);
      digitalWrite(buzz, HIGH);
      delay(200);
      digitalWrite(buzz, LOW);
      delay(50);
      digitalWrite(buzz, HIGH);
      delay(200);
      digitalWrite(buzz, LOW);
      return;
    }
    if (buff.substring(75, 85) == "TurntwoOFF") { // The spelling must be exactly the same
      Reply = "Power off circuit two";
      Serial.println(Reply);
      R_2 = " OFF";
      sendMSG();
      digitalWrite(RLY_2, LOW);
      digitalWrite(buzz, HIGH);
      delay(200);
      digitalWrite(buzz, LOW);
      delay(50);
      digitalWrite(buzz, HIGH);
      delay(200);
      digitalWrite(buzz, LOW);
      return;
    }
    if (buff.substring(75, 85) == "Reply Stat") { // The spelling must be exactly the same
      Reply = "Status: Relay 1 is" + R_1 + " Relay 2 is" + R_2 ;
      Serial.println(Reply);
      sendMSG();
      return;
    }
  }
}

void sendMSG() {
  mySerial.println("AT+CMGF=1");
  updateSerial();
  mySerial.println("AT+CMGS=\"+XXxxxxxxxxxx\""); // <<<< INSERT THE REPLY NUMBER >>>>
  delay(200);
  mySerial.println(Reply);
  delay(600);
  mySerial.println((char)26);
  delay(3000);
  updateSerial;
  mySerial.println();
  updateSerial();
}

void ResetSMS() {
  mySerial.println("AT+CMGDA=\"DEL ALL\""); // Delete All Messages
  updateSerial();
}

Credits

stevie135s
28 projects • 12 followers
I've been interested in microprocessors for a long time.
Contact

Comments

Please log in or sign up to comment.