Friday 19 August 2016

Raspberry pi Live video stream

After accessing raspberry pi remotely , ever wondered what all things can be accessed remotely?
The possibilities are endless and in this post you will get to access the live video stream remotely on your laptop.
Yes you can consider this as a project of "internet of things" where you are communicating to the webcam via internet.


Raspberry pi  can be used to stream live video from a USB webcam or a raspberry pi camera module. This concept can be useful for projects involving video streaming like a surveillance camera , drone , image processing etc.

There are 2 ways to stream video online
  • 1.       Using MJPG Video streamer  : MJPG-streamer takes JPGs from Linux-UVC compatible webcams, filesystem or other input plugins and streams them as M-JPEG via HTTP to webbrowsers, VLC and other software. It is the successor of uvc-streamer, a Linux-UVC streaming application with Pan/Tilt
  • 2.       Using motion – Which is a library file with inbuilt functions to stream , record , sense motion , etc which can be modified according to our convenience.( We will be using this method)



You will need:

  • 1.       Raspberry pi ( I have used raspberry pi 3)
  • 2.       SD Card with pre- installed raspbian
  • 3.       Usb webcam
  • 4.       Power adapter(2.5 Amps)
  • 5.       Wifi router/dongle
  • 6.       Laptop
  • 7 Wifi adapter( If you are using other versions of raspberry pi)

Pre requisites:


1.       Raspberry pi should be connected to internet
2.       Remote access via putty should be ready

Steps:


  • .       Update and upgrade your raspberry pi


                 sudo apt-get update
                 sudo apt-get upgrade
  • .       Install motion

                  sudo apt-get install motion
  • 3.       Open the file to reconfigure it


                  sudo nano /etc/motion/motion.conf
  • .       Press cntrl+w to search for keywords and press enter as it would be cumbersome to find the word in 700-800 lines of code:


·         daemon on #default off (This allows the motion to run in the background)
·         framerate 30 #default 2 (increased framerate)
·         width 640 #default 320 (changed width to match that of the webcam)
·         height 480 #default 240 (same as above but for height)
·         threshold 2000 #default 1500 (threshold value for motion detection)
·         pre_capture 2 #default 0 (captures 2 frames before motion was detected and adds that to the videos to make them smoother)
·         post_capture 5 #default 0 (same as above but captures frames after)
·         output_normal off #default on (this disables storing images, since we only require video)
·         ffmpeg_video_codec msmpeg4 #default swf (msmpeg4 is accepted by windows media player, hence easier to play on Windows)
·         target_dir /mnt/motionvideos #default /tmp/motion (changed the directory where videos will be stored)
·         webcam_maxrate 5 #default 1 (increase the max framerate on lie stream)
·         webcam_localhost off #default on (allows you to set up a live stream of the webcam)

  • If you want to store pics turn output_normal on , you can tun off ffmpeg video option if you don’t want to record video.
  • Instead of webcam _localhost and webcam_maxrate  your file may have stream_localhost and stream_maxrate respectively.
  • Press cntrl+x to exit , press y and then enter to overwrite the file
  • We will need to also create the directory we specified in targer_dir above and change the owner of that directory to motion. Use the following command to do this:

               sudo mkdir /mnt/motionvideos
               sudo chown motion /mnt/motionvideos
  • We’ll now enable daemon by using the following command:


               sudo nano /etc/default/motion
  • .           Once the file is opened, change start_motion_daemon=no to yes. Then hit Ctrl+X to close         and Y  to save (same as above). Enabling daemon also means that motion will start on boot by default.
  • .       Enter the commands to start webcam and stop it respectively

                sudo service motion start
                sudo service motion stop

  • if the output on your terminal is as:

               ubuntu@RPI:~$ sudo service motion start
              * Starting motion detection daemon motion       [ OK ]
              Then it is working properly . Otherwise type
              sudo motion.
  • .       Open a browser (chrome/firefox) . and in search bar type

          http://192.168.1.105:8081

         192.168.1.105 is the IP adress of my raspberry pi , you can get that by ifconfig in LXTerminal
          If its not working , stop the webcam , change the port
          webcam_port 8081 to 8086 in sudo nano /etc/motion/motion.conf
           again instead of webcam_ port you may have stream_port


This image was taken by webcam as it detected motion.

Adding security to web streaming:

You may need to add security to prevent other people from watching you if they are connected to same network.
Adding id and password may give you the first line of security to your web streaming.
To add security to your page , open the config file using sudo nano /etc/motion/motion.conf

Press cntrl+w to search the keyword authentication.

# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
stream_auth_method 0

# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
 stream_authentication id:password

This will be the default setting for authentication ,
Change the stream_auth_method  to 2
And add your id and password to the line-  stream_authentication id:password
Remove the semicolon in front of stream_authentication:id password

You may need to reboot the pi , login again , and run motion using sudo service motion start or sudo motion , open browser in one of your device and enter the ip address followed by port no. , you will be prompted with message for id and password.

Accessing the photos and videos:

The camera will click photos and shoot videos if it detects any movements. You can change the intensity by changing the threshold value in sudo nano /etc/motion/motion.conf
Edit  output_normal  on  in motion.conf  to click pictures
Edit  ffmpeg video on in motion.conf  to shoot videos

You may need enough space in SD card as the video occupies enough space and plenty of pictures will be clicked.
If you don’t have enough space you can test it for a small period of time and disconnect your webcam from pi and edit the motion.conf file by setting output_pictures and ffmpeg video to off.
Again if you don’t want several pictures to be clicked , you have several options in your motion.conf file:
# Output 'normal' pictures when motion is detected (default: on)
# Valid values: on, off, first, best, center
# When set to 'first', only the first picture of an event is saved.
# Picture with most motion of an event is saved when set to 'best'.
# Picture with motion nearest center of picture is saved when set to 'center'.
# Can be used as preview shot for the corresponding movie.
output_pictures off

To access your pictures type command startlxde-pi in your putty
The xming should be running in tray , only then you will be able to get the GUI of raspberry pi on your laptop screen.
Go to file explorer, type
/mnt/motionvideos
You will be directed to folder containing your image and videos


What next?

Try port forwarding concept to access your web streaming from any part of the world( i.e. outside your local network).
Currently I am working on it will be uploading its tutorial soon in this blog!



Saturday 2 April 2016

Basic model of IOT using raspberry pi A+

Hello technocrats,
In this tutorial you will get an introduction to the concept of internet of things and how this concept can be manipulated with the help of this 25$ chip .

