Advertisement
Guest User

SMS Relay

a guest
Sep 14th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #define debug Serial
  3.  
  4. char input[1024]; // variable to store the incoming characters
  5. int rdy = 0, rdyStart = 0;
  6.  
  7. //SoftwareSerial debug(4, 5);  //RX, TX
  8. SoftwareSerial sms(4, 5);  //RX, TX
  9. const int Relay = 8;
  10. const int SMSReset = 7;
  11.  
  12. void sendCommand(const char* cmd, int prnt = 1){
  13.   sms.println(cmd);
  14.   delay(300);
  15.  
  16.   char buf[128];
  17.   int x=0;
  18.   while(sms.available()){
  19.     buf[x++] = sms.read();
  20.   }
  21.   if(prnt && x){
  22.     buf[x++] = 0;
  23.     debug.write(buf);
  24.   }
  25. }
  26.  
  27.  
  28. void echoState(int state){
  29.   sendCommand(strcat("ATE", String(state).c_str()), 0);
  30. }
  31.  
  32. void initSMS(){
  33.   debug.println("SMS Init...");
  34.   echoState(0);
  35.   sendCommand("AT"); //Check AT Status
  36.  
  37.   //Get device information
  38.   sendCommand("AT+CMEE=2");
  39.   sendCommand("AT+CGMI");
  40.   sendCommand("AT+CGMM");
  41.   sendCommand("AT+CGMR");
  42.   sendCommand("AT+CGSN");
  43.   sendCommand("AT+CPAS");
  44.   sendCommand("AT+CSQ");
  45.   echoState(1);
  46.  
  47.   sendCommand("AT+CMGF=1"); // set SMS mode to text
  48.   sendCommand("AT+CNMI=2,2,0,0,0"); // just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out
  49. }
  50.  
  51. void setup() {
  52.   debug.begin(9600);
  53.   debug.println("Device Initializing...");
  54.  
  55.   pinMode(Relay, OUTPUT);
  56.   pinMode(SMSReset, OUTPUT);
  57.   memset(input, 0, sizeof(input)); //clear input buffer
  58.  
  59.   digitalWrite(Relay, LOW);
  60.   digitalWrite(SMSReset, LOW);
  61.  
  62.   delay(500);
  63.  
  64.   // wake up the GSM shield
  65.   sms.begin(9600);
  66.   digitalWrite(SMSReset, HIGH);
  67.   delay(500);
  68.   sms.println("");  //Needed for starting AT communication on serial
  69.   rdyStart = millis() / 1000;
  70. }
  71.  
  72. void executeCommand(){
  73.   if(!strcmp(input, "$a0")){
  74.       debug.println("Relay OFF");
  75.       digitalWrite(Relay, LOW);
  76.   } else if(!strcmp(input, "$a1")) {
  77.     debug.println("Relay ON");
  78.     digitalWrite(Relay, HIGH);
  79.   } else {
  80.     debug.println(strcat("Unknown Command: ", input));
  81.   }
  82.   sms.println("AT+CMGD=1,4"); // delete all SMS
  83.   delay(1000);
  84. }
  85.  
  86. void loop(){
  87.  
  88.  
  89.   //If a character comes in from the GSM...
  90.   int i = 0;
  91.   while(sms.available()){
  92.     input[i++] = sms.read();
  93.     delay(50);
  94.   }
  95.  
  96.   { //Debug listener scope
  97.     char buf[128];
  98.     int x=0;
  99.     while(debug.available()){
  100.       buf[x++] = debug.read();
  101.       if(buf[x-1] == '~') buf[x-1] = 26;
  102.     }
  103.     if(x){
  104.       buf[x++] = 0;
  105.       sms.write(buf);
  106.     }
  107.   }
  108.  
  109.   if(i){  //Output SMS data
  110.     input[i++] = 0; //null terminate our string
  111.     debug.println("OUTPUT:");
  112.     debug.println(input);
  113.     debug.println("--------------------");
  114.     //executeCommand(); //Execute SMS command
  115.     i = 0;
  116.   }
  117.  
  118.  
  119.   if(!rdy && millis()/1000 - rdyStart >= 3){
  120.       rdy = 1;
  121.       initSMS();
  122.   }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement