After discovering how to run the Pi headless without an internet connection, I made a simple sensor to detect if one of my bureau drawers is opened or closed. Basically, an LED turns on if the drawer is closed, and turns off when the drawer is opened.
View the code here. I used a Model B Pi.
I wired the drawer like a pull-up resistor button setup, and made contacts with Meccano Erector set pieces and lots of electrical tape. I added an LED, and then wrote some Python 3 code to run headless on the Pi. The picture below shows the Model B Pi with the LED.
This picture shows the contacts on the drawer. There’s thin piece of metal on the drawer, and the two contacts are connected to the red and black wires. When the drawer closes, the contacts both touch the metal strip, and the circuit becomes continuous.
Instructions For Running Headless on the Pi Without Wifi
Running headless mean having your Raspberry Pi run a program without having it hooked up to a monitor, keyboard, or mouse. Basically you just need to edit a certain system file called rc.local.
- Login to your Pi. Open up a terminal.
- Type cd /etc to change the current working directory (in Linux lingo, cd means ‘change directory’).
- To edit the rc.local text file, type sudo nano rc.local. Navigate to the end of the file.
- Right before the exit0 line, add the line python3 /home/pi/code.py & where ‘/home/pi/code.py’ is the absolute location of the file you want to run, and the ampersand (&) means that you want to run the program in the background and let the Pi’s boot process continue. Note that you don’t need to add sudo because rc.local runs as root.
- The program will now run whenever you power the Pi, regardless if there is any peripherals attached. If you want to test it out, type sudo reboot in the terminal.
- Now you can showcase computing projects with your Pi without a monitor!