MotoMetrics
What?
MotoMetrics is a system for motorcycles that displays various data during both regular rides and circuit days. Its goal is to provide riders with comprehensive information during and after their rides, including G-forces, maximum and average speed, braking, acceleration, tilt angle, route, and engine/ambient temperature. Riders can review this information for each ride, gaining insights into their performance. They have control over starting and stopping their own rides.
Additionally, MotoMetrics features an automatic LED lighting system that adjusts its brightness based on the outside light intensity, enhancing visibility. Furthermore, a motion sensor is included, enabling riders to activate an alarm system that sounds a siren when the motorcycle moves, enhancing security.
Supplies
Raspberry Pi (4)
Sensors
- DS18B20 (one-wire temperature)
- K Type Thermocouple CHT (higher engine temperatures)
- MAX6675 Module (To connect the thermocouple sensor to the Pi)
- MPU-6050 (G-Forces & tilt sensor)
- LDR (Light sensor to control the LED lighting)
- GPS (Location and speed)
Actuators
- I²C OLED display
- 12V sirene
- LED-lighting
Circuitry
- (Breadboard) wires
- Resistors (3x 470Ω, 1x 47KΩ)
- 3x TIP120 Darlington transistor (To control the siren and LED lighting that are on the electrical circuit of the engine with the Pi and controling components directly powered by the Pi)
- 0.1uf capacitor
Assembly
- Male and female headers
- perf-board
- heat-shrink tube
- Surface mount normaly open push button
- Surface mount female 2.1mm barrel jack connectors (3x)
- male 2.1mm jack (3x)
- 12v car socket male and female
- wire terminal kit (for making some non permanent connections, can always be soldered)
- (3D printed) housing
Tools
- Soldering iron, pincers, pliers, razor blade, dremel...
- 3D-printer
Circuit Protoyping
Before making anything permanent i suggest you assemble the project on a breadboard for testing purposes. If you are completely confident in your ability to avoid errors, you may skip this step and use step 8 to build the hardware of this project. The electrical/breadboard schematic can be found in the provided PDF's. I used an additional breadboard power supply to ensure that the maximum current of the Pi is not exceeded but after testing, everything should be able to run of the Pi directly.
The LED and buzzer connected to the TIP120s serve as representations of the additional LED lighting and piezo alarm on the motorcycle, which are powered directly from the motorcycle's battery (12V DC).
Note: Before powering up the Pi, carefully examine your circuit to ensure there are no short circuits.
Setup and Connect to the Pi
- Download the start image file here and load it to your SD for the Pi to boot of (If you don't know how to write an image to an external storage, here's a guide
- Plug in your Pi and connect it to to a PC with an ethernet cable, configure your pc to have a static ethernet ip here's a guide (i used 192.168.168.5 and subnetmask 255.255.255.0)
- Connect to the Pi with a SSH client like Remote - SSH on VSC. the Pi's ip is 192.168.168.169, user:"pi" and password:"W8w00rd"
- After logging in go to the raspi-config with "sudo raspi-config" to enable SPI, I2C, 1-Wire and Serial Port (no for "For a login shell" and yes for "For the port hardware"). Use the raspi-config GUI to also connect the Pi to your home WIFI-network
- Reboot the Pi and perform an "apt-get update" and "apt-get upgrade" to get your Pi up to date
- Clone/download this repository to the Pi
Database
On the Pi
Download, install and launch MySQL Workbench
Click on the "New Connection" button in the "MySQL Connections" section of the home screen.
Use the details in the image above
Open the .sql file inside the "database" folder of the repository and run it with MySQL Workbench to install the database on the Pi
The database
- Ride
Stores all the rides you make, when a ride is started only the id, name and start_time are stored. when the ride ends the backend gets all the values of the history table that corresponds to the ride and calculates the minimums, maximums and averages to then update the ride with all the information. The start_time defaults to "CURRENT_TIMESTAMP"
- history
Here is where the largest amount of data is stored. When a ride is started the backend sends every 5 seconds the captured data to the database. this data is later used to calculate maximums, minimums, averaged, distance and plot the route taken. This table is the only one with Foreign Keys: rideid, actionid, deviceid
- action
This table is to give more explanation to things that happen in the history table like "save speed"
- device
To know out of what sensor the value is from, we need to keep track of our devices. this is simply a list with all our components
Backend
On the Pi
To run the backend on the Pi we need to install some packages, use "pip install -r requirements.txt". this is a good time to test all your component, in the /backend/helpers folder of the repository you can find classes for all the used sensors, they all have a "Example usage" in comments at the end so you can test them out induvidualy. remember that if you followed the shematic of the diy test, you have to first set GPIO18 high to provide power to the components (use "testhat.py" to test the sensors in that case). If you want to test the screen you can use the examples in the Adafruit library.
If all the components work you can try to run the main script called "app.py". Check the config.py file in the "backend" folder and make sure the credentials from MySQL Workbench are right.
Running the app.py script is necesairy to use MotoMetrics. if you dont want to run the script manualy every time you can open the motometrics.service file and change the "<path to repository>" with the path to the repository. move it to "/etc/systemd/system/" with "sudo cp motometrics.service /etc/systemd/system/motometrics.service" and "sudo systemctl enable motometrics.service" to let it run on boot
The Backend
- app.py
The main act of the backend. this is where all the sensors are being read and send to the database and frontend to display to the user
- helpers
Like mentioned earlier, /helpers contains all the classes used to read and test the sensors
- repositories
The scripts in this folder are used to interact with the database: get, update, delete or create data and execute SQL
- config.py
This is used to configure the connection to your database
Frontend
On the Pi
Apache2 is already installed on the Pi image but it wil now only show the contents of "/var/www/html" so eather you move the contents of the "front" folder inside the repositroy with "sudo cp -r <path to repository>/front/. /var/www/html"
or
you change the settings of apache by using "sudo nano /etc/apache2/sites-available/000-default.conf" and change whatever is after "DocumentRoot" to <path to repository>/front. after that you just need to use sudo nano "/etc/apache2/apache2.conf" and change
<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
to
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
After a reboot you schould see the MotoMetrics site when u surf to the Pi's ip
The Frontend
- .html
The diferent pages of the site that you surf to with your browser
- style
The files used to style the pages. i used SCSS compiler so i could make use of a boilerplate and open-color
- scripts
the javascript's needed to display data from the backend and provide extra features. The datahandler.js is needed to get data from the backend trough routes. aigain, app.js is the headliner here
- maps
Because i won't always have an internet connection with my pi i wont be able to use api's. So i downloaded maps for the local area instead (this is also why i need to download the script's for socket.io and leaflet)
Make It Wireless
Unfortunately most motorcycles don't have networking capability's so we're going to connect to the pi directly by turning it into a WIFI network. to do this we need to install hostapd and dnsmasq
"sudo apt-get install hostapd dnsmasq"
Give the WLAN a static ip
"sudo nano /etc/dhcpcd.conf" and paste the following at the end
#wifi hostspot
interface wlan0
static ip_address=192.168.0.10/24
nohook wpa_supplicant
static domain_name_servers=8.8.8.8 8.8.4.4
(press Ctrl+X, then Y, then Enter to save the file and exit the editor)
Configure dnsmasq
"sudo nano /etc/dnsmasq.conf" and paste the following at the end
#The lines we added mean that we’re going to provide IP addresses between 192.168.0.11 and 192.168.0.30 for the wlan0 interface.
interface=wlan0
dhcp-range=192.168.0.11,192.168.0.30,255.255.255.0,24h
log-queries
enable-ra
(press Ctrl+X, then Y, then Enter to save the file and exit the editor)
Configure hostapd
"sudo nano /etc/hostapd/hostapd.conf" and paste the following at the end (you can change the SSID and wpa_passphrase as you please)
interface=wlan0
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ssid=MotoMetrics
wpa_passphrase=VroomVroomWIFI
(press Ctrl+X, then Y, then Enter to save the file and exit the editor)
"sudo systemctl enable hostapd dnsmasq" Enable hostapd and dnsmasq to run at boot.
Now perform a reboot and test the network
Note that if you're using the Pi to host a WIFI-network you wont be able to connect to a wireless network yourself. if you want to stop hosting a network with your Pi simply use "sudo systemctl disable hostapd dnsmasq" to disable the services to run at boot and comment the changes we made in "/etc/dhcpcd.conf"
Housing
I have designed a project box measuring 18cm x 12cm x 7cm to accommodate all the components. The box features specific holes for a power lead, status LED, power button, and three female 2.1mm barrel plugs, allowing the sensor and actuator to be "Hot pluggable." In the top view of the blueprint, you can observe two slits intended for attaching velcro or a tension strap. These slits are then covered with the floor of the box, enabling the electronics to be mounted while allowing the strap to be adjusted. The O-led display is affixed to the outer screen plate, which is subsequently glued to the box.
After printing the housing (and making necessary dremel adjustments), I installed all the surface-mount buttons and connectors. To enhance the watertightness of the project, I added rubber rings wherever possible.
Final Assembly
When the breadboard prototype can run MotoMetrics and use all the sensors and actuators. you can "hard wire" everything. In the new schematics i added a third TIP120 to manage the power of the components connected directly to the pi and a power button.
To make the project as sturdy as possible whilst also being completely reversable/hotpluggable i made a "Pi hat" out of perf board and male/female headers. I soldered everything that i could not mount easily to the case (the MPU6050 too because it had to be somewhat level). then i eather used regular wire or breadboard wire to connect everything up. I used a hot glue gun to mount the Pi, MAX6675, voltage step down module, oled display and GPS breakout board to the housing.
Note: If you're trying to recreate the diy "Pi hat" make sure you take note of the copper lead i cut trough. I only had a one sided perf board and would not recommend it, had a few short circuit scares whilst making it
Mount to Motorcycle
Now it's time for the final step. Mount everything to the motorcycle and look a the pictures above to see how i mounted everything. This obviously is different for every motorcycle.
- Engine temperature
The plan was to put the thermocouple temperature ring between the spark plug and the piston head but i could not get it tight enough so i didn't have any compression to start the engine (even after removing the spacer ring on the spark plug and replacing it with the sensor)
- Buzzer
For the piezo buzzer i only needed to drill it's hole bigger and use a bolt to mount it to the bodywork of the motorcycle
- Led lighting
The led strip was already mounted to my motorcycle so i only had to disconnect it and connect the wire to the project housing
Note: how i used electrical tape to prevent a short!
- Power
The project is directly powered of the 12V battery with a standard cigarette lighter car plug socket for ease of use. There was a fuse inside the male part of the 12V socket, make sure you have a fuse between your 12V source and step down module. I also tested beforehand if my Pi would lose power when starting up my motorcycle but it seems like the step down module can keep delivering a stable 5V.
- Housing
I put self fasteners trough the slits on the bottom of the housing and used them to mount it to my seat. you can mount it basically anywhere where the housing sits somewhat level. Make sure that you route all your wires in a neat a secure way. I used made sure to use already existing cable routes under the tank and hide all the access wire behind a side panel