Things used in this project

Hardware components:
Phpoc blue per 100x1000 euk22wtv9i
PHPoC PHPoC Blue
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Schematics

Wiring
Wiring 8zaxikdkqo

Code

Main task (task0.php)PHP
<?php

if(_SERVER("REQUEST_METHOD"))
	exit; // avoid php execution via http request

include_once "/lib/sd_340.php";
include_once "/lib/sn_dns.php";
include_once "/lib/sn_esmtp.php";
include_once "/lib/sn_mysql.php";

define("FULL_THRESHOLD",  20); // in centimeter

// setup trigger pulse timer
ht_ioctl(1, "set mode output pulse");
ht_ioctl(1, "set div us");
ht_ioctl(1, "set repc 1");
ht_ioctl(1, "set count 5 10"); // 10us pulse width
 
// setup echo capture timer
ht_ioctl(0, "reset");
ht_ioctl(0, "set div us");
ht_ioctl(0, "set mode capture toggle");
ht_ioctl(0, "set trigger from pin rise");
ht_ioctl(0, "set repc 4");

$pre_dist = 0;
$count = 0;
$full_state = 0;

$dustbin_id = 3; // primary key in mysql if using mysql

function send_email()
{
	esmtp_account("your_account@gmail.com", "Dustbin");
	esmtp_auth("your_account", "your_password");
	esmtp_msa("smtp.gmail.com", 465);
	
	$time = date("Y:M-d-TH:i:s", time());

	$subject = "Dustbin is full";
	$message  = "Location: Planet Earth\r\n";
	$message .= "Time: $time\r\n";
	$message .= "Your dustbin is almost full\r\n";
	$message .= "Come there to get it!\r\n";

	$msg = esmtp_send("your_account@gmail.com", "Trash General Manager", $subject, $message);

	if($msg == "221")
		return true;
	else
		return false;
}

function update_mysql($dustbin_id, $state)
{
	//Enter your DB Server's hostname or IP address!
	$server_addr = "192.168.0.3";

	//Enter your account information!
	$user_name = "your_username";
	$password = "your_password";
	
	//Connect to DB Server
	if(mysql_connect($server_addr, $user_name, $password))
	{
		$result = mysql_select_db("employee");
		
		$result = mysql_query("INSERT INTO tbl_dustbin_state (dustbin_id, state, time) VALUES ($dustbin_id, '$state',  NOW());");
		
		if($result === true)
			return true;
		else
			return false;
		
		mysql_close();
	}
}

function dustbin_loop()
{
	global $full_state;
	global $pre_dist;
	global $count;
	
	ht_ioctl(0, "start"); // we should start capture timer first
	ht_ioctl(1, "start"); // start trigger pulse
	 
	usleep(10000); // sleep 10ms
	 
	// 1st capture value ("get count 0") is always zero.
	// we should get 2nd capture value;
	$us = ht_ioctl(0, "get count 1");
	
	if($us == 0)
		return $full_state;
	
	$dist = $us * 340.0 / 2; // us to meter conversion
	$dist = floor($dist / 10000); // meter to centimeter conversion
	
	echo "$dist\r\n";
	
	//filter noise
	if((float)$pre_dist != 0.0)
	{
		if(abs($dist - $pre_dist) <= 1)
			$count++;
		else
			$count = 0;
	}
		
	$pre_dist = $dist;
	
	if($count > 5)
	{
		if($dist < FULL_THRESHOLD)
			$full_state++;
		else
			$full_state = 0;
	}
	
	return $full_state;
}

$is_notified = false;
$pre_state = 0;

while(1)
{
	$state = dustbin_loop();
	
	if($state != $pre_state)
	{
		switch($state)
		{
			case 0:
				//update_mysql($dustbin_id, "EMPTY"); // uncomment this line if using mysql
				$is_notified = false;
				break;
				
			case 1:
				sleep(4); // sleep and then do the sencond check to prevent the event that cover hides sensor
				break;
				
			default:
				// send notification
				if($is_notified == false)
				{
					//if(update_mysql($dustbin_id, "FULL")) // uncomment this line if using mysql
					if(send_email())
					{
						echo "notify/update successful\r\n";
						$is_notified = true;
					}
					else
						echo "notify/update mail failed\r\n";
				}
		}
		
		$pre_state = $state;
	}
}
?>

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Controlling Stepper Motor Precisely via Web
Easy
  • 212
  • 9

This project shows how to control the step motor precisely using micro-stepping method via Web.

PHPoC - Web-Based Two-Player Game
Easy
  • 261
  • 9

Have fun with a colleague during break time with this web-based game.

PHPoC - Detect Someone Taking Your Stuff
Easy
  • 617
  • 14

When your stuff is being taken away by a thief, PHPoC makes an alert and sends a notification to you via Gmail.

Biometric-Based Student Attendance System
Easy
  • 530
  • 9

This is a fun an easy DIY project that enables you automatically capture students attendance bio-metrically via fingerprint.

Arduino - Web-Based Two-Player Game
Easy
  • 2,254
  • 28

Full instructions

Have fun with a colleague during break time with this web-based game.

Arduino - Detect Someone Taking Your Stuff
Easy
  • 1,556
  • 12

When your stuff is being taken away by a thief, Arduino makes an alert and sends a notification to you via Gmail.

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog