controller network setup

flash image to SD card

Go to Raspbian official download page and download Raspbian Buster Lite. It is a headless OS, meaning you won't have a desktop.

Flash Raspbian OS to an SD card using Balena Etcher or another image flashing software.

headless setup

Now that you flashed Raspbian onto a blank SD card, you will have two partitions. The first one, which is the smaller one, is the boot partition. Create two files in the root folder of the boot partition:

ssh

  • Create a file named ssh, without any extension, onto the boot partition of the SD card from another computer. When the Pi boots, it looks for the ssh file. If it is found, SSH is enabled and the file is deleted. The content of the file does not matter; it could contain text, or nothing at all.

wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=CA

network={
 ssid="Wifi Network"
 psk="Wifi Password"
}

When the Pi first boots, it will copy that file into the correct location in the Linux root file system and use those settings to start up wireless networking.

Resource @ https://www.raspberrypi.org/documentation/configuration/wireless/headless.md

connect to ssh

You can lookup the IP address by hostname. Freshly installed Raspbian will name your device "raspberrypi".

Here is how you obtain the IP address using ping command and hostname.

ping raspberrypi                # From Linux or Windows
ping raspberrypi.local          # From OS X

Enter the IP into PuTTY and use following info for SSH login:

  • Login: pi
  • Password: raspberry

Resource @ https://www.raspberrypi.org/documentation/remote-access/ssh/README.md

static ip optional

Set up static IP

sudo service dhcpcd status      # Checks if DHCPCD is active, if not enter next 2 commands
sudo service dhcpcd start       # Enter only if DHCPCD is not active
sudo systemctl enable dhcpcd    # Enter only if DHCPCD is not active
sudo nano /etc/dhcpcd.conf      # Opens DHCPCD configuration file

Add following lines to the dhcpcd.conf file.

interface wlan                              # eth0 if connected through LAN
static ip_address=192.168.1.12/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Now CTRL+O to save and CTRL+X to exit.

Resource @ https://www.ionos.ca/digitalguide/server/configuration/provide-raspberry-pi-with-a-static-ip-address/

change password or create user optional

Here is how

passwd                      # Change your own password
sudo passwd pi              # Change password of user pi
sudo passwd pi -d           # Remove password of user pi
sudo adduser rashid         # Add user rashid

change network name optional

Change name that shows over your WiFi network.

sudo nano /etc/hostname     # In the file change raspberrypi to desired name
sudo nano /etc/hosts        # In the file change raspberrypi to desired name
reboot                      # Reboot when done

Now CTRL+O to save and CTRL+X to exit.

Resource @ https://blog.jongallant.com/2017/11/raspberrypi-change-hostname/

general purpose I/O setup

pinout

