Dragos Iosub
Published

Remote Controlled Relay(s) via SMS

Open/close light, ventilation, irrigation pumps or whatever. Old fashion, reliable via SMS remote controller w. 2-4 (and easy more) relays.

BeginnerFull instructions provided18 minutes270
Remote Controlled Relay(s) via SMS

Things used in this project

Hardware components

xyz-mIoT by itbrainpower.net [ARM0 + M95FA (2G modem), no embedded sensors]
xyz-mIoT by itbrainpower.net [ARM0 + M95FA (2G modem), no embedded sensors]
or,
×1
xyz-mIoT by itbrainpower.net [ARM0 + BG96 (LTE CATM1+NB IoT+2G modem +GNSS), no embedded sensors]
xyz-mIoT by itbrainpower.net [ARM0 + BG96 (LTE CATM1+NB IoT+2G modem +GNSS), no embedded sensors]
×1
1 cell LiPO battery
×1
GSM embedded antenna w. u-FL
×1
2 x relay [5V version] shield
×1
5V wall power adapter
×1

Software apps and online services

Arduino IDE
Arduino IDE
v. >= 1.8.5

Story

Read more

Schematics

SMS relay controller wiring

xyz-mIoT wiring with 2xrelay shield [5V version].

Use this for remote controlled RELAYs driven by SMS.

SMS chat

SMS chat screenshot for two/four relays SMS controller

Code

SMS-relay-controller.ino

Arduino
two/four relays remote controlled via SMS Arduino code.

In order to compile this code following additional files are required: "XYZMIOT_basic_lbr.h", "XYZMIOT_basic_lbr.ino", "XYZMIOT_SMS_lbr.h" and "XYZMIOT_SMS_lbr.ino". Those files can be found in "xyz-mIoT kickstart for Arduino" available for download at: https://itbrainpower.net/downloads#xyz-mIoT

itbrainpower.net invests significant time and resources providing those how to and in design phase of our IoT products.

Support us by purchasing our worldwide best LTE CATM1, NB IoT, 2G, 3G and 4G shields and IoT sensor breakout bords:
- xyz-mIoT by itbrainpower.net https://itbrainpower.net/order#xyz-mIoT
- u-GSM by itbrainpower.net https://itbrainpower.net/order#u-GSM
- s-Sense by itbrainpower.net air quality and environmental I2C sensors https://itbrainpower.net/order#s-Sense
/*
SMS-relay-controller.ino v 0.4/20190712 - for xyz-mIoT shields
COPYRIGHT (c) 2015-2019 Dragos Iosub / R&D Software Solutions srl

You are legaly entitled to use this SOFTWARE ONLY IN CONJUNCTION WITH xyz-mIoT DEVICES USAGE. Modifications, derivates and redistribution 
of this software must include unmodified this COPYRIGHT NOTICE. You can redistribute this SOFTWARE and/or modify it under the terms 
of this COPYRIGHT NOTICE. Any other usage may be permited only after written notice of Dragos Iosub / R&D Software Solutions srl.

This SOFTWARE is distributed is provide "AS IS" in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

IMPORTANT NOTICE!
Before use xyz-mIoT shields, you must download and install "xyz-mIoT shields Arduino class" from https://itbrainpower.net/downloads#xyz-mIoT 

In order to compile this code following additional files are required: "XYZMIOT_basic_lbr.h", "XYZMIOT_basic_lbr.ino", "XYZMIOT_SMS_lbr.h" 
and "XYZMIOT_SMS_lbr.ino". Those files can be found in "xyz-mIoT kickstart for Arduino" available for download at: 
https://itbrainpower.net/downloads#xyz-mIoT 

Copy this four file in the same folder where is this source file (SMS_relay_controller.ino)! 


Dragos Iosub, Bucharest 2019.
http://itbrainpower.net
*/

//#define modemType   BG96            // choose this for xyz-mIoT equipped with BG96 shield
#define modemType   M95FA             // choose this for xyz-mIoT equipped with M95FA shield

static char phoneNumber[15] = "0700111222";       //replace with your destination number
//#define FourRelays                              //remove comment to use it with 4 relays output

#define OutPort0   8                //Arduino digital port used to drive Relay0
#define OutPort1   9                //Arduino digital port used to drive Relay1
#define OutPort2   10               //Arduino digital port used to drive Relay2
#define OutPort3   11               //Arduino digital port used to drive Relay3



//#define atDebug             //remove comment if you want to supervise modem chat in Serial Monitor
//#define SMSLibDebug         //remove comment if debug SMS it's required


static char notunderstand[74] = "? relay0,relay1,relay2,relay3 >> 1 ->activate relay, 0 ->release relay";
static char executed[40] = " cmd executed";
static char boot[13] = "BOOT EVENT ";



/*#####NO REALLY NEED TO CHANGE PARAMS BELLOW#####*/
#define agsmSerial Serial
#define BUFFDSIZE 1024

#include "Arduino.h"
#include "XYZMIOT_basic_lbr.h"
#include "XYZMIOT_SMS_lbr.h"
//#include "modems_network.h"

#define BUFFDSIZE 1024              //used by modem library


char ch;
char buffd[BUFFDSIZE];
char readBuffer[200];
int state=0, i=0, powerState = 0; 


