phpoc_man
Published © GPL3+

Monitoring the Trash Can

Status of trash can in public area is monitored. When the trash can is full, an email is sent to trash manager or status of trash can.

EasyFull instructions provided8 hours580
Monitoring the Trash Can

Things used in this project

Hardware components

PHPoC Blue
PHPoC Blue
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Story

Read more

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

phpoc_man

phpoc_man

35 projects • 103 followers
Contact

Comments