Old Phone HTML5 Security Camera SSL Via Stunnel

A picture of a phone back camera to signify LAN Camera SSL

Track An Area Using Mobile Phone Camera

An obsolete phone can be turned into a webcam using a custom application. Most smartphones come with Wi-Fi to connect to the local area network and do not need to be connected to the internet. PHP will be used for the server on the work-station and to receive the images at a specific interval. HTML5 Camera access requires an SSL Certificate. A self-signed Certificate will be created and served using stunnel.

For my use case, I raised chickens and want them to roam on the pasture when I am working indoors. I envisioned a camera setup running off a battery and solar panel and a miniature computer that uses a Wi-Fi connection to record and send photos or a video at a specific interval.

This concept can also work with tablets and notebooks. I chose to use old phones because they are smaller and use less power. Phones can charged only a few times per week while turned on for several hours per day. A phone can be mounted outside the phone has water-proof protection. Phones with motion detectors can also be used to turn on only if something in detected just outside of the target area.

Stunnel can be downloaded and installed manually or through a package manager for desktop operating systems. You can use any self-signed certificate in the stunnel configuration file. Stunnel was ran before the PHP built-in web server.

Requirements For Security Camera

Glossary:

URL

Page to open.

Wi-Fi

Wireless network protocols based on the IEEE 802.11 standards for local area networks.

WLAN

Wireless LAN is a computer network that links 2 or more devices.

Tunneling

Communication protocol which allows movement of data from one network to another.

TLS

Transport Layer Security is a cryptographic protocol for secure communication over a network.

Discontinued Devices

Recording Devices
Name Description Recording Power
Apple iPhone SE Updated to iOS 15.8.3 and Safari 18.3. Takes photos and videos on front and back cameras. Lighting, USB 2.0 10W charging
Sony Xperia XA1 Ultra Updated to Android 8.0 and lastest web browser. Takes photos and videos on front and back cameras. USB Type-C 2.0 10W charging
Name Description Example

HTML Take Photo Every 3 Seconds

<!DOCTYPE html>
<html>

<head>
	<title>Record Photos</title>
	<meta charset="utf-8" />
</head>

<body>
	<button type="button">Record</button>
	<video></video>
	<script type="text/javaScript">
		// JavaScript Goes Here
	</script>
</body>

</html>

JavaScript Take Photo Every 3 Seconds

(() => {
	let str = null;
	let iid = null;
	let vid = document.querySelector('video');
	let btn = document.querySelector('button');
	let sel = document.querySelector('select');
	navigator.mediaDevices.getUserMedia({video: true, audio: false});
	navigator.mediaDevices.enumerateDevices().then((devices) => {
		let vds = devices.filter(device => device.kind === 'videoinput');
		let opt = vds.map(vd => {
			return `<option value="${vd.deviceId}">${vd.label}</option>`;
		});
		sel.innerHTML = `<option value="">Select Camera</option>` + opt.join('');
	});
	sel.addEventListener('change', (evt) => {
		if (str !== null) {
			str.getTracks().forEach(track => {
				track.stop();
				vid.srcObject.removeTrack(track);
			});
			vid.pause();
			vid.src = '';
			str = null;
		}
		if ( sel.value !== "") {
			navigator.mediaDevices.getUserMedia({video: {deviceId: sel.value}, audio: false}).then((stream) => {
				str = stream;
				vid.srcObject = stream;
				vid.play();
			});
		}
	});
	btn.addEventListener('click', (evt) => {
		if (btn.innerHTML == 'Record') {
			btn.innerHTML = 'Recording';
			iid = setInterval(() => {
				let canvas = document.createElement('canvas');
				let context = canvas.getContext('2d');
				canvas.width = vid.videoWidth;
				canvas.height = vid.videoHeight;
				context.drawImage(vid, 0, 0, canvas.width, canvas.height);
				canvas.toBlob((blob) => {
					let file = new File([blob], 'image.png', {type: blob.type});
					let fd = new FormData();
					fd.append('image', file);
					fetch('save-video.php', {
						method: 'POST',
						body: fd
					})
					.then(response => response.json())
					.then(data => console.log(data))		  
					.catch(error => console.error(error));	  
				});
			}, 3000);

		} else {
			btn.innerHTML = 'Record';
			clearInterval(iid);
		}
	});
})();

JavaScript Refresh Photo Every 3 Seconds

// Reload Every 3 Seconds
setInterval(function(){
	let img = document.querySelector('img');
	if ( !img.src.includes('?') ) {
		img.src = `${img.src}?${Date.now()}`;
	} else {
		img.src = img.src.slice(0, img.src.indexOf('?') + 1) + Date.now();
	}			
}, 3000);

PHP Upload Photo

if ( !empty($_FILES['image']) ) {
	$file = $_FILES['image']['tmp_name'];
	$destination = dirname(__FILE__) . '/image.png';
	if (move_uploaded_file($file, $destination) ) {
		echo json_encode(['status' => 'OK']);
	}
}
die();

Stunnel Configuration

[https]
accept = 192.168.1.22:8443
connect = 192.168.1.22:8000
cert = stunnel.pem

HTML5 Phone Camera Permissions
Web Browser Phone Camera Permission Request

HTML5 Camera Video Preview And Photo Snapshot
Web Browser Camera Video Preview And Photo Taken After 3 Seconds


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can be used to input and compile JavaScript code for the HTML5 Phone Security Camera.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Live Stream

Every day, you can join a live stream and ask questions. Check Ojambo.com for details and instructions.

Learn Programming Courses:

Get the Learning JavaScript Course or the Learning PHP Course for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

OjamboShop.com Learning PHP Course
OjamboShop.com Learning PHP Interactive Online Course

Learn Programming Books:

Learning JavaScript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.

Learning PHP Book is available as Learning JavaScript Ebook.

OjamboShop.com Learning JavaScript Book Announcement
OjamboShop.com Learning JavaScript Ebook And Paperback Announcement

OjamboShop.com Learning PHP Book Announcement
OjamboShop.com Learning PHP Ebook Announcement

Conclusion:

HTML5 makes it easy to request permission to use a phone camera and create a video or photograph at specified intervals. PHP is used to upload images and make them available for display on the receiving device. Stunnel is used as a proxy to add TLS encryption for the local area network. An old device with a camera such as a smartphone, tablet, laptop or miniature computer can be used as a security camera.

If you enjoy this article, consider supporting me by purchasing one of my WordPress Ojambo.com Plugins or programming OjamboShop.com Online Courses or publications at Edward Ojambo Programming Books or become a donor here Ojambo.com Donate

References:

Leave a Reply

Your email address will not be published. Required fields are marked *