Author Archives: admin

Raspberry Pi Scoreboard

Hello,

A few years ago I made a Raspberry Pi air hockey scoreboard as my electronics broke.  So out came my coding skillz.  I’m posting the link to the source and mp3 files for reference.  I hope you enjoy them as much we did.  🙂

Scoreboard.tgz

Raspberry Pi Trail Camera Code

index.php

<!DOCTYPE html>
<head>
<title>Raspberry Pi Trail Camera</title>
</head>
<body>
<h1 style="text-align:center">Live Raspberry Pi Trail Camera</h1>
<p align="center"><iframe width=680 height=520 frameborder="0" scrolling="no" src="http://<?php echo $_SERVER['SERVER_ADDR']; ?>:8081""></iframe></p>
<?php
#error_reporting(E_ALL);
#ini_set('display_errors', 1);

define('DIR_SORT_NAME',  1);
define('DIR_SORT_SIZE',  2);
define('DIR_SORT_ATIME', 3);
define('DIR_SORT_MTIME', 4);
define('DIR_SORT_CTIME', 5);

function readdir_sorted_array ($dir, $sortCol = DIR_SORT_NAME, $sortDir = SORT_ASC) {

  // Validate arguments
  $dir = rtrim(str_replace('\\', '/', $dir), '/');
  $sortCol = (int) ($sortCol >= 1 && $sortCol <= 5) ? $sortCol : 1;
  $sortDir = ($sortDir == SORT_DESC) ? SORT_DESC : SORT_ASC;
  $name = $size = $aTime = $mTime = $cTime = $table = array();

  // Open the directory, return FALSE if we can't
  if (!is_dir($dir) || (!$dp = opendir($dir))) return FALSE;

  // Fetch a list of files in the directory and get stats
  for ($i = 0; ($file = readdir($dp)) !== FALSE; $i++) {
    if (!in_array($file, array('.','..'))) {
      $path = "$dir/$file";
      $row = array('name'=>$file,'size'=>filesize($path),'atime'=>fileatime($path),'mtime'=>filemtime($path),'ctime'=>filectime($path));
      $name[$i] = $row['name'];
      $size[$i] = $row['size'];
      $aTime[$i] = $row['atime'];
      $mTime[$i] = $row['mtime'];
      $cTime[$i] = $row['ctime'];
      $table[$i] = $row;
    }
  }

  // Sort the results
  switch ($sortCol) {
    case DIR_SORT_NAME:
      array_multisort($name, $sortDir, $table);
      break;
    case DIR_SORT_SIZE:
      array_multisort($size, $sortDir, $name, SORT_ASC, $table);
      break;
    case DIR_SORT_ATIME:
      array_multisort($aTime, $sortDir, $name, SORT_ASC, $table);
      break;
    case DIR_SORT_MTIME:
      array_multisort($mTime, $sortDir, $name, SORT_ASC, $table);
      break;
    case DIR_SORT_CTIME:
      array_multisort($cTime, $sortDir, $name, SORT_ASC, $table);
      break;
  }

  // Return the result
  return $table;

}

function getDuration($dir, $file)
{
  $movie = $dir . $file;
  $mov = new ffmpeg_movie($movie);
  $duration = number_format($mov->getDuration(), 2, '.', '');
  return "$duration Seconds";
}
 
$dir    = '/home/pi/motion/';
$files = readdir_sorted_array($dir, DIR_SORT_CTIME, SORT_DESC);
#$allfiles = glob("$dir/*");
#$files = usort($allfiles, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
if (isset($_GET['page']))
{
  $page = $_GET['page'];
} else {
  $page = 0;
}

$fileCount = glob("$dir*.avi");
$pageSize = 20;
$endPage = floor(count($fileCount)/$pageSize);
echo "<h1><table width='100%' border='0'>\n";
echo "  <tr>\n";
echo "    <td colspan='2'>\n";
echo "<h1 align='center'>Page " . ($page + 1) . " of " . ($endPage + 1) . "</h1>";
echo "    </td>\n";
echo "  </tr>\n";
echo "  <tr>\n";
echo "    <td>\n";
echo "    <a href='?page=0' alt='First Page'><<</a>\n";
if (($page - 1) > 0)
{
echo "    <a href='?page=" . ($page - 1) . "' alt='Next Page'><</a>\n";
}
echo "    </td>\n";
echo "    <td align='right'>\n";
if (($page + 1) <= $endPage)
{
echo "    <a href='?page=" . ($page + 1) . "' alt='Previous Page'>></a>\n";
}
echo "    <a href='?page=$endPage' alt='Last Page'>>></a>\n";
echo "    </td>\n";
echo "  </tr>\n";
echo "</table></h1>\n";

$i = 0;
$j = 0;
$k = 0;
$start = $page * $pageSize;
$stop = ($page+1) * $pageSize;
echo "  <table width='100%'>\n";
foreach ($files as $file)
{
  if ( strpos($file['name'], 'avi') !== false && $k <= $stop) 
  {
    if ( $k >= $start && $file['size'] > 0)
    {
      if ( $j % 5 == 0)
      {
        echo "    <tr>\n";
      }
      $avi = "avi.php?name=$file[name]";
      echo "      ";
      echo "<td>";
      echo "<table border='1'>";
      echo "<tr>";
      echo "<td colspan='2'>";
      echo "<p><a href='$avi'><img src='img.php?name=" . $file['name'] . "&dir=" . $dir . "' alt='" . $file['name'] . "' /></a></p>";
      echo "</td>";
      echo "</tr>";

      echo "<tr>";
      echo "<td>";
      echo "<p>Index</p>";
      echo "</td>";
      echo "<td>";
      echo "<p>$j</p>";
      echo "</td>";
      echo "</tr>";

      echo "<tr>";
      echo "<tr>";
      echo "<td>";
      echo "<p>Name</p>";
      echo "</td>";
      echo "<td>";
      echo "<p>$file[name]</p>";
      echo "</td>";
      echo "</tr>";

      echo "<tr>";
      echo "<td>";
      echo "<p>Size</p>";
      echo "</td>";
      echo "<td>";
      #echo "<p>$file[size]</p>";
      echo "<p>" . getDuration($dir, $file['name']) . "</p>";
      echo "</td>";
      echo "</tr>";
  
      echo "<tr>";
      echo "<td>";
      echo "<p>Date</p>";
      echo "</td>";
      echo "<td>";
      echo "<p>" . date('m/d/Y h:i:s A',$file['mtime']) . "</p>";
      echo "</td>";
      echo "</tr>";
      echo "</table>";
      echo "</td>";
      echo "\n";
      $j++;
      if ( $j % 5 == 0)
      {
        echo "    </tr>\n";
      }
    }
    $k++; 
  }
  if ( $k > $stop) 
  {  
    break;
  }
  $i++;
}
echo "</table>";
echo "\n";
?>
</body>


img.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$name = $_GET['name'];
$dir = $_GET['dir'];
$movie = '/home/pi/motion/'.$name;
$thumbname = str_replace("avi", "png", $name);
$thumbnail = "thumbs/$thumbname";

function resizeImage($image, $newImage)
{
  $thumb = new Imagick($image);

  $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
  $thumb->writeImage($newImage);

  $thumb->destroy();
}

#echo "<p>thumbnail = $thumbnail</p>";
if (file_exists($thumbnail) == false) {
  $mov = new ffmpeg_movie($movie);
  $frameCount = $mov->getFrameCount();
  if ($frameCount <= 3)
  {
    $frame = $mov->getFrame(1);
    #Want to get last *.jpg that has the same name as this avi
    $thumbname = str_replace(".avi", "*.jpg", "$dir$name");
    $newImage = glob("$thumbname")[0];
    resizeImage($newImage, $thumbnail);
  } else {
    $frame = $mov->getFrame(10);
    if ($frame) {
      $gd_image = $frame->toGDImage();
      if ($gd_image) {
        imagepng($gd_image, $thumbnail);
        imagedestroy($gd_image);
        resizeImage($thumbnail, $thumbnail);
      }
    } else {
      #Need to cache the last jpg image here as well
      $thumbname = str_replace(".avi", "*.jpg", "$dir$name");
      $newImage = glob("$thumbname")[0];
      resizeImage($newImage, $thumbnail);
    }
  }
}
header('content-type: image/png');
header('Cache-Control: public, max-age=99936000');
header('content-disposition: inline; filename="'.$thumbnail.'";');
readfile($thumbnail);

?>

avi.php

<?php
    $name = $_GET['name'];
    $mimes = array
    (
        'avi' => 'media/specials.avi',
    );

    $ext = strtolower(end(explode('.', $name)));

    $file = '/home/pi/motion/'.$name;
    header('content-type: '. $mimes[$ext]);
    header('content-disposition: inline; filename="'.$name.'";');
    readfile($file);
?>

Raspberry Pi – error 110 transferring data

Hi,

I have a 5volt, 1 Amp usb power adaptor and a working model B raspberry pi.  All of a sudden it started showing the, “error 110 transferring data” problem.  After trolling for days I came across this solution from github’s forums.

What I had to do was remove the sd card (a Sandisk class 10, 16gig) from the pi and insert it in to my  Linux box.   Mount the /boot and  / partitions.  Then edit /boot/cmdline.txt and modify, “elevator=noop sdhci-bcm2708.missing_status=0 sdhci-bcm2708.sync_after_dma=0”

Then edit /etc/fstab and append nodiratime like this:

defaults,noatime,nodiratime for /

Then safely remove the sd card.

 

That fixed it for me.  Super thanks to remsnet for the fix! 🙂

How to build and install OpenCV 2.4.9 on Raspberry Pi

Hi,

Robert Castle Consulting provided an excellent tutorial on how to install earlier version of Open CV on a raspberry pi. I’m just sharing my experience with installing version 2.4.9 on a Raspberry Pi. Most of what you see here was provided by him except for the OpenCV build options.

Update

Make sure Raspbian is up to date:

sudo apt-get update
sudo apt-get upgrade

Install dependencies

First do this:

sudo apt-get -y install build-essential cmake cmake-curses-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev

you can add in cmake-qt-gui if you want a GUI for cmake, and don’t like ccmake.

Then do

sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev

You don’t need the lib1394 libraries as there is no FireWire on the Raspberry Pi, but something in this list will grab them anyway (sigh). We install in two stages as there is a possibility of broken package errors if the install order is wrong. It may be fine, but why risk having to fix it.

Install OpenCV

git clone https://github.com/Itseez/opencv.git

Unzip and prepare for build

unzip opencv-2.4.9.zip
cd opencv-2.4.9
mkdir release
cd release
ccmake ../

Configuring

press ‘c’ to configure
once done toggle the options you want. These were 2.4.9 options (note this is the ONLY difference between Robert Castle Consulting and mine) :

 

ANT_EXECUTABLE                          ANT_EXECUTABLE-NOTFOUND
BUILD_CUDA_STUBS                        OFF                 
BUILD_DOCS                              ON                  
BUILD_EXAMPLES                          ON                  
BUILD_JASPER                            ON                  
BUILD_JPEG                              ON                  
BUILD_OPENEXR                           ON                  
BUILD_PACKAGE                           ON                  
BUILD_PERF_TESTS                        ON                  
BUILD_PNG                               ON                  
BUILD_SHARED_LIBS                       ON                  
BUILD_TBB                               OFF                 
BUILD_TESTS                             ON                  
BUILD_TIFF                              ON                  
BUILD_WITH_DEBUG_INFO                   ON                  
BUILD_ZLIB                              ON                  
BUILD_opencv_apps                       ON                  
BUILD_opencv_calib3d                    ON                  
BUILD_opencv_core                       ON                  
BUILD_opencv_cuda                       OFF                 
BUILD_opencv_cudaarithm                 OFF                 
BUILD_opencv_cudabgsegm                 OFF                 
BUILD_opencv_cudacodec                  OFF                 
BUILD_opencv_cudafeatures2d             OFF                 
BUILD_opencv_cudafilters                OFF                 
BUILD_opencv_cudaimgproc                OFF                 
BUILD_opencv_cudaoptflow                OFF                 
BUILD_opencv_cudastereo                 OFF                 
BUILD_opencv_cudawarping                OFF                 
BUILD_opencv_features2d                 ON                  
BUILD_opencv_flann                      ON                  
BUILD_opencv_highgui                    ON                  
BUILD_opencv_imgproc                    ON                  
BUILD_opencv_ml                         ON                  
BUILD_opencv_nonfree                    ON                  
BUILD_opencv_objdetect                  ON                  
BUILD_opencv_optim                      ON                  
BUILD_opencv_photo                      ON                  
BUILD_opencv_python                     ON                  
BUILD_opencv_shape                      ON                  
BUILD_opencv_stitching                  ON                  
BUILD_opencv_superres                   ON                  
BUILD_opencv_ts                         ON                  
BUILD_opencv_video                      ON                  
BUILD_opencv_videostab                  ON                  
BUILD_opencv_world                      OFF                 
CLAMDBLAS_INCLUDE_DIR                   CLAMDBLAS_INCLUDE_DIR-NOTFOUND
CLAMDBLAS_ROOT_DIR                      CLAMDBLAS_ROOT_DIR-NOTFOUND
CLAMDFFT_INCLUDE_DIR                    CLAMDFFT_INCLUDE_DIR-NOTFOUND
CLAMDFFT_ROOT_DIR                       CLAMDFFT_ROOT_DIR-NOTFOUND
CMAKE_BUILD_TYPE:STRING                                     
CMAKE_CONFIGURATION_TYPES:STRING        Debug;Release       
CMAKE_CXX_COMPILER                      /usr/bin/c++        
CMAKE_C_COMPILER                        /usr/bin/gcc        
CMAKE_INSTALL_PREFIX                    /usr/local          
CUDA_BUILD_CUBIN                        OFF                 
CUDA_BUILD_EMULATION                    OFF                 
CUDA_HOST_COMPILER                      /usr/bin/gcc        
CUDA_SDK_ROOT_DIR                       CUDA_SDK_ROOT_DIR-NOTFOUND
CUDA_SEPARABLE_COMPILATION              OFF                 
CUDA_TOOLKIT_ROOT_DIR                   CUDA_TOOLKIT_ROOT_DIR-NOTFOUND
CUDA_VERBOSE_BUILD                      OFF                 
EIGEN_INCLUDE_PATH                      /usr/include/eigen3 
ENABLE_COVERAGE                         OFF                 
ENABLE_NEON                             OFF                 
ENABLE_NOISY_WARNINGS                   OFF                 
ENABLE_OMIT_FRAME_POINTER               ON                  
ENABLE_PRECOMPILED_HEADERS              ON                  
ENABLE_PROFILING                        OFF                 
ENABLE_SOLUTION_FOLDERS                 OFF                 
ENABLE_VFPV3                            OFF                 
EXECUTABLE_OUTPUT_PATH                  /home/pi/Downloads/opencv/release/bin
GIGEAPI_INCLUDE_PATH                    GIGEAPI_INCLUDE_PATH-NOTFOUND
GIGEAPI_LIBRARIES                       GIGEAPI_LIBRARIES-NOTFOUND
INSTALL_CREATE_DISTRIB                  OFF                 
INSTALL_C_EXAMPLES                      OFF                 
INSTALL_PYTHON_EXAMPLES                 OFF                 
INSTALL_TESTS                           OFF                 
INSTALL_TO_MANGLED_PATHS                OFF                 
OPENCV_CONFIG_FILE_INCLUDE_DIR          /home/pi/Downloads/opencv/release
OPENCV_EXTRA_MODULES_PATH                                   
OPENCV_WARNINGS_ARE_ERRORS              OFF                 
OPENEXR_INCLUDE_PATH                    OPENEXR_INCLUDE_PATH-NOTFOUND
PLANTUML                                PLANTUML-NOTFOUND   
PVAPI_INCLUDE_PATH                      PVAPI_INCLUDE_PATH-NOTFOUND
PYTHON_NUMPY_INCLUDE_DIRS               /usr/lib/pymodules/python2.7/numpy/core/include
PYTHON_PACKAGES_PATH                    lib/python2.7/dist-packages
SPHINX_BUILD                            SPHINX_BUILD-NOTFOUND
VTK_DIR                                 VTK_DIR-NOTFOUND    
WEBP_INCLUDE_DIR                        WEBP_INCLUDE_DIR-NOTFOUND
WITH_1394                               OFF                 
WITH_CLP                                OFF                 
WITH_CUBLAS                             OFF                 
WITH_CUDA                               OFF                 
WITH_CUFFT                              OFF                 
WITH_EIGEN                              ON                  
WITH_FFMPEG                             ON                  
WITH_GIGEAPI                            OFF                 
WITH_GSTREAMER                          ON                  
WITH_GSTREAMER_0_10                     ON                  
WITH_GTK                                ON                  
WITH_GTK_2_X                            ON                  
WITH_IPP                                OFF                 
WITH_JASPER                             ON                  
WITH_JPEG                               ON                  
WITH_LIBV4L                             ON                  
WITH_NVCUVID                            OFF                 
WITH_OPENCL                             OFF                  
WITH_OPENCLAMDBLAS                      ON                  
WITH_OPENCLAMDFFT                       ON                  
WITH_OPENEXR                            ON                  
WITH_OPENGL                             ON                  
WITH_OPENMP                             OFF                 
WITH_OPENNI                             OFF                 
WITH_PNG                                ON                  
WITH_PVAPI                              ON                  
WITH_QT                                 OFF                 
WITH_TBB                                OFF                 
WITH_TIFF                               ON                  
WITH_UNICAP                             OFF                 
WITH_V4L                                ON                  
WITH_VTK                                ON                  
WITH_WEBP                               ON                  
WITH_XIMEA                              OFF                 
WITH_XINE                               OFF                 

press ‘c’ again to configure with your new settings
press ‘g’ to generate the Makefile

And finally, build. This will take a long time (about 10 hours!).

make
sudo make install