Hello, there!
I’ve just spent a happy few weeks building my first ever Raspberry Pi project.
It’s been brilliant: I can now control my Edegoo robot car using my Android app.
I can drive the Edegoo, control its speed and see what it sees with the camera module I added.
I have even taken it on holiday and driven it along the beach!
However, the process has not been without its problems and frustrations.
So, if you are doing something similar, and getting as stuck at times as I did, let me share with you what I learned along the way . . . .
You can find my code here https://github.com/EdenSimkins/Edegoo
I found a tutorial on how to communicate between a simple app on my android phone to an Arduino via Bluetooth.
The app controls an LED, which could either be connected to the Arduino via a breadboard or I just used the
inbuilt LED connected to pin 12 in the Arduino Uno which came with my Elegoo car.
The problem I had was that when I tried to pair my phone to the HC-08 Bluetooth module provided in the Elegoo car kit,
I couldn’t. Luckily I reread the Elegoo instructions and came across this: “Please note:
The Smart Cars CANNOT be paired in the settings page, it can only be connected with “Elegoo BLE Tool” App.”
My solution was to buy a new HC-05 Bluetooth module and to connect it via female-female cables to the Arduino Uno.
Just so as you are aware, the company Elegoo is one which makes robot car kits. Confusingly I have named my robot Edegoo a combination of Elegoo and my name!
Initially when I was using the HC-08 Bluetooth Module the Arduino code specified a baud rate of 9600.
After replacing the HC-08 module with the HC-05, my LED app still didn’t work. Close to buying yet another
Bluetooth Module, I scanned the internet and found that the baud rate for the HC-05 must be 34800 and not 9600.
Once I made this correction to my code the LED and app worked perfectly.
I couldn’t find any example joysticks online, so I decided to make one completely from scratch.
This went relatively smoothly: I used two basic coloured circles for the base and knob of the joystick
and set an on touch listener on the knob. Within the on touch listener I used an if statement to test
for when the motion event was ACTION_MOVE and if this was true then I would change the position of the knob,
effectively allowing the knob to be dragged relative to the base of the joystick.
This worked almost perfectly, however, when dragging the knob I wanted the centre of the knob to be exactly
under the tip of the dragging finger, but for some reason the knob was misaligned by a seemingly random distance.
The solution I can up with may seem like a bit of a cheat, but it was extremely effective and highlights the
usefulness of the Logcat window in Android Studio. I wrote a small function, which obtained the coordinates
of the centre of the knob relative to the top left corner of the screen. Then when I moved the knob I logged
the x and y coordinates of the centre of the knob and the coordinates of the pointer. From this output I was able
to calculate the distance between the centre of the knob and the pointer. I could then correct my code which set
the new position of the knob to include this random distance and luckily it worked and the centre of the knob maintained
its position directly under the dragging finger.
I was thrilled. I had made a joystick and I was able to control my Elegoo car from my android app.
I was now ready to embark on the next stage of my project: adding a camera to the car allowing the
user to view where the car is going from the app.
Up until this point I had been using an Arduino to control the car, however, an Arduino does not have sufficient RAM to stream live video.
This meant swapping the Arduino for a Raspberry Pi. It is worth noting that before starting a project you should consider whether
to use an Arduino or a Raspberry Pi, both have their advantages and disadvantages and it will save you time down the line if
you do not need to swap from one to another.
At first I was unsure on exactly how I could stream a video from the Raspberry Pi to my Android app and my
first thought was that I could stream the video directly to a video view on my app, however, after completing
some tutorials on video views I realised that they would only work with videos of fixed length.
Obviously this was not what I wanted, so I had to change the way I approached the problem.
My solution was to stream the video to a webpage and then to view the webpage in a webView on my app.
I used code modified from this tutorial to stream the video:
https://randomnerdtutorials.com/video-streaming-with-raspberry-pi-camera/
The main modifications I made was to remove this line of code:
Raspberry Pi - Surveillance Camera
from the body tag and then I changed the width of the image to 400px by 400px so they would fit exactly on the screen of my phone.
Whereas when I was using the Arduino I was controlling the robot using Bluetooth, now I was using the Raspberry Pi I wanted to use WIFI.
Initially I used the local WIFI, but if I was going to drive the robot car outside in random places,
I would need to set up my Raspberry Pi as its own wireless access point.
To do this I used this tutorial:
https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md
When following these instructions, I came across one small problem: In the section entitled “Configuring the DHCP server (dnsmasq)” the final
line instructs you to reload the dnsmasq, however, I found I could not do this because the dnsmasq had been stopped
in a previous section of the tutorial. To get around this I simply entered sudo systemctl start dnsmasq into the
command line before sudo systemctl reload dnsmasq and there were no further problems.
So far my project doesn’t require internet access so I did not go on to use bridges after setting up the access point.
When I tried to connect my Android phone to the access point and use my app,
I made the mistake of keeping my mobile data on. This meant I couldn’t connect to the Raspberry Pi,
so my app didn’t work. Before I realised the problem, I reinstalled the Raspberry Pi and made the
access point again from scratch! Don’t do what I did, always look for simple solutions before completely
wiping the slate clean and starting again.
I decided to use fragments on my app: one fragment for each component, the two joysticks and the webview.
This way if later I didn’t want all the components on the screen at once I could just hide one of the
fragments without affecting the others.
In Android Studio it is possible to use wizards to create a “blank fragment”, but this creates a template
with a lot of excess code which is not necessary to get the fragment running. So instead I used this
example to create the bare bones necessary:
https://developer.android.com/training/basics/fragments/creating
After following this basic example I got an error saying onCreateView() overrides nothing.
Turns out you need to put a question mark after ViewGroup:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?)
The question mark means that the container is nullable.
To communicate between the app and the Raspberry Pi you must create a server socket on the Raspberry Pi and a client socket on the app.
This tutorial and accompanying code was really helpful when I made my socket:
https://github.com/trieutuanvnu/PiServoControlWifi/blob/master/Servomotor.py
https://www.youtube.com/watch?v=t8THp3mhbdA