skip to Main Content

At this point it’s assumed you’ve set up your Raspberry Pi in “headless” mode and added libraries to support CircuitPython, netatalk (if you’re a Mac user, so you can copy files to your Pi), PyGame (for sound), and the adafruit crickit library (which we’re using to power our Raspberry Pi). If not, you can set these up by following tutorials at https://bit.ly/raspberry-pi-tutorials

Add the robot_sounds folder to your Raspberry Pi

  • Visit the Google Drive at the URL below.
    https://bit.ly/circuitpython-school-files
  • Right click the “robot_sounds” file and download this to your computer.
  • Copy the entire robot_sounds folder from your computer to your Raspberry Pi.
    Mac users will use Finder: Go > Connect to Server to connect to their Pis (assuming you’ve installed Netatalk, as shown in a prior video).
    Windows users will use a program such as WinSCP.

When done, the robot_sounds folder on your “pi” directory on your Raspberry Pi (maybe shown as “Pi’s home” on the Mac) should have a bunch of mp3 files named 0.mp3 through 12.mp3, plus a file named startup.wav, which contains the startup sound for your robot.

Install Mosquitto and Python Libraries to Use MQTT in Python Programs

We’re going to install more software on our Pi, so it’s a good idea to switch from using the mobile phone battery back to using the Pi power supply. If you need to, you can follow the steps below to run your Pi off it’s plug-in power supply (if you have one), or the USB cable attached to your computer.

  • Stop your pi by typing sudo halt at the prompt and press return. Wait for the green light on the Pi to stop flashing (3-4 seconds). Turn off the AA batter pack. Unplug the Pi from the mobile phone battery, plug the Pi back into the power supply, and after about 30 seconds, open the terminal and log back into your Pi.

Your Pi will use a communication standard known as MQTT to take messages from the iOS app and pass them to our Python program running on the Pi so the robot can moveand play sounds. The Pi program that we’ll use to handle MQTT messaging is known as mosquitto (two ts in mosquitto for the two ts in MQTT).

  • To install the mosquitto software, enter the command below at the command prompt and press the return key. As always, select “Y” if asked if you’d like to continue, even though the operation will use additional disk space. NOTE: Some of the installations may seem like they are stalling at first, but after a minute or two you should see text showing installation progress. The install should only take a minute or two.
    sudo apt-get install mosquitto mosquitto-clients

Install the libraries for python support for MQTT by entering the command below and pressing the return key. The install sometimes takes a few seconds before messages appear, but the install should only take a minute or two.

  • sudo pip install paho-mqtt

Verify that the mosquitto broker software is now running by typing the command below and pressing the return key.

  • systemctl is-active mosquitto
    

    You should see a message stating “active” if everything is working properly. If, for some reason, you don’t receive an “active” message, you can try rebooting your Pi, waiting about 30 seconds, login, then re-enter the command above.

We now need to configure security for the mosquitto broker that we just installed. Starting with v. 2.0 (Spring 2021), mosquitto now requires security to be set up to access the broker from other computers and devices. This is what we’ll need to do since we’re going to access the mosquitto broker on our Raspberry Pi to send messages (publish) to the broker. We need to explicitly tell Mosquitto that it’s OK to accept anonymous messages. This’ll allow our existing app to use the broker on our robot. Normally when developing IoT projects, you want to have tighter security & implementing a password for publishers is a good idea, but since your robot isn’t on all the time, and you need to configure the app to specifically tap into the robot & it’s name, we’ll skip added security. This’ll also let use the app that was developed when the earlier version of mosquitto didn’t require additional security measures.

To set up security to allow anonymous connections:

  • Create a file in nano named connect.d, in the exact directory location required by mosquitto, by entering the following at the prompt, then press the Return key.
    sudo nano /etc/mosquitto/conf.d/connect.d
  • Copy the two lines below and paste them into nano, running in your terminal window:
    listener 1883
    allow_anonymous true

    The listener assumes mqtt is running on the standard “port” 1883, which it should be unless you’ve changed this when configuring MQTT. The listener needs to be setup, starting with mosquitto v. 2. Setting allow_anonymous to true will let our app connect to the Raspberry Pi without requiring any special login credentials.

  • Quit nano, saving your work, as usual: CTRL+X, Y, Return

