Jerry Zhang
Published © GPL3+

CarMon

The device that allows you to monitor and monetize your car, all while helping out your community.

IntermediateWork in progress4 hours3,916

Things used in this project

Hardware components

Arduino 101 & Genuino 101
Arduino 101 & Genuino 101
×1
OBD breakout board
For the first version, this will just be used for powering the Arduino 101, but later versions will access data from the car.
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Serial to File Interpretor

Python
Written in Python, this takes serial data coming from the Arduino 101 and puts it into a file on the Raspberry Pi to be used later. You need to find the serial port that the Arduino is using to communicate to the Raspberry Pi in order for this to work.
import serial

serial_port = '/dev/INSERT SERIAL PORT HERE'
baud_rate = 9600
write_to_file_path = "serial.txt"

output_file = open(write_to_file_path, "w+")
ser = serial.Serial(serial_port, baud_rate)
while True:
    line = ser.readline()
    line = line.decode("utf-8")
    print(line);
    output_file.write(line)

Arduino 101 Code

Arduino
This code, compiled from different pieces of sample code given by Intel, takes a button press and prints "BUMP" as well as the time, as well as detects sudden and excessive Z axis movement and also prints "BUMP" and the time. The data is then taken from the serial prints to a file using the Python script.
#include "CurieIMU.h"
#include <CurieTime.h>

boolean blinkState = false;          // state of the LED
unsigned long loopTime = 0;          // get the time since program started
unsigned long interruptsTime = 0;    // get the time when motion event is detected

const int buttonPin = 7;

bool bumpbool = false;
int UP = 0;
int DOWN = 0;
long upTime = 0;
long downTime = 0;


void setup() {
  Serial.begin(9600); // initialize Serial communication
  while(!Serial) ;    // wait for serial port to connect.

  /* Initialise the IMU */
  CurieIMU.begin();
  CurieIMU.attachInterrupt(eventCallback);

  /* Enable Motion Detection */
  CurieIMU.setDetectionThreshold(CURIE_IMU_MOTION, 150); // 20mg
  CurieIMU.setDetectionDuration(CURIE_IMU_MOTION, 10);  // trigger times of consecutive slope data points
  CurieIMU.interrupts(CURIE_IMU_MOTION);

  Serial.println("IMU initialisation complete, waiting for events...");

  pinMode(buttonPin, INPUT);

  setTime(10, 00, 00, 20, 05, 2017); // Use this if you want the time print to be relative to the actual time. Comment it out if you want it to be relative to the time that the program started.
}

void loop() {
  // if motion is detected in 1000ms, LED will be turned up
  loopTime = millis();
  if(abs(loopTime -interruptsTime) < 1000 )
    blinkState = true;
  else
    blinkState = false;
  digitalWrite(13, blinkState);
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    bumpbool = true;
  }

  if (bumpbool) {
    getTime();
    Serial.println("BUMP");
    bumpbool = false;
  }

  if ((abs(upTime - downTime) > 500) && abs(upTime - downTime) < 100) {
    Serial.println("BUMP");
  }
}

static void eventCallback(void){
  if (CurieIMU.getInterruptStatus(CURIE_IMU_MOTION)) {
    if (CurieIMU.motionDetected(Z_AXIS, NEGATIVE))
      DOWN = DOWN + 1;
      downTime = millis();
//      Serial.println(downTime);
//      Serial.println(hour() + minute() + second());
      getTime();
      Serial.println("BUMP");
//      Serial.println("Positive motion detected on Z-axis");
    interruptsTime = millis(); 
  }
}

void getTime() {
  print2digits(hour());
  Serial.write(':');
  print2digits(minute());
  Serial.write(':');
  print2digits(second());
  Serial.print(", ");
  Serial.print(day());
  Serial.write('/');
  Serial.print(month());
  Serial.write('/');
  Serial.print(year());
  Serial.println();
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

Credits

Jerry Zhang

Jerry Zhang

9 projects • 13 followers

Comments