[Sample] Timed Security / Nature Camera

191 views
Skip to first unread message

Dave

unread,
Apr 13, 2019, 8:22:57 AM4/13/19
to DroidScript
This example could be used as the starting point for a motion detecting security camera or nature camera.  It wakes up, watches for a while, then shuts down and allows the phone to sleep (saving lots of battery), then wakes up again etc, etc...

To save maximum battery on OLED screens, all the screen controls could be removed and the camera view reduced to a tiny square so that most of the screen is black.

A background service could be used to wake up the app instead of using the app.SetAlarm method (it might be more reliable).  This service could also be used to transmit captured images to a server or to email them to interested parties.



//Initialise some variables.
var sensitivity = 50; //percent.
var minPeriod = 500;  //millisecs.
var mosaic = "3x3";
var snapFolder = "/sdcard/Snaps";
 
//Called when application is started.
function OnStart()
{  
   
//Wake up the phone.
   
//(This can be quite slow, make sure lock screen is disabled)
    app
.Unlock();
   
 
//Fix orientation to landscape since
 
//most phones cameras work this way.  
 app
.SetOrientation( "Landscape" );
   
 
//Create horizontal layout that fills the screen.
 lay
= app.CreateLayout( "Linear", "Horizontal,FillXY" );
 
 
//Create frame layout on left for camera view.
 layLeft
= app.CreateLayout( "Frame" );
 layLeft
.SetMargins( 0,0.1,0.02,0 );
 lay
.AddChild( layLeft );
   
 
//Create camera view control.
 cam
= app.CreateCameraView( 0.4, 0.8 );
 layLeft
.AddChild( cam );  


 
//Create vertical layout on right for other stuff.
 layRight
= app.CreateLayout( "Linear", "Vertical" );
 lay
.AddChild( layRight );
 
 
//Create a text control to show logs.
 txt
= app.CreateText( "", 0.5, 0.4, "Multiline,Left" );
 txt
.SetMargins( 0,0.1,0,0 );
 txt
.SetPadding( 0.01,0.01,0.01,0.01 );
 txt
.SetTextColor( "#ffffffff" );
 txt
.SetBackColor( "#ff222222" );
 layRight
.AddChild( txt );


 
//Create sensitivity seek bar label.
 txtSens
= app.CreateText( "Sensitivity" );
 txtSens
.SetMargins( 0,0.05,0,0 );
 layRight
.AddChild( txtSens );
 
 
//Create sensitivity seek bar.
 skb
= app.CreateSeekBar( 0.3, -1 );
 skb
.SetOnTouch( skb_OnTouch );
 skb
.SetValue( sensitivity );
 layRight
.AddChild( skb );
 
 
//Create mosaic spinner label.
 txtTiles
= app.CreateText( "Mosaic" );
 txtTiles
.SetMargins( 0,0.05,0,0 );
 layRight
.AddChild( txtTiles )
 
 
//Create mosaic spinner.
 
var layouts = "3x2,3x3,5x5,10x10";
 spin
= app.CreateSpinner( layouts, 0.2, -1 );
 spin
.SetOnTouch( spin_OnTouch );
 spin
.SetText( "3x3" );
 layRight
.AddChild( spin );
 
 
//Set menus.
 app
.SetMenu( "Snap,Exit" );    
 
 
//Add main layout to app.
 app
.AddLayout( lay );
 
 
//Start capture after 1 second.
 setTimeout
( "StartDetection()", 1000 );
 
 
//Shut down the app again after 10 seconds.
    setTimeout
( function(){ app.Exit() }, 10000 );
   
   
//Wake up the app again after 20 seconds.
   
var now = new Date().getTime();
    app
.SetAlarm( "Set", 1234, null, now+20000 );
   
    app
.EnableBackKey( false );
}


//Start motion detection.
function StartDetection()
{
 
//Enable 3x3 matrix motion detection and mark
 
//motion detections in the image control.
 cam
.MotionMosaic( 3, 3, (100-sensitivity)/5, minPeriod );
 cam
.SetOnMotion( OnMotion );
 
 
//Create folder for saved pictures and
 
//set image size, counters etc.
 app
.MakeFolder( snapFolder );
 cam
.SetPictureSize( 1024, 768 );
 maxPics
= 10; count = 1;
 
 
//Create array to hold log messages.
 log
= new Array();
 
Log( "Ready" );
 
 
//Start preview.
 cam
.StartPreview();
}


//Called when the back key is pressed.
function OnBack()
{              
    app
.SetAlarm( "Cancel", 1234 );
    app
.Exit();
}


//Called when motion is detected.
//(data contains an array of detection strength
//values corresponding to each mosaic tile)
function OnMotion( data )
{
 
//Show info in log.
 
Log( data );
 
 
//Snap a photo.
 
Snap();
}


//Take a picture and store to sdcard.
function Snap()
{
 
//Limit number of pictures by recycling
 
//the file names.
 
if( count >= maxPics ) count = 1;
 
else count++;
 
 
//Save picture to sdcard.
 
var file = snapFolder+"/Snap"+count+".jpg";
    cam
.TakePicture( file );
   
Log( file );
}


