Devices & Components
Arduino Uno Rev3
PHPoC Shield for Arduino
USB Cable 2.0 Type A/B for Arduino Uno
Ultrasonic Sensor HC-SR04
Jumper Wires
Project description
Code
Source Code
arduino
You only need to replace your Gmail account's information To get Gmail library, visit here https://create.arduino.cc/projecthub/phpoc_man/gmail-arduino-uno-ipv6-01ecc0?ref=user&ref_id=160147&offset=3
1#define PIN_TRIG 5 2#define PIN_ECHO 6 3#define BIN_HEIGHT 20 // maximum containable height of bin in centimeters 4 5#include "SPI.h" 6#include "Phpoc.h" 7 8long ultrasonicGetDistance(); 9boolean dustbinSendGmail(); 10 11PhpocEmail email; 12boolean isSent = false; 13 14void setup() { 15 Serial.begin(9600); 16 while(!Serial) 17 ; 18 pinMode(PIN_TRIG, OUTPUT); 19 pinMode(PIN_ECHO, INPUT); 20 21 Phpoc.begin(PF_LOG_SPI | PF_LOG_NET | PF_LOG_APP); 22 23} 24 25void loop() { 26 long distance = 0; 27 // read 10 time and get average value to eliminate noises 28 for(int i = 0; i < 100; i++) 29 { 30 long temp = 0; 31 do 32 { 33 temp = ultrasonicGetDistance(); 34 } 35 while(temp > BIN_HEIGHT); 36 37 distance += temp; 38 delay(10); 39 } 40 distance /= 100; 41 42 Serial.print(distance); 43 Serial.println(" cm"); 44 45 if(distance <= 5) 46 { 47 if(!isSent) 48 { 49 Serial.println("Dustbin is almost full"); 50 isSent = dustbinSendGmail(); 51 } 52 } 53 else if(isSent && distance > 8) // avoid send alot of email when the distance is oscillated around 5cm 54 isSent = false; 55 56 delay(500); 57} 58 59long ultrasonicGetDistance(){ 60 long duration, distance; 61 digitalWrite(PIN_TRIG, LOW); 62 delayMicroseconds(2); 63 digitalWrite(PIN_TRIG, HIGH); 64 delayMicroseconds(10); 65 digitalWrite(PIN_TRIG, LOW); 66 duration = pulseIn(PIN_ECHO, HIGH); 67 distance = (duration/2) / 29.1; 68 return distance; 69} 70 71boolean dustbinSendGmail(){ 72 // setup outgoing relay server - gmail.com 73 email.setOutgoingServer("smtp.gmail.com", 587); 74 email.setOutgoingLogin("your_login_id", "your_login_password"); 75 76 // setup From/To/Subject 77 email.setFrom("from_email_address", "from_user_name"); 78 email.setTo("to_email_address", "to_user_name"); 79 80 email.setSubject("Your dustbin is almost full"); 81 82 // write email message 83 email.beginMessage(); 84 email.println("Location: Planet Earth."); 85 email.println(""); 86 email.println("Your dustbin is almost full."); 87 email.println(""); 88 email.println("Prepare to through it out."); 89 90 email.endMessage(); 91 92 // send email 93 if(email.send() > 0) 94 { 95 Serial.println("Email send ok"); 96 return true; 97 } 98 else 99 { 100 Serial.println("Email send failed"); 101 return false; 102 } 103}
Source Code
arduino
You only need to replace your Gmail account's information To get Gmail library, visit here https://create.arduino.cc/projecthub/phpoc_man/gmail-arduino-uno-ipv6-01ecc0?ref=user&ref_id=160147&offset=3
1#define PIN_TRIG 5 2#define PIN_ECHO 6 3#define BIN_HEIGHT 20 // maximum containable height of bin in centimeters 4 5#include "SPI.h" 6#include "Phpoc.h" 7 8long ultrasonicGetDistance(); 9boolean dustbinSendGmail(); 10 11PhpocEmail email; 12boolean isSent = false; 13 14void setup() { 15 Serial.begin(9600); 16 while(!Serial) 17 ; 18 pinMode(PIN_TRIG, OUTPUT); 19 pinMode(PIN_ECHO, INPUT); 20 21 Phpoc.begin(PF_LOG_SPI | PF_LOG_NET | PF_LOG_APP); 22 23} 24 25void loop() { 26 long distance = 0; 27 // read 10 time and get average value to eliminate noises 28 for(int i = 0; i < 100; i++) 29 { 30 long temp = 0; 31 do 32 { 33 temp = ultrasonicGetDistance(); 34 } 35 while(temp > BIN_HEIGHT); 36 37 distance += temp; 38 delay(10); 39 } 40 distance /= 100; 41 42 Serial.print(distance); 43 Serial.println(" cm"); 44 45 if(distance <= 5) 46 { 47 if(!isSent) 48 { 49 Serial.println("Dustbin is almost full"); 50 isSent = dustbinSendGmail(); 51 } 52 } 53 else if(isSent && distance > 8) // avoid send alot of email when the distance is oscillated around 5cm 54 isSent = false; 55 56 delay(500); 57} 58 59long ultrasonicGetDistance(){ 60 long duration, distance; 61 digitalWrite(PIN_TRIG, LOW); 62 delayMicroseconds(2); 63 digitalWrite(PIN_TRIG, HIGH); 64 delayMicroseconds(10); 65 digitalWrite(PIN_TRIG, LOW); 66 duration = pulseIn(PIN_ECHO, HIGH); 67 distance = (duration/2) / 29.1; 68 return distance; 69} 70 71boolean dustbinSendGmail(){ 72 // setup outgoing relay server - gmail.com 73 email.setOutgoingServer("smtp.gmail.com", 587); 74 email.setOutgoingLogin("your_login_id", "your_login_password"); 75 76 // setup From/To/Subject 77 email.setFrom("from_email_address", "from_user_name"); 78 email.setTo("to_email_address", "to_user_name"); 79 80 email.setSubject("Your dustbin is almost full"); 81 82 // write email message 83 email.beginMessage(); 84 email.println("Location: Planet Earth."); 85 email.println(""); 86 email.println("Your dustbin is almost full."); 87 email.println(""); 88 email.println("Prepare to through it out."); 89 90 email.endMessage(); 91 92 // send email 93 if(email.send() > 0) 94 { 95 Serial.println("Email send ok"); 96 return true; 97 } 98 else 99 { 100 Serial.println("Email send failed"); 101 return false; 102 } 103}
Downloadable files
Assembly
Assembly 1. Stack PHPoC Shield on Arduino 2. Connect LAN cable or USB wifi Dongle to the shield for Ethernet 3. Pin wiring between Arduino and Sensor.
Assembly
Pin wiring between Arduino and Sensor
Pin wiring between Arduino and Sensor
Assembly
Assembly 1. Stack PHPoC Shield on Arduino 2. Connect LAN cable or USB wifi Dongle to the shield for Ethernet 3. Pin wiring between Arduino and Sensor.
Assembly
All system
All system
Pin wiring between Arduino and Sensor
Pin wiring between Arduino and Sensor
Comments
Only logged in users can leave comments