Sunday, 6 November 2016

How to set date and time in your Raspberry Pi

Background

If you have internet connection in your Raspberry Pi then all you need to do is set your timezone and you are all set. In this post we will see how.

How to set date and time in your Raspberry Pi

  • First open your raspi-config. To do that execute following command in your command line - 
    • sudo raspi-config
  • Once the config open select "Internationalization Options"
  • Next select "Change Timezone"

  • Next select your "Geographic Area"
  • Finally Select your "Time zone"
  • Save and Exit


And that's it your date and time should be set.

How to take screenshots in Raspberry Pi

How to take screenshots in Raspberry Pi

Raspberry Pi does not seem to have a default screenshot application like screenshot or snip. I will explain what worked for me. I am using scrot command line utility to take the screenshots.

To install scrot execute following command -
  • sudo apt-get install scrot

As you can see from screenshot above you can enter
  • scrot -s
and select the window you want to take screenshot of. If you want to take a complete screenshot with with some delay you can do -
  • sudo scrot -d5
You even have a -c option to show a countdown. You can see various options available with man command -
  • man scrot


PS : This whole post is written on my Raspberry Pi 3 Model B and have used scrot command for above screenshots :)

Related Links

Saturday, 5 November 2016

Getting statrted with Raspberry pi

Background

The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries. More on Wiki.

So you can have your own computer with very little prior knowledge up and running in a very less time. And I am going to show you how.

Requirements

You need following things -
  • Raspberry pi (Link points to Raspberry pi 3 model b which I currently have)
  • A case to protect your pie (optional)
  • Monitor for external display
  • HDMI cable to connect your pi to the external display
  • A micro SD card to install your operating system
  • USB keyboard
  • USB mouse
  • micro usb charger to power your pi
  • Ethernet or lan cable if you want to connect your laptop to your pie (optional/you can use wifi)
This is how the pi looks like -

 Use cases for this are limitless. If you want to know what each component is and what does it do then refer the shot video I have created below -





Assemble and Deploy

  • Make sure you have all the components listed in the section above.
  • First thing you need to do is flash your micro SD card with some operating system. I am gong to use RASPBIAN but not directly. 
    • Go to https://www.raspberrypi.org/downloads/
    • Download NOOBS. It is New out of the box software. It will be a zip file.
    • Format your micro SD card
    • Copy the contents of the extracted zip file in your formatted micro sd card. Note content of the NOOBS folder must be copied not the top level directory. So there will be multiple files at top level.
    • Once you do that your micro sd card is all set up.
  • Next attach your micro SD card to your pi.
  • Connect USB keyboard and mouse to the pi.
  • Connect HDMI cable to PI and other end to your display.
  • Finally connect power supply. Once you connect power supply you should be able to see NOOBS getting loaded. It will promt you to install RASPBIAN which will take time to install.
  • Once the installation is complete your RASPBIAN will boot and you are all set to go.
Once it boots up it's same as any other computer you own. You can do all sorts of stuff.



Incase you missed in the youtube video above and wifi configuration point in deployment steps I would like to stress here that Raspberry pi 3 model b has inbuilt wifi and bluetooth module.

Connecting to your Pi


  • You can SSH into your machine.
  • SSH service is turned on bu default.


NOTE : Username is pi and password is raspberry.

  • You can do vnc too to remotely control your pi. Prior to that you need to enable it in the configuration. To do so go in
    • Menu -> Preferences -> Raspberry Pi configuration -> Interfaces
  • Here you will see lot of interfaces. Here SSH will already be enabled. You can enable vnc too.
  • Now from  your remote machine connect to your pi with vnc client.



References



  • You can open up raspi-config using command sudo raspi-config .You can use this to set varous thing like audi output to audio jack or hdmi.

Related Links


Sunday, 30 October 2016

How to Un-mount Android SD Card Before Removing it

Background

Many of us simply remove USB drive from the port without unmounting it which is wrong! But we still do. Perhaps in hurry or we just do not care. And there are a lot of memes around it -




In repeat it is wrong to do so! You might have seem prompt similar to below -


Well it's the OS way of warning you. Same goes for Android as well. After all it uses a Linux kernel.


 Lets understand why we should not be doing that.

Why you should not remove a USB drive without unmounting it

You might have already heard the answer - data loss, data corruption etc. Lets understand why this happens. 

  1. Normal case some read/write operation might be in progress and you simply disconnect the drive.  This will corrupt your data.
  2. Also most of the operating systems use caching feature which means OS holds on to the operations to be performed on external drive till a minimum threshold of operations is reached post which it execute all those operation. This is basically for performance. When you eject your drive it basically instructs OS to flush the cache and complete any pending operations. Now if you eject the disk and operations are not flushed your data is lost.

So always eject/unmount your drive before removing it. Same for Android as well. You can go to
  • Settings -> Storage -> Unmount SD card
Don't just remove it while your phone is on. There might be apps on your SD card, images or many other thing that might be in use. This also means you don't have to turn off your phone to remove SD card. Simply unmount it.  Adding screenshots below for better understanding -








It may take some time to unmount your SD card. Once it is unmounted you can plug it out. Also no restarts needed. Hope this helps!

Saturday, 15 October 2016

Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

Background

If you are a regular Linux user then you might already know of this or atleast seen these your shell scripts. For eg.
  • ls -a 2>/dev/null
These are called redirection. There are various other functions for redirections and in this post we are going to see and understand them.


File descriptors

In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS (somewhere in kernel). These entries are represented by integers like (...100, 101, 102....). This entry number is the file descriptor. So it is just an integer number that uniquely represents an opened file in operating system. If your process opens 10 files then your Process table will have 10 entries for file descriptors.


File descriptors  0,1 and 2 are generally given to std input,  std output and std error streams for a process. So you cannot generally use them for anything else. 
At the file descriptor level, 
  • stdin is defined to be file descriptor 0
  • stdout is defined to be file descriptor 1, and 
  • stderr is defined to be file descriptor 2.



 NOTE : The FD (File descriptor) table in per process table. So 2 different process can have same FD on calling open(). This is abstraction on user side. On kernel side of things each opened file has a unique FD and two open files cannot have same FD.  Please don't get confused.

Now that we know standard file descriptors created for each table lets see the redirection functions.

Redirection Basics

The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.

  •  > file redirects stdout to file
  • 1> file redirects stdout to file
  • 2> file redirects stderr to file
  • &> file redirects stdout and stderr to file

NOTE : If you don't specify a number then the standard output stream is assumed but you can also redirect errors

NOTE :  As mentioned before you can use >> instead of > to append . 

Finally 

/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output. 

Long story short - If you want any stream ignored redirect it to /dev/null

Now that we know the basics lets understand the functions
  • 2>/dev/null : This means redirect stderr of the process to /dev/null which essentially mean discard it.
  • 2>&- : This will essentially close the output of stderr for a process.
  • 2>&1: This combines stdout and stderr into single stream.
  • |& : This is just an abbreviation for 2>&1 | (Added in bash4)
  • &>/dev/null : This is an abbreviation for > /dev/null 2>&1 which essentially means combine stdout and stderr and redirect it to /dev/null (discard it)
  • >/dev/null : This is again an abbreviation for 1 > /dev/null which means discard standard output. Incase you missed my point sometime back if you do not give a FD standard output stream is assumed.


Have written a script that attempts to recover accidentally deleted files that makes use of redirections -

 Related Links



t> UA-39527780-1 back to top