In order to use the SD card with OpenWRT it must be formatted with the EXT2 filesystem. For flash based storage devices EXT2 is preferred as it reduces the amount of writes performed, as it is not a journaled filesystem. Flash devices have a limited number of write cycles, and while this is limit is very high (millions or more) reducing the number of writes will increase the lifespan of the device. A non journaled filesystem essentially means that it does not track changes to files, which results in less writes performed.
These are the steps to format an SD card using Ubuntu:
OpenWRT configuration on MR3020
The firmware used on the MR3020 can be obtained from:
MR3020 OpenWRT 12.09 r36088 for sysupgrade
MR3020 OpenWRT 12.09 r36088 for factory upgrade
The first link is the firmware image for a sysupgrade using OpenWRT, and the second link is the firmware image that can be used with the factory firmware update.
The following steps will configure OpenWRT to use the SD card as the overlay. The overlay is where any changes to the file system are stored, as the original file system is read only.
Logitech C170 Webcam configuration for OpenWRT
The next stage is installing packages and configuring the Webcam, which is as following.
- Check to see where SD card is mounted.
root@ubuntu:~# mount
...
/dev/sdb1 on /media/sean/9016-4EF8 type vfat
- Unmount the SD card.
root@ubuntu:~# umount /dev/sdb1
- Run fdisk to remove existing partition and create new partition for EXT2.
root@ubuntu:~# fdisk /dev/sdb
Command (m for help): d
Selected partition 1
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-15407103, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15407103, default 15407103):
Using default value 15407103
Command (m for help): p
Disk /dev/sdb: 7888 MB, 7888437248 bytes
13 heads, 13 sectors/track, 91166 cylinders, total 15407104 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdb1 2048 15407103 7702528 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
- Finally create the EXT2 file-system on the new partition.
root@ubuntu:~# mkfs.ext2 /dev/sdb1
...
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
OpenWRT configuration on MR3020
The firmware used on the MR3020 can be obtained from:
MR3020 OpenWRT 12.09 r36088 for sysupgrade
MR3020 OpenWRT 12.09 r36088 for factory upgrade
The first link is the firmware image for a sysupgrade using OpenWRT, and the second link is the firmware image that can be used with the factory firmware update.
The following steps will configure OpenWRT to use the SD card as the overlay. The overlay is where any changes to the file system are stored, as the original file system is read only.
- Update opkg repositories.
opkg update
- Install the kmod-usb-storage, kmod-fs-ext4 and block-mount packages to enabled usb flash drive support.
opkg install kmod-usb-storage kmod-fs-ext4 block-mount
- Create a directory to mount the USB drive on, and mount it.yes
mkdir /mnt/sda1
mount /dev/sda1 /mnt/sda1
- Copy the contents of the existing overlay to the flash drive.
tar -C /overlay -cvf - . | tar -C /mnt/sda1 -xf -
- Edit the file /etc/config/fstab and add the following.
config mount
option target /overlay
option device /dev/sda1
option fstype ext2
option options rw,sync
option enabled 1
option enabled_fsck 0
- Reboot.
- Check to see if it has mounted correctly by looking at the free space on the root.
OpenWrt:~# df -h
Filesystem Size Used Available Use% Mounted on
rootfs 7.2G 3.3M 6.8G 0% /
/dev/root 2.0M 2.0M 0 100% /rom
tmpfs 14.3M 72.0K 14.2M 0% /tmp
tmpfs 512.0K 0 512.0K 0% /dev
/dev/sda1 7.2G 3.3M 6.8G 0% /overlay
overlayfs:/overlay 7.2G 3.3M 6.8G 0% /
- Create the swap file using dd, the count is the number of kilobyes as the blocksize (bs) is a kilobyte (1024 bytes). For a count of 65536 a 64 MB swap will be created. Note this will take some time.
dd if=/dev/zero of=/swapfile bs=1024 count=65536
- The file needs to be formatted as a swap file using mkswap.
mkswap /swapfile
- The swapfile needs to be mounted by the fstab, so edit /etc/config/fstab and add the following.
config swap
option device /swapfile
option enabled 1
- Restart fstab to mount and enable the swap file.
/etc/init.d/fstab restart
- Check the swap is working using free. You should see 65532 under the total column for the Swap row.
# free
total used free shared buffers
Mem: 29212 27856 1356 0 1216
-/+ buffers: 26640 2572
Swap: 65532 0 65532
Logitech C170 Webcam configuration for OpenWRT
The next stage is installing packages and configuring the Webcam, which is as following.
- Install the packages ffmpeg, and kmod-video-uvc, the Logitech C170 webcam uses the usb video class (uvc) drivers and ffmpeg is used to encode videos.
opkg update
opkg install ffmpeg kmod-video-uvc
- Plug the C170 in and check that the drivers work by looking at the dev directory, it should contain video0.
- "Motion" is a software package that will capture videos or images when motion has been detected from a video source (the webcam in our case). The standard motion package that was compiled for OpenWRT does not have ffmpeg support, so I compiled a custom version. This version can be obtained here. To install it on the MR3020 do the following.
wget http://228899seankelly.googlecode.com/svn/trunk/ar71xx/motion/motion_20120605-224837-2_ar71xx.ipk
opkg install motion_20120605-224837-2_ar71xx.ipk
- Motion has a configuration file in /etc/motion.conf, to capture video from the C170 webcam the following options were set.
The pixel format of the C170 webcam is MJPEG which corresponds to a value of 8 for the v4l2_palette option:
v4l2_palette 8
The size of the image to be captured is 320x240 so this is given by width and height. 320x240 lowers the strain of motion detection.
width 320
height 240
Decreasing the frame rate to less than 5 fps will also reduce the processing required. For my setup I found 3 fps was adequate to detect and record motion, I set it using the following.
framerate 3
To detect motion a threshold is required, this is the number of pixels that must change in the picture for motion to be present. For the 320x240 image size and a wide angle camera about 500 to 1000 pixels should be adequate. To set the number of pixels the threshold is set as following.
threshold 1000
De-speckling of the motion to produce better defined bounds for the motion can be turned off to further reduce the processing required. To turn of the filters that do this comment out the line below using ";"
;despeckle_filter EedDl
In order to filter out some noise the minimum number of consecutive frames with motion can be set. This stops random noise from cause a motion event. The minimum number of frames is set as following, I used 2 frames as at 3 fps this is 0.6 of second of motion is required to start a motion event.
minimum_motion_frames 2
In order to create better video captures a number of frames after and before the motion event can be saved as well to create a more fluid video. The pre_capture and post_capture variables specify the number of frames to capture before and after and event. I set both to 2 frames, as following.
pre_capture 2
post_capture 2
The goal is to use ffmpeg to encode videos so we need to disable image output by setting the following.
output_pictures off
The default bitrate for ffmpeg is high for lower resolutions so lowering it to between 200000 bps to 300000 bps should be ok, to do this change the following.
ffmpeg_bps 200000
To enable playback in a browser, ffmpeg must encode videos in a compatible format such as a flash swf. To use the swf format set the following.
ffmpeg_video_codec swf
The last setting is the directory to store the videos in, to give access to the videos from the inbuilt http server they can be stored in the /www folder using the variable as following.
target_dir /www/cam