XirallicBolts

Flex - Remote Start SMS - 1.2.1

Nov 8th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.64 KB | None | 0 0
  1. /*
  2.  *  Remote Start via SMS for smartkey-equipped Flex
  3.  *  XirallicBolts
  4.  *  
  5.  *  v1.0 - Initial version
  6.  *  
  7.  *  v1.1 - Responds to sender instead of hardcoded number
  8.  *         Added PATS programming procedure
  9.  *         'Send SMS' moved to a separate function (to allow for immediate PATS message)
  10.  *         Corrected serial messages
  11.  *         Added support for an 'Always send confirmation messages' switch/wire
  12.  *  
  13.  *  v1.2   Fixed:  No longer gets stuck in a loop, reacting off the outgoing messages
  14.  *         Fixed:  A disconnected pin is apparently HIGH for confirmations.
  15.  *                 Connect pin 11 to GND to recieve confirmation texts automatically
  16.  *  v1.2.1 Removed: Resistor is unnecessary. No issues running over a week on main battery power.
  17.  */
  18.  
  19.  
  20. #include <SoftwareSerial.h>
  21.  
  22. // Create SoftwareSerial object for the SIM800L
  23. SoftwareSerial SIM800(2, 3);    // RX on 2, TX on 3
  24.  
  25. // Set a couple names so we don't have to remember which pin is the 'Start' button
  26. #define CONFIRM_TEXT  11        // Connect pin 11 to Ground to recieve a confirmation text
  27. #define FOB_BATTERY   6         // originally 8, reversed the glued-in leads like an idiot
  28. #define BUTTON_LOCK   7
  29. #define BUTTON_START  8         // 6, still an idiot
  30.  
  31. String incomingText = "";
  32. String incomingNumber = "";
  33. String messageHeader = "";
  34. String outgoingText = "";
  35.  
  36. void setup(){
  37.   pinMode(CONFIRM_TEXT, INPUT_PULLUP);
  38.   pinMode(FOB_BATTERY, OUTPUT);
  39.   pinMode(BUTTON_LOCK, OUTPUT);
  40.   pinMode(BUTTON_START, OUTPUT);
  41.  
  42.   // Make sure the relays are all shut off.  
  43.   // Relays behave backwards. HIGH is off, LOW is on.
  44.   digitalWrite(FOB_BATTERY, HIGH);
  45.   digitalWrite(BUTTON_LOCK, HIGH);
  46.   digitalWrite(BUTTON_START, HIGH);
  47.  
  48.   // Wait a few seconds, then start talking to both the laptop (Serial)
  49.   // and GSM module (SIM800).  Give the GSM time to connect to cell towers
  50.   delay(5000);
  51.   Serial.begin(19200);
  52.   SIM800.begin(19200);
  53.   SIM800.setTimeout(5000);
  54.   Serial.println("Waiting 10 seconds for cell to connect...");
  55.   delay(10000);
  56.   SIM800.print("AT+CMGF=1\r");                                       // Set SIM800 to SMS mode
  57.   delay(100);
  58.   SIM800.print("AT+CNMI=2,2,0,0,0\r");                               // Set SIM800 to send incoming messages to the Nano
  59.   delay(100);
  60.   Serial.println("You should now be connected to the cell towers");
  61.   //TODO: Actually parse the response to see if it's connected
  62. }
  63.  
  64.  
  65. void loop() {
  66.  
  67.   // Check for a new text message, convert it to uppercase, and let the laptop know.
  68.   if(SIM800.available()>0) {
  69.     incomingText = SIM800.readString();
  70.     delay(250);
  71.     incomingNumber = "";
  72.     incomingText.toUpperCase();
  73.     messageHeader = incomingText.substring(2,6);      // Extract the first five characters to ensure it's an incoming text
  74.     incomingNumber = incomingText.substring(9,21);    // Extract the phone number to text back
  75.     Serial.println("Incoming message: " + incomingText);
  76.     delay(10);
  77.   }
  78.  
  79.   // If the text contains the keyword LOCK, lock the doors
  80.  
  81.   if(incomingText.indexOf("LOCK")>=0 && messageHeader == "+CMT"){
  82.     Serial.println("Attempting to Lock doors");
  83.     digitalWrite(FOB_BATTERY, LOW);   // Battery ON
  84.     delay(1000);
  85.     digitalWrite(BUTTON_LOCK, LOW);   // Lock PRESSED
  86.     delay(250);
  87.     digitalWrite(BUTTON_LOCK, HIGH);  // Lock RELEASED
  88.     delay(500);
  89.     digitalWrite(FOB_BATTERY, HIGH);  // Battery OFF
  90.     Serial.println("Doors locked");
  91.  
  92.     // If the text contains the word CONFIRM, or the Always Confirm switch is on,  send a text back
  93.     if(incomingText.indexOf("CONFIRM")>=0 || digitalRead(CONFIRM_TEXT) == LOW){
  94.        outgoingText = "Doors Locked";
  95.        SendText(incomingNumber, outgoingText);
  96.     }
  97.     incomingText = "";                // Clear the incoming and outgoing strings so
  98.     outgoingText = "";                // you can't trigger both START and STOP (etc) in one message
  99.   }
  100.  
  101.  
  102.   if(incomingText.indexOf("START")>=0 && messageHeader == "+CMT"){
  103.     Serial.println("Attempting to Remote Start");
  104.     digitalWrite(FOB_BATTERY, LOW);   // Battery ON
  105.     delay(1000);
  106.     digitalWrite(BUTTON_LOCK, LOW);   // Lock PRESSED
  107.     delay(250);
  108.     digitalWrite(BUTTON_LOCK, HIGH);  // Lock RELEASED
  109.     delay(500);
  110.     digitalWrite(BUTTON_START, LOW);  // Start PRESSED
  111.     delay(250);
  112.     digitalWrite(BUTTON_START, HIGH); // Start RELEASED
  113.     delay(500);
  114.     digitalWrite(BUTTON_START, LOW);  // Start PRESSED
  115.     delay(250);
  116.     digitalWrite(BUTTON_START, HIGH); // Start RELEASED
  117.     delay(500);
  118.     digitalWrite(FOB_BATTERY, HIGH);  // Battery OFF
  119.     Serial.println("Car Started");
  120.     if(incomingText.indexOf("CONFIRM")>=0 || digitalRead(CONFIRM_TEXT) == LOW){
  121.        outgoingText = "Remote Started";
  122.        SendText(incomingNumber, outgoingText);
  123.     }
  124.     incomingText = "";
  125.     outgoingText = "";
  126.   }
  127.  
  128.  
  129.   if(incomingText.indexOf("STOP")>=0 && messageHeader == "+CMT"){
  130.     Serial.println("Attempting to Stop");
  131.     digitalWrite(FOB_BATTERY, LOW);   // Battery ON
  132.     delay(1000);
  133.     digitalWrite(BUTTON_START, LOW);   // Lock PRESSED
  134.     delay(3000);
  135.     digitalWrite(BUTTON_START, HIGH);  // Lock RELEASED
  136.     delay(500);
  137.     digitalWrite(FOB_BATTERY, HIGH);  // Battery OFF
  138.     Serial.println("Remote Start Cancelled");
  139.     if(incomingText.indexOf("CONFIRM")>=0 || digitalRead(CONFIRM_TEXT) == LOW){
  140.        outgoingText = "Engine Stopped";
  141.        SendText(incomingNumber, outgoingText);
  142.     }
  143.     incomingText = "";
  144.     outgoingText = "";
  145.   }
  146.  
  147.  
  148.  
  149.   if(incomingText.indexOf("PATSPROGRAMMING")>=0 && messageHeader == "+CMT"){
  150.     Serial.println("Turning on remote for PATS programming...");
  151.     outgoingText = "Key is ON for 60 seconds. Begin PATS procedure now!";
  152.     SendText(incomingNumber, outgoingText);
  153.     digitalWrite(FOB_BATTERY, LOW);   // Battery ON
  154.     delay(70000);
  155.     digitalWrite(FOB_BATTERY, HIGH);  // Battery OFF
  156.     Serial.println("PATS timer complete");
  157.     incomingText = "";
  158.     outgoingText = "";
  159.   }
  160.  
  161.  
  162.   if(incomingText.indexOf("STATUS")>=0 && messageHeader == "+CMT"){
  163.     outgoingText = "I'm awake!  v1.2";
  164.     SendText(incomingNumber, outgoingText);
  165.     incomingText = "";
  166.     outgoingText = "";
  167.   }
  168.  
  169.   delay(100);
  170. }
  171.  
  172.  
  173.  
  174.  
  175. void SendText(String incomingNumber, String outgoingText){
  176.    Serial.println("Preparing to send a text ... ");
  177.    SIM800.println("AT+CMGF=1");
  178.    delay(100);
  179.    //SIM800.println("AT+CMGS=\"+01xxxxxxxxxx\"");        // Depreciated, now responds to sender.
  180.    SIM800.println("AT+CMGS=\"" + incomingNumber + "\"");
  181.    delay(100);
  182.    SIM800.println(outgoingText);
  183.    delay(100);
  184.    SIM800.println((char)26);
  185.    delay(5000);  
  186.    incomingText = "";
  187. }
Add Comment
Please, Sign In to add comment