void procRespose(char* msg){
    memset(readBuffer,0x00,sizeof(readBuffer));
    sprintf(readBuffer, "%s", msg);
    SerialUSB.println(readBuffer);
    SerialUSB.flush();
    delay(150);
    sendSMS(phoneNumber, readBuffer);     //confirm command 
    delay(100);
}

void setup(){
  
    pinMode(OutPort0, OUTPUT);        //configure the Dxx as output
    pinMode(OutPort1, OUTPUT);        //configure the Dxx as output
    digitalWrite(OutPort0, 0);
    digitalWrite(OutPort1, 0);
    #if defined(FourRelays)
      pinMode(OutPort2, OUTPUT);        //configure the Dxx as output
      pinMode(OutPort3, OUTPUT);        //configure the Dxx as output
      digitalWrite(OutPort2, 0);
      digitalWrite(OutPort3, 0);
    #endif
  
    agsmSerial.begin(115200);
    SerialUSB.begin(115200);//1ms
    delay(3000);
  
    clearagsmSerial();
    clearSerial();
    delay(10);
  
    modemHWSetup();             //configure Arduino IN and OUT to be used with modem
    
    SerialUSB.flush();
    agsmSerial.flush();

    SerialUSB.println(F("xyz-mIoT boot.."));
    SerialUSB.flush();

    
    powerOnModem();
  
    clearBUFFD();
    while(strlen(buffd)<1){
      getIMEI();
      delay(500);
    }
  
    ready4SMS = 0; 
    ready4Voice = 0;
  
    setupMODEMforSMSusage();
  
    SerialUSB.println(F("xyz-mIoT ready.. let's proceed"));
    SerialUSB.flush();
    delay(100);
  
    procRespose((char*)boot);     //send SMS 
  
}

int checkSMSAuthNo(int SMSindex){
    int ret = 0;
    clearBUFFD();
    if(ready4SMS != 1) setupMODEMforSMSusage();
    if(totSMS<1) listSMS();
    if(SMSindex > noSMS || SMSindex < 1) return -1;

    clearagsmSerial();
    agsmSerial.print("AT+CMGR=");//send command to modem
    agsmSerial.print(SMSindex);//send command to modem
    agsmSerial.println(",0");//send command to modem
    delay(1);

    readline();//just 2 remove modem cmd echo
    readline();//just 2 read second line ==> (containing OK / +CMS ERROR / SMS header)

    SerialUSB.println(buffd);

    if(strstr(buffd, phoneNumber)) ret = 1;
    clearBUFFD();
    clearagsmSerial();  
    
    return ret;
}

void loop(){
  //process SMS commands - start
  listSMS();                                //find the last used SMS location
  clearagsmSerial();
  int cnt;
  cnt = noSMS;
  while (cnt>0){
    if(checkSMSAuthNo(cnt)<1){
      deleteSMS(cnt);
      SerialUSB.println("no auth no");
      clearBUFFD();
    }
    else{
      readSMS(cnt);                           //the SMS content will be returned in buffd 
      SerialUSB.print(F("SMS content: "));SerialUSB.flush();delay(50);
      SerialUSB.println(buffd);SerialUSB.flush();delay(50);
    }
    //here process SMS the content - start

    delay(50);
    clearagsmSerial();
    delay(50);

    if(strlen(buffd) > 0){                        //non empty SMS
      int OutputVal0 = -1;
      int OutputVal1 = -1;

      #if defined(FourRelays)
        int OutputVal2 = -1;
        int OutputVal3 = -1;
        sscanf(buffd, "%i,%i,%i,%i", &OutputVal0, &OutputVal1, &OutputVal2, &OutputVal3);
        clearBUFFD();
        sprintf(buffd, "%i,%i,%i,%i >> %s", OutputVal0, OutputVal1, OutputVal2, OutputVal3, (char*)executed);
      #else
        sscanf(buffd, "%i,%i", &OutputVal0, &OutputVal1);
        clearBUFFD();
        sprintf(buffd, "%i,%i >> %s", OutputVal0, OutputVal1, (char*)executed);
      #endif

      SerialUSB.println(buffd);
      
      #if defined(FourRelays)
        if(OutputVal0<0 || OutputVal1<0 || OutputVal2<0 || OutputVal3<0){//check parse message success
      #else
        if(OutputVal0<0 || OutputVal1<0){//check parse message success
      #endif
        procRespose((char*)notunderstand);
        //SerialUSB.println("error");
      } 
      else{
        digitalWrite(OutPort0, OutputVal0);
        digitalWrite(OutPort1, OutputVal1);
        #if defined(FourRelays)
          digitalWrite(OutPort2, OutputVal2);
          digitalWrite(OutPort3, OutputVal3);
        #endif
        procRespose(buffd);
        //SerialUSB.println("fine");
      }

      deleteSMS(cnt);                         //free the SMS location
    }
    //here process SMS the content - end
    clearBUFFD();
    clearagsmSerial();
    cnt--;
  }
  //process SMS commands - stop
  delay(500);
}

Credits

Dragos Iosub

Dragos Iosub

19 projects • 3 followers
Electronics & software, electronics & software, ...

Comments