11// ====================================================================================
22// Authors: Sigurd Myhre & Sivert Gullberg Hansen
3- // Version: 4 .0
3+ // Version: 5 .0
44// Required Modules: SIM800L
55// ====================================================================================
66
@@ -11,10 +11,11 @@ SoftwareSerial sim800l(3, 2); //SIM800L TX and RX pins
1111
1212// User variables (You have to change these)
1313// ====================================================================================
14- String userPhone = " 95756768" ; // Whitelisted phone number (country code not required)
15- String statusString = " The engine is X,\n The running time is set to Y min" ; // The string we return to the userPhone when asking for a status. X is engineStatus, Y is RunTime
16- String engineStopped = " stopped" ; // Used in status-string
17- String engineStarted = " started" ; // Used in status-string
14+ String userPhone = " 95756768" ; // Whitelisted phone number (country code not required)
15+ String statusString = " The engine is X,\n The run time is set to Ymin" ; // The string we return to the userPhone when asking for a status. X is engineStatus, Y is RunTime
16+ String engineStopped = " stopped" ; // Used in status-string
17+ String engineStarted = " started" ; // Used in status-string
18+ int maxStartAttempts = 3 ;
1819// ====================================================================================
1920
2021// Arduino to car pins (Change if you're not using the same digital pins on the arduino as me)
@@ -39,14 +40,14 @@ String cmdStop = "stop"; //Trigger word for the engine to stop, this wil
3940// ====================================================================================
4041long startTime = 0 ; // Which time our engine started running
4142long RunTime = 900000 ; // ms to have engine running before shutting off after starting
42- int maxTime = 40 ; // max minutes RunTime is allowed to be set to by the user via SMS
43+ int maxRunTime = 30 ; // max minutes RunTime is allowed to be set to by the user via SMS
4344// ====================================================================================
4445
4546// Timers for the engine start procedure(Change these if necesarry)
4647// ====================================================================================
47- int primeDuration = 2000 ; // This is the time the engine is primed for, on petrol cars this doesnt need to be long. But on diesel cars this may be set to longer for the glowplug to warm up
48- int starterMaxDuration = 2800 ; // This is the maximum time for our starter to be enganged
49- int starterDelay = 1000 ; // This is the time between shutting the starterPin off
48+ int primeDuration = 3000 ; // This is the time the engine is primed for, on petrol cars this doesnt need to be long. But on diesel cars this may be set to longer for the glowplug to warm up
49+ int starterMaxDuration = 3000 ; // This is the maximum time for our starter to be enganged
50+ int starterDelay = 2000 ; // This is the time between shutting the starterPin off and checking voltages
5051// ====================================================================================
5152
5253// Resistors for voltage devider(Change these if necesarry)
@@ -83,7 +84,7 @@ void sim800Setup() // Initialize sim-reader
8384 sim800l.println (" AT+CNMI=1,2,0,0,0" ); // Decides how newly arrived SMS messages should be handled
8485}
8586
86- void changePin (int Pin, int PinMode, int DelayTime)
87+ void changePin (int Pin, int PinMode, int DelayTime) // Sets pin to either high or low with a delay after
8788{
8889 digitalWrite (Pin, PinMode);
8990 delay (DelayTime);
@@ -106,58 +107,66 @@ void sendStatus() // Sends a reply to the userPhone including vehicle-status
106107 Reply (returnString);
107108}
108109
109- void changeRunTime (string message ) // Changes how long the engine stays powered on before powering down
110+ void changeRunTime (String messageString ) // Changes how long the engine stays powered on before powering down
110111{
111- int newRunTime = int ();
112- RunTime = newRunTime;
112+ String msg = messageString;
113+ msg = msg.substring (messageString.indexOf (cmdChangeTime) + cmdChangeTime.length () + 1 );
114+ int newRunTime = msg.toInt ();
115+
116+ if (newRunTime >= maxRunTime || newRunTime <= 0 )
117+ RunTime = maxRunTime * 60000 ;
118+ else
119+ RunTime = newRunTime * 60000 ;
120+
121+ Reply (" The run time is set to: " + String (RunTime / 60000 ) + " min" );
113122}
114123
115124void startEngine () // Starts our engine
116125{
117- if (digitalRead (NeutSwitchPin == HIGH) && digitalRead (IgnHotPin == LOW) )
126+ if (digitalRead (NeutSwitchPin) == LOW && digitalRead (IgnHotPin) == LOW)
118127 {
119128 changePin (ignPin, 1 , primeDuration);
120- int currentStarterTime = millis ();
121- float currentBatteryVolt = 0 ;
129+ long currentStarterTime = millis ();
122130
123- while (
124- batteryVoltage () > currentBatteryVolt + 1 ||
125- batteryVoltage () > 13 ||
126- millis () - currentStarterTime <= starterMaxDuration)
131+ while (true )
127132 {
128- changePin (startPin, 1 , 0 ); // Set starter to ON
129- currentBatteryVolt = batteryVoltage ();0
130- Serial.println (" Starter engaged" );
133+ changePin (startPin, 1 , 0 ); // Set starter to ON with 0 delay
134+
135+ if (batteryVoltage () > 15 || millis () - currentStarterTime >= starterMaxDuration)
136+ {
137+ break ;
138+ }
131139 }
132140
133- Serial.println (" Starter disengaged" );
134141 changePin (startPin, 0 , starterDelay); // Set starter to OFF
135142
136- if (batteryVoltage () > 13 )
143+ if (batteryVoltage () > 15 )
137144 {
138145 digitalWrite (lightFanPin, HIGH);
139146 startTime = millis ();
140147 delay (2000 );
141148 }
142149 else
143150 {
144- Reply ( " Engine has failed starting, retrying " );
145- startEngine ();
151+ changePin (ignPin, 0 , 0 );
152+ Reply ( " Engine has failed starting, starter max duration reached " ); // Runs the startEngine loop again if it detected that the engine has not started
146153 }
147154 }
155+ else if (digitalRead (NeutSwitchPin) == LOW)
156+ Reply (" Failed to start, car is in gear!" );
148157}
149158
150159void stopEngine () // Stops our engine
151160{
152161 digitalWrite (ignPin, LOW);
153162 digitalWrite (startPin, LOW);
154163 digitalWrite (lightFanPin, LOW);
155- Reply (" Engine is stopped" ); // Change this if you want to change what the reply text is for when the stop command is sent
164+ Reply (" Engine has stopped" ); // Change this if you want to change what the reply text is for when the stop command is sent
156165}
157166
158167void readCommand (String messageString) // Read commands from messageStrings
159168{
160- Serial.println (" Message Received: \n " + messageString);
169+ Serial.println (" Message Received: " + messageString);
161170
162171 if (messageString.indexOf (cmdStatus) >= 0 )
163172 {
@@ -167,7 +176,7 @@ void readCommand(String messageString) // Read commands from messageStrings
167176 else if (messageString.indexOf (cmdChangeTime) >= 0 )
168177 {
169178 Serial.println (" Command: Change Time Confirmed" );
170- changeRunTime ();
179+ changeRunTime (messageString );
171180 }
172181 else if (messageString.indexOf (cmdStart) >= 0 )
173182 {
@@ -202,10 +211,6 @@ void CheckPhone(String messageString) // Compares userPhone-value to message's p
202211 {
203212 readCommand (messageString);
204213 }
205- else if (messageString.length () > 18 )
206- {
207- Serial.println (" Phone number invalid! Has to be: " + userPhone);
208- }
209214}
210215
211216float batteryVoltage ()
@@ -215,7 +220,7 @@ float batteryVoltage()
215220
216221void engineLoop () // Checks our engine's status and applies measures
217222{
218- if (digitalRead (IgnHotPin) == HIGH) // If key is turned while remote start active, disable remote start-script
223+ if (digitalRead (IgnHotPin) == HIGH) // If key is insterted while remote start active, then this will disable the remote start-script
219224 {
220225 digitalWrite (startPin, LOW);
221226 digitalWrite (ignPin, LOW);
@@ -229,9 +234,18 @@ void engineLoop() // Checks our engine's status and applies measures
229234 }
230235}
231236
237+ void changeCommandsToLowercase ()
238+ {
239+ cmdChangeTime.toLowerCase ();
240+ cmdStatus.toLowerCase ();
241+ cmdStart.toLowerCase ();
242+ cmdStop.toLowerCase ();
243+ }
244+
232245void setup () // Setup arduino
233246{
234- Serial.begin (115200 ); // Start serial over USB at speed 115200
247+ changeCommandsToLowercase (); // Changes all commands to lower-case
248+ Serial.begin (115200 ); // Start serial over USB at speed 115200
235249 sim800Setup ();
236250 pinSetup ();
237251}
@@ -264,8 +278,8 @@ void Reply(String text) // Sends a reply to our phone
264278 sim800l.print (" AT+CMGS=\" " + userPhone + " \"\r " );
265279 delay (1000 );
266280 sim800l.print (text);
267- delay (100 );
281+ delay (300 );
268282 sim800l.write (0x1A ); // ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26
269- delay (1000 );
283+ delay (500 );
270284 Serial.println (" SMS Sent Successfully." );
271- }
285+ }
0 commit comments