Introduction: GPS Based Tripmaster
I have a hobby, which is driving around with the car with someone who is navigating based on a unknown roadbook. It's called 'rittensport'.
You have to make sure distances, time, average speed, .... is all perfect. So to help myself and the navigator, I decided to make a "tripmaster" myself with a GPS. Because standard tripmasters don't suit my needs and are really expensive and a pain to calibrate, I decided to make my own.
If you want to make this, the cost is approximately 200 euro. It could be less, it could be more, depending on where you buy it.
See the PDF with the Bill Of Materials where I bought every component.
You can find all of my code on GitHub.
Step 1: Supplies/Materials/Tools
So, in order to make such thing, you kinda need alot of things, I've listed down everything what I have used to make this.
Supplies & materials
- 1x GPS module
- 1x UPS (uninterrupted power supply)
- 1x Battery (3.7V)
- 2x USB cables (5m)
- 1x touchscreen (I used a 7")
- 1x micro SD (16G minimum)
- 1x HDMI cable (5m)
- 1x push button
- a bunch of wires
- 1x 40 pin IDE wire
- 1x car charger with USB outputs (7.2A)
- 1x (white) plexi glass (I used a 18x20x2 cm board)
- 1x GPS antenna extention (5m)
- 1x MCP3800
- 1x Raspberry Pi 3 Model B
- a couple of standoffs
- LEDS (optional, I didn't use extra LEDS)
- 1x Telenet router (for the case)
- Soldering iron
- Tin (to solder)
- glue gun
- utillity knife
- pincet (if you have thick fingers like me)
- a third hand if you need to
- a saw if you want to use plexi too
Step 2: Making the Schemes and Diagrams
You can build the hardware from the schemes.
Step 3: Making the Case
So, I started with making the case. I got an old router from Telenet (thank you!) in which I could easely drop my components in.
I started by opening up the case and looked at the flat part. The first issue I encountered was, "how can I attach all the components to the case?"
Then I thought, well, I have some plexi laying around, I can press that in. I took the inside measurements of the router and started making everything in cartboard. Then once that fit the router I made it on the plexy. I started aligning in the case and then I drew everything on the plexy. Once everything was marked, I drilled some holes in it and tighten everything to it with spacers and screws.
Step 4: Modifying and Soldering the Hardware
So, I had to make a couple of changes in the hardware... I had the UPS, but that one had a power button on it, so it needed to be extended, since I had to be able to power the pi on the oudsite of the case. I connected the wires and then hot glued the button in place on the other side of the case.
To make the ports fit in the case, I removed some of the material to make it fit.
So, I had a piece of PCB laying around and I decided (for my own ease) to solder the MCP3008 on to that PCB.
The wires on the PCB are connected to the GPS HAT but can be connected to the PI as decribed in the schematics.
Step 5: Installing Raspbian
First of all, we need Raspbian on our SD card. You can download raspbian from here. Once that is downloaded, we can install raspbian on an SD card. You can install the software on the SD card by installing Win32Discmanager on your computer.
Don't forget to add a file called 'ssh' without extention to enable the SSH on the pi.
Once all that is done, you can boot your raspberry and add it to your network. Once the pi is booted, you should be able to find your IP address on your router and connect to your pi.
I used PyCharm to develop the software, but you can use whatever software you like.
You can also, to find your IP, attach a mouse, keyboard and screen to your PI, then you can find the IP using the terminal by typing in;
sudo ifconfig or via the menu top right.
Step 6: Configuring the System
Once you found the IP address, you can connect via SSH to the pi and log in. The default user is pi with password raspberry. Now, when you are logged in, you should confgure a few things. First of all type:
sudo raspi-config. Now you get a new screen with a bunch of options. First select the Change user password this is to set a new password for the user pi. You can change the name of the pi by selecting the change hostname option, but that is not imporant. What you sould do and that is important is go to Interfacing options and enable the following interfacing options:
- 1 Wire
Once everything is enabled you can go ahead and select the Update option.
After the system update you will be prompted to reboot the system, you can go ahead and reboot.
Step 7: Installing the Required Software
You'll need some software to run this, so we can go ahead and install everything trough the CLI.
First update and upgrade all packages:
sudo apt-get update && sudo apt-get upgrade
Then install all the python plugins by typing:
pip3 install Flask mysql-connector smbus2 netifaces
When everything is installed, you can start on assembling everything in your case.
Step 8: Installing the GPS Module
It will depend on which module you use, but mine had an installation required. I'll explain here how I did this.
You need to edit the /boot/config.txt file, open the file either on your desktop computer or using the Raspberry Pi via SSH
sudo nano /boot/config.txt
You need to add the following lines:
Also add these lines for the LCD (if you are using the same one as I do)
hdmi_cvt 800 480 60 6 0 0 0
Then use Ctrl+O to save changes and use Ctrl+X to exit.
Now edit /boot/cmdline.txt.
sudo nano /boot/cmdline.txt
Change the file to the following:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Make sure it is one line and there is no enter at the end of this file.
Exit and save your changes
If you have edited the cmdline.txt and config.txt files on your desktop put the SD card back in your Raspberry Pi 3 and boot to either a network SSH session or desktop and run the following in a shell window. If you want to disable the built in bluetooth you need to stop hciattach trying to use the modem via uart0 which will disable the relevant systemd service.
sudo systemctl disable hciuart
/lib/systemd/system/hciuart.server and replace ttyAMA0 with ttyS0.
sudo nano /lib/systemd/system/hciuart.service
Replace "After=dev-serial1.device" with "After=dev-ttyS0.device"
Exit and save your changes
You need to update the operating system with the latest patches with:
sudo apt-get update
sudo apt-get upgrade
Once your Raspberry Pi 3 has rebooted you should now have access to the serial console via the GPIO header at 9600 baud.
If you just want to do a quick check to see what data is coming out of the GPS, you can enter the following command, following by CTRL+C to quit:
sudo cat /dev/ttyS0
Step 9: Installing the Software
So, you need a database on your pi, I used MySQL as database driver. You have to install this on your raspberry pi.
So first of all, execute the comand
sudo apt-get install mysql-server --fix-missing
This will install the MySQL server on your pi.
You should be prompted for a root password during the installation. Choose a secure password, and keep it safe. You will need this later to log in into the database. You will be prompted to confirm your password.
Once you’ve confirmed your password, MySQL server should finish installing and the service should start.
Now we need to install an extra package, mysql-client. The mysql-client allows us to connect to our local MySQL server through the CLI.
sudo apt-get install mysql-client
Step 10: Configuring the Database
Okay, so we have our MySQL server, but we don't have a database yet, so you first want to login to the server.
mysql -uroot -hlocalhost -p
This will prompt you a password. Type in the password you've typed in during the installation and press enter.
Now create a database. Mine will be called "tripmaster".
CREATE DATABASE tripmaster;
This will create the database. With MySQL, ensure you end each command with a semicolon.
Now we have to create a user. You can use MySQL workbench as I did or do this through the CLI.
CREATE USER 'tripuser'@'localhost' IDENTIFIED BY 'password_here';
Allright, now give access to your user (tripuser) to access the database you created in the beginning.
GRANT ALL PRIVILEGES ON tripmaster.* TO 'tripuser'@'localhost';
Finally, flush the privileges for the changes to take effect:
Press CTRL + C to exit MySQL Client.
Step 11: Setting Up Your Raspberry Pi to Work As a Kiosk Screen
We also have to make our pi act as an kiosk device. So what we want to do is make it boot up and display our software directly on the screen.
You can achieve this by going to your SSH terminal and type in the following;
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
This will bring you to a file in which there is some text already. Comment out
@xscreensaver -no-splash (by adding a # in front of that line) and add at the bottom of the file the following;
@unclutter -display :0 -noevents -grab
@xset s noblank
@xset s off
@chromium-browser --kiosk http://127.0.0.1:5000/ --overscroll-history-navigation=0 --incognito --disable-pinch This will remove your cursor from your screen and will start the chromium browser on boot.
Save and exit the file.
Note: to get out of the kiosk mode, you can go to your SSH session and type in
sudo killall chromium-browser. This will close all the chromium browser instances.
Step 12: Installing the Tripmaster Software
As I mentioned earlier, you can find the code on my github. So, you can clone the code on your raspberry pi by executing the following command:
git clone firstname.lastname@example.org:RobinRosiers/Tripmaster.git
This will make a new directory called "Tripmaster" in which the code will be.
Just edit the file which holds the database configuration and you're good to go.
sudo nano /model/DB.py
There, fill in your user and password you made at the beginning of the instructables and save it.
Also, we want some scripts to start at boot, so edit the rc.local file:
sudo nano /etc/rc.local
and add those lines:
python3 /home/pi/Tripmaster/main.py &
python3 /home/pi/Tripmaster/index.py &
Add them right above the exit 0
Step 13: Explaining Some Code
Here, I'll explain some of the code, why I did it and how it works.
Python - Flask
I used Flask as framework to serve the HTML and CSS content on the pages.
In Flask I have 3 main sections. The first section contains the app request settings and the processor settings (to set gobal variables).
The second section contains the main routing. This contains the links for the main application which are used by the user.
The third section has the AJAX request routes. These aren't seen by the user, but still used by the system. These routes are recognizable by the starting of "/ajax/...".
Python - main scripts
The python itself doesn't need alot of explanation, since it tells itself. But here it is, some more explaining about it.
First of all, I connect to the GPS module via Serial on a baudrate of 9600.
The data is really a mess when it gets in the monitor, so I read line per line in python via
ser.readline() which reads the lines, but they still need to be decoded... So I add a
.decode() and then I split all the values via
With that said, I made my own parser to retreive the data I want. It's NMEA data, so I took the index of the list and added a number to the index depending on the value I want.
The database file is just ordinary SQL. I'll add views and stored procedures in the feature.
I tried to write as much functions as possible for everything.
Used front-end frameworks
Ofcourse, I used some frameworks. Here is a list of used frameworks:
Step 14: How to Use
It should be simple to use.
Just power it up, turn on the screen and you should be ready to rock!
Step 15: Problems I Faced With
I had alot of trouble getting the GPS to work, not the GPS itself, but the serial writing. At first I used code without the decode() function, which was a small war crime... That didn't work out as I wanted and it bugged the whole code.
Then I had to calculate distances between points, I could have done this in the python, by letting run a script in the background, but then I thought well, imagine I have 100K lines of data, and it starts looping trough every single line without remembering where it was and so on, the program would get really slow, the database would maybe crash, and it would just give me more trouble then it was worth. SO! I made a trigger, which activates every time there is a new record added in the gpsdata table.
This calculates the distance between the inserted point and the previous point and saves in the information table.