/***********************************************************************************************Author: Ngoc KhuuTac gia: Khuu Xuan NgocChỉnh sửa: Nguyễn Nguyệt PhươngMo ta: Dung dien thoai gui tin SMS voi noi dung phu hop voi tu khoa trong mang KeyWordON[x] de bat thiet bi x (Relay_pin[x]) hoac trong mang KeyWordOF[x] de tat thiet bi x (Relay_pin[x]).Decription: If SMS has content that match array KeyWordON or KeyWordOF, it will turn on or turn off relay.******
IMPORTAIN: - Make sure UPLOAD sketch "FirstUpLoad" first (Only 1 time), then run this sketch!********  - Replace delay [origin] by *.delay [example: HGSM.delay(1000)]
Tutorial:********
Code download:  https://github.com/Khuuxuanngoc/Hshopvn_GSMLink product:   http://hshop.vn/products/mach-gsmsim800ds-toch-hop-nguon-va-ic-dem
P/s: Remember SET country code if NOT in VietNam***********************************************************************************************/#include "SoftwareSerial.h"#include "Hshopvn_GSM.h"

SoftwareSerial sim800ds(2, 3);HshopGSM HGSM = HshopGSM(&sim800ds);
// Declare pin of relays:unsigned char Relay_pin[2] = {5,6};unsigned char Relay_active = 1; // 1 mean OFF or 0 volt, 0 mean ON or 5 volt. The Relay is active when LOW (0)String KeyWordPush[2] = {"dong", "cat"}, adminuser="84913909190", user1="84913332344";bool passOK = false, isadmin=false, isuser1=false;



void setup() {  // put your setup code here, to run once:  // Relay init:1  //Only show monitor  Serial.begin(9600);  Serial.println("Start!!");
  // Init Country code default: VietNam (+84)  HGSM.init(&GSM_Ready, 9600);}



void loop() {  // put your main code here, to run repeatedly:  HGSM.handle();}


void GSM_Ready() {  if (HGSM.getDataGSM() != "")   {    Serial.println(HGSM.getDataGSM());  //show data comming    if(HGSM.checkData(adminuser)) isadmin=true ; else if(HGSM.checkData(user1)) isuser1=true;else;
    if(isadmin or isuser1)      {      passOK = true;      Serial.println("You are authorized person");      } else;          for (unsigned char count_for = 0; count_for < 2; count_for++)       {      if (passOK && HGSM.checkData(KeyWordPush[count_for])) //// if Key word (PUSH) match        {             pinMode(Relay_pin[count_for], OUTPUT);        digitalWrite(Relay_pin[count_for], !Relay_active);    // Turn on relay        delay(1000);        digitalWrite(Relay_pin[count_for], Relay_active);    // Turn on relay        Serial.println("I have pushed relay: " + KeyWordPush[count_for+1]);
        if (isadmin=true)          {            HGSM.sendsms(adminuser,"I have pushed: "+ KeyWordPush[count_for+1]);           isadmin=false;           passOK = false;          } else if (isuser1=true)            {            HGSM.sendsms(adminuser,"I have pushed: "+ KeyWordPush[count_for+1]);             HGSM.sendsms(user1,"I have pushed: "+ KeyWordPush[count_for+1]);             isuser1=false;            passOK = false;            } else;                   } else;      }    } else;

/************************************************************************Tác giả: Khưu Xuân NgọcEmail: xuanngoc1992@gmail.comWebsite: http://hshop.vn/ Date: 02/02/2017Link download: https://github.com/Khuuxuanngoc/Hshopvn_GSMVersion: 1.0.0

Đây là nội dung file: "Hshopvn_GSM.h"

*************************************************************************/#ifndef _HSHOP_GSM_#define _HSHOP_GSM_
// #define hardwareSerial// #define GSML_DB
#if (ARDUINO >= 100) #include "Arduino.h" #ifndef hardwareSerial #include <SoftwareSerial.h> #endif#else #include "WProgram.h" #ifndef hardwareSerial #include <NewSoftSerial.h> #endif#endif
class HshopGSM{ protected: String getData; public: #ifdef hardwareSerial HshopGSM(HardwareSerial *refSer); #else #if ARDUINO >= 100   HshopGSM(SoftwareSerial *refSer); #else   HshopGSM(NewSoftSerial *refSer); #endif #endif #ifdef hardwareSerial HardwareSerial *myGMS; #else #if ARDUINO >= 100   SoftwareSerial *myGMS; #else   NewSoftSerial *myGMS; #endif #endif #ifdef GSML_DB #define DBshow(x) Serial.print(x) #define DBshown(x) Serial.println(x) #else #define DBshow(x) #define DBshown(x) #endif void DB_init(); void init(void (*p_void_)(), long baud, String _CountryCode); // Khi dung o nuoc ngoai void init(void (*p_void_)(), long baud); void Re_init(); void call(String _callNumber); void call(unsigned long _callNumber); void hangcall(); void answer(); void sendsms(String _callNumber,String _content); void sendsms(unsigned long _callNumber,String _content); void deleteSms(unsigned char); void atcm(String); void read(); void Setbaud(long baud); void set_timeWaitRespond(unsigned long); void setUserFunct(void (*_p_UserFunct)()); void handle(); void checkConnect(); void GSM_funct(); void delay(unsigned long timeDelay); // thay thế ham delay của Arduino void begin(long baud); String getDataGSM(); void clearData(); void addMoney(String _code); void checkMoney(); // Use in VietNam bool checkData(String StringNeedCheck); bool checkData(unsigned long NumberNeedCheck); String splitString(String v_G_motherString, String v_G_Command, String v_G_Start_symbol, String v_G_Stop_symbol, unsigned char v_G_Offset); void (*p_UserFunct)(); void VitualFunction(); String at; String crlf; String atd; String CountryCode; String UserData; unsigned long ul_timeWaitResp; unsigned long ul_timeCheckConnect; bool isGetdata; unsigned char uc_CountErr; // nếu uc_CountErr = 2 -> Re_init(). unsigned char isUser; unsigned char isSendSMS; unsigned char isRespond; bool isComplete; enum _isRespond{ _no_act, _re_act, _tr_act }; };
#endif // end _HSHOP_GSM_