Pi Simple Examples

Pi Linux Commands
PI first login:

  • login: pi
  • password:raspberry
  • startx (start GUI)
  • sudo apt-get update //update the sources
  • sudo apt-get upgrade //upgrade all packages to the latest versions  
  • sudo raspi-config //run RPi configuration
PI info:
  • uname -a (display linux kernel)
  • sudo apt-get install lsb-release
  • lsb_release -a
WiFi network settings:
  • sudo iwlist wlan0 scan 
  • sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  • add this at the bootom of above file:
network={
ssid="wifi name"
psk="wifi password"
}
  • save the file (ctrl+x then y and enter)
  • sudo reboot
  • ifconfig wlan0 (inet addr field has an address)
 
USB devices checks:
  • lsusb
  • sudo lsusb -v -d 0c5d (the first 4 characters of ID)
HCI:
  • hciconfig
  • sudo hciconfig hci0 down
  • sudo hciconfig hci0 up
  • sudo hcitool lescan
If "hcitool lescan" command fails then enter the "hciconfig hci0 down" and then "hciconfig hci0 up". To stop "hcitool lescan" press Ctrl+C.

BLE:
  • bluetoothd -v // check BlueZ version
  • lsusb | grep -i bluetooth
  • hcitool dev
  • sudo hcitool lescan
  • find / -name  "bluetooth*"
Python:
  • sudo apt-get update
  • sudo apt-get install python-pip python-dev ipython
  • sudo apt-get install bluetooth libbluetooth-dev
  • sudo pip install pybluez 
PI safety shutdown or reboot:
  • sudo shutdown -h now or sudo halt  //halt before power off
  • sudo shutdown -r now  or sudo reboot 
Killing process:
  • ps -ef //show all running processes in Linux
  • kill -s KILL  
CRON:
*Sytem Cron
  • sudo nano /etc/crontab
  • sudo /etc/init.d/cron restart 
Installing Python Packages:
  • sudo apt-get install python-pip
  • sudo apt-get install python-requests
  • sudo pip install requests --upgrade  
GPIOs in Python:
// sudo apt-get update
// sudo apt-get install python-rpi.gpio
sudo python blinkingLed.py
Ctrl+C -> to stop the python script and return to the command line

// Dynamic Web Server
sudo apt-get install python-pip
sudo pip install flask

  • Toggle output state (blinkingLed.py):
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)

while True:
 GPIO.output(25, GPIO.HIGH)
 time.sleep(1)
 GPIO.output(25, GPIO.LOW)
 time.sleep(1)

  • Read the status of the buttons:
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.IN)
GPIO.setup(8, GPIO.IN)

counter1 = 0
counter2 = 0

while True:
 button1 = GPIO.input(7)
 button2 = GPIO.input(8)
 if(button1 == True):
  counter1 = counter1 + 1
  print("button1 pressed " + str(counter1) + " times.")
  time.sleep(.3)
 elif(button2 == True):
  counter2 +=1
  print("button2 pressed " + str(counter2) + " times.")
  time.sleep(.3)
 time.sleep(.01)