Programatically controlling a RasPi vidcam w/o a browser

For PC type software that runs under some PC opsys.

Programatically controlling a RasPi vidcam w/o a browser

Postby Doug Coulter » Sun Jan 31, 2016 12:51 pm

I needed this for my fusor remote control lashup. While I might be willing to have several browsers up to see all 3 cameras streaming during a run, it'd be nice to be able to start them all at the same time (for later time-sync, or close anyway), and have them all record for posterity (to be put into the database indexed to the run number in question).

First, you will need RPi-Cam-Web-Interface installed and working on the pi(s) in question. To save yourself some hair-pulling, install this first, as their install script tends to kind of wipe out any existing server and web content you might have on your pi already. Shame on sloppy script writers, but it's so convoluted in there, I haven't fixed it. The install will wipe out for example /etc/nginx/sites-available/default. I prefer NGINX on pi's as I don't have a life invested in learning apache configuration, and it's faster and simpler by far. I did use the /html option for where to put the camera stuff, so I'd have the rest of /var/www for my own content. For example, one of my raspis has PHPBB running on it as kind of a super notepad for my private use! All the LAN of things ones also have their own content and CGI servers. The skinny on that can be found on the board elsewhere.

You will then need Samba on the pi (and on the other end if it's linux, which I'll assume for this, since it's all I run). You will need lines something like these at the end of /etc/samba/smb.conf:
Code: Select all
[cam]
comment = camera  on pi (use its network name for clarity)
browseable = yes
path = /var/www/html
guest ok = yes
read only = no

The above alone doesn't necessarily give write permissions to a guest if they aren't allowed at the pi end. Using the pi file browser, check and adjust permissions as required. In particular, the file /var/www/html/FIFO needs to be world-writable by anybody. I of course have added a few other shares to these pis, like for /home/pi/ and a public space on a big USB stick mounted on the filesystem. Makes it easier to remote-administer the pi in question. With TightVNC, you can have a remote GUI even on an otherwise headless pi (subject of another post, but run vncserver :1 from a terminal (or ssh) in the pi so you can put in a password before putting that command in say, /etc/rc.d or it'll never fly).

It works out that /var/www/html/FIFO is indeed a fifo, and it's the one raspimjpeg reads to get commands. Now that we have samba sharing that, we can send commands just like the web interface would do, but we don't need a browser to do that anymore, and of course, with code, if we needed to do say, 3 of them at a time (as I do) - it's now easy, no navigating and clicking required.

Here is example code in perl to do one of them - record for 10 seconds, which can run from some other machine on your LAN.
Code: Select all
#!/usr/bin/perl
use warnings;
use strict;
use Filesys::SmbClient;

my $fh; # file handle
my $smb = new Filesys::SmbClient(username => "pi",password => "dotdot", workgroup => "MOUNTAIN");
print "starting\n"; # for user comfort - a sidetone
$fh = $smb->open(">smb://fusorpi/fcam/FIFO") or die "couldn't open FIFO $!";
$smb->write($fh, "ca 1") or die "couldn't write FIFO $!";  #ca 1 is the command to capture
$smb->close($fh);
print "sleeping\n"; # user comfort
sleep(10); # get ten seconds worth before we stop it
print "stop capture\n"; # stop it
$fh = $smb->open(">smb://fusorpi/fcam/FIFO") or die "couldn't open FIFO $!";
$smb->write($fh, "ca 0") or die "couldn't write FIFO $!";
$smb->close($fh);
print "done\n";

This does look quite a bit better when it's syntax-highlighted in say, gedit. Don't leave home without syntax highlighting!
remoterecord.png
Syntax highlighted. Make more sense now?



It turns out that Filesys::SmbClient isn't in the standard perl distribution (no surprise). It also happens that cpanminus and friends won't install this correctly, as samba is now a later version than they know how to look for the includes for.
And, by default, samba doesn't even install the required header files at all,much less where they are expected. The simplest workaround is to just download the archive here: http://search.cpan.org/~alian/Filesys-S ... bClient.pm and then installing it manually, which makes it ask for where some of this stuff is. But first, you have to install ibsmbclient-dev to get the header file for the library at all! I use package manager for that. I've also installed "locate" on my machines, so a simple sudo updatedb will find whatever you need to find with a subsequent locate somecrap. You'll need to know where the libsmbclient stuff is to do the module install.
The module install is the usual manual perl thing. After extracting this somewhere, get a terminal there and do
perl Makefile.PL
make
sudo make install

Which won't work unless you do the above things first...

Once you've got this working, you can get the captured video files from /var/www/html/media - if you know what they are named, right now, it's a timestamp.mp4. You could just get *.mp4, and erase that directory before starting capture, or somehow set a name (probably needs some editing in the php or java to get that). I'm still working on doing that part programatically myself, but when I get it - and I will - I'll post it in a follow up to this thread. It might just be a simple script to mount the share and do a file copy..quick and dirty works too.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Programatically controlling a RasPi vidcam w/o a browser

Postby Doug Coulter » Sun Jan 31, 2016 3:26 pm

To make recovering your remote-controlled video a little easier, you can add this code to /var/www/html/macros:
(name the file end_box.sh, it's what is called by their code when boxing is finished)
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
my $fh;
open ($fh, ">", "/home/pi/vid.txt");
print $fh "$ARGV[0]\n";
close $fh;


For this to actually work, well, you kind of have to copy it there as root...then run sudo pcmanfm on the pi, navigate there, and change the owner and group to www-data and ensure it's executable. You also have to make sure that /home/pi allows anybody to write files. You'll get a file, /home/pi/vid.txt, that has the full path of the latest video file in /var/www/html/media (including the path just named). Now you know what to copy, in case your media folder has other junk and videos in it.
A simple script can then read this file, do the copy to wherever, and delete the file. It will re-appear automatically the next time a video is available to copy, so you can use that as a check to be sure that the post-capture "boxing" from h.264 into .mp4 is done. A little plumbing here and there - and pretty soon you don't even need this dumb human to do most of the clicking anymore.

I'm sure a shell guy would just do this as a real shell script. I'm not a shell guy. If anyone out there is, it'd be cool to do it that way with an explanation, and maybe even show a nice file copy script that took this file as the source data and did a copy to "somewhere". In my case, it's going to have to be done via the SMB stuff.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Programatically controlling a RasPi vidcam w/o a browser

Postby Doug Coulter » Sun Jan 31, 2016 4:12 pm

Wrong place for this (someday I'll do something comprehensive on the pi cam software and how to fix it so it's really right), but a handy tip on the raspi cam stuff. index.php is the camera homepage. If you've done as I suggest, and used the /html directory for this (which leaves /var/www/ for your other web stuff), then it's link to "home" isn't home anymore it's back to itself - not really useful in any case at all.

Line 185 in my version looks a bit like this:
(some indentation) <a class="navbar-brand" href="#"><?php echo $CAMSTRING; ?></a>
(I may have misspelled camstring - I've already wiped it out)

Change the # to .. (for the real home one directory above) and the $CAMSTRING to "Home" and now it will go to your normal home and you won't have to hope it's gone off the back history in your browser.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to PC

Who is online

Users browsing this forum: No registered users and 1 guest