//Called when user touches sensitivity seek bar.
//( value ranges from 0 to 100 )
function skb_OnTouch( value )
{
 sensitivity
= value;
 
ChangeSettings();
}


//Called when user touches mosaic spinner.
function spin_OnTouch( item )
{
 mosaic
= item;
 
ChangeSettings();
}


//Change the motion detection settings.
function ChangeSettings()
{
 
var x=3, y=2;
 
if( mosaic=="3x3" ) { x = 3; y = 3; }
 
else if( mosaic=="5x5" ) { x = 5; y = 5; }
 
else if( mosaic=="10x10" ) { x = 10; y = 10; }
 
 cam
.MotionMosaic( x, y, (100-sensitivity)/5, minPeriod, img );
}


//Add messages to log.
function Log( msg )
{
 
var maxLines = txt.GetMaxLines()-1;
 
if( log.length >= maxLines ) log.shift();
 log
.push( msg + "\n" );
 txt
.SetText( log.join("") );
}



John Calder

unread,
Apr 21, 2019, 3:40:12 AM4/21/19
to DroidScript
I am experimenting with security/monitoring android phones with some of my apps based on sample app "Camera Motion". These operate at night and I have been trying to save power during the day with a variety of coding trials but I did not think of "Alarms" so thank you for this. It goes on the TODO list. What is working on one phone so far, is a continuously running app that creates "cam" (CreateCameraView) at night then destroys it in the morning. 

Maybe we can enjoy seeing "CameraView - cam.SetOnMotion(OnMotion);" catch something?

Something - stray cat? possum?(this is in NZ) triggers a security light at night, which in turn triggers the camera. 

Night-Event-2019-04-18.JPG


And a better "nature" result - a birdie! 


Birdie-Event-2019-04-18.JPG

Birdie-Image-2019-04-18.jpg



Some other info. On testing on an "Alcatel 1" with "Android 8 Go" I am seeing different behaviours for sleep and lock. These are doing more freezing of DroidScript apps than on earlier Android versions.  "Pinning" the app works in testing on my desk. The Alcatel is currently running "pinned" out in the field but it is not recording images on my server. I will be able to get to it in 2 days and find out if it has any locally recorded images. Issue: apps that run on unattended devices "in the field" need to run in such a way that the device has good defences in place if a human steals it - and that seems to be a challenge.


This is for an academic project which is open source including a server web app to cloud-store images as they happen. "XMRemoteMonitor" on "GitHub" - https://github.com/manukautech/XMRemoteMonitor

The current DroidScript client there: "XMMonitorDSL" does not have a night-timer... yet.





Dave

unread,
Apr 23, 2019, 6:47:58 AM4/23/19
to DroidScript
Hi John,

It's looking good so far... good to see you are managing to capturing some images.

Android 8 introduced a number of power saving features which play havoc with traditional services and alarms, unfortunately.  For this reason I suggest you use older Android phones if possible. This also has the added advantage that if they get stolen or damaged, then you won't be so out of pocket.   

Actually a biologist acquaintance of mine in Cambridge was very interested in doing something similar, using 'recycled' Android phones to capture tree frog activity in Madagascar.  You can put the cameras in water proof pouches and keep the phones charged using one of those large lithium ion power banks and/or a solar charger.

The cameras on the Samsung Galaxy S series are very good and you could probably pick up a Galaxy S4 (perhaps with a scratched/cracked screen) for about 30 pounds or less.

Regards
David

John Calder

unread,
Apr 24, 2019, 5:58:09 AM4/24/19
to DroidScript
The Alcatel 1 is working. The local-storage photos over Easter were good and their datetimes were all at night. Alcatel is running again tonight, it has turned itself on at 6:30pm and this time it is saving to my server. The "pinned app" option is looking good as a workable solution. A strange effect is that photos are upside down. They do look right way up while testing during setup. The Alcatel 1 is of interest because (1) its camera seems to do remarkably well in low light and (2) low measured power consumption. Other phones running DroidScript CameraView/SetOnMotion draw about 500 mA. I measure the Alcatel 1 at 340 mA. It has also had a recent price reduction here in NZ and it is down to the equivalent of 30 pounds new. Our main workhorse is the Vodafone VFD-300/320 which on special here is the equivalent of 15 pounds new. The newer 320 has Android 8 Go. I have worked out how to get the infra-red-cut filter out of these - it is in the back of the lens - so that opens up extra possibilities. The 300/320 are physically small and lightweight so they go well as robot brains including running our Big Trak Rover that we bought from DroidScript, and catching rides on model rockets. We find that buying new does save a lot of time.Rephrasing the other way round, working out the quirks and broken functions of old phones can waste a lot of time. e.g. I have put a lot of work into one old phone only to then find that it would not respond to adding a SIM card.

Photo - from Alcatel 1 watching a building site tonight: Photo is via remote monitoring.

20-52-55_Front_r180.jpg



Droidscript and Rockets as a student project: VFD-300 student-programmed with DroidScript flies as an instrument package. 

https://www.facebook.com/groups/nzrocketry/permalink/10160980281785696/


Howto convert VFD-300 to infrared

http://hitechfromlotech.blogspot.com/2019/03/smartphone-vfd-300-as-infra-red-camera.html







Reply all
Reply to author
Forward
Message has been deleted
0 new messages