IOT final

parent e588d651
int trig_pin = 8;
int echo_pin = 9;
long echotime; //in micro seconds
float distance; //in cm
//Ultrasonic sendor
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
const int ledPin = 13; // Built in LED in Arduino board
String msg,cmd;
//TDS
#include <EEPROM.h>
......@@ -21,7 +24,7 @@ float tdsValue = 0;
float temperature = 25;
int turbidity_str = 0;
//Turbidity
#define sensor_pin A1
......@@ -31,6 +34,13 @@ float turbidity = 0.00;
float Vclear = 2.85; // Output voltage to calibrate (with clear water).
int sensorValue = 0;
//ph
#define SensorPin A3 // the pH meter Analog output is connected with the Arduino’s Analog
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10],temp;
float phValue;
......@@ -77,25 +87,40 @@ long microsecondsToCentimeters(long microseconds) {
void setup()
{
Serial.begin(115200);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
digitalWrite(trig_pin, LOW); //Start with trigger LOW
Serial.begin(9600); // Communication rate of the Bluetooth Module
pinMode(TdsSensorPin,INPUT);//TDS analog input A0
pinMode(sensor_pin, INPUT);//Turbifity analog input A1
// Initialization
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
msg = "";
}
void loop()
{
//Ultrasonic
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
//trigger a pulse-echo measurement
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
digitalWrite(trig_pin, LOW);
//get the result
echotime= pulseIn(echo_pin, HIGH);
distance= 0.0001*((float)echotime*340.0)/2.0;
//send over Bluetooth
//Serial.print("*T"+String(echotime)+"*");
//Serial.print("*D"+String(distance,1)+"*");
delay(100);
//TDS
static unsigned long analogSampleTimepoint = millis();
......@@ -140,7 +165,33 @@ voltage = sensorValue * (5.000 / 1023.000); // Convert analog (0-1023) to voltag
turbidity = 100.00 - (voltage / Vclear) * 100.00; // as relative percentage; 0% = clear water;
//ph
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i]=analogRead(SensorPin);
delay(10);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue+=buf[i];
phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
phValue=3.5*phValue; //convert the millivolt into pH value
delay(800);
/*
//Ultrasinic
Serial.print("Distance To Water Level : {");
Serial.print(inches);
......@@ -157,19 +208,54 @@ Serial.print("ppm }");
Serial.print(" Turbidity: ");
Serial.print(turbidity);
Serial.print(" ");
Serial.print(" "); */
if(turbidity<-28) turbidity_str = 1;
if(turbidity>=-28 && turbidity<-16) turbidity_str = 2;
if(turbidity>=-16) turbidity_str = 3;
if(turbidity<-28) Serial.print("Water Very Clean ");
if(turbidity>=-28 && turbidity<-16) Serial.print("Water Norm Clean ");
if(turbidity>=-16) Serial.print("Water Very Dirty/Outside from water ");
// To read message received from other Bluetooth Device
if (Serial.available() > 0){ // Check if there is data coming
msg = Serial.readString(); // Read the message as String
Serial.println("Android Command: " + msg);
}
// tds
if (msg == "<get tds>"){
digitalWrite(ledPin, HIGH); // Turn on LED
Serial.print("tds_"); // Then send status message to Android
Serial.print(tdsValue);
Serial.println("\n");
msg = ""; // reset command
} else if (msg == "<get dis>"){
digitalWrite(ledPin, HIGH); // Turn off LED
Serial.print("distance_"); // Then send status message to Android
Serial.print(distance);
Serial.println("\n");
msg = ""; // reset command
}
else if (msg == "<get ph>"){
digitalWrite(ledPin, HIGH); // Turn off LED
Serial.print("ph_"); // Then send status message to Android
Serial.print(phValue);
Serial.println("\n");
msg = ""; // reset command
}
else if (msg == "<get turbidity>"){
digitalWrite(ledPin, HIGH); // Turn off LED
Serial.print("turbidity_"); // Then send status message to Android
Serial.print(turbidity);
Serial.println("\n");
Serial.println();
msg = ""; // reset command
}
delay(100);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment