This is a guide for a project I made for school. The goal was to create a device with the Raspberry Pi that was connected to the internet, while collecting data from sensors. This data had to be stored in a (MySQL) database. The data had to be visualised with a website, coded in Python with the Flask webapp package and Jinja2 templating.
My idea was to create a 'hacked' robot vacuum, using a Raspberry Pi, an Arduino, an already broken robot vacuum and a bunch of sensors.
Step 1: Collecting the Parts
Before actually creating anything, I did a lot of research about what parts to use, how to connect them, writing software classes in Python and so on.
The parts list will be included as a file, so that you can easilly search for the parts you will need.
I decided to use an Arduino Uno next to the Raspberry Pi, so that I could efficiently read my sensors without stressing my Raspberry Pi. I also chose for the Uno because it has a nice clock speed, and because of the analog pins. I could have used an ADC (ex. MCP3008) for my Raspberry Pi, but I would have needed a lot more wires, it would have been more expensive and my Pi would have to work a lot harder.
I then started to inspect the parts that were usable from the broken robot vacuum. The internal electronics where damaged, but that was no problem, because I would totally replace those anyways. Luckilly the dc motors were still working, so none of those had to be replaced.
- Raspberry Pi 3 with at least 8GB MicroSD class 10 and a case;
- Raspberry Pi T-cobbler and breadboard(s);
- Arduino Uno or similar (preferrably a non-chinese version with a decent ADC, some chinese have AREF issues) with some sort of a case;
- An ethernet cable;
- A (broken) robot vacuum;
- 3x HC-SR04 ultrasonic modules;
- 1x hall sensor module;
- Several resistors in different values;
- An LDR;
- 6x 18650 Li-ion batteries + 3-cell holders for 12v (preferrably you should use new batteries or even better use LiPo batteries, these will last way longer);
- 18650 (or whatever type of battery you will use) 12v 3-cell charging pcb;
- Some PCB DIY boards to solder your components;
- A sheet of polyurethane plastic;
- A laptop / desktop computer.
- A drill with several drill bits;
- An angle grinder (don't use it if you have no experience) or something like a Dremel;
- A piece of sanding paper;
- Several screwdrivers;
- Superglue, mounting glue, ...;
- A soldering iron (use grease to solder your wires easier);
- A pair of wire cutters and a stripping tool.
Software list (optional):
- Adobe XD: wireframing and creating a prototype;
- Fritzing: creating an electrical scheme;
- PyCharm Professional: Python IDE with capabilities of using deployment & remote interpreter;
- Putty: quick and easy ssh connection with Pi;
- Etcher.io: easy tool to flash a Raspbian image to an SD card;
- Win32DiskImager: easy tool to create an image from an existing Raspbian setup;
- Programmers Notepad: simple tool you can use to safely edit the /boot/cmdline.txt file.
Step 2: Spray-painting & Designing the Interface
Before I started to create the design, I spray-painted the exterior, because I didn't like the colors at all. I went to the store and picked up a plastic primer, a can of white and a can of turquoise to respray the top case.
After letting the spray-paint dry, I looked up the exact hex colorcode for the paint I used, so that I could match my web-interface with my device perfectly. Finding this hex code was pretty easy, since I used Montana 94 graffiti, and the hex & RGB codes were on their website.
I created high-fidelity wireframes for every page of my website, so that I knew perfectly fine how I was going to create this interface. After showing my interface to my teachers, I got the advice to make the background a little more grey and the buttons white, and the result was good in my opinion.
Step 3: Creating the Database
The next logical step was to start thinking about what data I wanted to store in the MySQL database. There isn't so much people like to know about their vacuum, so I went with tables for users and their login data, as well as tables for sensors (battery, distance & dust container).
The image shows you the layout of my tables, drawn in MySQL Workbench, with all the relations between the tables.
For my users, I wanted to keep track of their name and surname to personalize the interface & the emails. Ofcourse to send e-mails, I needed their e-mail address as well. I also added a table to keep track about the e-mail preference of my users (whether or not they'd like to receive emails). The last thing I wanted to store about users, is their role for the device. I divide users as administrators & normal users. Administrators have the right to add, remove & manage users in the system, while normal users can not access these tools.
The next table contains "deviceruns", which are the actual runs the device has done. The deviceruns are owned by a certain user (the person who started the run), and have a starttime and endtime, to calculate the runtime.
Another table is used to link sensordata to each devicerun. The sensors themselves are stored in yet another table, containing their id, name & description.
Step 4: Connecting the Parts Together
After creating the database scheme, I started to use breadboards & wiring to connect all of the parts together in one working prototype.
Step 5: Setting Up the Raspberry Pi
To download the latest image of Raspbian, go to the Raspbian website. Here you can choose which distro you want to download. For faster operation, you can download the headless distro, for less RAM usage, or download the desktop with GUI if you prefer a graphical user interface.
To install the OS, simply download Etcher, it is a GUI tool to write the image to your micro SD card fast and easilly.
To enable headless mode, so that you can access the pi, you will need to install Putty on your computer. The next step is to go to the Boot folder created by Etcher, and to open the file cmdline.txt with your favourite text editor, ex Programmers Notepad. Add this text to the end of the file:
Just make sure that you do not create a new line, add this to the end of the line!
Next, go back to the root of the Boot folder, and create a file named ssh. Do not add any extention, this will ensure that the SSH server launches every time your Pi launches. Now simply put the SD card in your Pi, connect a sufficient power source to your Pi and add an ethernet cable between your Pi and your computer.
Open putty, and type in the IP address: 169.254.10.1. Now click YES and log in, the default username is pi and the password is raspberry.
Next, execute the following command to get up-to-date:
sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
The final step is to create a virtual Python interpreter on your Raspberry Pi, this will execute your code. To do this, simply open up putty and type the following:
sudo mkdir project1 cd project1 python3 -m pip install --upgrade pip setuptools wheel virtualenv python3 -m venv --system-site-packages venv
Step 6: Writing the Web-application
After connecting every part and setting up the Raspberry Pi, I started to write my main web-application using Flask and Jinja2. Flask is an easy-to-use backend framework for Python, and Jinja2 is the templating language that I used. With Jinja, you can create normal HTML files with for loops, if structures and so on.
Besides the main web-app, I also created 2 other main programs. One is written to send the device's IP address to the users in the list. Every registered user that has accepted to receive emails, will receive a mail with a link to start the web interface. This program runs as a systemd service.
The other main file is for the actual device. This main is callable through my Flask application, to start & stop the device, and collect data. The collected data is also uploaded to the device's database through this main. This data can then be visualised in the web-app.
The web-app is connected to the device run main with Python Threading. When a user clicks the start button, a thread is generated to run the device in the background. Meanwhile, the user can perfectly browse through the app. When clicking stop, this thread is stopped and so the device stops.
Step 7: Creating Pieces to Finish the Casing
After writing the biggest part of the application, I started modifying the device casing, so that my sensors & other parts would actually fit. To do this, I bought a sheet of polyurethane in a local DIY shop, and started cutting 2 brackets. I used this sheet of polyurethane since it is not likely to break into pieces while cutting, and because it is fairly flexible, which is perfect since my robot has a circular shape.
The first bracket is made to fill a hole on the top, where a display used to sit in. I replaced the display with an on/off switch so that the batteries can be actually turned off.
The other bracket is a support for my HC-SR04 ultrasonic sensors that are positioned in the front of the device.
The last thing left to do, was to cut out a hole in the reinforcements of one of the wheels, and inserting a magnet, so that I can monitor the rotations of the wheel.
After finishing these brackets, I also spray-painted them with the paint I had left, to make it fit the design.
Step 8: The Code
To install the program, download the file code.zip and unzip it in the project1 directory.
Next, perform this command in a terminal on Raspbian or Putty:
sudo cp project1/conf/project-1* /etc/systemd/system/ sudo cp project1/conf/project1-* /etc/systemd/system/<br>sudo systemctl daemon-reload sudo systemctl enable project-1* sudo systemctl enable project1* sudo systemctl restart nginx
Next, open the database you have generated and create a new user with an Argon2 hashed password. Now you can use the program.