Nicolas Hammannkindicane
Published

Car Security System

Putting 21st century technology into a 1989 Ford Mustang. A security system that gives the user control of their vehicle's ignition.

IntermediateFull instructions provided6 hours433
Car Security System

Things used in this project

Hardware components

Photon
Particle Photon
×2
SPDT 10A 3V Relay
×1
Solar Motion Sensor Light
×2
Resistor 10k ohm
Resistor 10k ohm
×4
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×15

Software apps and online services

Maker service
IFTTT Maker service
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Car Relay Photon Schematic

Displays the pin out and schematic of the car relay photon.

Car Door Sensor Photon Schematic

Displays the pin out and schematic of the car door sensor photon.

Code

Webhook Form

JSON
This is the code needed to enter into the Webhook form integration for the car door sensor photon so that it will graph using Thingspeak.
{
    "event": "cardoor_open_",
    "url": "https://api.thingspeak.com/update",
    "requestType": "POST",
    "form": {
        "api_key": "{{k}}",
        "field1": "{{1}}",
        "field2": "{{2}}",
        "field3": "{{3}}",
        "field4": "{{4}}",
        "field5": "{{5}}",
        "field6": "{{6}}",
        "field7": "{{7}}",
        "field8": "{{8}}",
        "lat": "{{a}}",
        "long": "{{o}}",
        "elevation": "{{e}}",
        "status": "{{s}}"
    },
    "mydevices": true,
    "noDefaults": true
}

Webhook Form Car Relay

JSON
This is the code needed to enter into the form section of your Webhook Integration for the car relay photon.
{
    "event": "carrelay_open_",
    "url": "https://api.thingspeak.com/update",
    "requestType": "POST",
    "form": {
        "api_key": "{{k}}",
        "field1": "{{1}}",
        "field2": "{{2}}",
        "field3": "{{3}}",
        "field4": "{{4}}",
        "field5": "{{5}}",
        "field6": "{{6}}",
        "field7": "{{7}}",
        "field8": "{{8}}",
        "lat": "{{a}}",
        "long": "{{o}}",
        "elevation": "{{e}}",
        "status": "{{s}}"
    },
    "mydevices": true,
    "noDefaults": true
}

Car Door Sensor Photon

Arduino
This is the code that corresponds to the car door sensor photon.
/*  begin particle webhook code snippit to add to particle webhook interface on console
{
    "event": "cardoor_open_",
    "url": "https://api.thingspeak.com/update",
    "requestType": "POST",
    "form": {
        "api_key": "{{k}}",
        "field1": "{{1}}",
        "field2": "{{2}}",
        "field3": "{{3}}",
        "field4": "{{4}}",
        "field5": "{{5}}",
        "field6": "{{6}}",
        "field7": "{{7}}",
        "field8": "{{8}}",
        "lat": "{{a}}",
        "long": "{{o}}",
        "elevation": "{{e}}",
        "status": "{{s}}"
    },
    "mydevices": true,
    "noDefaults": true
}
*/

char publishString[40];
int doorpin = D0;
int volttestpin = A5;
int voltpowerpin = A4;

const String key = "0W9OG8025VEKLUCM";



int rssival = 0;
volatile bool doorsend = FALSE;
volatile bool rssisend = FALSE;
int doortriggers = 0;
long waketime = 0;
SYSTEM_THREAD(ENABLED); //enable multitasking.  allows the ability to catch the doorpin trigger and send the message
SYSTEM_MODE(AUTOMATIC);  //run code. then connect     
     
