Spencer James
Published © GPL3+

"Secured by SONY®" How-To BYO Vehicle Security System

Don't rely on parking lot cameras! — Build Your Own VSS with the SONY Spresense! Get Started with this Completed Arduino Sketch and How-To!

BeginnerFull instructions provided6 hours890
"Secured by SONY®" How-To BYO Vehicle Security System

Things used in this project

Hardware components

Spresense boards (main & extension)
Sony Spresense boards (main & extension)
×1
Spresense camera board
Sony Spresense camera board
×1
Industrial SLC microSD
Delkin Industrial SLC microSD
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
Connected to a power supply.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

SONY Spresense Board Assembly Connected to USB

Connect your SONY Spresense Board Assembly to the micro-USB port on the Main Board — Not the Extension Board — for uploading the "Secured By SONY" Sketch.

Code

Secured by SONY Version 0.7

Arduino
Don't rely on parking lot surveillance systems — Build Your Own with the SONY Spresense! Completed Arduino Sketch and How-To! Full directions: https://www.hackster.io/sjem/secured-by-sony-how-to-byo-vehicle-security-system-vss-5bf8be
// "Secured by SONY" Vehicle Security System (VSS), Version 0.7
// An Arduino Sketch By Spencer Elliott-Manheim, (c) 2018-2019.
//
// Designed for SONY Spresense.
// Requires Main, Extension, & Camera Boards.
// https://developer.sony.com/develop/spresense
//
// Requires SD Card/Reader, USB Cable, Serial Monitor, and Resource Files.
//
// Prepared for the "Make it Better" Contest hosted by Hackster.io with SONY.
// https://www.hackster.io/contests/sony
//
// Read the full documentation:
// https://www.hackster.io/sjem/secured-by-sony-vehicle-security-system-vss-5bf8be
//
// See Reference Examples cited at end of program.
// Copyright 2018 SONY Semiconductor Solutions Corporation.
//
//
#include <SDHCI.h>
#include <stdio.h>

#include <Camera.h>

#define BAUDRATE (115200)

SDClass theSD;

// Counter for Motion Detection Comparison
int image_count = 0;

// Tolerance for Motion Detection
float motion_tolerance = 13.0;

// Motion Detection Timer
int motion = 0;
int next_motion = 0;

// Motion Detection Photoset Length
int motion_duration = 10;

// Counters to keep track of overall loops and incidents
// Tracking past incidents for potential calibration mechanism.
int cycle_count = 0;
int this_incident = 0;
int last_incident = 0;
int incident_before_last = 0;
float incident_count = 0;


// Various Variables for Motion Detection Comparison Calculation
float image0_size = 0;
float image1_size = 0;
float image_diff = 0;
float image_perdiff = 0;



// The following section based on camera.ino by SONY.

// Define Camera Error Feedback; Called when video streaming is attempted.

void CamCB(CamImage img) {

  if (img.isAvailable()) {
    img.convertPixFormat(CAM_IMAGE_PIX_FMT_RGB565);

    /*  Hidden for verbosity

        Serial.print("Image Count = ");
        Serial.print(image_count);
        Serial.print(". ");

        Serial.print("Image data size = ");
        Serial.print(img.getImgSize(), DEC);
        Serial.print(" , ");

        Serial.print("buff addr = ");
        Serial.print((unsigned long)img.getImgBuff(), HEX);
        Serial.println("");
    */
  }
  else {
    Serial.print("Failed to get video stream image\n");
  }
}

// End camera.ino excerpt.



void setup() {

  // LEDs Light up to indicate program is starting.
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  digitalWrite(LED0, HIGH);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  delay(500);

  digitalWrite(LED0, LOW);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  delay(500);



  // The following section based on camera.ino by SONY.

  // Open Serial Port Communications
  Serial.begin(BAUDRATE);

  // Wait until serial port connects.
  while (!Serial) {
    ;
  }

  // Prepare Camera for Photography; Initialization Requied.
  Serial.println("Preparing Camera.");
  theCamera.begin();

  // Start streaming video from the camera module.
  Serial.println("Start Streaming.");
  theCamera.startStreaming(true, CamCB);

  // Set Auto White Balance to automatic for Night/Day Outdoor Use.
  Serial.println("Set Auto White Balance.");
  theCamera.setAutoWhiteBalance(true);

  // Set Still Picture Parameters
  Serial.println("Set Still Picture Parameters");
  theCamera.setStillPictureImageFormat(
    CAM_IMGSIZE_QUADVGA_H,
    CAM_IMGSIZE_QUADVGA_V,
    CAM_IMAGE_PIX_FMT_JPG);

  // End camera.ino excerpt.


  // Wait 2 more seconds before beginning surveillance duties.
  // Provide opportunity to upload new programming before adding photos to SD card.
  delay(2000);
}


