LECTURE - 2 (AIOT) Essential Linux Commands

Download as pdf or txt
Download as pdf or txt
You are on page 1of 125

APPLIED INTERNET OF THINGS (AIOT)

(CSE 2198)
(LECTURE – 2)
What Is Outcome-Based Education? OBE Vs
Traditional Education System
 Outcome-based education is a system where all the parts and aspects of
education are focused on the outcomes of the course. The students take up
courses with a certain goal of developing skills or gaining knowledge and
they have to complete the goal by end of the course.

 There is no specific style or time limit of learning. The student can learn as
per their choice. The faculty members, moderators, and instructors guide
the students based on the target outcomes.
Benefits Of Outcome-Based Education (OBE)
For Students

 Brings clarity among the teachers and students


 Every student has the flexibility and freedom of learning in their
ways.
 There is more than one method of learning
 Reduces comparison among the students as everyone has a
different target
 Completely involves students taking responsibility for their goals
About your subject

 Grading Pattern : 1
 Credit : 4
 TextBook: • 1) Practical Python Programming
for IoT: Build advanced IoT projects using a
Raspberry Pi 4, MQTT, RESTful APIs,
WebSockets, and Python 3 by Gary Smart, Packt
Publishing
2) Raspberry Pi Cookbook by Simon Monk,
shroff/O’Reilly
Evaluation Scheme

 ATTENDANCE : 5
 WEEKLY ASSIGNMENTS / QUIZZES : 20 MID-TERM
 MID TERM : 15
 TOTAL INTERNAL : 40

 END TERM: 45
 LAB BASED PROJECT : 15 END-TERM
 TOTAL EXTERNAL : 60
Course Outcomes
Course Outcomes
Apply foundational principles of Python programming and configure
CO1 Raspberry Pi to develop IoT solutions.
Design IoT networking solutions employing RESTful APIs, Web Sockets, and
CO2 MQTT protocol.
CO3 Integrate sensors and actuators with Raspberry Pi to acquire and process
real-world data.
Develop IoT applications for environmental monitoring, control, and
CO4 automation.
Implement multithreading and asynchronous programming methodologies
CO5 for efficient IoT applications.

CO6 Analyze end-to-end IoT solutions for performance, scalability, and security.
What Students will learn?
1.Programming with Python and Raspberry Pi: Students will gain proficiency in Python programming language
and learn how to use it in conjunction with the Raspberry Pi platform for IoT applications.
2.Setting Up Development Environment: They will learn how to set up their development environment on the
Raspberry Pi, including necessary software and configurations.
3.Understanding IoT Concepts: Students will understand the fundamental concepts of the Internet of Things (IoT),
including its applications, architecture, and potential impact.
4.Networking with RESTful APIs and Web Sockets: They will learn how to implement networking functionalities
using RESTful APIs and Web Sockets, enabling communication between IoT devices and other systems.
5.Networking with MQTT Protocol: Students will understand the MQTT protocol and its application in IoT
communication, including setting up and using the Mosquitto MQTT broker.
6.Connecting Raspberry Pi to the Physical World: They will learn how to connect Raspberry Pi to various sensors,
actuators, and other physical components to interact with the real world.
7.Electronics Fundamentals: Students will gain a basic understanding of electronics principles, including GPIO pins,
sensors, motors, and actuators.
8.IoT Visualization and Automation Platforms: They will explore different IoT visualization and automation
platforms, learning how to visualize data and automate processes in IoT applications.
9.Integration and Tying It All Together: Finally, students will learn how to integrate various components, concepts,
and platforms to develop comprehensive IoT solutions, tying together their knowledge and skills acquired
throughout the course.
Install the latest packages
ADD/REMOVE Panel items
ADD/REMOVE Panel items

Click “Terminal”, Right


Click on Screen and select
“Preferences”
ADD/REMOVE Panel items

Change the
Background and
Foreground like this
Learning Basic Linux Commands
 Linux is an open operating system based on Unix and has been developed collaboratively by many companies
and universities since 1991.
 In general, Linux is harder to manage than some other operating systems like Windows, but offers more flexibility
and wider configuration options.
 There are several popular versions of the Linux operating system such as Debian, Ubuntu, Red Hat, Fedora and
so on.

A big part of using a Raspberry Pi is also using


the terminal. The terminal is something that
many people try to avoid because they feel like it
is a bit hard to use.
Learning Basic Linux Commands
After you learn these commands you’ll hopefully feel really comfortable using the terminal. When you access
the terminal through an SSH communication, you can install software on your Pi remotely, create files or
folders and run any scripts directly from your computer.

You also don’t need to know all these commands by heart; you can always access this PPT LECTURE - 2 as a
reference to remind you how to do something.

Here’s the table of contents:


• Exploring the Linux File System
• Editing Files using the Terminal
• Managing Software on Your Raspberry Pi
• Changing the Raspberry Pi Default Settings
• Shutting Down and Rebooting
Exploring the Linux File System
Exploring the Linux File System

It’s time to play around with the command line.

For starters, type pwd, which means print working


directory:

pi@raspberry:~ $ pwd ~(squiggly): shortcut


/home/pi for home directory
$: command prompt
The output is /home/pi.

Forward slashes are always used to indicate folders


and files within other folders. In this case, the current
working directory is pi, which is inside home, which
is inside the root of the file system. Here, pi is the
username with which you are logged in.
Note: The commands in Linux are case-sensitive, which means that PWD, PwD, pWd, and any other variations
are completely different from pwd. The same holds true for all other commands and for any code written in the
programming languages addressed in this course.
Exploring the Linux File System
Navigating the file system
The most frequent commands you will use are ls (list) and cd (change directory). They are used for listing the
contents of a directory and moving from one directory to another.

When you first open the terminal, it will open up in your home folder (as you’ve seen with the pwd command). You
can display exactly what kind of files or folders are in the working directory with ls:

pi@raspberry:~ $ ls

Right now your directory is empty, so you won’t see anything when you try to list your files and folders.
Navigating the file system
Navigating the file system
Want to create a new folder? Usemkdir followed by the name you want to give the
folder:

pi@raspberry:~ $ mkdir NewFolder


pi@raspberry:~ $ ls
NewFolder

To navigate, we’ll be using the cd command, followed by the location you want to
move to. This can be done like so:

pi@raspberry:~ $ cd NewFolder
pi@raspberry:~/NewFolder $
This moved you to the NewFolder directory that you just created.

Use command rmdir followed by the subdirectory name to remove a subdirectory.


Navigating the file system
Here’s one trick you can use so you don’t have to remember the exact name of the path – the command line or
terminal will try to autocomplete the phrase if you press the Tab key while something is only partially typed. Try
the cd command again (use cd .. to move up one directory):

pi@raspberry:~/NewFolder $ cd ..
pi@raspberry:~ $ ls
NewFolder
Now start writing your cd command again…

pi@raspberry:~ $ cd NewF

… by pressing Tab when you’ve only written ‘NewF’ It will autocomplete the file path:

pi@raspberry:~ $ cd NewFolder

Finally, there are some quick commands you can use to manipulate files. Create a new file with the touch command:

pi@raspberry:~/NewFolder $ touch NewFile.txt


pi@raspberry:~/NewFolder $ ls
NewFile.txt
Navigating the file system
Individual files can be copied using the command cp, followed by the file name and you can also use this to
rename files by doing:

pi@raspberry:~/NewFolder $ cp NewFile.txt OtherFile.txt


pi@raspberry:~/NewFolder $ ls
NewFile.txt OtherFile.txt
The original file can then be deleted by using the rm command followed by the file name:

pi@raspberry:~/NewFolder $ rm NewFile.txt
pi@raspberry:~/NewFolder $ ls
OtherFile.txt

You can move files using the mv command:

pi@raspberry:~/NewFolder $ mv OtherFile.txt /home/pi


pi@raspberry:~/NewFolder $ cd ..
pi@raspberry:~/$ ls
NewFolder OtherFile.txt
Editing Files using the Terminal
Nano is an easy-to-use text editor that is installed
by default in Raspbian distribution and many other
Linux distributions.

You can use the following commands to edit


the OtherFile.txt created in the previous Unit:

pi@raspberry:~ $ cd
pi@raspberry:~ $ nano OtherFile.txt

Nano will follow the path and open that file if it exists.
If it does not exist, it’ll start a new buffer with that file
name in that directory.

Let’s take a look at the default nano screen:


At the top row, you’ll see the name of the program version number, the
name and extension of the file you’re editing, and whether the file has
been modified since it was last saved. final two rows at the bottom
are the shortcut lines
Note: If you have a new file that isn’t saved yet, you’ll see “New Buffer.”
Shortcuts
Program functions are referred to as “shortcuts” in
nano, such as saving, quitting, searching, etc. The
most common ones are listed at the bottom of the
screen (as shown in the preceding Figure), but there
are many more that aren’t.

Warning: nano does not use the Shift key in shortcuts.


All shortcuts use lowercase letters and unmodified
number keys, so Ctrl+G is NOT Ctrl+Shift+G.

Press Ctrl+G to bring up the Help menu and scroll


down with your arrow keys to see a list of valid
shortcuts.

When you’re done looking at the list, hit Ctrl+X to exit


Help menu.
Shortcuts
Now let’s say you’re working on your text file
and you want to save it and exit nano. This
is executed by pressing Ctrl+X.

Nano will ask you if you want to save the


changes, you can type:
•Y, then Enter – to save all your changes
•N, then Enter- to cancel any changes
Demonstration of Use of nano
Demonstration of Use of nano
Demonstration of Use of nano
Demonstration of Use of nano
Demonstration of Use of nano
Managing Software on Your Raspberry Pi
 When you know your way around the command line, downloading and installing new software on a computer or
device running the Linux OS is quite easy and straightforward.

 The software comes in what are called packages — software programs that can be downloaded from the Internet
and installed simply by typing a command in the prompt.

 To download and install these packages, you normally use a package manager, which downloads and installs not
only the software you requested but also all other required software, known as dependencies.

 The Raspbian distribution uses a package manager called apt.

 To manage your software, you need the authorization of the administrator, whom you already know as the
superuser. To do so, type sudo (superuser do) before a command.
Updating and Upgrading
Updating and Upgrading
Updating and Upgrading
Upgrade Raspbian To The Latest Version
Upgrade Raspbian To The Latest Version
Upgrade Raspbian To The Latest Version
Upgrade Raspbian To The Latest Version
Update Python on Raspberry Pi
 First and foremost, you have to update the list of available package versions that your package manager is aware
of. (The package manager keeps such a list in the Raspberry’s file system.) Type the following command:

pi@raspberry:~ $ sudo apt-get update


You need to be connected to the Internet for this command to work. Text scrolls by after you type the command,
giving information about the newest listings.

Next, you should update the software, which you can achieve by commanding apt to upgrade. This command
upgrades all the packages you’ve installed to their most recent versions:

pi@raspberry:~ $ sudo apt-get upgrade

 In terms of wording, the difference between updating and upgrading is subtle, but what they do is quite different
(even though they’re usually done together).

 sudo apt-get update updates the list of available package versions but doesn’t install or upgrade any of them,
whereas sudo apt-get upgrade updates the packages themselves, checking the list to do so. For that reason, you
should always run update before upgrade.
Installing , Running and Removing software
 To install a package for which you already know the name, you have to type the following command:

pi@raspberry:~ $ sudo apt-get install <desired application name>


 To run programs directly from the prompt, simply type their names, as shown in the following command:

