Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Sensors

You're reading from   Raspberry Pi Sensors Integrate sensors into your Raspberry Pi projects and let your powerful microcomputer interact with the physical world

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781784393618
Length 192 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Rushi Gajjar Rushi Gajjar
Author Profile Icon Rushi Gajjar
Rushi Gajjar
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Meeting Your Buddy – the Raspberry Pi FREE CHAPTER 2. Meeting the World of Electronics 3. Measuring Distance Using Ultrasonic Sensors 4. Monitoring the Atmosphere Using Sensors 5. Using an ADC to Interface any Analog Sensor with the Raspberry Pi 6. Uploading Data Online – Spreadsheets, Mobile, and E-mails 7. Creating an Image Sensor Using a Camera and OpenCV A. Shopping List Index

A crash course on Linux

Many authors and books will teach you the concepts of the Linux operating system, so I will just quickly introduce Linux here. You can refer to Beginning Linux Programming 4th Edition, Wrox Publishing, written by Neil Matthew and Richard Stones. For shell scripting, you can refer to Linux Shell Scripting Cookbook Second Edition, Packt Publishing, written by Shantanu Tushar and Sarath Lakshman. This operating system is mostly known for its non-user-friendliness to beginners! When users start using Linux, they often wonder, "Why is this operating system widely used and famous?" Linux is the biggest open source platform for hobbyists like us, and it allows us to modify the kernel of the operating system the way we want. Some advantages of using Linux include (and are certainly not restricted to) being free, stable, quick, and dependable under the General Public License (GPL).

You can build your own personalized operating system using Linux kernel distributions, for example, a Raspbian developed on the Debian flavor of Linux. We need our customized operating systems because if we develop our personalized hardware, we know how it should be programmed and we develop the drivers according to our needs on top of the Linux kernel. We will now go through the most powerful tool in any Linux operating system—the terminal.

The terminal and shell

The most important tool of the Linux operating system is the terminal—the CLI of Linux. Windows users may have already come across Command Prompt (as we used in the previous section) and Mac OS X users may be familiar with the terminal. Once you learn the commands of the terminal, the Linux world opens up to you. Using the terminal, you can easily interact with the operating system and its kernel, which indirectly connects you and enables you to access the hardware resources. Shell is a command language translator (or interpreter) that executes command input from user. Shell uses the Linux kernel to execute commands.

In the terminal, you can use many shells. One of the most common shells is called Bourne-Again Shell (Bash). Unless you get into the complicated programming of shell, which is also known as shell scripting, you may not feel the strengths and weaknesses of a particular shell. Most of the simple commands remain the same for different shells. Shell scripting is useful when you want to do postprocessing on your files in the same or different directories, modify the usual commands of Linux by your own way, or just print or execute a program. Shell scripts allow several commands that would otherwise be entered manually in a command-line interface to be executed automatically, without waiting for a user to trigger each stage of the sequence. This allows us to create the preconfigured file to execute the C programs for our sensors in the upcoming projects, which can really save our time while creating the projects.

Note

In the RasPi, LXTerminal is the tool that is ultimately the terminal for RasPi. If you are using PuTTY instead of the desktop, PuTTY CLI is the terminal, ultimately!

Useful and frequently used Linux commands

Well, this can be a very long list if we introduce and explain all the commands of Linux. Even a separate book twice this size is not enough to completely illustrate all the functionalities of the commands. We will see the commands that are essential and will be used throughout this book. This list can be used as a reference, and it's necessary to understand these commands:

  • pi@raspberrypi~$: It's now time to introduce the most commonly seen command. It welcomes you on the first login and every subsequent login to your RasPi. This shows your username and the hostname of the RasPi. Here, the username is pi and the hostname is raspberrypi.
  • sudo: This is an abbreviation of Superuser DO. This command gives you all privileges of the superuser (root, the most powerful user) of Linux. It is used in concatenation with other commands such as nano, su, chmod, and so on. By writing the sudo su command, you can enter superuser mode, in which you can execute, delete, and create any kind of files in any folders. This really gives you superpowers in Linux!

    Tip

    The sudo command can be dangerous if not used properly. It can be used to hack into the Linux systems, or this superpower can allow you to delete the entire kernel of Linux; keep in mind that next time, the PC won't boot!

  • man: This is the command that shows the manual of the Linux commands and different other function definitions. Type man sudo and you will get all the details related to the sudo command.
  • pwd: This is an abbreviation of the present working directory. This shows you the current directory you are working in. Type pwd and press the Enter key. This should display something like /home/pi.
  • ls: This is a command used to list the files or search for some files contained in a particular directory. Just typing ls and pressing the Enter key will give you a list of all files contained in the system. The options with ls are -a, -l, and so on. Just type man ls followed by the Enter key to see the different options available with it.
  • cd: This command stands for change directory. Just give a path followed by the cd command and you will be taken to that directory. For example, cd /home/pi/python_games moves you directly to the python_games folder, while cd .. takes one step out of a particular directory.
  • apt-get: This is the package manager for any Debian-based Linux distribution. It allows you to install and manage new software packages on your RasPi. Once you have an Internet connection on the RasPi, type sudo apt-get install <package-name>. It will first download the package and then install the same package. To remove a package, just use sudo apt-get remove <package-name>. To update and upgrade the operating system, you can use sudo apt-get update and sudo apt-get upgrade respectively.
  • cp: This is used to copy the file from one directory to another, for example, cp /home/pi/python_games/gem1.png /home/pi/gem1.png will copy the gem1.png file to the /home/pi folder from /home/pi/python_games. You can use the mv command instead of cp to move the file from one folder to another.
  • rm: This removes the specified file (or directory when rmdir is used). For example, rm gem1.png.

    Note

    Here's an important warning: files deleted in this way are generally not restorable.

  • cat: This lists the content of the file; for example, cat example.sh will display the content of the example.sh file.
  • mkdir: This creates the new directory in the present working directory; for example, mkdir packt will create a directory named packt in the present working directory. Just use the ls command to check whether it has been created or not by checking the list.
  • startx: This command provides RasPi users with the user interface for running a window session.
  • sudo shutdown -h: This leads to terminate all the processes on the RasPi, whereas sudo halt stops the CPU from running mode and halts the OS. The sudo poweroff command safely turns off the RasPi module.

These are the most frequently used commands for the RasPi. If more are needed in your projects, you will be introduced to them where relevant.

Let's create our first shell file:

  1. Type sudo nano example.sh in the CLI of your RasPi (you can use PuTTY or the terminal on your PC and connect the RasPi through Ethernet connection with your PC). Just type the following code in the nano text editor:
    echo hello world
    echo this is my first shell program
  2. Press Ctrl + X to exit and press Y to confirm the exit while also saving the file. The echo command simply displays the text on the screen of the terminal when executed; this is similar to the printf command in C, but is really simple compared to it, right?
  3. Now enter the sudo chmod +x example.sh command in the terminal to provide execution permissions on the example.sh file.
  4. Execute the shell program by just typing ./example.sh (./ means a dot followed by a forward slash, which makes the shell execute the filename that is after the forward slash).

Notice that this is very short introduction to shell, and now you will learn the useful commands that will be used throughout the book to create the projects.

You have been reading a chapter from
Raspberry Pi Sensors
Published in: Apr 2015
Publisher:
ISBN-13: 9781784393618
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime