Advertisement
Guest User

SMS Relay

a guest
Sep 14th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2.  
  3. char input[64]; // variable to store the incoming characters
  4.  
  5. SoftwareSerial debug(4, 5);  //RX, TX
  6. const int Relay = 8;
  7.  
  8. void setup() {
  9.   debug.begin(9600);
  10.   debug.println("Device Initializing...");
  11.  
  12.   pinMode(Relay, OUTPUT);
  13.   memset(input, 0, 64); //clear input buffer
  14.  
  15.   digitalWrite(Relay, LOW);
  16.  
  17.   // wake up the GSM shield
  18.   Serial.begin(9600);
  19.  
  20.   delay(2000);
  21.   Serial.println("AT+CMGF=1"); // set SMS mode to text
  22.   delay(100);
  23.   Serial.println("AT+CNMI=2,2,0,0,0");
  24.   // just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out
  25.   delay(100);
  26. }
  27.  
  28. void executeCommand(){
  29.   if(!strcmp(input, "$a0")){
  30.       debug.println("Relay OFF");
  31.       digitalWrite(Relay, LOW);
  32.   } else if(!strcmp(input, "$a1")) {
  33.     debug.println("Relay ON");
  34.     digitalWrite(Relay, HIGH);
  35.   } else {
  36.     debug.println(strcat("Unknown Command: ", input));
  37.   }
  38.   Serial.println("AT+CMGD=1,4"); // delete all SMS
  39.   delay(1000);
  40. }
  41.  
  42. void loop(){
  43.   //If a character comes in from the GSM...
  44.  
  45.   int i = 0;
  46.   while(Serial.available()){
  47.     char c = Serial.read();
  48.     input[i++] = c;
  49.     delay(12);
  50.   }
  51.  
  52.   if(i){
  53.     input[i++] = 0; //null terminate our string
  54.     executeCommand();
  55.     i = 0;
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement