IOTlab Deepak
IOTlab Deepak
Bachelor of Technology
In
Computer Science & Engineering
(Bikaner Technical University, Bikaner)
2-6
Start Raspberry Pi and try various Linix commands in
1 command terminal window: ls, cd, touch, mv, rm, man, mkdir,
rmdir, tar, gzip, cat, more, less, ps, sudo, cron, chown, chgrp, ping
etc.
1
Experiment – 1
Aim: Start Raspberry Pi and try various Linux commands in command terminal window:ls,
cd, touch, mv, rm, man, mkdir, rmdir, tar, gzip, cat, more, less, ps, sudo,cron, chown, chgrp,
ping, chmod, curl, echo, exit, find, finger, free, grep, kill etc.Use command line terminal of
linux operating system, preferably Raspberry Pi operating system.
Linux Commands:
Syntax:
$ cd [directory]
- cd “dir name”: This command is used to navigate to a directory with white spaces.
2
4. mv: mv stands for move. mv is used to move one or more files or directories from
one place to another in file system.
It has two distinct functions:
5. rm : rm stands for remove here. rm command is used to remove objects such as files,
directories, symbolic links and so on from the file system Syntax: rm [OPTION]... FILE...
6. man: man command in Linux is used to display the user manual of any
command
Syntax :
$man [OPTION]... [COMMAND NAME]...
Syntax :
$man [OPTION]... [COMMAND NAME]...
Syntax:
rmdir [-p] [-v | –verbose] [–ignore-fail-on-non-empty] directories …
9. tar: The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the
Archive files.
Syntax:
tar [options] [archive-file] [file or directory to be archived]
10. gzip: gzip command compresses files. Syntax : gzip [Options] [filenames]
3
Example:
$ gzip [mydoc.txt]
11. cat: It reads data from the file and gives their content as output. It helps us to
create, view, concatenate files.
Command:
$cat filename
12. more: more command is used to view the text files in the command prompt, displaying
one screen at a time in case the file is large (For example log files). The more command
also allows the user do scroll up and down through the page.
Syntax: more [-options] [-num] [+/pattern] [+linenum]
[file_name]
[-options] : any option that you want to use in order to change the way the file is
displayed. Choose any one from the followings: (-d, -l, -f, -p, -c, - s, -u)
[-num] : type the number of lines that you want to display per screen. ∙
[+/pattern]: replace the pattern with any string that you want to find in the text file.
[+linenum]: use the line number from where you want to start displaying the text
content.
[file_name]: name of the file containing the text that you want to display on the screen.
13. Less: less command is linux utility which can be used to read contents of text
file one page (one screen) per time. It has faster access because if file is large, it don’t
access complete file, but access it page by page.
syntax :
less filename
14. ps : ps for viewing information related with the processes on a system which
stands as abbreviation for “Process Status”
Syntax – ps [options]
15. sudo: sudo ( S uper U ser DO ) command in Linux is generally used as a prefix of some
command that only superuser are allowed to run.
16. cron: automates the scheduled task at a predetermined time. It is a daemon
process , which runs as a background process.
4
Syntax: cron [-f] [-l] [-L
loglevel]
17. chown: chown command is used to change the file Owner or group. Whenever you
want to change ownership you can use chown command.
Syntax:
chown [OPTION]… [OWNER][: [GROUP]] FILE… chown
18. chgrp : chgrp command in Linux is used to change the group ownership of a file or
directory.
Syntax:
19. ping: PING (Packet Internet Groper) command is used to check the network
connectivity between host and server/host.
20. chmod : the chmod command is used to change the access mode of a file. The
name is an abbreviation of change mode .
Syntax :
chmod [reference][operator][mode] file...
21. curl: curl is a command line tool to transfer data to or from a server, using any of the
supported protocols
Syntax:
curl [options] [URL...]
22. echo: echo command in linux is used to display line of text/string that are
passed as an argument .
23. exit: exit command in linux is used to exit the shell where it is currently
running.
It takes one more parameter as [ N] and exits the shell with a return of status N .
Syntax:
exit [n]
5
24. find: It can be used to find files and directories and perform subsequent
operations on them. It supports searching by file, folder, name, creation date,
modification date, owner and permissions.
Syntax :
25. Finger: command is a user information lookup command which gives details of all the
users logged in
Syntax:
$finger[username]
26. Free: command which displays the total amount of free space available along with
the amount of memory used and swap memory in the system, and also the buffers
used by the kernel.
Syntax:
$free [OPTION]
27. Grep: The grep filter searches a file for a particular pattern of characters, and
displays all lines that contain that pattern.
Syntax:
28. Kill: command in Linux (located in /bin/kill), is a built-in command which is used to
terminate processes manually. kill command sends a signal to a process which
terminates the process.
6
Experiment – 2
Python Program:
name = input('What is
your name?\n') print ('Hello
%s.' % name)
Output:
b) Read two numbers and print their sum, difference, product and division.
Python Program:
print("Enter which operation would you like to perform?")ch = input("Enter any of these
char for specific operation +,-,*,/: ") result =
0 if ch == '+':
7
print("Input character is not recognized!") print(num1,
Output 1: Addition
100+ 5:105
Enter which operation would you like to perform? Enter any of these char for
specific operation +,-,*,/: * 6 * 8 : 48
Enter which operation would you like to perform? Enter any of these char for
specific operation +,-,*,/: /20 5 : 4.0
Python Program:
word_count = 0 char_count
=0
8
for word in split_string: char_count +=
len(word)
Output:
characters : 16
d) Area of a given shape (rectangle, triangle and circle) reading shape and appropriate
values from standard input.
Python Program:
Output:
Python Program:
Output:
Python Program:
acircle: '))
diameter = 2 * radius
10
Combined Python Program:
if s=='1':
x=int(input("Enter length:"))
y=int(input("Enter breadth:"))
print("Area={}".format(x*y))
elif s=='2':
x=int(input("Enter base:"))
y=int(input("Enter height:"))
print("Area={}".format(0.5*x*y)) elif
print("Area={}".format(3.14*x*x))
else:
11
Experiment – 3
a) Print a name 'n' times, where name and n are read from standard input, using for
and while loops.
i=1
name(n):
if n != 0:
name(n-1)
print("Name") name(10)
Python Program:
12
n1=raw_input()
n2=raw_input()
n1=int(n1) n2=int(n2)
try:
Output:
y=int(input("Second No:"))
try:
print("x/y={}".format(x/y))
except Exception:
print("DivideByZero
Exception")
Python Program:
zz=time.asctime(time.localtime(time.time())) zz=zz[11:19]
print (zz)
13
Python Program:
for i in range(0,10):
print(datetime.datetime.now().time()) time.sleep(10)
d) Read a file line by line and print the word count of each line. Python
for i in file:
print ("print the line no=" , line , "and line is =" , i) z=i.split()
Python Program:
L = ["Welcome to India", "Delhi is the capital\n", "Have fun\n"] file1 = open('myfile.txt', 'w')
True:
count+=1 line =
file1.readline()
14
Experiment -4
GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN,
GPIO.OUT)
try: while
True:
GPIO.output(LED_PIN, GPIO.HIGH) # LED ON time.sleep(2) # Wait
for 2 seconds
GPIO.output(LED_PIN, GPIO.LOW) # LED OFF time.sleep(2) # Wait
for 2 seconds
except KeyboardInterrupt:
GPIO.cleanup()
LED_PIN_1 = 18
LED_PIN_2 = 23
SWITCH_PIN_1 = 17
SWITCH_PIN_2 = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN_1, GPIO.OUT)
GPIO.setup(LED_PIN_2, GPIO.OUT)
GPIO.setup(SWITCH_PIN_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SWITCH_PIN_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try: while
True:
input_state_1 = GPIO.input(SWITCH_PIN_1)
input_state_2 = GPIO.input(SWITCH_PIN_2) if
input_state_1 == GPIO.LOW:
15
if input_state_2 == GPIO.LOW:
GPIO.output(LED_PIN_2, GPIO.HIGH) else:
GPIO.output(LED_PIN_2, GPIO.LOW)
except KeyboardInterrupt:
GPIO.cleanup()
file:
LED_PIN = 18
GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN,
GPIO.OUT)
while True:
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(on_time)
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(off_time)
16
Experiment-5
Aim:(a) Flash an LED based on cron output (acts as an alarm)
Python Program:
import time
def blink():
while True:
off time.sleep(1)
def destroy():
try:
bink()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
17
a) Switch on a relay at a given time using cron:
Hardware Setup:
● Connect the relay module to the Raspberry Pi's GPIO pins.
● Connect the load (such as a light bulb or any other electrical appliance) to
the relay's contact terminals.
Cron Job Setup:
● Create a Python script (let's name it relay_control.py ) that will turn on the
relay at a specific time using gpiozero library.
This setup will execute the Python script at the specified time, turning on the relay, which
in turn powers the connected load.
b) Get the status of a bulb at a remote place (on the LAN) through the web:
For remote access to the bulb's status over the LAN through a web interface, you can:
Set Up a Web Server on Raspberry Pi:
● Use a Python web framework like Flask or Django to create a web server on
the Raspberry Pi.
18
Implement an Endpoint for Bulb Status:
● Within your web application, create an endpoint (URL) that returns the
status of the bulb.
● The status could be retrieved by reading the GPIO pin connected to a sensor
or a relay controlling the bulb.
Python code-
app = Flask(__name__)
# Code to read GPIO pin status and determine the bulb status # For
if __name__ == '__main__':
(c) Get the status of a bulb at a remote place (on the LAN) through web.
19