Raspberry Pi – Blink Light – Python

A Simple Python script to blink a Raspberry Pi LED.

import RPi.GPIO as GPIO
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)  # Uses the physical pin numbering
GPIO.setup(7, initial=GPIO.LOW)  # Set GPIO pin to off

while True:
    GPIO.output(7, GPIO.HIGH)
    sleep(0.2)
    GPIO.output(7, GPIO.LOW)
    sleep(0.2)

Change pin numbers as needed.

We can also do this with BASH.

Control LED using BASH

Turn 3.5mm Jack on Raspberry Pi Running LineageOS 16

You will need an Android Terminal. You can turn on the default one in the developer settings. Need to turn on developer mode?

You will also need to enable root which can also be done in the Developer settings

Open up the terminal app and run

su
rpi3-audio-jack.sh

More info here

https://konstakang.com/devices/rpi3/LineageOS16.0/

Installing LineageOS on Raspberry Pi B+

Download LineageOS

Download the unofficial LineageOS 16 build from the following page

https://konstakang.com/devices/rpi3/LineageOS16.0/

Unzip

Unzip the file with

unzip ~/Downloads/lineage-16.0-20200207-UNOFFICIAL-KonstaKANG-rpi3.zip

Write to SD Card

Either use the instructions on the following link to write it to the SD card

https://www.raspberrypi.org/documentation/installation/installing-images/windows.md

Or use DD

WARNING! Make sure “/dev/mmcblk0” is the correct SD Card. Refer to here if you need to locate the path for the SD Card.

sudo dd if=~/Downloads/lineage-16.0-20200207-UNOFFICIAL-KonstaKANG-rpi3.img of=/dev/mmcblk0 bs=1M status=progress

Plug you SD Card into your Pi and boot it up.

Raspberry Pi – Ping IP Address and Toggle LED

The following script is for monitoring if an IP address is reachable or not. If it becomes unavailable the script will turn on a LED that is plugged into one of the GPIO pins of the Raspberry Pi. View pinout here

Script

#!/bin/bash
# Script to ping ip address and turn on LED on if device is unreachable.
                                                                                                                                                                                                 nPin="18"  # Change if GPIO pin is different                                                                                                     
ledPin="gpio${nPin}"                                                                                                                                                                                                                            toPing="8.8.8.8"  # Change to address you want to ping

echo "${nPin}" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/${ledPin}/direction

if ( fping -r1 $toPing | grep -v alive ); then
         echo "Internet unreachable"
         # Turn on LED
         echo "1" > /sys/class/gpio/${ledPin}/value
 else
         # Turn off LED 
         echo "0" > /sys/class/gpio/${ledPin}/value
 fi

Save script as ping_led.sh and make it executable.

chmod +x ping_led.sh

and run the script.

sh ping_led.sh

Run script in crontab

You can setup the script to run every minute using a crontab

crontab -e

Add the following line

*/1 * * * * /home/pi/ping_led.sh

Should now execute the script every minute and not need any human interaction.

Cannot open mailbox /var/mail/pi: Permission denied

This is on a Raspberry Pi, but should be the same on any Linux distro.

Error :

pi@raspberrypi:~ $ mail
Cannot open mailbox /var/mail/pi: Permission denied
No mail for pi
pi@raspberrypi:~ $ 

Fix :

sudo touch /var/mail/$USER
sudo chown $USER:mail /var/mail/$USER
sudo chmod 660 /var/mail/$USER

You can replace “$USER” if you need to run the commands for a different account.
Example:

sudo touch /var/mail/pi
sudo chown pi:mail /var/mail/pi
sudo chmod 660 /var/mail/pi

How to Set a Static IP Address on a Raspberry Pi

The examples given here are for modifying the wlan0 interface.  Replace wlan0 with the interface you are configuring. i.e. (eth0,wlan1)

Method 1

This was the typical way to add a static IP address to a Pi, if you have issues with this, then try Method 2.

sudo vi /etc/network/interfaces

In the file it is pretty easy to see which lines control which interface, find the lines that control wlan0 (or the interface your configuring) and change/add to look like below.

iface wlan0 inet static
address 192.168.42.109
netmask 255.255.255.0
gateway 192.168.42.1

Save the file, reboot, and the Pi should come up with the new static IP.

Method 2

It looks like on the newer versions of Raspbian, the above method does not work anymore, so now you have to edit the following file

sudo vi /etc/dhcpcd.conf

and add the following lines.

interface wlan0
static ip_address=192.168.42.109/24
static routers=192.168.42.1
static domain_name_servers=192.168.42.1

