Things used in this project

Hardware components:
Phpoc blue per 100x1000 euk22wtv9i
PHPoC Blue
×1
51gzz5eu9pl. sx425
HC-SR04
×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)
{
	//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;
		}
	}
}
?>

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

Web-Based Internet Signboard
Easy
  • 178
  • 5

Full instructions

Build a web-based Internet Signboard with PHPoC.

Alcohol Gas Detector
Easy
  • 210
  • 9

Full instructions

I made a simple project called "Alcohol Gas Detector".

MQTT for IoT Devices, Support TLS/SSL, QoS Level: 0, 1, 2
Easy
  • 218
  • 7

Protip

This project describes MQTT library for an IoT Platfrom so-called PHPoC. This library supports MQTT over TLS/SSL and all QoS Level: 0, 1, 2.

IFTTT's Android SMS Service with PHPoC Blue
Easy
  • 311
  • 7

This project shows how to use IFTTT Maker channel.

Web-based Temperature and Humidity Monitoring
Easy
  • 253
  • 8

Full instructions

Web-based Temperature and Humidity Monitoring

Parking System
Easy
  • 210
  • 8

Full instructions

I made a simple project called "smart car parking system".

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback