Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Zeeshan
Published

IoT Car

This project was done to create an understanding of IoT and how one can step into it.

IntermediateFull instructions provided5 hours5,006
IoT Car

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
Battery Bank 5V
×1
Cell Compartment
×1
DIY Car Kit
×1

Software apps and online services

Notepad ++
Raspberry Pi Raspbian Stretch Lite

Story

Read more

Schematics

L298N to Raspberry Pi

This is basic schematic. Make sure you use same GPIOs as mentioned in code

Code

index.php

PHP
<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8" />
        <title>Raspberry Pi Vehicle</title>
    </head>
	
    <body style="background-color: white;">
		<?php
			$val_array = array(1,2,3,4);
			echo ("<img id='st_sp' src='data/start_stop.png' style='position:absolute; top:20px; left:20px; width:170px; height:150px' onclick='engine()'/>");			
		?>
		<div><img id='indi' src='data/red_led.png' style='position:absolute; top:20px; left:200px; width:20px; height:20px'/></div>
		
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script type="text/javascript">
			var status=0;
			function engine() {
				if(status==0){
					document.getElementById("indi").src="data/green_led.png"
					status=1;
				}
				else if(status==1){
					document.getElementById("indi").src="data/red_led.png"
					status=0;
				}
			}
			$(document).ready(function(){				
				$(window).keydown(function (e) {
				$k_code=e.keyCode;
				$k_char=String.fromCharCode(e.keyCode);
				var pin = <?php echo $val_array[0]; ?>;
				if( status==1 ){
					if($k_code==37){ //left key
						call_php(pin);
					}
					else if($k_code==38){ //up key
						call_php(pin);
					}
					else if($k_code==39){ //right key
						call_php(pin);
					}
					else if($k_code==40){ //down key
						call_php(pin);
					}
				}
				else{
					alert ("Please Start Engine!");
				}});
			});
			function call_php(pin){
				var request = new XMLHttpRequest();
				request.open( "GET" , "gpio.php?pin=" + pin, true);
				request.send(null);
			}
		</script>
		<iframe src="control.php" frameborder='0'; style='position:absolute; top:410px; left:450px; width:400px; height:200px'></iframe>
		<iframe src="http://localhost/emms/led.html" frameborder='0'; style='position:absolute; top:10px; left:450px; width:400px; height:400px'></iframe>
    </body>
</html>

control.php

PHP
<html>
	<body>
		<?php			
			$val_array = array(1,2,3,4);
			//this php script generate the first page in function of the file
			for ( $i= 0; $i<3; $i++) {
				//set the pin's mode to output and read them
				system("gpio mode ".$i." out");
			}
			echo ("<img id='up' src='data/up.png' style='position:absolute; top:10px; left:150px; width:80px; height:80px' onclick='moveup()'/>");
			echo ("<img id='down' style='position:absolute; top:110px; left:150px; width:80px; height:80px' src='data/down.png' onclick='movedwn()'/>");
			echo ("<img id='right' style='position:absolute; top:60px; left:230px; width:80px; height:80px' src='data/right.png' onclick='movert()'/>");
			echo ("<img id='left' style='position:absolute; top:60px; left:70px; width:80px; height:80px' src='data/left.png' onclick='movelt()'/>");
		?>
		
		<!-- javascript -->
			<script type="text/javascript"> 
				function moveup() {
					var pin = <?php echo $val_array[0]; ?>;
					call_php(pin);
					return false;
				}
				function movedwn() { 
					var pin = <?php echo $val_array[1]; ?>;
					call_php(pin);
					return false; 
				} 
				function movert() { 
					var pin = <?php echo $val_array[2]; ?>;
					call_php(pin);
					return false; 
				} 
				function movelt() { 
					var pin = <?php echo $val_array[3]; ?>;
					call_php(pin);
					return false; 
				}
				function call_php(pin){
					var request = new XMLHttpRequest();
					request.open( "GET" , "gpio.php?pin=" + pin, true);
					request.send(null);
				}
			</script>			
    </body>
</html>

gpio.php

PHP
<?php
if (isset ( $_GET["pin"] )) {
	$pin = strip_tags ($_GET["pin"]);
	if ($pin == 0) {
		echo pin;
		system("gpio mode 4 out");
		system("gpio write 4 1");		
		system("gpio mode 5 out");
		system("gpio write 5 0");
		system("gpio mode 28 out");
		system("gpio write 28 0");		
		system("gpio mode 29 out");
		system("gpio write 29 1");
	}
	else if ($pin == 2) {
		echo pin;
		system("gpio mode 4 out");
		system("gpio write 4 0");		
		system("gpio mode 5 out");
		system("gpio write 5 1");
		system("gpio mode 28 out");
		system("gpio write 28 1");		
		system("gpio mode 29 out");
		system("gpio write 29 0");
	}
	else if ($pin == 3) {
		echo pin;
		system("gpio mode 4 out");
		system("gpio write 4 1");		
		system("gpio mode 5 out");
		system("gpio write 5 0");
		system("gpio mode 28 out");
		system("gpio write 28 1");		
		system("gpio mode 29 out");
		system("gpio write 29 0");
	}
	else if ($pin == 7) {
		echo pin;
		system("gpio mode 4 out");
		system("gpio write 4 0");		
		system("gpio mode 5 out");
		system("gpio write 5 1");
		system("gpio mode 28 out");
		system("gpio write 28 0");		
		system("gpio mode 29 out");
		system("gpio write 29 1");
	}
	else { 
		echo ("fail"); 
	}
}
else { echo ("fail"); }
?>

Credits

Zeeshan
13 projects • 50 followers
Creative and destructive design master. Ability to use the brain waves for both construction and destruction

Comments