diff --git a/README.md b/README.md index c7b2fa7..6a69761 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,32 @@ - - -# Project: {Fuzzy-logic-with-arduino} -This project uses Fuzzy Logic to determine water quality using readings from a Turbidity sensor and Electrical conductivity sensor -## Step 1: Installation -This code has been run and tested on Arduino IDE and Arduino Uno R3 - -Install the following libraries: - -1. eFLL (Fuzzy logic library) -2. DFRobot_EC -3. One wire and Dallas temprature -4. LCD I2C library - -## Step 2: Assemble the circuit -Here is a circuit on proteus - -![SVG image](./circuit.SVG) - - - - - -## Contributing -To contribute to this project please contact: ryankiprotich42@gmail.com + +# Project: {Fuzzy-logic-with-arduino} +This project uses Fuzzy Logic to determine water quality using readings from a Turbidity sensor and Electrical conductivity sensor +## Step 1: Installation +This code has been run and tested on Arduino IDE and Arduino Uno R3 + +Install the following libraries: + +1. eFLL (Fuzzy logic library) +2. DFRobot_EC +3. One wire and Dallas temprature +4. LCD I2C library + +## Step 2: Assemble the circuit +Here is a circuit on proteus + +![SVG image](./circuit.SVG) + + + + + +## Contributing +To contribute to this project please contact: ryankiprotich42@gmail.com + + + + + + +## Contributing +To contribute to this project please contact: ryankiprotich42@gmail.com diff --git a/fuzz.h b/fuzz.h index b381ac8..132c917 100644 --- a/fuzz.h +++ b/fuzz.h @@ -1,3 +1,4 @@ + #include #include #include @@ -24,6 +25,12 @@ void fuzzy_logic_init(){ FuzzySet *gutE = new FuzzySet(1.5, 2.0, 2.5, 3.0); FuzzySet *badE = new FuzzySet(2.5, 3.0, 5.0, 6.0); + // Water Temperature + FuzzySet *coldT = new FuzzySet(0, 0, 20, 30); + FuzzySet *moderateT = new FuzzySet(20, 30, 45, 55); + FuzzySet *warmT = new FuzzySet(45, 55, 75, 75); + + //water Q FuzzySet *exelW = new FuzzySet(70, 80, 100, 100); FuzzySet *gutW= new FuzzySet(30, 50, 70, 80); @@ -44,6 +51,15 @@ void fuzzy_logic_init(){ ec->addFuzzySet(badE); fuzzy->addFuzzyInput(ec); + // Water Temperature + FuzzyInput *waterTemperature = new FuzzyInput(3); + waterTemperature->addFuzzySet(coldT); + waterTemperature->addFuzzySet(moderateT); + waterTemperature->addFuzzySet(warmT); + fuzzy->addFuzzyInput(waterTemperature); + + + //water Quality FuzzyOutput *water = new FuzzyOutput(1); water->addFuzzySet(exelW); @@ -55,6 +71,7 @@ void fuzzy_logic_init(){ // rule1: if turbidity and EC are Excellecnt water Q is excellent FuzzyRuleAntecedent *rule1 =new FuzzyRuleAntecedent(); rule1->joinWithAND(exelT,exelE); + // rule1->joinWithAND(coldT); FuzzyRuleConsequent *waterEx = new FuzzyRuleConsequent(); waterEx->addOutput(exelW); FuzzyRule *fuzzyRule1 =new FuzzyRule(1,rule1,waterEx); @@ -63,6 +80,7 @@ void fuzzy_logic_init(){ //rule2: if tub and Ec are good water Q is goood FuzzyRuleAntecedent *rule2 =new FuzzyRuleAntecedent(); rule2->joinWithAND(gutT,gutE); + //rule2->joinWithAND(moderateT); FuzzyRuleConsequent *watergut = new FuzzyRuleConsequent(); watergut->addOutput(gutW); FuzzyRule *fuzzyRule2 =new FuzzyRule(2,rule2,watergut); @@ -71,6 +89,7 @@ void fuzzy_logic_init(){ //rule3 :if tub is good but Ec is ecclent water Q is good FuzzyRuleAntecedent *rule3 =new FuzzyRuleAntecedent(); rule3->joinWithAND(gutT,exelE); + //rule2->joinWithAND(moderateT); FuzzyRuleConsequent *watergut2 = new FuzzyRuleConsequent(); watergut2->addOutput(gutW); FuzzyRule *fuzzyRule3 =new FuzzyRule(3,rule3,watergut2); @@ -79,6 +98,7 @@ void fuzzy_logic_init(){ //rule 4 : if tub is excellent and Ec is good the water is good FuzzyRuleAntecedent *rule4 =new FuzzyRuleAntecedent(); rule4->joinWithAND(gutE,exelT); + // rule4->joinWithAND(moderateT); FuzzyRuleConsequent *watergut3 = new FuzzyRuleConsequent(); watergut3->addOutput(gutW); FuzzyRule *fuzzyRule4 =new FuzzyRule(4,rule4,watergut3); @@ -94,19 +114,28 @@ void fuzzy_logic_init(){ //rule 6 : if EC is bad water Q is bad FuzzyRuleAntecedent *rule6 =new FuzzyRuleAntecedent(); - rule5->joinSingle(badE); + rule6->joinSingle(badE); + FuzzyRuleConsequent *watergut5 = new FuzzyRuleConsequent(); + watergut5->addOutput(badW); + FuzzyRule *fuzzyRule6 =new FuzzyRule(6,rule6,watergut5); + fuzzy->addFuzzyRule(fuzzyRule6); + + //rule 7 waarm water bad water + FuzzyRuleAntecedent *rule7 =new FuzzyRuleAntecedent(); + rule7->joinSingle(warmT); FuzzyRuleConsequent *watergut6 = new FuzzyRuleConsequent(); watergut6->addOutput(badW); - FuzzyRule *fuzzyRule6 =new FuzzyRule(6,rule6,watergut6); - fuzzy->addFuzzyRule(fuzzyRule6); + FuzzyRule *fuzzyRule7 =new FuzzyRule(7,rule7,watergut6); + fuzzy->addFuzzyRule(fuzzyRule7); } -float fuzzy_result (float tub,float ec) +float fuzzy_result (float tub,float ec,float t) { // fuzzyfication - fuzzy->setInput(1, tub); // dist as fuzzy input 1 (distance) - fuzzy->setInput(2, ec); // light as fuzzy input 2 (ldr) + fuzzy->setInput(1, tub); + fuzzy->setInput(2, ec); + fuzzy->setInput(3,t); fuzzy->fuzzify(); // defuzzyfication diff --git a/gsm.h b/gsm.h new file mode 100644 index 0000000..8724a21 --- /dev/null +++ b/gsm.h @@ -0,0 +1,70 @@ + +#include + +//Create software serial object to communicate with SIM900 +SoftwareSerial mySerial(7, 8); //SIM900 Tx & Rx is connected to Arduino #7 & #8 +void updateSerial(); +void gsm_init() +{ + //Begin serial communication with Arduino and Arduino IDE (Serial Monitor) + + + //Begin serial communication with Arduino and SIM900 + mySerial.begin(9600); + + Serial.println("Initializing..."); + delay(1000); + + mySerial.println("AT"); //Handshaking with SIM900 + updateSerial(); + + mySerial.println("AT+CMGF=1"); // Configuring TEXT mode + updateSerial(); + mySerial.println("AT+CMGS=\"+254799453385\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms + updateSerial(); + +} +void updateSerial() +{ + delay(500); + while (Serial.available()) + { + mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port + } + while(mySerial.available()) + { + Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port + } +} +void send_gsm(int n) +{ int m; + if (n!=m) + { + switch(n) + { + case 1: + mySerial.print("Water Quality is Excellent"); //text content + updateSerial(); + mySerial.write(26); + break; + case 2 : + mySerial.print("Water Quality is Good"); //text content + updateSerial(); + mySerial.write(26); + break; + case 3 : + mySerial.print("Water Quality is Bad"); //text content + updateSerial(); + mySerial.write(26); + break; + default: + mySerial.print("Device Error"); //text content + updateSerial(); + mySerial.write(26); + + + } + m=n; + } +} + diff --git a/waterQfuzzy.ino b/waterQfuzzy.ino index 04ca586..992306c 100644 --- a/waterQfuzzy.ino +++ b/waterQfuzzy.ino @@ -1,19 +1,22 @@ -#include //fuzzy logic #include "fuzz.h" +//gsm +#include "gsm.h" //temperature #include #include -#define ONE_WIRE_BUS 4 //D4 +int ONE_WIRE_BUS =4; //D4 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); float get_temp() { sensors.requestTemperatures(); + delay(100); float temp=sensors.getTempCByIndex(0); + temp=24.0; return temp; } @@ -48,25 +51,45 @@ float get_ec () #include LiquidCrystal_I2C lcd(0x27,16,2); //sda A4, scl A5 + +#define relay 9 + +void stop_pump() +{ + digitalWrite(relay,HIGH); +} +void pump() +{ + digitalWrite(relay,LOW); +} + void setup() { // put your setup code here, to run once: + Serial.begin(9600); //initiating fuzzy logic fuzzy_logic_init(); //initializing temperature sensor sensors.begin(); + //ec sensor ec.begin(); + delay(100); + //lcd lcd.init(); //initialize the lcd lcd.backlight(); lcd.setCursor(0,0); lcd.print("Water Quality"); - delay(2000); + lcd.setCursor(0,1); + lcd.print("GROUP 3"); + gsm_init(); + delay(500); lcd.clear(); - Serial.begin(9600); - - + //pins + pinMode(EC_PIN,INPUT); + pinMode(relay,OUTPUT); + digitalWrite(relay,HIGH); } @@ -84,13 +107,36 @@ void loop() { Serial.print("EC: "); Serial.println(Ec); - float output= fuzzy_result(Tub,Ec);// tub ,ec respectively + float output= fuzzy_result(Tub,Ec,Temp);// tub ,ec respectively lcd.clear(); lcd.setCursor(0,0); - lcd.print("Output"); - lcd.setCursor(0,0); + lcd.print("Fuzzy T Ec"); + lcd.setCursor(0,1); lcd.print(output); + lcd.setCursor(5,1); + lcd.print(Tub); + lcd.setCursor(10,1); + lcd.print(Ec); + + + if (output >50) +{ + + pump(); + send_gsm(1); + +} +else if(output>30 && output <50){ + + pump(); + send_gsm(2); - delay(2000); + } +else if (output<30) +{ + stop_pump(); + send_gsm(3); + +} }