If you just need to assign a static IP address, to the device, because it is going to be setup as a hotspot or something, you can get away with the following.

interface wlan0
static ip_address=192.168.42.1/24

If you run into issues with it not assigning the address, check the /etc/network/interfaces file and make sure that the line that starts with “iface wlan0” says manual at the end and not static.  If it says “iface wlan0 inet static”, change it to “iface wlan0 inet manual”

Control LED from Command Line – Raspberry Pi

Replace “4” with the GPIO pin your using.

echo "4" > /sys/class/gpio/export

Setup the direction.  If it was a button or switch we would change “out” to “in”.

echo "out" > /sys/class/gpio/gpio4/direction

Turn the LED on.

echo "1" > /sys/class/gpio/gpio4/value

Turn the LED off.

echo "0" > /sys/class/gpio/gpio4/value

1 = on and 0 = off.

 

How to Install CraftBukkit Minecraft Server on Raspberry Pi

First we need to Install Java.

sudo apt-get install openjdk-7-jre

Next lets create a CraftBukkit directory to store all the files.

mkdir ~/mc-bukkit
cd ~/mc-bukkit

And lets download CraftBukkit.

wget "http://dl.bukkit.org/downloads/craftbukkit/get/02389_1.6.4-R2.0/craftbukkit.jar" -O "craftbukkit.jar"

Now lets create a script that we can use to start the Server.

sudo vi /etc/init.d/mcstart.sh

Copy and paste the following text.

#! /bin/sh
# /etc/init.d/mcserver

### BEGIN INIT INFO
# Minecraft CraftBukkit Start script
### END INIT INFO

case "$1" in
  start)
    echo "Starting mcserver"
    MCDIR=$("/home/pi/craftbukkit")
    cd "$MCDIR"
    java -Xmx512M -jar craftbukkit.jar -o true &
;;
  stop)
    echo "Stopping mcserver"
    kill `pgrep mcserver`
    ;;
  *)
    echo "Usage: /etc/init.d/mcserver {start|stop}"
    exit 1
    ;;
esac
exit 0

Now lets make the file executable.

chmod +x mcstart.sh

And launch

sudo /etc/init.d/mcstart.sh

The first lunch will take a long time so be patient.

If you want to start CraftBukkit when your Pi boots up then run the following command.

sudo /etc/init.d/mcstart.sh start

Turn Your Raspberry Pi into a Wireless Hotspot

Goal:

The goal of this guide is to turn a Raspberry Pi into a wireless home/mobile router.

Equipment:

  1. Raspberry Pi with raspbian
  2. Wireless USB device.  The guide uses a Edimax Nano USB Wifi (EW-7811Un) adapter
  3. Ethernet cat5 cable to connect to the Internet

Lets get started.

Install the following packages.

sudo apt-get install hostapd dnsmasq iptables

Open up “/etc/network/interfaces” and add the following lines.  If there is a line for wlan0 you can comment it out with a “#”.

iface wlan0 inet static
address 192.168.42.17
netmask 255.255.255.0

Configure Hostapd

Edit the following file “/etc/default/hostapd” so it looks like this.

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Now edit the hostapd config file “/etc/hostapd/hostapd.conf” and configure the wireless access point.

interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=MC
channel=1
wmm_enabled=0
wpa=1
wpa_passphrase=min3cr@ft
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0 

 Configure the DHCP Server

For the DHCP server we just need to add the following lines to “/etc/dnsmasq.conf”

interface=wlan0
dhcp-range=192.168.42.20,192.168.42.152,255.255.255.0,12h
dhcp-option=3,192.168.42.17

Configure Iptables

Now we need to set it up so the pi can forward traffic from wlan0 to eth0.

Edit “/etc/sysctl.conf” and uncomment the following line

net.ipv4.ip_forward=1

then execute

sysctl -p

Next create an iptables “config” file.

sudo touch /etc/network/if-up.d/router.sh
sudo chmod +x /etc/network/if-up.d/router.sh
sudo su -c "echo '/etc/network/if-up.d/router.sh' >> /etc/rc.local"

Add the following line to the file.

sudo iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
sudo iptables --append FORWARD --in-interface wlan0 -j ACCEPT

If you are using the same wireless adapter that is used in this guide then you will need to execute the following commands to replace the hostapd binary.  You can find more info here.

wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
unzip hostapd.zip
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
sudo mv hostapd /usr/sbin/hostapd.edimax
sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd
sudo chown root.root /usr/sbin/hostapd
sudo chmod 755 /usr/sbin/hostapd

That should do it. Plug in the Ethernet cable and reboot  your pi and you should be good to go.