Hacking a UCTRONICS Robot Car With JupyterHub
Why use default apps when you can tweak your own?
Join the DZone community and get the full member experience.Join For Free
Last week, I assembled a Raspberry Pi-based car kit. I bought the kit from Amazon. (Affiliate link.) The car has a live video camera, a sonic collision sensor, and a line-following sensor. You'll need a Raspberry Pi to go with it. I recommend a 3 B+.
The car works with an Android or iOS app, and the apps are functional enough, but where's the fun in simply relying on them?
We must hack this car!
First, we need to make a few configuration changes to make hacking it easier. You’ll need a monitor with an HDMI cable, a USB keyboard, and a USB mouse for these first few steps.
You’ll also need Wi-Fi so you can connect to the car drive it around without a cable following it.
After we’re done, you’ll be able to control your car via Wi-Fi from any web browser.
UCTRONICS configures the car to act as a Wi-Fi Access point. It creates a network named UCTRONICS with the password 12345678.
This makes connecting to the robot while still having access to the Internet difficult since it requires a second network connection. So, let’s switch the car over to Wi-Fi client mode and connect it to your home network.
UCTRONICS has instructions for switching it over on their GitHub page. This isn't the only way to do it, but it's easy enough.
Here’s how to open a terminal if you’re not familiar with how: Connect the car to a monitor and screen and turn it on.
After it boots, you’ll see a screen similar to this. I’m using VNC for these screenshots. You’ll have better screen quality with a direct connection.
Dismiss the warning for now. We’ll take care of that later.
Click the terminal icon to open a command shell.
Now, enter the commands in the instructions above. With my robot, the script rebooted the car for me. I never made it to the reboot command.
Now, wait for the car to finish rebooting, and you’ll be back at the desktop. Click on the Wi-Fi icon on the upper right-hand side of the screen, select your network, and enter the password, just as you would for Windows or macOS.
You’re on the air!
Now, get the car’s new IP address. Open another terminal and enter the command ip addr.
On my system the address is 192.168.7.131.
Hopefully, your Wi-Fi router will give your car the same address every time it starts. If not, find the documentation for it and read about how to assign static IP addresses.
Finally, type passwd at the command prompt and change the login password to something other than raspberry.
Install WiringPi and JupyterHub
Now it’s time to install the Python libraries you’ll need to get hacking.
First, install WiringPi. We’ll be using this library to talk to the robot hardware.
Enter sudo pip3 install wiringpi into a terminal.
Your output might be slightly different. The important part is “Successfully installed wiringpi.”
Next, install notebook and jupyterhub with pip3. These packages will allow you to write and execute Python commands in a web browser.
You’ll see a lot of output, and this install may take a few minutes.
Finally, install two more Raspian packages for Jupyterhub. Enter these two commands into the terminal.
Now it’s time to set up JupyterHub. We’re almost there!
The first commands put you in the root (superuser) account. This is generally discouraged, but the next few steps create a configuration file for Jupyterhub in root’s home directory, so becoming root makes things easier.
Next. pwd verifies that you’re in the right place.
Finally, you told Jupyterhub to write the config.
Now, run Jupyterhub and make sure it’s ready to go.
From the same directory, run jupyterhub -f jupyterhub_config.py
After Jupyterhub starts, point a web browser at your robot’s IP address, followed by :8000. So on my system, it's https://192.168.7.131:8000
You’re ready to code after one more step!
Quit JupyterHub with CTRL-C. Then exit the root shell with exit.
Now, edit /etc/rc.local and add these 2 lines.
I used the nano editor.
Now, reboot the car.
When it comes back, you can attach a browser to Jupyterhub.
Login to JupyterHub with the pi account. You’ll be taken to a screen like this. Click on the new button and select Python 3.
Jupyter will take you to a code window.
Enter this code:
Then click “run.”
The car will beep twice. You can program your car from a browser!
What Just Happened?
It's almost time to wrap this up for the week. But before I do, I'll give you an overview of the code.
WiringPi is a common library for talking to hardware via Arduino and Raspberry Pi. The digitalwrite() function allows you to send messages to specific pins on hardware attached to the Pi. Pin #26 is the beeper. 1 means turn on, 0 means turn off. So you told the robot to beep for a half-second twice.
So next, you'll need to figure out the pin numbers for the rest of the hardware on this robot and how to read and write from them.
More next week!
Published at DZone with permission of Eric Goebelbecker, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.