void loop() {

  // Wait 1 Second before returning to surveillance duties.
  delay(1000);

  // Up the motion counter to allow for multiple incidents to be recorded.
  next_motion = motion + motion_duration;

  // Indicate the Cycle Count.
  Serial.print("Beginning Cycle Count = ");
  Serial.print(cycle_count);
  Serial.println(". ");

  while (image_count < 2) {

    // The following section based on camera.ino by SONY.

    Serial.print("Taking Sample Image ");
    Serial.print(image_count + 1);
    Serial.println(".");

    CamImage img = theCamera.takePicture();

    /* Check availability of the img instance. */
    /* If any error was occured, the img is not available. */

    if (img.isAvailable())
    {
      /* Create file name */
      Serial.print("Captured Sample. Image Count = ");
      Serial.print(image_count);
      Serial.println(". ");

      // End camera.ino excerpt.


      // If this is the first image captured, record its size in image0_size.
      if (image_count == 0) {
        image0_size = img.getImgSize();
        Serial.print("First Image Size is ");
        Serial.print(img.getImgSize());
        Serial.print(". ");


        Serial.print("Recorded Image 1. Image Count = ");
        Serial.print(image_count);
        Serial.println(". ");
      }

      // If this is the second image captured, record its size in image1_size.
      if (image_count == 1) {
        image1_size = img.getImgSize();
        Serial.print("Second Image Size is ");
        Serial.print(img.getImgSize());
        Serial.print(". ");


        Serial.print("Recorded Image 2. Image Count = ");
        Serial.print(image_count);
        Serial.println(". ");
      }

    }

    image_count++;


    Serial.print("Finished Image Collection. Image Count = ");
    Serial.print(image_count);
    Serial.println(". ");
  }



  // If the two still photos are different, find percent difference.
  // If percent difference exceeds tolerance, Instigate Motion Capture.
  // Else, take two more photos (Main Loop Repetition).

  if (image0_size != image1_size) {

    // Find the larger of the two images to keep Percent Difference Positive.

    if (image0_size > image1_size) {
      image_diff = (image0_size - image1_size);
      Serial.print("The two images are different by ");
      Serial.print(image_diff);
      Serial.println(" bytes.");

      // Calculate Percent Difference.
      image_perdiff = (image_diff / image0_size);
      image_perdiff *= 100;
      Serial.print("The two images are different by ");
      Serial.print(image_perdiff);
      Serial.println(" percent.");
    }

    if (image1_size > image0_size) {
      image_diff = (image1_size - image0_size);
      Serial.print("The two images are different by ");
      Serial.print(image_diff);
      Serial.println(" bytes.");

      // Calculate Percent Difference.
      image_perdiff = (image_diff / image1_size);
      image_perdiff *= 100;
      Serial.print("The two images are different by ");
      Serial.print(image_perdiff);
      Serial.println(" percent.");
    }


    // Now, Knowing Percent Difference, Compare to Motion Tolerance.

    // Print out the current Motion Tolerance.
    Serial.print("The current Motion Tolerance is ");
    Serial.print(motion_tolerance);
    Serial.println(" percent.");

    // If the Percent Difference is greater than the motion tolerance,

    // Instigate Surveillance.

    if (image_perdiff >= motion_tolerance) {

      // Record Cycle Count of Incident; Step back prior incidents.
      // Incident-Count Relationship records in which cycle an incident occurred.
      // Tracking past incidents for potential calibration mechanism.
      incident_before_last = last_incident;
      last_incident = this_incident;
      this_incident = cycle_count;
      incident_count = incident_before_last + last_incident + this_incident;
      Serial.print("This incident belongs to cycle number ");
      Serial.print(this_incident);
      Serial.println(".");



      // Indicate Trigger with Visual Cue
      // Wait Audible Cue until after image capture completes.
      for (int i = 0; i <= 4; i++) {
        Serial.print("Motion detected!");
        Serial.print(5 - i);
        Serial.println("!");
      }

      // Reset Image Counter to repeat difference trials.
      image_count = 0;

      // Close Motion Detector (Percent Difference beyond Tolerance).
    }
    else {
      // Reset Image Counter to repeat difference trials.
      image_count = 0;
    }

    // Close Motion Detector, Difference within Tolerance.
  }
  else {
    // Reset Image Counter to repeat difference trials.
    image_count = 0;

    // Close Motion Detector, No Motion Detected.
  }


  // End Image Comparison for Motion Detection.

  // If instigated, begin surveillance.

  // Because 0 = 0, Surveillance will always run the first Cycle, Cycle 0.

  if (this_incident == cycle_count) {

    // Begin Capturing Photographs EVIDENCE!

    while (motion < next_motion) {

      Serial.print("Motion Count = ");
      Serial.print(motion);
      Serial.println(". ");

      // The following section based on camera.ino by SONY.

      Serial.println("Save current image to file.");
      CamImage img = theCamera.takePicture();

      char filename[16] = {0};
      sprintf(filename, "MOTION%03d.JPG", motion);

      Serial.print("Save taken picture as ");
      Serial.print(filename);
      Serial.println("");

      /* Save to SD card as the finename */

      File myFile = theSD.open(filename, FILE_WRITE);
      myFile.write(img.getImgBuff(), img.getImgSize());
      myFile.close();

      // End camera.ino excerpt.

      motion++;

      // Close While for Motion Capture.
    }

    // Record Location to File, Report to Owner, Alarm in Vicinity
    // To be added.

    Serial.print("This is when the audio plays. During Cycle ");
    Serial.print(cycle_count);
    Serial.println(".");
  }


  // Incremet Cycle Counter to keep track of repetitions.
  cycle_count++;

  // Close Main Loop
}

/*
  camera.ino - One minute interval time-lapse Camera
  Copyright 2018 SONY Semiconductor Solutions Corporation

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  This is a test app for the camera library.
  This library can only be used on the Spresense with the FCBGA chip package.
*/

// Thank you for reading!

Credits

Spencer James

Spencer James

2 projects • 2 followers
#MEATS Enthusiast (Math, Engineering, Art, Technology, & Science)

Comments