MotoMetrics

by EggermontLouis in Circuits > Raspberry Pi

1041 Views, 10 Favorites, 0 Comments

MotoMetrics

_DSF0545.JPG
_DSF0533.JPG
_DSF0539.JPG
_DSF0523.JPG

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

Schermafbeelding 2023-06-20 225100.png
Schermafbeelding 2023-06-05 213430.png
Schermafbeelding 2023-06-05 213202.png
348360994_649746013226509_2663748992817982470_n.jpg
348362120_929269681462304_4028908293817946238_n.jpg

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

353091057_973557327221398_3682980731701517388_n.jpg


  1. 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
  2. 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)
  3. 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"
  4. 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
  5. Reboot the Pi and perform an "apt-get update" and "apt-get upgrade" to get your Pi up to date
  6. Clone/download this repository to the Pi


Database

Schermafbeelding 2023-06-20 195309.png
Schermafbeelding 2023-06-20 224458.png

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

Schermafbeelding 2023-06-21 141030.png
Schermafbeelding 2023-06-21 141050.png
Schermafbeelding 2023-06-21 141123.png

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

Schermafbeelding 2023-06-05 220425.png
Schermafbeelding 2023-06-05 220703.png
353110178_1340323123364197_5201997097547014638_n.jpg
353078872_795786075491930_5619945553579066004_n.jpg
353055778_631350935596233_2563460083604838756_n.jpg
353048699_1016620196164515_1717005893947047013_n.jpg
353064487_287219120446137_7186647446884340343_n.jpg

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

Schermafbeelding 2023-06-20 231326.png
Schermafbeelding 2023-06-20 231344.png
Schermafbeelding 2023-06-20 231424.png
353102748_1064766567829211_5662040425222713180_n.jpg
353045888_884559399912775_7126027223973048946_n.jpg
353039227_3563028053926646_3962591330502986941_n.jpg
353132653_261745799879487_2339322540885428043_n.jpg
353125540_237843689004157_7649445224472571116_n.jpg
353102720_621238326739608_4126639146627505369_n.jpg
353081543_644277331061966_2413332848597157800_n.jpg
353039246_673684877925653_4971414981057406045_n.jpg
353094464_296312169459463_353552960669074254_n.jpg
353087579_3387949111417096_6733389809523590274_n.jpg
353094440_225111133676982_5430333248929041111_n.jpg
353074101_574275184879485_1905446372082485342_n.jpg
353084274_1611462659330420_9182507447977610153_n.jpg
353087579_109522585515107_2106231383749145307_n.jpg

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

353069424_1803593360095352_1215175452022040545_n.jpg
353064508_219324120997884_7145128681820653316_n.jpg
353045896_573114691564290_2972139037469815517_n.jpg
353074113_990818725391644_8326922399339563296_n.jpg
353031587_214731014807583_6783690605504553093_n.jpg
353118241_3612473075665580_5895765815275974790_n.jpg
353059673_594965529413307_6443730762016421694_n.jpg
353089836_786708339762384_3945414111367975154_n.jpg
353137436_177990428347591_5890018358594946203_n.jpg
353128865_1004538917589655_1486448646177490256_n.jpg
353026916_621158659749541_5207048786066952517_n.jpg
353110160_284561377393848_2720519793782901700_n.jpg
353112719_788359602740041_4892072647780704514_n.jpg
353105791_962199398257421_2326285654623983561_n.jpg
353110694_1683994245439274_4224200414764706593_n.jpg
353110703_6619985074720519_2879353154244931363_n.jpg
353115942_185130914507248_4395359074548969979_n.jpg
353115939_967558751054799_1137695838894352374_n.jpg

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