Congfeng Jiang
Published © Apache-2.0

Water Quality Monitor

Use pH and Electricity conductivity sensor to monitor the water quality. Using temp and light sensor for demo.

EasyWork in progress2 hours187
Water Quality Monitor

Things used in this project

Story

Read more

Schematics

simple connection layout

Just connect the pH sensor and EC sensor to the seeed grove starter kit shield.
Img 1790

Code

WaterQualityMonitor

Arduino
Note that the pH sensor and EC sensor are being shipped to us and the code here use temperature sensor and light sensor to pretend as the pH sensor and EC sensor. When pH value or EC value is out of the normal range, it can beep the buzzer and flash the LCD screen.
//A0 is temperature
//A1 is pH sensor
//

//Function: The PH sensor output voltage value,
//          convert into PH and then display in the serial monitor.

#include <Wire.h>
#include "rgb_lcd.h"
#include <SPI.h>
#include <Ethernet.h>

#define Vref 4.95 //const for pH value tranformation
const int B = 3975; //const for temperature value tranformation
rgb_lcd lcd;
// the number of the tilt pin
const int buzzerPin =  3;      // the number of the buzzer pin
int speakerPin = 3;
// variables will change:
int tiltState = 0;         // variable for reading the tilt status


//for buzzer
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 0, 0, 84);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 8080 is default for HTTP):
EthernetServer server(8000);

void setup()
{
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 2);
  // Print a message to the LCD.
  lcd.print("WaterQualityMon");

  //BUZZER
  // initialize the buzzer pin as an output:
  pinMode(buzzerPin, OUTPUT);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}



void loop()
{
  // Turn off the display:
  lcd.noDisplay();
  delay(500);
  // Turn on the display:
  lcd.display();
  // delay(500);

  //read and parse pH value from A1
  int sensorValue;
  int m;
  long sensorSum;
  for (m = 0; m < 50; m++)
  {
    sensorValue = analogRead(A1); //Connect the PH Sensor to A0 port
    sensorSum += sensorValue;
  }
  sensorValue =   sensorSum / 50;
  float pHvalue = 7 - 1000 * (sensorValue - 372) * Vref / 59.16 / 1023;
  Serial.print(" the PH value is");
  //Serial.println(7-1000*(sensorValue-372)*Vref/59.16/1023);
  pHvalue = 7.5;
  Serial.println(pHvalue);
  lcd.setCursor(0, 0);

  lcd.print("pH:");
  lcd.println(pHvalue);




  int tempval = analogRead(A0);

  // Determine the current resistance of the thermistor based on the sensor value.
  float resistance = (float)(1023 - tempval) * 10000 / tempval;

  // Calculate the temperature based on the resistance value.
  float temperature = 1 / (log(resistance / 10000) / B + 1 / 298.15) - 273.15;

  // Print the temperature to the serial console.
  Serial.print(" the temperature value is: ");
  Serial.println(temperature);
  lcd.print("temp:");
  lcd.println(temperature);
  // Wait one second between measurements.

  lcd.setCursor(0, 1);
  if (temperature > 28.5)
  {
    lcd.setRGB(255, 0, 0);  //set RED color
    lcd.blink();
    for (int i = 0; i < 30; i++) {
      digitalWrite(buzzerPin, HIGH);
      delay(200);
    }
  }
  else if (temperature > 24)
  {
    lcd.setRGB(255, 165, 0); //SET ORANGE

    for (int i = 0; i < 20; i++) {
      digitalWrite(buzzerPin, LOW);
      delay(500);
    }

  }
  else
  {
    lcd.setRGB(0, 255, 0);  //SET GREEN
    // play music
    for (int i = 0; i < length; i++)
    {
      if (notes[i] == ' ')
      {
        delay(beats[i] * tempo); // rest
      }
      else
      {
        playNote(notes[i], beats[i] * tempo);
      }

      // pause between notes
      delay(tempo / 2);
    }

  }

  //Sending data to webserver

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<title>Water Quality Monitoring Portal</title>");
          client.println("The real time water quality index is :<br>");

          // output the value of pH value and temerature
          client.print("pH value is ");
          client.print(pHvalue);
          client.println("<br />");

          client.print("Temperature  is ");
          client.print(temperature);
          client.println("<br />");


          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }


  delay(3000);

}

Credits

Congfeng Jiang

Congfeng Jiang

1 project • 0 followers
Research scholar at Wayne State University and Associate Professor at Hangzhou Dianzi University
Thanks to Yinghui Zhao and Leilei Ruan.

Comments