pi@raspberry:~ $ python

 This opens the Python interpreter we will explore in the next Module.

 To remove software from your RPi, you resort to the apt package manager once more. Here’s an example:

pi@raspberry:~ $ sudo apt-get remove <desired application name>

 This command, however, leaves behind files that are somehow related to the software, such as configuration files
and logs. If you don’t intend to use those files in any way, you can remove everything by using purge:

pi@raspberry:~ $ sudo apt-get purge <desired application name>

 Do not remove any package that you didn’t install yourself unless you’re absolutely certain that you know what it’s
for. It may be a necessary package that comes with the Linux OS, and removing it may lead to a system crash.
Changing the Raspberry Pi Default Settings
 To change the Raspberry Pi configurations you can use a tool written by Alex Bradbury. To open the configuration
tool, simplRaspberry Pi configurations,y run the following from the command line:

pi@raspberry:~ $ sudo raspi-config

 The sudo is required, because you will be changing files that you do not own as the pi user.

 You should see a blue screen with options in a grey box in the center:

raspi-config aims to provide the functionality to make the most common configuration changes. keep in mind that
some options require a reboot to take effect. If you changed any of those, raspi-config will ask if you wish to reboot
now when you select the<Finish> button.
Changing the Raspberry Pi Default Settings
It has the following options available:

1.Expand Filesystem

2.Change User Password

3.Boot Options

4.Wait for Network at Boot

5.Internationalisation Options

6.Enable Camera

7.Add to Rastrack

8.Overclock

9.Advanced Options

10.About raspi-config
Expanding your file system
I recommend expanding your file system.

Choosing option 1 from the raspi-config menu will expand


your installation to fill the rest of the microSD card, giving
you more space to use for files.

Note: you will need to reboot the Raspberry Pi to make


this available. Note there is no confirmation; selecting the
option begins the partition expansion immediately (as
shown in the Figure below).

Right now you don’t need to change anything else.


Shutting Down and Rebooting
 There are better ways to shut down and reboot your Raspberry Pi than simply unplugging it. Unplugging your RPi
is the equivalent of shutting down your computer by pressing the power button or even removing its power
source, which can lead to file corruption.

 To shut down your Raspberry Pi, simply type this command on the command line:

pi@raspberry:~ $ sudo poweroff

 You see the following information after you use the shutdown command:
Shutting Down and Rebooting
 To reboot, type this:

pi@raspberry:~ $ sudo reboot

 This is the result:

You need to login again through SSH after rebooting.


Let’s Do Some Practice
Exploring the Linux File System

/: root directory
Exploring the Linux File System

../: go to one up
directory
Exploring the Linux File System

sudo : Super User Do


to get the permission
Exploring the Linux File System
Exploring the Linux File System

Remove Putty
Exploring the Linux File System

Completely Remove Putty


Exploring the Linux File System

Blue Colours : Folders

Create a File named as


“myFile”
Exploring the Linux File System

Rename a File from


“myFile” to “myFile.txt”

Creating another
directory “Scratch” to
move files
Exploring the Linux File System
Exploring the Linux File System

Copy a File
Exploring the Linux File System

In Linux we just
“Keeping Track of Paths”
Exploring the Linux File System

Remove a File
Exploring the Linux File System

Remove a Directory
Exploring the Linux File System
Exploring the Linux File System

You can remove files one by one

Remove all .txt files at a time


Exploring the Linux File System
Exploring the Linux File System

Remove all .txt files at a


time except .py file
Exploring the Linux File System

Remove all files with all


extension at a time
Exploring the Linux File System

Creating two .txt file and


two .py files inside Scratch
Exploring the Linux File System

Remove everything from


Scratch directory
Exploring the Linux File System
Exploring the Linux File System
Exploring the Linux File System

Remove Scratch Recursively


Exploring the Linux File System

Creating catalogue using cat


nano : Text editor
Exploring the Linux File System

