Create a device that will recognize when a car alarm is sounding and text the user that his/her car alarm is going off and they should investigate. The device shouldn't go off if another car is just honking for a few seconds or if a loud bang occurs - it should only go off if there is a sustained car alarm.
IntroductionCar alarms are present on almost all new cars. Some cars will let you set the tolerance setting for when the car alarm goes off and various other settings but this is only helpful if you are: 1) near enough to the car to hear/see the alarm go off or 2) can recognize your car's alarm sound from a distance so you can go investigate. To address this issue, and using a Nova Hologram, a Raspberry Pi W, a USB microphone and a portable battery, I've assembled a device that will recognize the car alarm and alert you.
MethodsThe requirements are outlined above and in order to address them I had to make a couple of assumptions:
- I assume that the device is going to go inside the glove box of the car. The reason being is the glove box is relatively insulated compared to the rest of the car. This allows distant noises (like birds chirping or general traffic) to be diminished, allowing the tolerance level detected to be lower
- Initially, I set the tolerance low for testing. If you are building this for your car, you can set the tolerance level higher as needed.
- I had to distinguish between a car loudly stopping or a car honking for a couple seconds from a sustained car alarm. The assembled device listens for several seconds and if the tolerance is above that threshold for that period of time, it sends the alarm. If the device only hears one peak and then doesn't hear another peak within a waiting period, it will reset the count.
.This project was initially developed on a Raspberry Pi B+ and then the SD card was transferred to the Raspberry Pi Zero W after most of the testing was complete. This was done because the Raspberry Pi B+ has four USB ports = two for keyboard and mouse, one for the microphone USB plug and one for the Nova Hologram.
The code was developed in python 2.7 and required the following libraries:
- Hologram Cloud library - please follow the excellent guide to initially setting it up at https://projects.hologram.io/hologram/add-cellular-to-a-raspberry-pi-with-hologram-nova-ea5926
- Time library - this was used to create a delay in the code that would "wait" a certain amount of time, send the text and then delay again before starting
- audioop - this python library is incredibly useful. The rms function was used to take the root mean squared of the 1/2 second interval that is sampled.
- pyaudio - this was used in tandem with the audioop library to take in samples.
In order to import the pyaudio library into python, use:
sudo apt-get install python-pyaudio
In case this is your first time using python, to verify that you have imported the libraries correctly, open up a window and type:
import pyaudio
If it doesn't result in an error, you've done imported the library correctly.
After you've imported all of the libraries, the next step is verify that you can text from hologram.
Here is the link to the Python SDK for Hologram: https://hologram.io/docs/reference/cloud/python-sdk/
The most important part of this is where it talks about HologramCloud(credentials, network='',authentication_type='topt'). The default mode for the authentication_type is totp but in order to send texts from the device, the authentication_type must be csrpsk. credentials are set as:
credentials = {'devicekey':'your 8 letter device key found in the hologram overview'
The device utilizes the four usb ports and the power supply was initially from the wall but final installation is using a lipo battery.
See attached code for the commented program.
The initial setup included playing a car alarm audio off of my computer and setting the threshold down to 100.
After 10 seconds of the noise, I received a text.
To make the script run when the device boots, open a command window on your raspberry pi and type:
crontab -e
It will ask you which one you would like to boot into, select option 2.
Enter in at the bottom of the script:
@reboot sudo python /home/pi/test.py
test.py is what I called the script I wrote. You can change based off of your naming.
Press CTRL+X to exit the script and make sure you saved. I followed the following instructions for this: https://www.dexterindustries.com/howto/auto-run-python-programs-on-the-raspberry-pi/
Here is the image of the text I received after setting up the script to start on boot:
The final step was to take the SD card out of the Raspberry Pi 3 and plug it into the Raspberry Pi Zero W and verify it works!
So it successfully boots up and I can run the program without any issues. I plugged it into a portable battery and the same result occurred!
Using a Raspberry Pi W Zero, a Hologram Nova Global 3G/2G Cellular Modem, and a USB microphone, I've created a device that will send alerts to me when my car alarm goes off. Future implementations could be to charge the battery off of the car when it's on so that you wouldn't have to swap the portable charger. I had to adjust the threshold value to be perfect for my car (not as easy of a task as I'd like because I had to remote into it over wifi and my wifi range doesn't extend that far).
Comments