void rssi_update()
{
    //delay (2000);
    static int count = 0;
    Serial.println(count++);
    rssival = WiFi.RSSI();
    sprintf(publishString,"%d",rssival);
    bool success = Particle.publish("RSSI",publishString);
    
    sprintf(publishString, "%1.4f", checkbattery());



       Particle.publish("cardoor_open_All", "{ \"1\": \"" + String(rssival) + "\"," +
       "\"2\": \"" + String(checkbattery()) + "\"," +
       "\"3\": \"" + String(doortriggers) + "\"," +
     //"\"4\": \"" + String(var3) + "\"," +
     //"\"5\": \"" + String(var4) + "\"," +
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
    if (success == TRUE) {
        //delay(10000);
        rssisend = FALSE; //if sent, then turn of the send flag, otherwise let it try again.
    }
}


void enabledoorsend(void){
    doorsend=TRUE;
    digitalWrite(D7,1);
}


      



void setup() {
    //setADCSampleTime(ADC_SampleTime_239Cycles5);  //sets adc to longest time for low noise
    Time.zone(-4);  //set eastern time zone
    pinMode(doorpin,INPUT);
    pinMode(voltpowerpin, INPUT);
    pinMode(D7, OUTPUT);
    attachInterrupt(doorpin, enabledoorsend, FALLING);
    Particle.variable("doortrig", doortriggers);
    rssisend = TRUE;
}



void loop()


{
    if ((Particle.connected())&&(doorsend == TRUE)&&WiFi.ready()) 
    {
        doornotification();
    }
    
    if ((Particle.connected())&&(rssisend == TRUE)&&WiFi.ready()) 
    {
        rssi_update();
    }
    

    if ((millis()-waketime > 200000) && (doorsend == FALSE) && (rssisend == FALSE)) 
    {
        Particle.disconnect();
        
        //System.sleep(doorpin,RISING,30); 
        System.sleep(doorpin,FALLING);
        pinMode(doorpin,INPUT); // this may need resetting after sleep.
        waketime = millis();  //millis haults when sleeping.  find out the time on boot and compare from there.
        rssisend = TRUE;  //on wakeup report rssi but do not report door trigger
        doorsend = FALSE;
        
    }

    if (digitalRead(doorpin) == LOW) {
        enabledoorsend();
    }
    
    if (Particle.connected() == false) 
    {
        Particle.connect();
    }
    /*if (waitFor(Particle.connected, 20000))
    {
    }
    */
   
}

float checkbattery(){
    float battvolts;
    pinMode(volttestpin, INPUT);  
    pinMode(voltpowerpin, OUTPUT); //sent power pin as output
    digitalWrite(voltpowerpin, 0); //send a 0, or LOW to turn on voltage test
    delay(1); //wait for volts to settle good measure
    battvolts = analogRead(volttestpin); //sample voltage
    battvolts = float(battvolts)*3.3/4096.0*2.0*3.85/3.827;  //calibrate voltage reading
    pinMode(voltpowerpin, INPUT); //disable powerpin and set as high Z input
    return(battvolts); //return calibrated battery voltage
}

void doornotification(void){
    digitalWrite(D7,1);
    rssi_update();
    sprintf(publishString, "%1.4f", checkbattery());
    Particle.publish("mail/volts", publishString,60,PRIVATE);
    
    sprintf(publishString, "%d", doortriggers);
    bool success = Particle.publish("Someone is in your caarrrrrr!!!!", Time.format(Time.now(), "240027000f51353338363333"), 60, PRIVATE);
    delay(1000);
    if (success == TRUE) {
        doortriggers++;
        doorsend = FALSE; //if sent, then turn off the send flag, otherwise let it try again.
        digitalWrite(D7,0);
        }
}

Car Relay Photon

Arduino
This is the code that corresponds to the car relay photon.
/*  begin particle webhook code snippit to add to particle webhook interface on console
{
    "event": "carrelay_open_",
    "url": "https://api.thingspeak.com/update",
    "requestType": "POST",
    "form": {
        "api_key": "{{k}}",
        "field1": "{{1}}",
        "field2": "{{2}}",
        "field3": "{{3}}",
        "field4": "{{4}}",
        "field5": "{{5}}",
        "field6": "{{6}}",
        "field7": "{{7}}",
        "field8": "{{8}}",
        "lat": "{{a}}",
        "long": "{{o}}",
        "elevation": "{{e}}",
        "status": "{{s}}"
    },
    "mydevices": true,
    "noDefaults": true
}
*/

char publishString[40];
int doorpin = D0;
int volttestpin = A5;
int voltpowerpin = A4;
int carpin1 = D2;
int carpin2 = D3;


const String key = "8AX5KQ0S06M3E3EZ";



int rssival = 0;
volatile bool doorsend = FALSE;
volatile bool rssisend = FALSE;
int doortriggers = 0;
long waketime = 0;
SYSTEM_THREAD(ENABLED); //enable multitasking.  allows the ability to catch the doorpin trigger and send the message
SYSTEM_MODE(AUTOMATIC);  //run code. then connect     
     
void rssi_update()
{
    //delay (2000);
    static int count = 0;
    Serial.println(count++);
    rssival = WiFi.RSSI();
    sprintf(publishString,"%d",rssival);
    bool success = Particle.publish("RSSI",publishString);
    
    sprintf(publishString, "%1.4f", checkbattery());


       Particle.publish("carrelay_open_All", "{ \"1\": \"" + String(rssival) + "\"," +
       "\"2\": \"" + String(checkbattery()) + "\"," +
       "\"3\": \"" + String(doortriggers) + "\"," +
     //"\"4\": \"" + String(var3) + "\"," +
     //"\"5\": \"" + String(var4) + "\"," +
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
    if (success == TRUE) {
        //delay(10000);
        rssisend = FALSE; //if sent, then turn of the send flag, otherwise let it try again.
    }
}


void enabledoorsend(void){
    doorsend=TRUE;
    digitalWrite(D7,1);
}


      



void setup() {
    //setADCSampleTime(ADC_SampleTime_239Cycles5);  //sets adc to longest time for low noise
    Time.zone(-4);  //set eastern time zone
    pinMode(doorpin,INPUT);
    pinMode(voltpowerpin, INPUT);
    pinMode(D7, OUTPUT);
    attachInterrupt(doorpin, enabledoorsend, FALLING);
    Particle.variable("doortrig", doortriggers);
    rssisend = TRUE;
        //We set the pin mode to output
    pinMode(carpin1, OUTPUT);
    pinMode(carpin2, OUTPUT);
    //We "Subscribe" to our IFTTT event called button so that we get events for it 
    Particle.subscribe("button", myHandler);
}

//The function that handles the event from IFTTT
void myHandler(const char *event, const char *data){
    // We'll turn off the car
    digitalWrite(carpin1, HIGH);
    digitalWrite(carpin2, HIGH);
    
    delay(900000);
    
    
     digitalWrite(carpin1, LOW);
    digitalWrite(carpin2, LOW);
}
void loop()


{
    if ((Particle.connected())&&(doorsend == TRUE)&&WiFi.ready()) 
    {
        doornotification();
    }
    
    if ((Particle.connected())&&(rssisend == TRUE)&&WiFi.ready()) 
    {
        rssi_update();
    }
    

    if ((millis()-waketime > 200000) && (doorsend == FALSE) && (rssisend == FALSE)) 
    {
        Particle.disconnect();
        
        //System.sleep(doorpin,RISING,30); 
        System.sleep(doorpin,FALLING);
        pinMode(doorpin,INPUT); // this may need resetting after sleep.
        waketime = millis();  //millis haults when sleeping.  find out the time on boot and compare from there.
        rssisend = TRUE;  //on wakeup report rssi but do not report door trigger
        doorsend = FALSE;
        
    }

    if (digitalRead(doorpin) == LOW) {
        enabledoorsend();
    }
    
    if (Particle.connected() == false) 
    {
        Particle.connect();
    }
    /*if (waitFor(Particle.connected, 20000))
    {
    }
    */
   
}

float checkbattery(){
    float battvolts;
    pinMode(volttestpin, INPUT);  
    pinMode(voltpowerpin, OUTPUT); //sent power pin as output
    digitalWrite(voltpowerpin, 0); //send a 0, or LOW to turn on voltage test
    delay(1); //wait for volts to settle good measure
    battvolts = analogRead(volttestpin); //sample voltage
    battvolts = float(battvolts)*3.3/4096.0*2.0*3.85/3.827;  //calibrate voltage reading
    pinMode(voltpowerpin, INPUT); //disable powerpin and set as high Z input
    return(battvolts); //return calibrated battery voltage
}

void doornotification(void){
    digitalWrite(D7,1);
    rssi_update();
    sprintf(publishString, "%1.4f", checkbattery());
    Particle.publish("mail/volts", publishString,60,PRIVATE);
    
    sprintf(publishString, "%d", doortriggers);
    bool success = Particle.publish("Someone is in your caarrrrrr!!!!", Time.format(Time.now(), "240027000f51353338363333"), 60, PRIVATE);
    delay(1000);
    if (success == TRUE) {
        doortriggers++;
        doorsend = FALSE; //if sent, then turn off the send flag, otherwise let it try again.
        digitalWrite(D7,0);
        }
}

Credits

Nicolas Hammann

Nicolas Hammann

1 project • 0 followers
Contact
kindicane

kindicane

1 project • 0 followers
Contact

Comments

Add projectSign up / Login