Ctrl + O : Save
Ctrl + X : Exit
Exploring the Linux File System

Catalogue “cat” is used to


see the .txt content
Exploring the Linux File System

Again some modifications in


“myCats.txt” and save

Trying to edit the python


file cats.py
Exploring the Linux File System

Ctrl + O : Save
Ctrl + X : Exit
Exploring the Linux File System

Read the Python file


cats.py
Exploring the Linux File System

Ctrl + O : Save
Ctrl + X : Exit

To close press : CTRL + X + Y + Enter


Exploring the Linux File System
Exploring the Linux File System
Exploring the Linux File System
Exploring the Linux File System
Exploring the Linux File System
Command Description Example Usage Command Description Example Usage
Execute a command with
ls List directory contents ls /home/pi sudo sudo apt update
superuser privileges
Package manager for Debian-
cd Change directory cd /home/pi apt apt install package_name
based distributions
wget
pwd Print working directory pwd wget Download files from the internet
http://example.com/file.tar.gz

mkdir Make directory mkdir my_folder ssh Secure Shell, remote login ssh username@hostname

scp file.txt
rm file.txt or rm -r Securely copy files between
rm Remove files or directories scp username@hostname:/destin
my_folder machines
ation
cp file.txt
cp Copy files or directories df Display disk space usage df -h
/destination_folder
mv file.txt
mv Move or rename files or directories new_location or mv free Display system memory usage free -m
old_name new_name

cat Display file contents cat file.txt .. Parent directory shortcut cd ..

nano / vi / Text editors (nano is simpler, vi/vim nano file.txt or vi


~ User's home directory shortcut cd ~
vim more advanced) file.txt
Creating and Running a Python Program
You may find that both version 2.x and 3.x are installed on your Raspberry Pi. Enter the
following commands to display the versions of Python you have on your Raspberry Pi (Figure
3.1):
Creating and Running a Python Program
Method 1 — Interactively from command prompt
In this method, you will login to your Raspberry Pi remotely using the SSH and then create and run
your program interactively. This method is excellent for small programs. The steps are as follows:

• Log in to the Raspberry pi using SSH.


• At the command prompt enter python3. You should see the Python command mode which is
identified by three characters >>>.
• Type the program:
print ("Hello From Raspberry Pi Zero 2 W")
• The required text will be displayed interactively on the screen as shown in Figure 3.2.
Creating and Running a Python Program
Creating and Running a Python Program

Exit from Python : CTRL + Z or type “quit()”


Creating and Running a Python Program

Press “TAB key” to make


proper Indentation

After completion Press “CTRL + X + Y + Enter”


Creating and Running a Python Program
Method 3 — Create a Python file in GUI mode
Troubleshoot copy & paste code from Pi
to other OS

Install Thonny Through “CMD”


Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS

Change “font” and “theme”


Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS

Copy the codes from Thonny


(Windows) , Open thonny IDE on
Raspberry Pi and paste
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS
Troubleshoot copy & paste code from Pi
to other OS

All the python codes should be written in Thonny (windows) but


save and executed on Raspberry Pi in the given directory:
/home/pi/Python<sec_name>/Exp_<name><sec_name>
Plotting in Python

pi@raspberrypi~ $ sudo apt-get install python-matplotlib

pi@raspberrypi~ $ sudo apt-get install python3-matplotlib


Plotting in Python
Graph of a quadratic function

Function linspace(0,4,100) creates a list 100 integer numbers


