Truk and Trick for Raspberry Pi

by moustick in Circuits > Raspberry Pi

728 Views, 4 Favorites, 0 Comments

Truk and Trick for Raspberry Pi

GPIOButton_bb-21.png

First instructable for the raspberry Pi

Usefull Button(GPIO 21) to Stop Cleanely the Raspberry

GPIOButton_bb-21.png

Note: here are informations found on the internet I am not claiming to be the author of them, if is your property and you want I remove them or put a link let me know.

It is always better to stop the Raspberry properly to avoid contamination files.

to do that we will modify the RC.local (/etc/rc.local) to start the monitoring of the discret we will decide to.

sudo nano rc.local

we will modify it to look lik this

#!/bin/sh -e
# # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi sudo python /home/pi/Scripts/shutdown_pi.py & exit 0

Note: for some reason I didn't success to start two python file in same time, it seam it is waiting the first one to finish before starting the second. The symbol "&" at the end is mandatory to indicate it is a file that will never finish.

Next step is to create the Python file that will monitor the discret "shutdown_pi.py"

nano shutdown_pi.py

We want to monitor the GPIO 21, so here is the code to put in the file we just created

#!/bin/python
# Simple script for shutting down the raspberry Pi at the press of a button. # by Inderpreet Singh

import RPi.GPIO as GPIO import time import os

# Use the Broadcom SOC Pin numbers # Setup the Pin with Internal pullups enabled and PIN in reading mode. GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Our function on what to do when the button is pressed def Shutdown(channel): os.system("sudo shutdown -h now")

# Add our function to execute when the button pressed event happens GPIO.add_event_detect(21, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

# Now wait! while 1: time.sleep(1)

When that is copy in the file to exit, Ctrl+X and "Y" and "enter".

You just created a file that will monitor a ground on the GPIO21 on your next boot up. You will notice the red led on the raspberry will flash 10 times indicating you that the shutdown is finish. Usually after 30 second it is safe to turn off the power.

For the hardware just put a OC (open contact switch) on the GPIO21 ( Header pin 40). Dont need to put a resistor because is already inside the rappsberry.

The picture is from http://invent.module143.com/daskal_tutorial/rpi-3-tutorial-9-gpio-how-to-use-a-push-button/

Display IP Addres on OLED Headless

When operating a raspberry pi headless is could be practical to have is IP address to do a SSH communication.

Base on this link From the fondation we will do an event at every reboot.