0% found this document useful (0 votes)
6 views4 pages

Student Code

Uploaded by

Darshan Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Student Code

Uploaded by

Darshan Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

IoT Based Garbage Monitoring System

// Standard include statements


#include <DHT11.h>
#include <SoftwareSerial.h>

// Connection for SIM800 module


SoftwareSerial gprsSerial(2, 3);

// DHT 11 connection
int pin = A2;
DHT11 dht11(pin);

// Connection for Ultrasonic Sensor 1 and 2


const int echoPin_1 = A3; // Echo Pin
const int trigPin_1 = A4;// Trigger Pin

const int echoPin_2 = A5; // Echo Pin


const int trigPin_2 = A6;// Trigger Pin

// Moisture sensor connection


const int Sensor = A7;

// Motor Connection
const int Motor_1_A = 12;
const int Motor_1_B = 11;

// Buzzer and Switch connection


const int SW = 10;
const int Buzzer = 9;

// Variables declaration
int current_temp = 0, current_humidity = 0;
long duration, distance; // Duration used to calculate distance
int buttonState = 0;

void setup()
{

// See if the SIM900 is ready


gprsSerial.println("AT");

// SIM card inserted and unlocked?


gprsSerial.println("AT+CPIN?");

// Is the SIM card registered?


gprsSerial.println("AT+CREG?");

// Is GPRS connection active?


gprsSerial.println("AT+CGATT?");

// Set connection type to GPRS


gprsSerial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
// Enable GPRS
gprsSerial.println("AT+SAPBR=1,1");

// All well, beep Buzzer


digitalWrite(Buzzer, LOW);
delay(2000);
digitalWrite(Buzzer, HIGH);
}

void loop()
{
buttonState = digitalRead(SW); // check if button pressed

// if pressed (pressed = 0 ); inverted logic of Arduino


if (buttonState == 0)
{
// Buzzer beep
digitalWrite(Buzzer, LOW);
delay(2000);
digitalWrite(Buzzer, HIGH);

// Humidity read
read_Dht11();

// ultrasonic read (both sensor)


ultrasonic_1();
delay(10);
ultrasonic_2();
delay(10);
sensor_read();

// Update the IoT system


update_data();

// Tilt motor Left or Right depending upon Sensor reading


if (final_sensor_1<80) // Left tilt
{
// Left
digitalWrite(Motor_1_A, HIGH);
digitalWrite(Motor_1_B, LOW);
delay(3000);
// Stop
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_1_B, LOW);

// back to normal/straight
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_1_B, HIGH);
// Stop
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_1_B, LOW);
}

if (final_sensor_1 >= 80) // Right Tilt


{
// Right
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_1_B, HIGH);
delay(3000);
//Stop
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_1_B, LOW);

// back to normal/straight
digitalWrite(Motor_1_A, HIGH);
digitalWrite(Motor_1_B, LOW);
delay(3000);
// Stop
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_1_B, LOW);
}

// Beep the buzzer


digitalWrite(Buzzer, LOW);
delay(2000);
digitalWrite(Buzzer, HIGH);
}

void ultrasonic_1()
{

ultra_count = 0;
final_distance = 0;
while (ultra_count != 5) // Lets average 5 readings; for better approximation
{
delay(10);
ultra_count++;
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin_1, LOW);
delayMicroseconds(2);

digitalWrite(trigPin_1, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin_1, LOW);
duration = pulseIn(echoPin_1, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration / 58.2;
final_distance = final_distance + distance;
}
final_distance_1 = final_distance / 10;
}
void ultrasonic_2()
{
distance = 0;
ultra_count = 0;
final_distance = 0;
while (ultra_count != 5) // Lets average 5 readings; for better approximation
{
delay(10);
ultra_count++;
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin_2, LOW);
delayMicroseconds(2);

digitalWrite(trigPin_2, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin_2, LOW);
duration = pulseIn(echoPin_2, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration / 58.2;
final_distance = final_distance + distance;
}
final_distance_2 = final_distance / 5;
}

void update_data()
{

// Log the http IoT value

gprsSerial.print("http://iot.theproject.in/SS_IoT_WS.asmx/");
gprsSerial.print("SQL_IoT_Insert?Userkey=IT001&Personalid=SomePiD&sensor_input=");

gprsSerial.print(current_temp); gprsSerial.print(",");
gprsSerial.print(current_humidity); gprsSerial.print(",");
gprsSerial.print(final_distance_1); gprsSerial.print(",");
gprsSerial.print(final_distance_2); gprsSerial.print(",");
gprsSerial.print(final_sensor_1); gprsSerial.print(",");

gprsSerial.println("&RetMsg=s\"");

delay(500);

You might also like