pinout              # Display a GPIO pinout diagram
> ,--------------------------------.
> | oooooooooooooooooooo J8     +====
> | 1ooooooooooooooooooo        | USB
> |                             +====
> |      Pi Model 3B  V1.2         |
> |      +----+                 +====
> | |D|  |SoC |                 | USB
> | |S|  |    |                 +====
> | |I|  +----+                    |
> |                   |C|     +======
> |                   |S|     |   Net
> | pwr        |HDMI| |I||A|  +======
> `-| |--------|    |----|V|-------'
> 
> Revision           : a02082
> SoC                : BCM2837
> RAM                : 1024Mb
> Storage            : MicroSD
> USB ports          : 4 (excluding power)
> Ethernet ports     : 1
> Wi-fi              : True
> Bluetooth          : True
> Camera ports (CSI) : 1
> Display ports (DSI): 1
> 
> J8:
>    3V3  (1) (2)  5V    
>  GPIO2  (3) (4)  5V    
>  GPIO3  (5) (6)  GND   
>  GPIO4  (7) (8)  GPIO14
>    GND  (9) (10) GPIO15
> GPIO17 (11) (12) GPIO18
> GPIO27 (13) (14) GND   
> GPIO22 (15) (16) GPIO23
>    3V3 (17) (18) GPIO24
> GPIO10 (19) (20) GND   
>  GPIO9 (21) (22) GPIO25
> GPIO11 (23) (24) GPIO8 
>    GND (25) (26) GPIO7 
>  GPIO0 (27) (28) GPIO1 
>  GPIO5 (29) (30) GND   
>  GPIO6 (31) (32) GPIO12
> GPIO13 (33) (34) GND   
> GPIO19 (35) (36) GPIO16
> GPIO26 (37) (38) GPIO20
>    GND (39) (40) GPIO21

GPIO documentation with a pin diagram https://www.raspberrypi.org/documentation/usage/gpio/

Documentation on gpiozero python library https://gpiozero.readthedocs.io/

enable i2c and spi optional

sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo raspi-config

You are now in Raspberry config tool. Click on the following.

  • Interfacing Options
    • I2C (In older versions I2C is in Advanced Options)
      • Enable
    • SPI
      • Enable
sudo reboot                     # Reboot
sudo i2cdetect -y 1             # Display addresses for connected i2c devices
>      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
> 00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
> 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
> 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
> 30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 
> 40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
> 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
> 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
> 70: -- -- -- -- -- -- -- --      
ls /dev/i2c* /dev/spi*          # Display addresses for connected spi devices
> /dev/i2c-1  /dev/spidev0.0  /dev/spidev0.1

Resource @ https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi

packages

install dependencies

echo "$HOME"                        # Display your home directory (~)
cd ~                                # Go to home folder
sudo apt-get update                 # Update apt-get
sudo apt-get upgrade                # Upgrade apt-get
sudo apt-get install git-core       # Install git
sudo apt-get install python3-pip    # Install pip for Python 3
sudo pip3 install --upgrade setuptools
sudo pip3 install RPi.GPIO          # Install RPi.GPIO library
sudo pip3 install gpiozero          # Install gpiozero library
sudo pip3 install adafruit-blinka   # Python board library from Adafruit

install docker optional

curl -sSL https://get.docker.com | sh   # Install Docker
sudo usermod -aG docker pi              # Add permission to Pi User to run Docker Commands
reboot                                  # Reboot or run the next commands with a sudo
docker run hello-world                  # Test Docker installation
sudo apt-get install -y libffi-dev libssl-dev # IMPORTANT! Install proper dependencies
sudo apt-get remove python-configparser # OPTIONAL!
sudo pip3 install docker-compose        # Install Docker Compose

Resource @ https://dev.to/rohansawant/installing-docker-and-docker-compose-on-the-raspberry-pi-in-5-simple-steps-3mgl

  1. install docker
  2. create logger image ccci-logger
  3. https://raspberrypi.stackexchange.com/questions/8734/execute-script-on-start-up
  4.  

install nginx optional

sudo apt-get update
sudo apt-get install nginx -y
systemctl status nginx.service
sudo systemctl restart nginx.service

Config file

install midnight commander optional

sudo apt -y install mc
select-editor

install visual studio code

curl -L https://raw.githubusercontent.com/headmelted/codebuilds/master/docs/installers/apt.sh | sudo bash

scripts

run and always restart a script

Set up a continuously running python script on Raspberry Pi that will start on boot and restart if fails.

Lets create an infinite loop script named ccci_init.py with following content:

#!/usr/bin/env python3

import time

while True:
    print('Hello world!')
    time.sleep(5)

Create ccci.service file and enter the following:

[Unit]
Description=CCCI Service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u ccci_init.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Resource @ https://www.raspberrypi.org/documentation/linux/usage/systemd.md

sudo cp ccci.service /etc/systemd/system/ccci.service
sudo systemctl start ccci.service       # Start systemd service (testing)
sudo systemctl status ccci.service      # Display service status (testing)
sudo systemctl stop ccci.service        # Stop service (testing)
sudo systemctl enable ccci.service      # Enable autorun service
sudo reboot

save to usb

Save to usb

# find out

connect xbox controller

sudo apt-get install xboxdrv
sudo bash -c echo 1 > /sys/module/bluetooth/parameters/disable_ertm
sudo bluetoothctl
agent on
default-agent
scan on
connect B8:27:EB:A4:AC:11   # Replace BT address
trust B8:27:EB:A4:AC:11     # Trust it
sudo xboxdrv --detach-kernel-driver --silent --mouse    # Use controller as mouse
sudo apt-get install joystick
sudo jstest /dev/input/js0
sudo pip install evdev

projects

home thermostat

Build a very smart thermostat for your home central heating system with Raspberry Pi Zero.