in x, starting from 0 and terminating at 4. Function plot draws
the graph where the y value is equal to x2 . Function show()
physically displays the graph. Notice that there are some buttons
at the bottom of the window. These buttons are (from left to right
as shown in Figure
Plotting in Python
Code Result

Take a screenshot of the code(using snipping tool) and the result,


paste in word file and mail to your email id before you leave the
lab.
Plotting in Python
Graph of a quadratic function
The graph can be made more user friendly by labeling the axes
and giving a title to the graph. The modified program is given
below:
Plotting in Python
Drawing multiple graphs
Plotting in Python
Drawing multiple Scatter graphs
Function plot draws a smooth graph by joining the x,y values. You can also draw different
types of graphs. For example, function scatter draws a scatter graph. The program for this
graph is as follows:
Plotting in Python
Drawing multiple Scatter graphs
Function plot draws a smooth graph by joining the x,y values. You can also draw different
types of graphs. For example, function scatter draws a scatter graph. The program for this
graph is as follows:
How to fix broken packages
on Raspberry Pi OS

METHOD - 1
Use apt command to fix broken packages
Use apt command to fix broken packages on Raspberry Pi OS
The apt command is a terminal-based package management tool used for installing, updating
or upgrading the packages on Raspberry Pi OS. Along with these features, it also has the
potential to fix those broken packages that prevent you from installing any package onto your
Raspberry Pi OS.

If at any stage, you will encounter a broken packages error while installing a package onto
your Raspberry Pi, then you should run the following command in the terminal which might
fix the issue.

$ sudo apt --fix-missing update


Use apt command to fix broken packages

Now, when you perform the following command, you will need to choose an option
between “y” or “n” and you should press “y” key on the three requests which appears
on your terminal window. This will prepare the installation of the required packages for
your Raspberry Pi.
Use apt command to fix broken packages
Once it is done, you will need to provide a force for the installation of required
packages which are ready to be upgraded and for that you will be required to execute
the below given command in the terminal.

$ sudo apt install -f

Once it is done, you can repeat the installation process again to check whether your
package installs without any error.
How to fix broken packages
on Raspberry Pi OS

METHOD - 2
Use apt command to fix broken packages
To encounter the error of “broken install”, you should execute the below mentioned
command the terminal.
$ sudo apt --fix-broken install

Once you execute the above command, you can then go on to install the package again
and hope it works.
How to fix broken packages
on Raspberry Pi OS

METHOD - 3
Use apt command to fix broken packages
If the problem still won’t be solved while trying everything possible then you should
provide a full-upgrade of your Raspberry Pi OS as it might install the required packages
needed to install an application and also it will remove the older packages on your
Raspberry Pi OS that prevents your OS from installing a package.

$ sudo apt full-upgrade


How to fix broken packages
on Raspberry Pi OS

METHOD - 4
Configure dpkg to fix broken packages
If you are still experiencing the broken packages error while attempting every apt
command then there might be some issue in the setup process which is handled by
Debian package manager(dpkg). So, instead of selecting to go with the apt command,
you will have to fix the problem through dpkg configuration. The following command will
need to be executed in the terminal first which will force the dpkg to reconfigure the
broken packages which have not yet been installed on the Raspberry Pi device.

$ sudo dpkg --configure -a


Configure dpkg to fix broken packages
Thereafter, if the above command won’t solve the issue, then you can move one step
further and execute the below command to check which package will need a reinstall.

$ sudo dpkg -l | grep ^..r


Configure dpkg to fix broken packages
After executing the above command, you will be able to see the packages which dpkg
marked as reinstall and then you can forcefully remove those broken packages that are
causing problems in the installation process through the following command.

$ sudo dpkg --remove --force-remove-reinstreq [Name of package]

Once it is done, you can then use the apt command to cleanup the system.

$ sudo apt clean


Configure dpkg to fix broken packages
After the cleanup, install the packages update through the update command and once it
is completed you can then try and install the packages again and hopefully it will work
fine.
$ sudo apt update
Configure dpkg to fix broken packages

Installing packages on Raspberry Pi is quite a basic operation until you encounter broken
package errors. You are suggested to stay with installing those programs which are useful
as installing unnecessary packages would create a mess and problems that would be
hard to figure out. In case the broken packages problem occurs, you might need help
sorting them out and the above methods will be handy in that case, providing you with
some commands to correct such errors.

You might also like