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 ledPin = 13; // Built in LED in Arduino board
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor String msg,cmd;
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
//TDS //TDS
#include <EEPROM.h> #include <EEPROM.h>
...@@ -21,7 +24,7 @@ float tdsValue = 0; ...@@ -21,7 +24,7 @@ float tdsValue = 0;
float temperature = 25; float temperature = 25;
int turbidity_str = 0;
//Turbidity //Turbidity
#define sensor_pin A1 #define sensor_pin A1
...@@ -31,6 +34,13 @@ float turbidity = 0.00; ...@@ -31,6 +34,13 @@ float turbidity = 0.00;
float Vclear = 2.85; // Output voltage to calibrate (with clear water). float Vclear = 2.85; // Output voltage to calibrate (with clear water).
int sensorValue = 0; 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;
...@@ -76,26 +86,41 @@ long microsecondsToCentimeters(long microseconds) { ...@@ -76,26 +86,41 @@ long microsecondsToCentimeters(long microseconds) {
void setup() 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(TdsSensorPin,INPUT);//TDS analog input A0
pinMode(sensor_pin, INPUT);//Turbifity analog input A1 pinMode(sensor_pin, INPUT);//Turbifity analog input A1
// Initialization
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
msg = "";
} }
void loop() void loop()
{ {
//Ultrasonic //Ultrasonic
long duration, inches, cm; //trigger a pulse-echo measurement
pinMode(pingPin, OUTPUT); digitalWrite(trig_pin, HIGH);
digitalWrite(pingPin, LOW); delayMicroseconds(10);
delayMicroseconds(2); digitalWrite(trig_pin, LOW);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10); //get the result
digitalWrite(pingPin, LOW); echotime= pulseIn(echo_pin, HIGH);
pinMode(echoPin, INPUT); distance= 0.0001*((float)echotime*340.0)/2.0;
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration); //send over Bluetooth
cm = microsecondsToCentimeters(duration); //Serial.print("*T"+String(echotime)+"*");
//Serial.print("*D"+String(distance,1)+"*");
delay(100);
//TDS //TDS
static unsigned long analogSampleTimepoint = millis(); static unsigned long analogSampleTimepoint = millis();
...@@ -139,8 +164,34 @@ sensorValue = analogRead(sensor_pin); ...@@ -139,8 +164,34 @@ sensorValue = analogRead(sensor_pin);
voltage = sensorValue * (5.000 / 1023.000); // Convert analog (0-1023) to voltage (0 - 5V) voltage = sensorValue * (5.000 / 1023.000); // Convert analog (0-1023) to voltage (0 - 5V)
turbidity = 100.00 - (voltage / Vclear) * 100.00; // as relative percentage; 0% = clear water; 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 //Ultrasinic
Serial.print("Distance To Water Level : {"); Serial.print("Distance To Water Level : {");
Serial.print(inches); Serial.print(inches);
...@@ -157,19 +208,54 @@ Serial.print("ppm }"); ...@@ -157,19 +208,54 @@ Serial.print("ppm }");
Serial.print(" Turbidity: "); Serial.print(" Turbidity: ");
Serial.print(turbidity); Serial.print(turbidity);
Serial.print(" "); Serial.print(" "); */
if(turbidity<-28) Serial.print("Water Very Clean "); if(turbidity<-28) turbidity_str = 1;
if(turbidity>=-28 && turbidity<-16) Serial.print("Water Norm Clean "); if(turbidity>=-28 && turbidity<-16) turbidity_str = 2;
if(turbidity>=-16) Serial.print("Water Very Dirty/Outside from water "); if(turbidity>=-16) turbidity_str = 3;
// 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);
}
Serial.println(); // 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");
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