Prerequisites for this tutorial
1. Your SD card should be pre installed  with raspbian OS.(You will get its tutorial in YouTube)
2. Raspberry pi should be connected to internet via LAN cable or WiFi adapter( if it is connected via       WiFi adapter then the adapter should be configured )
3. Knowledge of accessing raspberry pi remotely via putty.(I'll cover a bit of this concept in this             tutorial)

Requirements
1. Raspberry pi with pre-installed SD card( I have used model A+)
2. LAN cable or WiFi adapter
3. breadboard
4. LEDs (5-6)
5. 100 ohm resistors(5-6)
6. male to female jumper wires (10)

Internet of Things ( IoT)
Internet of things is a future model where every real life entities will be connected virtually via internet .





The Internet of Things (IoT) is the network of physical objects—devices, vehicles, buildings and other items—embedded  with electronics,software, sensors, and network activity that enables these objects to collect and exchange data. The IoT allows objects to be sensed and controlled remotely across existing network infrastructure, creating opportunities for more direct integration of the physical world into computer-based systems, and resulting in improved efficiency, accuracy and economic benefit; when IoT is augmented with sensors and actuators, the technology becomes an instance of the more general class of cyber-physical systems, which also encompasses technologies such as smart grids, smart homes, intelligent transportation and smart cities.

The raspberry pi is an open sourced processor having Linux OS and its ability to perform data sharing by connecting to internet via LAN cable or WiFi adapter makes it useful for IoT projects as the devices and electronic components can be easily connected to GPIO pins and manipulated remotely using python programming.

STEPS FOR BASIC IOT PROJECT (LED BLINK) 

1. Access raspberry pi remotely
    
    Remote access basically means getting the Graphical user interface of raspberry pi on your laptop       screen or mobile screen and controlling it via internet . For this to happen  your laptop /android cell     phone should be connected to the same router to which your raspberry pi is connected.
    In this project ( LED Blink) you may connect it using LAN cable or WiFi adapter .
   
    The protocol through which remote access of raspberry pi is possible is known as SSH network or          secure shell network.
     Secure Shell, or SSH, is a cryptographic (encrypted) network protocol operating at layer 7 of                the OSI Model to allow remote login and other network services to operate securely over an                   unsecured network.[1]
     SSH provides a secure channel over an unsecured network in a client - server architecture,                    connecting an SSH client application with an SSH server .Common applications include                          remote command line login and remote command execution, but any network service can be                secured with SSH.
     


  •     Router assigns dynamic IP to all the devices connected to it . We can get the IP address by softwares like Advanced IP Scanner ( For PC) and Fing ( for Android smartphone) .
  •  The Image will give you the idea of connectivity and  IP address . Usually home group network IP address starts with 192.168.**.*** .
  • Software like Xming will be helpful to create the GUI of raspberry pi . It acts as an emulator so that you can easily switch between your windows screen and  raspberry pi screen simultaneously
  • Putty ( For PC ) and connectbot ( for Android smartphone ) is a SSH Clients where you have to enter the dynamic IP of raspberry pi obtained from IP scanner applications .
  • After entering the IP address and clicking OK you will be prompted with a warning , just click OK 
  • default ID - pi , default password - raspberry . You will gain access to the terminal of raspberry pi which is basically the command line  where you enter command " startlxde-pi" for GUI
  • Command line login looks something like this
This tutorial will guide you the steps for remote access

2. GPIO Access 
    
   .Till now you should be having the screen of raspberry pi on your laptop screen after entering "startlxde-pi".
GPIO stands for general purpose input output . This  allows you to connect different electronic components and devices , Recent models of raspberry pi comes with 40 GPIO pins  The function of each pin can be referred by the following diagram .



  •  Enter the following commands in the LX - Terminal of Raspberry pi ( Command line ). You will need to connect raspberry pi to internet as the commands will download certain MB s of files
  •  sudo apt-get update (checks for updates and installs it )
  • sudo apt-get dist upgrade (checks for the upgraded version of Raspbian annd installs it)
  • sudo apt-get install python-rpi.gpio python3-rpi.gpio ( installs the gpio library files ).
Open the python shell which is the python IDE where you can write the programs through LX-Terminal . It is named as Idle .The code for opening idle through terminal ( this step is important ) is "sudo idle " or ( if it fails ) " gksudo idle ".
It will look like this .

Go to file> New . Which will open a new shell where you can write the program
  1. import RPi.GPIO as GPIO  #imports library files
  2. import time  #imports variable time
  3. # blinking function  
  4. def blink(pin):  
  5.         GPIO.output(pin,GPIO.HIGH)  
  6.         time.sleep(1)  
  7.         GPIO.output(pin,GPIO.LOW)  
  8.         time.sleep(1)  
  9.         return  
  10. # to use Raspberry Pi board pin numbers  
  11. GPIO.setmode(GPIO.BOARD)  
  12. # set up GPIO output channel  
  13. GPIO.setup(3, GPIO.OUT)  
  14. # blink GPIO17 50 times  
  15. for i in range(0,50):  
  16.         blink(3)  
  17. GPIO.cleanup() #stops the program and de-assigns the pins
  • Save the file ( cntrl+s) , Check the module for errors (alt+x) .
  • if there is no error proceed for connections
3. Connections and Program run

Connect your led and resistors ( 50 ohm or 100 ohm) as :

Finally press F5 to run the program . If it doesnt work , open terminal and write command as "sudo python programname.py " .
  programname -  Name of the led blink program that you saved in raspberry pi

Demonstration video:


4 . Modifiations
Add your own modifications like changing the program to allow the user to enter the no. of blinks and time interval of led blinks . 
Access Pi through android smartphone , in connect bot after successful login , run yor blink program same as " sudo python programname.py
This demonstration will look way more advanced . This was your first step towards IOT and by now you may have realized that sky is the limit for the possibilities of IOT  . Various other devices like motors , robots , sensors etc can be manipulated and controlled via internet .




Hope you have found this post informative.
Thank you
 

 

Sunday 10 January 2016

What is Raspberry Pi?



The Raspberry Pi is a series of credit card sized single board computers produced with the intention of promoting computer education in schools and developing countries . This 25$ chip is popular among tech-enthusiasts and programmers which can be used for various purposes,  which includes automation and embedded projects , IoT Projects , robotics projects etc. Raspberry Pi started its production in UK with its first model 1. Then it started the production of model B, B+ , A+ and recently it has launched its model 2.

Why Raspberry Pi model A+?

The model A+ is smaller and lighter as compared to other models. This chip is quite compact which is quite useful for embedded projects.



  • More GPIO pins - Model A+ has 40 GPIO pins which is quite useful as model B has only 26
  • Micro SD- old friction SD card has been replaced with new push- push micro SD card slot
  • Lower power consumption-  Reduced Power consumption to 0.5W to 1W
  •  Better audio 
  • Smaller , Neater form factor - USB connectors are aligned with broad edge , moving composite video into 3.5 mm jack , and 4 squarely placed mounting holes are added.
This Video will give you a Brief introduction of Raspberry pi model A+



Hardware and Accessories Requirements-

1. Raspberry Pi Model A+
2. 16 GB Micro SD card with adapter
3. 5 V rated up to 1 A Power adapter
4. HDMI / VGA monitor/TV
5. HDMI to VGA adapter ( if you are using VGA monitor)
6. USB Port Expander
7. USB mouse and keyboard / PS/2 Mouse and Keyboard
8. USB to PS/2 converter ( if you are using PS/2 mouse and keyboard)
9. ethernet cable or USB Wifi Adapter ( for network)
10 . USB to ethernet adapter if you are using ethernet cable for network
11. HDMI / VGA cable ( for display)


Links for  neccessary software downloads -
1 .NOOBS - (New out of box software) which is an easy operating system installer for beginners
                     https://www.raspberrypi.org/downloads/noobs/
2. SD card formatter - Required to format the SD card completely and  write the operating system                                             installer , i.e. , NOOBS.
                                    https://www.sdcard.org/downloads/formatter_4/
3. Putty - SSH client used for accessing raspberry pi's terminal in laptop
                 http://www.putty.org/
4. XMing - Used for accessing Raspberry Pi's screen on your laptop after booting OS.
                 http://sourceforge.net/projects/xming/