SW(A)MP - Smart Water Taps

A smart IoT-based water management system designed for water usage databasing and management using remotely controlled water sensors.

IntermediateFull instructions provided12 hours1,859
SW(A)MP - Smart Water Taps

Things used in this project

Hardware components

CC3220SF-LAUNCHXL SimpleLink Wi-Fi CC3220SF LaunchPad
Texas Instruments CC3220SF-LAUNCHXL SimpleLink Wi-Fi CC3220SF LaunchPad
×1
SEA Water Flow Sensor Model: YF-S201
Used to detect flow rate of water flowing through tap or pipe
×1
S3003 Servo Motor
To be attached to pipe to control opening and closing
×1

Story

Read more

Schematics

Block Diagram

Code

SW(A)MP

Arduino
Open with Energia IDE
#ifndef _CC3200R1M1RGC_
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>

// your network name also called SSID
char ssid[] = "Nikhil";
// your network password
char password[] = "m3ch4l1f3";

// your network key Index number (needed only for WEP)
int keyIndex = 0;
int servo=3;
int angle;
int pwm;
int total=0;
int water=0;
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned int flowsensor = 4; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
int flag=0; //variable that tracks current status of tap (ON/OFF)
int leak=0; //counts leakages

WiFiServer server(80); //connect server to port 80
void flow () // Interrupt function
{
   flow_frequency++;
}
void setup() {
  Serial.begin(115200);      // initialize serial communication
  pinMode(RED_LED, OUTPUT);      // set the LED pin mode
  pinMode(servo,OUTPUT);  //set servo pin to output
  
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);


    pinMode(flowsensor, INPUT_PULLUP);
   //digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
   attachInterrupt(digitalPinToInterrupt(4), flow, RISING); // Setup Interrupt
  // Enable interrupts
  
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  
  // you're connected now, so print out the status  
  printWifiStatus();

  Serial.println("Starting webserver on port 80");
  server.begin();                           // start the web server on port 80
  Serial.println("Webserver started!");
}

void loop() {

  flow_frequency = 0;
        interrupts();
         delay (1000);   //Wait 1 second 
        noInterrupts(); //Disable the interrupts on the Arduino
  
  //Start the math
  l_hour = (flow_frequency * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
  // l_hour= l_hour* 60;         //Convert seconds to minutes, giving you mL / Minute
 //l_hour = l_hour / 1000;       //Convert mL to Liters, giving you Liters / Minute
total+=l_hour; //assuming flow rate is equal to the average amount of water that leaves the tap per second, we add the flow rates of each individual second and store them in this variable
water+=l_hour; 
  //Serial.println(flowRate);         //Print the variable flowRate to Serial
      Serial.print("Flow frequency is :");
      Serial.println(flow_frequency);

      Serial.print("Flow rate is: ");   
      Serial.println(l_hour,DEC); // Print mlitres/hour
      Serial.print("Total:");
      Serial.println(total);
      
      //Serial.println(" L/hour");
      if (flag==0 && l_hour>0){
        leak=1;
      }
      if(water>=2000){                       //switch off tap if water that leaves exceeds a threshold
        for(angle=60;angle>=0;angle-=5){
          servoPulse(servo,angle);}
        water=0;
        flag=0;
      }
  int i = 0;
  WiFiClient client = server.available();   // listen for incoming clients
  

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    char buffer[150] = {0};                 // make a buffer to hold incoming data
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (strlen(buffer) == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            /*client.println("<html><head><title>Smart Water Management</title></head><body align=center>");
            client.println("<h1 align=center><font color=\"red\">Welcome to the CC3200 WiFi Web Server</font></h1>");
            client.print("RED LED <button onclick=\"location.href='/H'\">HIGH</button>");
            client.println(" <button onclick=\"location.href='/L'\">LOW</button><br>");*/
            client.println("<html><head><TITLE>Smart Water Managment</TITLE></HEAD>");
            client.println("<body align='center' BGCOLOR=\"018C75\"><marquee>Imagineer 2019</marquee><h1 ><font face = 'Impact'>Welcome to SW(a)MP!</font>");
            client.println("</h1><font color=\"BBDBD6\" face=\"Georgia\">");
            client.println("<p align=\"left\">Although we are near the beach and surrounded by water, we fall short of water in summers, oh, the irony. SW(a)mp presents small solutions to counter these problems.</p><hr>");
            client.println("<h2>Control the tap</h2><button onclick=\"location.href='/H'\">Switch on</button><button onclick=\"location.href='/L'\">Switch off</button><hr>");
            client.println("<h2>Leakages detected:");
            client.println(leak);
            client.println("</h2><hr><h2>Amount of water used:");
            client.println(total);
            client.println("ml");
            if(total>6000){
              client.println("<p align='center'>Please note: Excessive Usage</p> ");
            }
            client.println("</h2><hr></font></BODY></HTML>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear the buffer:
            memset(buffer, 0, 150);
            i = 0;
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          buffer[i++] = c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (endsWith(buffer, "GET /H")) {
          digitalWrite(RED_LED, HIGH); // GET /H turns the LED on
          for(angle=0;angle<=140;angle+=5){ //switches tap on
          servoPulse(servo,angle);}
           flag=1;
          
        }
        if (endsWith(buffer, "GET /L")) {
          digitalWrite(RED_LED, LOW);  // GET /L turns the LED off
          for(angle=140;angle>=0;angle-=5){ //switches tap off
          servoPulse(servo,angle);
          flag=0;
          
          
          }
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

//
//a way to check if one array ends with another array
//
boolean endsWith(char* inString, char* compString) {
  int compLength = strlen(compString);
  int strLength = strlen(inString);
  
  //compare the last "compLength" values of the inString
  int i;
  for (i = 0; i < compLength; i++) {
    char a = inString[(strLength - 1) - i];
    char b = compString[(compLength - 1) - i];
    if (a != b) {
      return false;
    }
  }
  return true;
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void servoPulse(int servo, int angle){  //servo.h library wasn't functioning, so this function is used to control the servo using pwm
  pwm=(angle*11)+500;
  digitalWrite(servo,HIGH);
  delayMicroseconds(pwm);
  digitalWrite(servo,LOW);
  delay(50);
}

Credits

hrithik bhat

hrithik bhat

1 project • 1 follower
Linu George

Linu George

1 project • 9 followers
2nd Year student pursuing IT engineering.
Nikhil Varghese

Nikhil Varghese

1 project • 7 followers
abhishek choudhary

abhishek choudhary

1 project • 2 followers
3rd year ECE engg. student
Texas Instruments University Program

Texas Instruments University Program

91 projects • 120 followers
TI helps students discover what's possible to engineer their future.

Comments