Alex Merchen
Published

Car Alarm Tracker

Create a device that will send you a text when your car's alarm goes off.

IntermediateFull instructions provided3 hours1,615
Car Alarm Tracker

Things used in this project

Story

Read more

Code

Hologram Texting

Python
This is the program to run. I saved it as \home\pi\test.py on my raspberry pi.
from Hologram.HologramCloud import HologramCloud
import audioop
import pyaudio
import time

credentials = {'devicekey':'12345678'}
hologram = HologramCloud(credentials, network='cellular',authentication_type='csrpsk')

# this is the threshold that determines whether or not sound is detected. The higher the number, the louder it is
# according to 
THRESHOLD = 10000

p = pyaudio.PyAudio()

chunk = 22050

# 'rate' specifies the desired sampling rate in Hz (samples/seconds)
# 'channels' specify the desired number of channels
# 'input' specifies whether this is an input stream - defaults to False
# 'output' specifies whether this is an output stream - defaults to False
# 'frames_per_buffer' specifies the number of frames per buffer (samples)
# the 'frames_per_buffer' divided by the 'rate' will determine how long the period we're sampling will be
# For example: 22050/44100 = 0.5 seconds so each data chunk will be for 0.5 seconds
# stream will open 
stream = p.open(format=pyaudio.paInt16,
                channels=1,
                rate=44100,
                input=True,
                frames_per_buffer=chunk)

#open your audio stream    

# countAboveThreshold specifies the number of chunks that are above the threshold value
countAboveThreshold = 0

# countSinceLastThreshold specifies the number of chunks that have occurred since the last threshold
# if the countSinceLastThreshold is above a certain number, we reset the countAboveThreshold
countSinceLastThreshold = 0

# wait until the sound data breaks some level threshold
while True:
    # data is the chunk of information from the current stream
    data = stream.read(chunk)
    # take the rms value of that and set it to be rms
    rms = audioop.rms(data,2)
    # if rms is greater than the threshold level
    #print rms
    if rms > THRESHOLD:
        # increase the countAboveThreshold
        countAboveThreshold += 1
        # reset the countSinceLastThreshold
        countSinceLastThreshold = 0
        # if the countAboveThreshold is above 5 (that means that there have been 2.5 seconds of a loud noise)
        if countAboveThreshold > 5:
            # reset coundAboveThreshold
            countAboveThreshold = 0
            # reset the countSinceLastThreshold
            countSinceLastThreshold = 0
            # Send a text message alerting the user that their car is going off and they should look into it
            delay(5)
            hologram.sendSMS("+112345678","Your car's alarm is currently going off")
            delay(5)
            #break

    # if the countAboveThreshold is above 0 that means that we have heard at least one loud noise
    if countAboveThreshold > 0:
        # increase the countSinceLastThreshold by 1
        countSinceLastThreshold += 1
        # if the countSinceLastThreshold is above 9 that means that there has been at least 5 seconds since the last loud noise
        if countSinceLastThreshold > 9:
            # reset the countAboveThreshold. This indicates that the loud noise wasn't long enough to be important
            countAboveThreshold = 0
            # reset the countSinceLastThreshold
            countSinceLastThreshold = 0

Credits

Alex Merchen

Alex Merchen

8 projects • 17 followers
I'm an EE with a Masters in ECE. I like building things.

Comments