Demonstration
How It Works
Distance sensor is used to measure distance from cover of trash can to surface of trash in trash can. When the distance is below a threshold, the trash can is considered as full, and we can send notification via email or update status on mysql database.
Things Used In This Project
- PHPoC Blue + USB Wi-Fi Dongle
- Ultrasonic Distance Sensor
- Jumper wires
Wiring Diagram
Source code
task0.php
PHP Code:
<?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("[email protected]", "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("[email protected]", "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)
{
//if(ws_state(0) == TCP_CONNECTED)
{
$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;
}
}
}
?>
You can also see this project in hackster.io web site: https://www.hackster.io/phpoc_man/mo...ash-can-fe9368