This is an old revision of the document!
Author: Careja Alexandru-Cristian
Previously working on another project that enhances the experience on riding a bike (at PM in 2021 when I designed a dashboard for my bike) I thought of what can I make with what I've learnt during the IoT course to make my bike rides better. Since moving to Bucharest for uni I've left my bike at home because I'm afraid of riding a bicycle in Bucharest's traffic. This has determined me to create a crash detection system for my bike. I already had the Neo-6M GPS module and the bike attachment “system” I designed 2 years ago, I just had to remove the old Arduino and the display.
Objects used:
The GPS Neo-6M module has the following connections to the ESP32:
The ADXL335 accelerometer has the following connections to the ESP32:
The program running on the ESP32 reads every 50 miliseconds the accelerometer data. It converts the analog reading into g units acceleration. The ADXL335 is capable of recording in a range from -3g to 3g. The analog read is a measure of voltage which lands in the interval [0, 3300 mV], where 1.65V should mean acceleration zero, and the edges (0 and 3300mV) -3g respectively 3g. When testing the readings myself, I've observed that when acceleration was 0, the reading was ~1.8V, so that's the value I used in my g force calculation.
x_adc_value = analogRead(ACCEL_X_Pin); /* Digital value of voltage on x_out pin */ y_adc_value = analogRead(ACCEL_Y_Pin); /* Digital value of voltage on y_out pin */ x_g_value = ((((double) x_adc_value /1023) - 1.8 ) / 0.3); /* Acceleration in x-direction in g units */ y_g_value = ((((double) y_adc_value /1023) - 1.8 ) / 0.3); /* Acceleration in y-direction in g units */
For every accel read, the acceleration values are checked, and if any of them is higher than a set threshold, then an alarm is triggered. The set threshold was chosen to be higher than the maximum deceleration a bicycle is physically capable of doing when using its brakes. That maximum is 0.71 according to this source. So, the threshold I chose is 0.8g.
Every approximately 50 seconds, the program tries to record the location to have it stored for future use if needed.
When an alarm is triggered, the program tries to read the location from the GPS module 5 times with 100ms in between tries. In case the GPS isn't available, it will use the last known location which is recorded every 50 seconds. It creates an alert message which includes a Google Maps location of where the crash happened or the last known location before the crash and sends it to the phone via Bluetooth using the BluetoothSerial library.
while (!updated_location && tries_left-- != 0) { if (ss.available() > 0) { if (gps.encode(ss.read())) { if (gps.location.isValid()) { latitude = gps.location.lat(); longitude = gps.location.lng(); strcat(alarm_msg, "Current location: "); updated_location = true; } } } delay(100); } if (!updated_location) { strcat(alarm_msg, "Last known location: "); } sprintf(location_string, " https://www.google.com/maps/@%.7f,%.7f,18z\n", latitude, longitude); strcat(alarm_msg, location_string); Serial.print("Sending alarm message"); Serial.println(alarm_msg); SerialBT.println(alarm_msg);