Water Level Monitoring over IoT for ECE 4180 in Spring 2020 at Gatech

Dependencies:   mbed Servo 4DGL-uLCD-SE Ultrasonic HCSR04 Seed_Ultrasonic_Range

Committer:
angadmakes
Date:
Thu Apr 30 14:28:29 2020 +0000
Revision:
0:12f7ba5bc6c2
ECE4180FinalProjectV1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
angadmakes 0:12f7ba5bc6c2 1 /*
angadmakes 0:12f7ba5bc6c2 2 ECE 4180 Final Project: Rainwater Harvesting System with IoT
angadmakes 0:12f7ba5bc6c2 3
angadmakes 0:12f7ba5bc6c2 4 SAMPLE DATA: There's a lot of noise specially in the beginning when water just bounces around the empty tank
angadmakes 0:12f7ba5bc6c2 5 reading the sensor at longer intervals of time might help reduce the noise but the data in general looks like
angadmakes 0:12f7ba5bc6c2 6 this with bounds (~empty : ~full) => (~192mm : ~ 34mm)
angadmakes 0:12f7ba5bc6c2 7
angadmakes 0:12f7ba5bc6c2 8 */
angadmakes 0:12f7ba5bc6c2 9
angadmakes 0:12f7ba5bc6c2 10 //adding the libraries
angadmakes 0:12f7ba5bc6c2 11 #include "mbed.h"
angadmakes 0:12f7ba5bc6c2 12 #include "ultrasonic.h"
angadmakes 0:12f7ba5bc6c2 13 #include "uLCD_4DGL.h"
angadmakes 0:12f7ba5bc6c2 14 #include "Servo.h"
angadmakes 0:12f7ba5bc6c2 15
angadmakes 0:12f7ba5bc6c2 16
angadmakes 0:12f7ba5bc6c2 17 //defining the pinouts
angadmakes 0:12f7ba5bc6c2 18 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
angadmakes 0:12f7ba5bc6c2 19 //ultrasonic mu(p6, p7, .1, 1, &dist);
angadmakes 0:12f7ba5bc6c2 20 //I2C rangefinder(p9, p10); //sda, sc1
angadmakes 0:12f7ba5bc6c2 21 Serial pc(USBTX, USBRX); //tx, rx
angadmakes 0:12f7ba5bc6c2 22 //Servo myservo(p21);
angadmakes 0:12f7ba5bc6c2 23
angadmakes 0:12f7ba5bc6c2 24 //defining arbitrary variables
angadmakes 0:12f7ba5bc6c2 25 int height;
angadmakes 0:12f7ba5bc6c2 26 //bool LidClosed = false;
angadmakes 0:12f7ba5bc6c2 27 //bool empty;
angadmakes 0:12f7ba5bc6c2 28 //float p = 0;
angadmakes 0:12f7ba5bc6c2 29
angadmakes 0:12f7ba5bc6c2 30
angadmakes 0:12f7ba5bc6c2 31 //sensor function for height
angadmakes 0:12f7ba5bc6c2 32 void dist(int distance)
angadmakes 0:12f7ba5bc6c2 33 {
angadmakes 0:12f7ba5bc6c2 34 height = distance;
angadmakes 0:12f7ba5bc6c2 35 //height = mu.getCurrentDistance(void);
angadmakes 0:12f7ba5bc6c2 36 printf("DISTANC %d mm\r\n", distance);
angadmakes 0:12f7ba5bc6c2 37
angadmakes 0:12f7ba5bc6c2 38 }
angadmakes 0:12f7ba5bc6c2 39
angadmakes 0:12f7ba5bc6c2 40 ultrasonic mu(p6, p7, .5, 1, &dist);
angadmakes 0:12f7ba5bc6c2 41
angadmakes 0:12f7ba5bc6c2 42 //pass on the height return volume
angadmakes 0:12f7ba5bc6c2 43 int Volume(int h)
angadmakes 0:12f7ba5bc6c2 44 {
angadmakes 0:12f7ba5bc6c2 45 double pi = 3.14;
angadmakes 0:12f7ba5bc6c2 46 double radius = 6.5;
angadmakes 0:12f7ba5bc6c2 47 return(pi*radius*radius*h);
angadmakes 0:12f7ba5bc6c2 48 }
angadmakes 0:12f7ba5bc6c2 49
angadmakes 0:12f7ba5bc6c2 50
angadmakes 0:12f7ba5bc6c2 51
angadmakes 0:12f7ba5bc6c2 52 //Displays the water level and volume on LCD
angadmakes 0:12f7ba5bc6c2 53 void Display()
angadmakes 0:12f7ba5bc6c2 54 {
angadmakes 0:12f7ba5bc6c2 55 int V = height/3;
angadmakes 0:12f7ba5bc6c2 56 volatile int level = 27 + V;
angadmakes 0:12f7ba5bc6c2 57 uLCD.baudrate(BAUD_3000000);
angadmakes 0:12f7ba5bc6c2 58 uLCD.background_color(BLACK);
angadmakes 0:12f7ba5bc6c2 59 uLCD.cls(); //replace with a black retangle instead oc clear screen
angadmakes 0:12f7ba5bc6c2 60 uLCD.locate(3,12);
angadmakes 0:12f7ba5bc6c2 61 uLCD.printf("VOLUME= %d",Volume(height));
angadmakes 0:12f7ba5bc6c2 62 //uLCD.printf("Volume = ");
angadmakes 0:12f7ba5bc6c2 63 //void rectangle(int, int, int, int, int);
angadmakes 0:12f7ba5bc6c2 64 //uLCD.locate(1,1);
angadmakes 0:12f7ba5bc6c2 65 uLCD.rectangle(50, 25, 102, 92, 0x0FFFFF);
angadmakes 0:12f7ba5bc6c2 66 // dynamic rectangle size -> second argument of rectangle should be based on the sonar reading level.
angadmakes 0:12f7ba5bc6c2 67 uLCD.filled_rectangle(52, level, 100, 90, 0x00FF00); // fill rectangle based on current volume levels
angadmakes 0:12f7ba5bc6c2 68 wait(0.5);
angadmakes 0:12f7ba5bc6c2 69 }
angadmakes 0:12f7ba5bc6c2 70
angadmakes 0:12f7ba5bc6c2 71
angadmakes 0:12f7ba5bc6c2 72
angadmakes 0:12f7ba5bc6c2 73 //void tankLid()
angadmakes 0:12f7ba5bc6c2 74 //{
angadmakes 0:12f7ba5bc6c2 75 // //check is tank is empty and lid is closed then opens
angadmakes 0:12f7ba5bc6c2 76 // if(( empty == 1) && (LidClosed == true))
angadmakes 0:12f7ba5bc6c2 77 // {
angadmakes 0:12f7ba5bc6c2 78 // for(p=0; p<=10; p++)
angadmakes 0:12f7ba5bc6c2 79 // {
angadmakes 0:12f7ba5bc6c2 80 // myservo = p/10.0;
angadmakes 0:12f7ba5bc6c2 81 //
angadmakes 0:12f7ba5bc6c2 82 // }
angadmakes 0:12f7ba5bc6c2 83 // LidClosed = false;
angadmakes 0:12f7ba5bc6c2 84 //
angadmakes 0:12f7ba5bc6c2 85 // }
angadmakes 0:12f7ba5bc6c2 86 //
angadmakes 0:12f7ba5bc6c2 87 // //checks if tank is full and lid is open then closes
angadmakes 0:12f7ba5bc6c2 88 // else if((empty == 0) &&(LidClosed == false))
angadmakes 0:12f7ba5bc6c2 89 // {
angadmakes 0:12f7ba5bc6c2 90 // for(p=10; p>=0; p--)
angadmakes 0:12f7ba5bc6c2 91 // {
angadmakes 0:12f7ba5bc6c2 92 // myservo = p/10.0;
angadmakes 0:12f7ba5bc6c2 93 //
angadmakes 0:12f7ba5bc6c2 94 // }
angadmakes 0:12f7ba5bc6c2 95 // LidClosed = true;
angadmakes 0:12f7ba5bc6c2 96 // }
angadmakes 0:12f7ba5bc6c2 97 // }
angadmakes 0:12f7ba5bc6c2 98
angadmakes 0:12f7ba5bc6c2 99 int main()
angadmakes 0:12f7ba5bc6c2 100 {
angadmakes 0:12f7ba5bc6c2 101 mu.startUpdates();
angadmakes 0:12f7ba5bc6c2 102
angadmakes 0:12f7ba5bc6c2 103 while (1)
angadmakes 0:12f7ba5bc6c2 104 {
angadmakes 0:12f7ba5bc6c2 105
angadmakes 0:12f7ba5bc6c2 106 //tank empty == 1 open lid
angadmakes 0:12f7ba5bc6c2 107 mu.checkDistance(); //checks height value fro sonar
angadmakes 0:12f7ba5bc6c2 108 //height = mu.getCurrentDistance();
angadmakes 0:12f7ba5bc6c2 109 wait(0.5);
angadmakes 0:12f7ba5bc6c2 110 Display(); //computes volume and display volume level
angadmakes 0:12f7ba5bc6c2 111 wait(0.5);
angadmakes 0:12f7ba5bc6c2 112 //stream current level value to SDcard repeat
angadmakes 0:12f7ba5bc6c2 113 //pc.printf("DISTANCE %d mm\r\n", height);//
angadmakes 0:12f7ba5bc6c2 114 //pc.printf("TEST\n");
angadmakes 0:12f7ba5bc6c2 115
angadmakes 0:12f7ba5bc6c2 116 }
angadmakes 0:12f7ba5bc6c2 117
angadmakes 0:12f7ba5bc6c2 118 }
angadmakes 0:12f7ba5bc6c2 119