NOTE: In the video we did not perform the next step, but it looks like this is now necessary.

  • Open the mosquitto.conf file using the nano command, below:
    sudo nano /usr/share/doc/mosquitto/examples/mosquitto.conf
  • You’ll see a file full of comments. Copy the two lines below and paste them above the first two lines of nano, running in your terminal window:
    listener 1883
    allow_anonymous true

    As mentioned above, the listener assumes mqtt is running on the standard “port” 1883, which it should be unless you’ve changed this when configuring MQTT. The listener needs to be setup, starting with mosquitto v. 2. Setting allow_anonymous to true will let our app connect to the Raspberry Pi without requiring any special login credentials.

  • Quit nano, saving your work, as usual: CTRL+X, Y, Return

Use systemctl to Automatically Run mosquitto Each Time Your Pi Boots

Enter the command below and press the return key to ensure that the mosquitto broker runs automatically every time you boot your Pi (no need to ssh in or type additional commands). This is exactly what we’ll want if this Pi is dedicated to our robotics project – ideally we’d just turn our robot on and the software we need will begin running.

  • sudo systemctl enable mosquitto.service

You can verify everything is working by stopping your Pi with the command:

  • sudo halt
  • Wait 3-4 seconds until the green light on your Pi stop flashing and turns off. Then power your Pi off.
  • Plug your speaker in to your Raspberry Pi’s audio jack and turn it on (make sure it is charged).
  • Turn your Pi on again.
  • Log back into your Pi using ssh
  • Then retype the command below and press the return key.
    systemctl is-active mosquitto

    You should see “active”, confirming that mosquitto has begun running on its own.

Create a Subscriber Program to Listen for Robot Commands and Control to Our Robot

We’ll create a python program named control-pibot.py. This program will listen for messages from mosquitto. When it receives a message that it can use, it will respond by sending commands to our robot. For example, if the mosquitto broker receives a “forward” message from our iOS app, it will send the “forward” message to control-pibot.py, and this program will move our robot forward. Follow the steps below to create the control-pibot.py program:

  • From the Terminal, enter the command below, followed by the return key, to create an empty file in nano named control-pibot.py
    nano control-pibot.py
  • Open a browser and visit the project’s GitHub page at: https://github.com/gallaugher/crickit-pi-bot
  • Find the link labeled control-pibot.py and click it. A page will open showing all of the code for this python program.
  • Highlight and copy all of the lines in this python program.
  • Return to the Terminal and paste the program into nano.
  • Find the line that reads:
    serverAddress = "hostname"

    Change “hostname to the name of your Pi

  • Exit nano by typing Control-X
  • Press Y, then the return key to save the file.

And at this point you should have a working MQTT subscriber running on the Pi that works in conjunction with the broker.

  • If you haven’t done so already, download the “robot_sounds” file from the URL below, then copy this entire folder into the root (pi directory, or “pi’s home” when viewed from the Mac Finder).
  • Make sure your Crickit also has power (plug in the battery pack). Also turn on your speaker. Make sure the speaker is charged.
  • Run the program by typing the line below at the command prompt, then press the return key.
    python control-pibot.py

If things are working properly you’ll see a PyGame message print, that’s part of the PyGame libraries we’re using, you’ll hear a message played through the speaker stating “ready to roll, lets go!”, then you’ll see confirmation messages that the program is subscribing and has subscribed.
You will NOT be returned to the command prompt in this terminal window because the program was run from within this window and it is continuing to execute.

If, after running control-pibot.py you see the error message below and are then returned to the prompt:
– ValueError: No I2C device at address: 0x49

then just click the reset on the Crickit (the small button next to the USB port on the cricket labeled “Seesaw Reset” & rerun. It should work.

Still happening? Check to make sure the Crickit is plugged into power & the batteries are fresh. Also make sure your speaker is properly charged.

Open a New Terminal Window then as a Test, Write a Command to “Publish” a Command to Move our Robot

  • From with the Terminal program, select the Shell menu, then New Window > New Window with Profile – Basic (or use the shortcut Command-N). This will open up another terminal window and return you to the command prompt.
  • From within this new window, enter the commands log in to your Pi using ssh (the same commands, Pi name, and password you’ve been using throughout this tutorial).

NOTE: Substitute your Pi’s hostname for hostname in the mosquitto_pub commands, below.

  • Try out a sound by entering the command below and pressing the return key. You should hear a sound coming from your speaker. If it works, feel free to experiment by changing the sounds (make sure any number corresponds to the name of a sound in your Pi’s “sounds” folder).
    mosquitto_pub -h hostname -t "pibot/move" -m "0"

WARNING: You can also try out commands to move the robot. Be sure to turn on the AA battery pack, also, be sure to prop up your bot so the wheels aren’t touching the ground, otherwise be prepared to run after a runaway robot, and for it to possibly run into something.

  • Enter the command below and press return to send a “forward” command to the mosquitto broker. Mosquitto will then relay this command to the subscriber program (control-pibot.py), which will tell your bot to start moving forward.
    mosquitto_pub -h hostname -t "pibot/move" -m "forward"

    IMPORTANT NOTE: substitute your Pi’s name for hostname in the commands, below.

  • Stop your bot by entering the command below, followed by the return key (as above, changing hostname to the name of your Pi).
    mosquitto_pub -h hostname -t "pibot/move" -m "stop"

If everything works, congratulations – your Pi is now set up to listen for and act on commands sent to it over the Internet (and that’s what we’ll do in the next video when we write an iOS app to control the robot).

Create a Service to Run control-pibot.py Automatically Whenever the Pi is Started

  • Close the Terminal window where you entered the mosquitto_pub commands above (you can click the “Terminate” button when closing this window)

Look at the output in our first window. You should notice that underneath the “subscribing” and “subscribed” messages, there are now messages for each command you entered (e.g. to play a particular sound file, go forward, or stop). We wrote control-pibot.py to print out these messages when it receives them, so this is a nice additional check that confirms that our python program is receiving messages as a subscriber to our mosquitto server.

Finally – let’s make sure control-pibot.py will run each time our Pi is started. To do this, follow the steps in the final section:

  • From the Terminal window, type Control-C to stop the execution of control-pibot.py. You can ignore the messages that show up as a result of stopping the program.
  • Enter the command below to begin creating a nano file with the name of the service we are going to create, then press the return key.
    sudo nano /lib/systemd/system/pibot-mqttclient.service
  • Open a browser window and return to the GitHub repo for the project at: https://github.com/gallaugher/crickit-pi-bot
  • Find and click on the link for the file named: pibot-mqttclient-service. This will open a page containing code for the service we’ll write to automatically run control-pibot.py after the Pi starts.
  • Highlight all of the lines in the program in this file and copy them to the clipboard.
  • Return to the Terminal and paste the program into nano.
  • Type Command-X to exit nano, then the return key to save using the name we gave the file.
  • Enter the command below and press the return key to run this new pibot-mqttclient-service file whenever we start this Pi.
    sudo systemctl enable pibot-mqttclient.service

    You should see a verification message that says something like:

    Created symlink /etc/systemd/system/multi-user.target.wants/pibot-mqttclient.service → /lib/systemd/system/pibot-mqttclient.service

Verify Everything is Working As Expected

  • Enter the command below and press enter to stop your Pi.
    sudo halt
  • Wait until the green light on the Pi stops flashing and turn off (3-4 min), then turn off power to the Pi and the battery pack attached to the Motor HAT.
  • Plug the Pi back into the mobile phone battery, then  turn on the AA battery pack and (if you it has an on/off switch) the mobile phone battery.
  • After about 30 seconds to a minute you should hear the Pi play the startup message in its speaker. And you haven’t even logged into the terminal! Nice work!

Congratulations! You’ve got an Internet-aware robot set up and ready to receive orders. Up next – create an iOS App to Control Your Robot!

  • You can now turn off your mobile phone battery and AA battery pack to preserve batteries while we set up the iOS app.

Install the App on your Device!

Options:
  • Visit the App Store on your iOS device and download the Mil Mascaras app:
    ​​https://apps.apple.com/us/app/mil-mascaras/id1550345112
    Be sure your robot is charged & turned on (mobile phone battery, 4AA battery pack, and speaker). Shortly after turning on your robot you should hear “Ready to Roll! Let’s Go!” Make sure your iOS device is on the same Wi-Fi network as your robot. Click the “Connect to Robot” button. Press a cell in the list of sounds to play that sound. Adjust the volume by moving the volume slider. Hold your finger down on an arrow button to begin moving in that direction and raise your finger to stop the robot. Have fun!
  • If you have and know how to use Xcode, you’re welcome to use the app you downloaded as part of the GitHub project GitHub repo at https://github.com/gallaugher/mil-mascaras. Just be sure to click the file named mil-mascaras.xcworkspace to open the project, NOT the .xcodeproj file. This project uses cocoapod libraries, so it needs to be opened with the .xcworkspace project.

No COVID Bro or Plague Princess in the app: Apparently this is a violation of Apple’s guidelines, BUT if you like these statements, they are still in your sounds file and you can simply click the gear symbol at the top of the app and add the “COVID Bro…” sound as sound 10 and “Plague Princess…” sound as sound 11. Consider them to be easter eggs 🙂

And if you got this far – congratulations! Please like, post a comment in YouTube, send a tweet, and consider subscribing. Interest in the channel & expansion of reach motivates me to continue to create more content like this. I’m honored to have been part of your robotics journey.

– Peace, Prof. G.

❖    ❖    ❖    ❖    ❖

BONUS: Adding Your Own Sounds + Changing Your Pi’s Hostname

Replacing Sounds

You can easily add your own sounds to this project. Replacing sounds involves two steps:

  1. Replacing sounds on the Raspberry Pi that powers the robot, and
  2. Using the iOS app to edit the list of sounds.

The project standardizes on sounds named numerically, starting with 0.mp3. To replace sounds or add new sounds.

  • Use the Mac Finder to Connect To… the Raspberry Pi that controls your robot (techniques described in an earlier step when you first copied your sounds from the Mac to your Pi).
  • Open the “Pi’s Home” directory, then open the “sounds” directory. You should now see the original sounds used in this project, labeled 0.mp3 through 11.mp3, plus startup.mp3 (which is the sound that plays when the robot first starts: “Ready to roll! Let’s go!”
  • Replace or delete any of these files with new ones that you’d like to use. Just be sure that the files are numbered consecutively, beginning with 0.mp3. You can replace the startup sound, just make sure your new sound is also named startup.mp3.

Now edit the sound list in your iOS app:

NOTE: This does NOT change things on your Pi – it just changes the list on the app and should be used after you update sounds on your Pi to make your app’s sound list consistent with the sounds in the “sounds” folder on your Pi. Clicking on the first cell will always trigger the play of a sound named 0.mp3, clicking the 2nd will trigger 1.mp3, etc. The descriptions are only there to assist the app user in recognizing sounds, so if you enter the wrong description for a sound file, you’ll still hear whatever sound number was clicked.

To reorder the list of sounds:

  • Click the cog wheel in the upper right-hand corner of the app. You’ll receive a warning about changing file names and host names. Click OK.
  • To Move and reorder sounds, click the “Move/Delete” button. The list will change, adding red circles with minus signs for deletion on the left-hand side, and three lines on the right of each cell for moving.
  • Move a cell by clicking and dragging the three lines on the right of the cell that you want to move. Release it in the space where you want it to be repositioned. You’ll now see the new name for that file in the left-hand side of this cell. E.g. if you drag 6.mp3 to 2.mp3, then the cell that you drag will now show as 2.mp3 and all of the cells below it will be renumbered in sequence.
  • Click “Done” when you are done moving cells.

To change the description of a sound:

  • Click the cell where you wish to change its description. You’ll see this description shown in a new app screen.
  • Enter the new description and press Save. You’ll be returned to the list.

To delete a cell from the sounds list:

  • You can do this either from within “Edit” mode by clicking the red minus delete circle on the left side of any cell that you want to delete, or you can swipe left on the cell to delete. Either gesture accomplishes the same thing.

After deleting a cell, the cells below the deleted cell will be renumbered accordingly. Again, make sure that what you see in your app corresponds with what you’ve got in your “sounds” file.

If you want to restore the original sound list and descriptions, simply delete the app from your iOS device and reinstall it. The list is stored in what Apple calls “User Defaults”, and is specific to each device, so if you have this app on multiple devices, you’ll have to update the access list on each device.

❖    ❖    ❖    ❖    ❖

Ready for more? Check out tutorials on Robotics, Wearables, App Development and more at: https://youtube.com/profgallaugher. Please consider subscribing and do share with others who may be interested!

Look for more updates at gallaugher.com as well as on Twitter @gallaugher

Back To Top