# Rpi Basics

---

## 🐧 **Day 2: Linux & Terminal Basics on Raspberry Pi**

---

### 🎯 **Learning Objectives**

By the end of today, learners will:

* Navigate the Linux filesystem using the terminal.
    
* Use essential Linux commands for managing files and directories.
    
* Install and remove software using `apt`.
    
* Understand file permissions and user roles.
    
* Enable and use SSH & VNC to remotely access the Raspberry Pi.
    

---

`ssh pi1@raspberry1.local`

### 🧠 **Topics & Content**

---

### 🗂️ 1. **Linux Filesystem Structure**

Explain the hierarchy using a tree:

```javascript
/
├── bin        # Essential binary programs
├── boot       # Boot loader files
├── dev        # Device files
├── etc        # System configuration files
├── home       # User home directories
│   └── pi     # Home directory for user 'pi'
├── lib        # System libraries
├── media      # Mounted drives (USB, SD)
├── opt        # Optional application software
├── tmp        # Temporary files
├── usr        # User programs and data
└── var        # Variable data (e.g., logs)
```

> ✅ Tip: Your working directory is usually `/home/pi`  
> Use `pwd` to check where you are.

---

### 💻 2. **Basic Linux Commands**

Practice the following:

| Command | Description |
| --- | --- |
| `pwd` | Print current directory |
| `ls` | List contents |
| `cd foldername` | Change directory |
| `cd ..` | Go to parent directory |
| `mkdir name` | Create new folder |
| `rm file` | Delete file |
| `rm -r folder` | Delete folder recursively |
| `nano file.txt` | Open file in editor |
|  |  |
| `top` | Show system processes |
|  |  |

**Practice Task:**

```bash
cd ~
mkdir day2
cd day2
nano hello.txt
```

---

### 📦 3. **Package Management with APT**

APT (Advanced Package Tool) is used for installing and managing software.

* Update package list:
    
    ```bash
    sudo apt update
    ```
    
* Upgrade installed packages:
    
    ```bash
    dsudo apt upgrade -y
    ```
    
* Install a package:
    
    ```bash
    sudo apt install cowsay
    cowsay "Linux is fun!"
    ```
    
* Remove a package:
    
    ```bash
    sudo apt remove cowsay
    ```
    

> 🔍 Explore: Try installing `neofetch`, `htop`, or `python3-pip`

---

### 🔐 4. **File Permissions and Users**

Use `ls -l` to check permissions:

```bash
-rw-r--r-- 1 pi pi  1234 Jun 14  hello.txt
```

**Breakdown:**

* `rw-` → owner can read/write
    
* `r--` → group can read
    
* `r--` → others can read
    

**Change permissions:**

```bash
chmod 755 myscript.sh
```

**Change ownership:**

```bash
sudo chown pi:pi myfile
```

**User management:**

```bash
whoami
sudo adduser student
```

---

### 🔗 5. **Remote Access: SSH and VNC**

* **SSH** (for terminal access over network)
    
* **VNC** (for GUI desktop access)
    

#### ✅ Enable SSH & VNC:

```bash
sudo raspi-config
# Go to "Interface Options" > Enable SSH and VNC
```

Alternatively:

```bash
sudo systemctl enable ssh
sudo systemctl start ssh
```

#### 🖥️ Install VNC Viewer on PC/Mac:

* Download: [https://www.realvnc.com/en/connect/download/viewer](https://www.realvnc.com/en/connect/download/viewer)
    
* Enter Raspberry Pi’s IP (e.g., `192.168.1.42`) and login
    

To check IP:

```bash
hostname -I
```

---

### 🛠️ **Activities**

1. **Create and Navigate Folders**
    

```bash
mkdir ~/projects/day2
cd ~/projects/day2
```

2. **Edit and View Files**
    

```bash
nano notes.txt
cat notes.txt
```

3. **Install Software**
    

```bash
sudo apt install figlet
figlet "Hello Pi"
```

4. **Enable SSH & VNC and connect from another device**
    

---

### 📋 **Homework**

* Try installing any fun terminal program (`cmatrix`, `sl`, etc.)
    
* Create a bash script named `myinfo.sh` that displays:
    
    * Current user (`whoami`)
        
    * Date/time (`date`)
        
    * IP address (`hostname -I`)
        

---

### ✅ **Summary**

* Terminal is your gateway to full control in Linux.
    
* You can move, create, and edit files efficiently.
    
* With `apt`, you can install nearly anything.
    
* SSH and VNC let you work remotely from any device.
    

# LED Blinking with Raspberry Pi

To make an LED blink on a **Raspberry Pi**, you'll need:

---

### 🧰 **Hardware Requirements**

* Raspberry Pi (any model with GPIO)
    
* 1x LED
    
* 1x 330Ω resistor
    
* Breadboard and jumper wires
    

---

### ⚙️ **Wiring**

Connect like this:

| LED Pin | Connects to |
| --- | --- |
| Anode (+) | GPIO pin (e.g., GPIO 17) via resistor |
| Cathode (–) | GND on Raspberry Pi |

📌 **Example:**

* **GPIO 17** → Resistor → **LED anode**
    
* **LED cathode** → **GND**
    

---

### 💻 **Python Code to Blink LED**

You can use the `gpiozero` or `RPi.GPIO` library.

#### ✅ Using `gpiozero` (simpler):

```python
from gpiozero import LED
from time import sleep

led = LED(17)  # GPIO pin 17

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)
```

#### ✅ Using `RPi.GPIO`:

```python
import RPi.GPIO as GPIO
import time

LED_PIN = 17

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

try:
    while True:
        GPIO.output(LED_PIN, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(LED_PIN, GPIO.LOW)
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()
```

---

### ▶️ **To Run the Script**

1. Save it as [`blink.py`](http://blink.py)
    
2. Open terminal and run:
    

```bash
python3 blink.py
```

---

Let me know if you want to:

* Control the LED via a **web interface**
    
* Blink with **different patterns**
    
* Use a **button** to control it
    

---

## LED.html

```javascript
<!DOCTYPE html>
<html>
<head>
    <title>LED Control</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding-top: 50px;
        }
        button {
            padding: 15px 30px;
            font-size: 18px;
            margin: 10px;
            cursor: pointer;
            border: none;
            border-radius: 8px;
        }
        .on {
            background-color: #4CAF50;
            color: white;
        }
        .off {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>
<body >
    <h1>LED Control</h1>
    <p>Current LED status: <strong>{{ 'ON' if status else 'OFF' }}</strong></p>
    <a href="/on"><button class="on">Turn ON</button></a>
    <a href="/off"><button class="off">Turn OFF</button></a>
</body>
</html>
```

flask\_led.py

```javascript
from flask import Flask, render_template
import RPi.GPIO as GPIO

app = Flask(__name__)

# GPIO setup
LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)

def is_led_on():
    return GPIO.input(LED_PIN) == GPIO.HIGH

@app.route('/')
def index():
    return render_template('led.html', status=is_led_on())

@app.route('/on')
def turn_on():
    GPIO.output(LED_PIN, GPIO.HIGH)
    return render_template('led.html', status=is_led_on())

@app.route('/off')
def turn_off():
    GPIO.output(LED_PIN, GPIO.LOW)
    return render_template('led.html', status=is_led_on())

@app.route('/cleanup')
def cleanup():
    GPIO.cleanup()
    return "GPIO Cleaned up!"

if __name__ == "__main__":
    try:
        app.run(host="0.0.0.0", port=5000, debug=True)
    finally:
        GPIO.cleanup()
```
