Doorbell

Objectives

  • Learn about daemons
  • Create a program that runs on boot
  • Deal with different modes of program operation

Introduction

Welcome to the end of the semester! You have come very far in your abilities to understand how computers work under the hood and how you can control them programmatically. The difference between your doorbell unit as currently configured and most single-use devices that exist on the market is it still acts as any normal desktop/laptop/phone. When you turn the device on, the operating system loads and then you are expected to start any program that you wish to have running.

This is not the case for most single-use devices. For example, when you turn on a microwave, it immediately starts heating up your food. When you turn on an electric kettle, it immediately starts heating up your water. When you push the button on a doorbell, it immediately starts ringing. This is because these devices are programmed to start running as soon as they are turned on. In this lab, we will work to make the Raspberry Pi act as a smart doorbell on power up. This entails having your doorbell program run on boot.

systemd Daemons

To enable the doorbell program to run on boot in Linux, we need to understand how Linux manages its processes. Linux uses a system called systemd to manage its processes. systemd is a system and service manager that runs as the first thing when your computer boots (process ID 1). It is responsible for starting and stopping processes, as well as managing the boot process.

systemd also manages programs that run in the background, called daemons. A daemon is a program that runs in the background and is not directly started by the user. Daemons are often used to provide services to the user, such as the sshd daemon that provides the ability to connect to the doorbell over SSH.

Service Files

For our doorbell to run on boot, we need to create a daemon that runs our program. This is done by creating a systemd service file. A systemd service file is a file that contains information about a service that can be used by systemd to start and stop the service.

One specific command that can be used to monitor and control the operations of daemons in systemd is called systemctl. systemctl is a command line utility that can be used to control the systemd system and service manager. It can be used to start, stop, restart, and check the status of services.

For example, let’s say we want to observe the sshd daemon on our doorbell unit. We can use the following command to see the status of the sshd daemon:

sudo systemctl status sshd    # Shows status of the sshd daemon

Likewise, if we wanted to start or stop the daemon, we could use the following commands (don’t actually run these commands):

sudo systemctl start sshd     # Starts the sshd daemon
sudo systemctl stop sshd      # Stops the sshd daemon

However, these commands only work for daemons that are already installed on the doorbell unit. If we want to create our own daemon, we need to create a service file that systemd can use to start and stop our program.

To create a service file, we need to create a file in the /etc/systemd/system directory. The name of the file must end in .service. The contents of the file must be in the following format:

[Unit]
Description=<Name of your service>

[Service]
WorkingDirectory=<Absolute path to your project folder>
ExecStart=<Absolute command to start your program>

[Install]
WantedBy=multi-user.target

The file path to your executable must be absolute. This means if you ran your code by doing:

./my-program

You would be running your program using a relative path. However, if you ran your program by doing:

/home/USERNAME/path/to/my-program

you would be using an absolute path. The path to your executable must be absolute in the .service file, so you must use the second command to run your program.

Once we have created the .service file, we can use the commands above to start and stop our program. However, what if I want to start my program when the computer boots? We need to enable our service. To do this, we can use the following command:

sudo systemctl enable my-service.service    # Enables the my-service service

This will make it so that my-service.service will run whenever I turn on the doorbell unit. If I don’t want my program to run when the doorbell boots, I can use the following command to disable my service:

sudo systemctl disable my-service.service    # Disables the my-service service

Procedure

Requirements

Create your final doorbell program, which should run automatically when you start the Raspberry Pi.

  • Change your program to be more like a real smart doorbell:

    • When the program starts, it should show a welcome message on the screen.

    • When the center button is pressed, do the following:

      • In a thread, take a picture, save it to your viewer folder, and send the image to the website.

      • Change the display to show the message to let the user know that doorbell was rung (e.g., “Ding dong!”). Make this message visible for 2 seconds and then change back to the initial welcome message.

      • NO STATUS BAR LOGIC IS NECESSARY

  • Add a hidden menu to your doorbell. When the following sequence of buttons are pressed (Up-Up-Down), take the user to the file browser menu. To go back to the original welcome message, press the left button of the d-pad.

  • Create a service file in /etc/systemd/system named doorbell.service. You will need to create the file with sudo privileges. Test the service by running sudo systemctl start doorbell.service and make sure your program starts successfully. If you run sudo systemctl status doorbell.service, you can see log messages from your program. To test out your service file, run sudo systemctl start doorbell.service to make sure that your program works as expected. If you run into problems, you can use status to see what the problem is: sudo systemctl status doorbell.service

  • Before you set up your program to run on boot, add a 15 second sleep to the first line of your main function. This is because the program that prints the IP addresses also uses the displays and if they run at the same time, the display can get messed up. This ensures that the IP address program is finished before your program starts. Make sure to rebuild your program (make) so the change takes effect.

  • Set the program to run on boot by enabling the service. Test it out by rebooting your doorbell unit and making sure your program starts up.

Pass Off & Submission

  • Your program must compile without warnings or errors. Your Makefile has the -Werror flag to ensure that it doesn’t have errors.
  • Pass off with a TA by demonstrating that your main program running on the doorbell fulfills all the requirements outlined above.
  • Take the Pass off Quiz on Learning Suite.
  • Follow the instructions in the README.md file in the repository. Add the requested detail about features and usage to this file.
  • Once you have finished updating your README.md file for this lab, it is time to upload it to Learning Suite! Make sure to review the rubric link in the description of the assignment on Learning Suite and remove the instructions inside README.md.
  • Make sure to push your changes back to your Git remote repository. Follow the instructions on the Lab Setup page.

Explore More