#include #include #include "DHT.h" #include //code for testing Ultrasonic Module #define ECHOPIN 11 // Digital Pin 11 to receive echo pulse #define TRIGPIN 12 // Digital Pin 12 to send trigger pulse // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) #define DHTPIN 2 // Digital pin 2 where DHT 22 is connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); //Accelerometer connected to I2C of LinkIT One ADXL345 accel; //variable accel is an instance of the accel345 library char buff[256]; //Info about battery level and charging //Temperature and Humidity sensor readings from DHT 22 float t = 0.0; float h = 0.0; //Accelerometer adxl345 outputs int x_initial,y_initial,z_initial; double xyz_initial[3]; double ax_initial,ay_initial,az_initial; //Function to Store initial axis value of x,y & z for comparision void axis_initialize(){ accel.readXYZ(&x_initial, &y_initial, &z_initial); //read the accelerometer values and store them in variables x_initial,y_initial,z_initial accel.getAcceleration(xyz_initial); ax_initial = xyz_initial[0]; ay_initial = xyz_initial[1]; az_initial = xyz_initial[2]; delay(500); } //To compare initial values with current reading of x,y & z values and see whether someone tried to move our project (for theft protection) int compareResult(){ //Boring accelerometer stuff int x,y,z,Xchange,Ychange,Zchange; accel.readXYZ(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z double xyz[3]; double ax,ay,az; accel.getAcceleration(xyz); ax = xyz[0]; ay = xyz[1]; az = xyz[2]; if((int)ax!=(int)ax_initial){ Xchange=1; } else { Xchange=0; } Serial.print("Xchange:"); Serial.println(Xchange); if((int)ay!=(int)ay_initial){ Ychange=1; } else { Ychange=0; } Serial.print("Ychange:"); Serial.println(Ychange); if((int)az!=(int)az_initial){ Zchange=1; } else { Zchange=0; } Serial.print("Zchange:"); Serial.println(Zchange); if((Xchange==1)||(Ychange==1)||(Zchange==1)){ return 1; } else return 0; } void setup() { Serial.begin(9600); //For Serial debugging accel.powerOn(); //Power on accelerometer dht.begin(); //Initializing DHT 22 sensor //Initializing Ultrasonic sensor pinMode(ECHOPIN, INPUT); pinMode(TRIGPIN, OUTPUT); axis_initialize(); //Initializing accelerometer sensor } void loop() { if(dht.readHT(&t, &h)) //Reading Temperature and humidity from DHT 22 { Serial.println("------------------------------"); Serial.print("Temperature = "); Serial.println(t); Serial.print("Humidity = "); Serial.println(h); //Reading battery level and checking if whether it is charging or not? sprintf(buff,"Battery level = %d", LBattery.level() ); Serial.println(buff); sprintf(buff,"Is charging = %d",LBattery.isCharging() ); Serial.println(buff); // Start Ranging -Generating a trigger of 10us burst digitalWrite(TRIGPIN, LOW); delayMicroseconds(2); digitalWrite(TRIGPIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGPIN, LOW); float time = pulseIn(ECHOPIN, HIGH); // Distance Calculation /* Speed of sound = 340 m/s Speed of sound = 340 * 1000 cm/(1000000 microseconds) = (1/29) cm per us The Ultrasonic burst travels out & back.So to we have to divide the time the echo pin was high by 2 Distance = (Time echo pin was high/2) * speed of sound = (Time echo pin was high/2) * (1/29) = (Time echo pin was high/58) */ float distance= time/58; Serial.print("Garbage Level: "); Serial.print(distance); Serial.println(" cm"); delay(200); int activity; activity=compareResult(); if(activity==1) { Serial.println("There is unauthorised activity..."); } else { Serial.println("No activity..."); } delay(500); } }