Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHT Temperature #18

Closed
ryanescorial opened this issue May 24, 2019 · 13 comments
Closed

DHT Temperature #18

ryanescorial opened this issue May 24, 2019 · 13 comments

Comments

@ryanescorial
Copy link

#include <DHT.h>
#include <DHT_U.h>
#include <Fuzzy.h>

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
Fuzzy *fuzzy = new Fuzzy();

void setup() {
Serial.begin(9600);
while(!Serial){}

Serial.println("Comfort Meter");
dht.begin();

//FuzzyInput
FuzzyInput *temperature = new FuzzyInput(1);

//FuzzySets - Input
FuzzySet *verycold = new FuzzySet(-10, 0, 0, 10);
temperature->addFuzzySet(verycold);
FuzzySet *cold = new FuzzySet(5, 15, 15, 25);
temperature->addFuzzySet(cold);
FuzzySet *normal = new FuzzySet(20, 30, 30, 40);
temperature->addFuzzySet(normal);
FuzzySet *hot = new FuzzySet(35, 40, 50, 55);
temperature->addFuzzySet(hot);
fuzzy->addFuzzyInput(temperature);

//FuzzyOutput
FuzzyOutput *valve = new FuzzyOutput(1);

//FuzzySets - Output
FuzzySet *ten = new FuzzySet(0, 15, 15, 30);
valve->addFuzzySet(ten);

FuzzySet *fifty = new FuzzySet(25, 40, 40, 55);
valve->addFuzzySet(fifty);

FuzzySet *seventy = new FuzzySet(50, 65, 65, 80);
valve->addFuzzySet(seventy);

FuzzySet *openvalve = new FuzzySet(75, 88, 88, 100);
valve->addFuzzySet(openvalve);
fuzzy->addFuzzyOutput(valve);

//Fuzzy Rules
FuzzyRuleAntecedent *iftempverycold = new FuzzyRuleAntecedent();
iftempverycold->joinSingle(verycold);
FuzzyRuleConsequent *thenvalveten = new FuzzyRuleConsequent();
thenvalveten->addOutput(ten);
FuzzyRule *rule1 = new FuzzyRule(1, iftempverycold, thenvalveten);
fuzzy->addFuzzyRule(rule1);

FuzzyRuleAntecedent *iftempcold = new FuzzyRuleAntecedent();
iftempcold->joinSingle(cold);
FuzzyRuleConsequent *thenvalvefifty = new FuzzyRuleConsequent();
thenvalvefifty->addOutput(fifty);
FuzzyRule *rule2 = new FuzzyRule(1, iftempcold, thenvalvefifty);
fuzzy->addFuzzyRule(rule2);

FuzzyRuleAntecedent *iftempnormal = new FuzzyRuleAntecedent();
iftempnormal->joinSingle(normal);
FuzzyRuleConsequent *thenvalveseventy = new FuzzyRuleConsequent();
thenvalveseventy->addOutput(seventy);
FuzzyRule *rule3 = new FuzzyRule(1, iftempnormal, thenvalveseventy);
fuzzy->addFuzzyRule(rule3);

FuzzyRuleAntecedent *iftemphot = new FuzzyRuleAntecedent();
iftemphot->joinSingle(hot);
FuzzyRuleConsequent *thenvalveopenvalve = new FuzzyRuleConsequent();
thenvalveopenvalve->addOutput(seventy);
FuzzyRule *rule4 = new FuzzyRule(1, iftemphot, thenvalveopenvalve);
fuzzy->addFuzzyRule(rule4);
}

void loop() {
// put your main code here, to run repeatedly:

//float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t)){
Serial.println("Failed to read from DHT");
}
else{

  Serial.println("Temperature:");
  fuzzy->setInput(1, t);
  fuzzy->fuzzify();
  float output = fuzzy->defuzzify(1);
  Serial.println("Valve open:");
  Serial.println(output); 
 
}
delay(12000);

}

Anyone can help me regarding with my output. I'm still a newbie with this library. I want to fetch temperature value then print the result using the given fuzzy rule.

Capture

@alvesoaj
Copy link
Owner

I take a quick look in the code, It seems ok. Please, print the 't' input too, to see if it is changing along the time.

@ryanescorial
Copy link
Author

Thank you for your help. I'm testing now with 3 inputs and 1 output. By the way, is there any plot or graph available on this lib? Thanks.

@alvesoaj
Copy link
Owner

No, there are no plot or graph available, the aim of this library is to be very simple to run on micro-systems. To do that, just print the data in the terminal and format it using pyplot or other tool.

@ryanescorial
Copy link
Author

Hi Sir,

Thank you so much for your assistance and information. But I now stumble with my code.
It works fine with random inputs but when I tested it with actual sensors, I failed to receive the output.

#include <DHT.h>
#include <DHT_U.h>
#include <Fuzzy.h>

#define DHTPIN 2 //d2 pin
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

float sm;
float humid;
float temp;
int sensorPin = A0;

// Fuzzy
Fuzzy *fuzzy = new Fuzzy();

// FuzzyInput - Soil Moisture

FuzzySet *dry = new FuzzySet(-87, -60, -60, -34);
FuzzySet *normal = new FuzzySet(-44, -17, -17, 9);
FuzzySet *adeWet = new FuzzySet(0, 26, 26, 52);
FuzzySet *saturated = new FuzzySet(44, 66, 66, 87);

// FuzzyInput - Humidity
FuzzySet *low = new FuzzySet(0, 15, 15, 30);
FuzzySet *medium = new FuzzySet(25, 40, 40, 55);
FuzzySet *high = new FuzzySet(50, 65, 65, 80);
FuzzySet *exhigh = new FuzzySet(75, 88, 88, 100);

// FuzzyInput - Temperature
FuzzySet *cold = new FuzzySet(-30, -5, -5, 20);
FuzzySet *normal1 = new FuzzySet(10, 30, 30, 50);
FuzzySet *hot = new FuzzySet(40, 60, 60, 80);
FuzzySet *vhot = new FuzzySet(70, 95, 95, 120);

// FuzzyOutput - Pump
FuzzySet *ten = new FuzzySet(0, 10, 10, 20);
FuzzySet *tfive = new FuzzySet(15, 30, 30, 45);
FuzzySet *fifty = new FuzzySet(40, 55, 55, 70);
FuzzySet *sfive = new FuzzySet(65, 75, 75, 85);
FuzzySet *full = new FuzzySet(80, 90, 90, 100);

void setup()
{
// Set the Serial output
Serial.begin(9600);
// Set a random seed
randomSeed(analogRead(0));
Serial.print("Data from Sensors \n");
pinMode(DHTPIN, INPUT);//DHT sensor
dht.begin();

// FuzzyInput
FuzzyInput *soilM = new FuzzyInput(1);

soilM->addFuzzySet(dry);
soilM->addFuzzySet(normal);
soilM->addFuzzySet(adeWet);
soilM->addFuzzySet(saturated);
fuzzy->addFuzzyInput(soilM);

// FuzzyInput
FuzzyInput *humid = new FuzzyInput(2);

humid->addFuzzySet(low);
humid->addFuzzySet(medium);
humid->addFuzzySet(high);
humid->addFuzzySet(exhigh);
fuzzy->addFuzzyInput(humid);

// FuzzyInput
FuzzyInput *temp = new FuzzyInput(3);

temp->addFuzzySet(cold);
temp->addFuzzySet(normal1);
temp->addFuzzySet(hot);
temp->addFuzzySet(vhot);
fuzzy->addFuzzyInput(temp);

// FuzzyOutput
FuzzyOutput *valve = new FuzzyOutput(1);

valve->addFuzzySet(ten);
valve->addFuzzySet(tfive);
valve->addFuzzySet(fifty);
valve->addFuzzySet(sfive);
valve->addFuzzySet(full);
fuzzy->addFuzzyOutput(valve);

// Building FuzzyRule - 1
FuzzyRuleAntecedent *hlowAndtvhot = new FuzzyRuleAntecedent();
hlowAndtvhot->joinWithAND(low, vhot);
FuzzyRuleAntecedent *smdry1 = new FuzzyRuleAntecedent();
smdry1->joinSingle(dry);
FuzzyRuleAntecedent *ifsmdryAndhlowAndvhot = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndvhot->joinWithAND(hlowAndtvhot, smdry1);
FuzzyRuleConsequent *thenValveFull = new FuzzyRuleConsequent();
thenValveFull->addOutput(full);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifsmdryAndhlowAndvhot, thenValveFull);
fuzzy->addFuzzyRule(fuzzyRule1);

// Building FuzzyRule - 2
FuzzyRuleAntecedent *hlowAndthot = new FuzzyRuleAntecedent();
hlowAndthot->joinWithAND(low, hot);
FuzzyRuleAntecedent *ifsmdryAndhlowAndthot = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndthot->joinWithAND(hlowAndthot, smdry1);
FuzzyRuleConsequent *thenValveSfive2 = new FuzzyRuleConsequent();
thenValveSfive2->addOutput(sfive);
FuzzyRule *fuzzyRule2 = new FuzzyRule(1, ifsmdryAndhlowAndthot, thenValveSfive2);
fuzzy->addFuzzyRule(fuzzyRule2);

// Building FuzzyRule - 11

FuzzyRuleAntecedent *smsaturated11 = new FuzzyRuleAntecedent();
smsaturated11->joinSingle(saturated);
FuzzyRuleAntecedent *ifsmsaturated11AndhlowAndthot11 = new FuzzyRuleAntecedent();
ifsmsaturated11AndhlowAndthot11->joinWithAND(hlowAndthot, smsaturated11);
FuzzyRuleConsequent *thenValveTfive11 = new FuzzyRuleConsequent();
thenValveTfive11->addOutput(tfive);
FuzzyRule *fuzzyRule11 = new FuzzyRule(1, ifsmsaturated11AndhlowAndthot11, thenValveTfive11);
fuzzy->addFuzzyRule(fuzzyRule11);

// Building FuzzyRule - 8
FuzzyRuleAntecedent *smadeWet8 = new FuzzyRuleAntecedent();
smadeWet8->joinSingle(adeWet);
FuzzyRuleAntecedent *ifsmadeWetAndhlowAndthot8 = new FuzzyRuleAntecedent();
ifsmadeWetAndhlowAndthot8->joinWithAND(hlowAndthot, smadeWet8);
FuzzyRuleConsequent *thenValveTfive8 = new FuzzyRuleConsequent();
thenValveTfive8->addOutput(tfive);
FuzzyRule *fuzzyRule8 = new FuzzyRule(1, ifsmadeWetAndhlowAndthot8, thenValveTfive8);
fuzzy->addFuzzyRule(fuzzyRule8);

// Building FuzzyRule -3
FuzzyRuleAntecedent *hlowAndtnormal = new FuzzyRuleAntecedent();
hlowAndtnormal->joinWithAND(low, normal1);
FuzzyRuleAntecedent *ifsmdryAndhlowAndtnormal = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndtnormal->joinWithAND(hlowAndtnormal, smdry1);
FuzzyRuleConsequent *thenValveSfive3 = new FuzzyRuleConsequent();
thenValveSfive3->addOutput(sfive);
FuzzyRule *fuzzyRule3 = new FuzzyRule(1, ifsmdryAndhlowAndtnormal, thenValveSfive3);
fuzzy->addFuzzyRule(fuzzyRule3);

// Building FuzzyRule -4
FuzzyRuleAntecedent *hlowAndtcold = new FuzzyRuleAntecedent();
hlowAndtcold->joinWithAND(low, cold);
FuzzyRuleAntecedent *ifsmdryAndhlowAndtcold = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndtcold->joinWithAND(hlowAndtcold, smdry1);
FuzzyRuleConsequent *thenValveFifty = new FuzzyRuleConsequent();
thenValveFifty->addOutput(fifty);
FuzzyRule *fuzzyRule4 = new FuzzyRule(1, ifsmdryAndhlowAndtcold, thenValveFifty);
fuzzy->addFuzzyRule(fuzzyRule4);

// Building FuzzyRule -13
FuzzyRuleAntecedent *smsaturated13 = new FuzzyRuleAntecedent();
smsaturated13->joinSingle(saturated);
FuzzyRuleAntecedent *ifsmsaturated13AndhlowAndtcold13 = new FuzzyRuleAntecedent();
ifsmsaturated13AndhlowAndtcold13->joinWithAND(hlowAndtcold, smsaturated13);
FuzzyRuleConsequent *thenValveTen13 = new FuzzyRuleConsequent();
thenValveTen13->addOutput(ten);
FuzzyRule *fuzzyRule13 = new FuzzyRule(1, ifsmsaturated13AndhlowAndtcold13, thenValveTen13);
fuzzy->addFuzzyRule(fuzzyRule13);

// Building FuzzyRule -5
FuzzyRuleAntecedent *hmediumAndtcold = new FuzzyRuleAntecedent();
hmediumAndtcold->joinWithAND(medium, cold);
FuzzyRuleAntecedent *smnormal = new FuzzyRuleAntecedent();
smnormal->joinSingle(normal);
FuzzyRuleAntecedent *ifsmnormalAndhmediumAndtcold = new FuzzyRuleAntecedent();
ifsmnormalAndhmediumAndtcold->joinWithAND(hmediumAndtcold, smnormal);
FuzzyRuleConsequent *thenValveSfive5 = new FuzzyRuleConsequent();
thenValveSfive5->addOutput(sfive);
FuzzyRule *fuzzyRule5 = new FuzzyRule(1, ifsmnormalAndhmediumAndtcold, thenValveSfive5);
fuzzy->addFuzzyRule(fuzzyRule5);

// Building FuzzyRule -6
FuzzyRuleAntecedent *hhighAndtnormal6 = new FuzzyRuleAntecedent();
hhighAndtnormal6->joinWithAND(high, normal1);
FuzzyRuleAntecedent *ifsmnormalAndhhighAndtnormal6 = new FuzzyRuleAntecedent();
ifsmnormalAndhhighAndtnormal6->joinWithAND(hhighAndtnormal6, smnormal);
FuzzyRuleConsequent *thenValveFifty6 = new FuzzyRuleConsequent();
thenValveFifty6->addOutput(fifty);
FuzzyRule *fuzzyRule6 = new FuzzyRule(1, ifsmnormalAndhhighAndtnormal6, thenValveFifty6);
fuzzy->addFuzzyRule(fuzzyRule6);

// Building FuzzyRule -7
FuzzyRuleAntecedent *hexhighAndthot7 = new FuzzyRuleAntecedent();
hexhighAndthot7->joinWithAND(exhigh, hot);
FuzzyRuleAntecedent *ifsmnormalAndhexhighAndthot7 = new FuzzyRuleAntecedent();
ifsmnormalAndhexhighAndthot7->joinWithAND(hexhighAndthot7, smnormal);
FuzzyRuleConsequent *thenValveSfive7 = new FuzzyRuleConsequent();
thenValveSfive7->addOutput(sfive);
FuzzyRule *fuzzyRule7 = new FuzzyRule(1, ifsmnormalAndhexhighAndthot7, thenValveSfive7);
fuzzy->addFuzzyRule(fuzzyRule7);

// Building FuzzyRule -17
FuzzyRuleAntecedent *ifsmsaturated17AndhexhighAndthot17 = new FuzzyRuleAntecedent();
ifsmsaturated17AndhexhighAndthot17->joinWithAND(hexhighAndthot7, smsaturated11);
FuzzyRuleConsequent *thenValveTfive17 = new FuzzyRuleConsequent();
thenValveTfive17->addOutput(tfive);
FuzzyRule *fuzzyRule17 = new FuzzyRule(1, ifsmsaturated17AndhexhighAndthot17, thenValveTfive17);
fuzzy->addFuzzyRule(fuzzyRule17);

// Building FuzzyRule -9
FuzzyRuleAntecedent *hmediumAndthot9 = new FuzzyRuleAntecedent();
hmediumAndthot9->joinWithAND(medium, hot);
FuzzyRuleAntecedent *ifsmadeWetAndhmediumAndthot9 = new FuzzyRuleAntecedent();
ifsmadeWetAndhmediumAndthot9->joinWithAND(hmediumAndthot9, smadeWet8);
FuzzyRuleConsequent *thenValveTfive9 = new FuzzyRuleConsequent();
thenValveTfive9->addOutput(tfive);
FuzzyRule *fuzzyRule9 = new FuzzyRule(1, ifsmadeWetAndhmediumAndthot9, thenValveTfive9);
fuzzy->addFuzzyRule(fuzzyRule9);

// Building FuzzyRule -12

FuzzyRuleAntecedent *ifsmsaturated12AndhmediumAndthot12 = new FuzzyRuleAntecedent();
ifsmsaturated12AndhmediumAndthot12->joinWithAND(hmediumAndthot9, smsaturated11);
FuzzyRuleConsequent *thenValveTfive12 = new FuzzyRuleConsequent();
thenValveTfive12->addOutput(tfive);
FuzzyRule *fuzzyRule12 = new FuzzyRule(1, ifsmsaturated12AndhmediumAndthot12, thenValveTfive12);
fuzzy->addFuzzyRule(fuzzyRule12);

// Building FuzzyRule -10
FuzzyRuleAntecedent *hhighAndthot10 = new FuzzyRuleAntecedent();
hhighAndthot10->joinWithAND(high, hot);
FuzzyRuleAntecedent *ifsmadeWetAndhhighAndthot10 = new FuzzyRuleAntecedent();
ifsmadeWetAndhhighAndthot10->joinWithAND(hhighAndthot10, smadeWet8);
FuzzyRuleConsequent *thenValveTfive10 = new FuzzyRuleConsequent();
thenValveTfive10->addOutput(tfive);
FuzzyRule *fuzzyRule10 = new FuzzyRule(1, ifsmadeWetAndhhighAndthot10, thenValveTfive10);
fuzzy->addFuzzyRule(fuzzyRule10);

// Building FuzzyRule -14
FuzzyRuleAntecedent *hexhighAndtcold14 = new FuzzyRuleAntecedent();
hexhighAndtcold14->joinWithAND(exhigh, cold);
FuzzyRuleAntecedent *ifsmsaturated14AndhexhighAndtcold14 = new FuzzyRuleAntecedent();
ifsmsaturated14AndhexhighAndtcold14->joinWithAND(hexhighAndtcold14, smsaturated11);
FuzzyRuleConsequent *thenValveTen14 = new FuzzyRuleConsequent();
thenValveTen14->addOutput(ten);
FuzzyRule *fuzzyRule14 = new FuzzyRule(1, ifsmsaturated14AndhexhighAndtcold14, thenValveTen14);
fuzzy->addFuzzyRule(fuzzyRule14);

// Building FuzzyRule -16

FuzzyRuleAntecedent *ifsmsaturated16AndhexhighAndtcold16 = new FuzzyRuleAntecedent();
ifsmsaturated16AndhexhighAndtcold16->joinWithAND(hexhighAndtcold14, smsaturated11);
FuzzyRuleConsequent *thenValveTen16 = new FuzzyRuleConsequent();
thenValveTen16->addOutput(ten);
FuzzyRule *fuzzyRule16 = new FuzzyRule(1, ifsmsaturated16AndhexhighAndtcold16, thenValveTen16);
fuzzy->addFuzzyRule(fuzzyRule16);

// Building FuzzyRule -15
FuzzyRuleAntecedent *hhighAndtcold15 = new FuzzyRuleAntecedent();
hhighAndtcold15->joinWithAND(high, cold);
FuzzyRuleAntecedent *ifsmadeWet15AndhhighAndtcold15 = new FuzzyRuleAntecedent();
ifsmadeWet15AndhhighAndtcold15->joinWithAND(hhighAndtcold15, smadeWet8);
FuzzyRuleConsequent *thenValveTen15 = new FuzzyRuleConsequent();
thenValveTen15->addOutput(ten);
FuzzyRule *fuzzyRule15 = new FuzzyRule(1, ifsmadeWet15AndhhighAndtcold15, thenValveTen15);
fuzzy->addFuzzyRule(fuzzyRule15);

}

void loop()
{
// get random input
// sm = random(-87, 87);
// humid = random(0, 100);
// temp = random(-30, 120);

sm = analogRead(sensorPin);
sm = map(sm,550,10,0,100);
humid = dht.readHumidity();
temp = dht.readTemperature();

Serial.print("Soil Moisture: ");
Serial.print(sm);
Serial.print("%");
Serial.print(", Humidity: ");
Serial.print(humid);
Serial.print("%");
Serial.print(", and Temperature: ");
Serial.println(temp);
Serial.print("C");

fuzzy->setInput(1, sm);
fuzzy->setInput(2, humid);
fuzzy->setInput(3, temp);

fuzzy->fuzzify();
float output1 = fuzzy->defuzzify(1);

Serial.print("\t\t\t\nPumping: ");
Serial.print(output1);
Serial.print("\n");

delay(10000);

}

output problems

@alvesoaj
Copy link
Owner

alvesoaj commented May 28, 2019 via email

@ryanescorial
Copy link
Author

Yes, the output in random inputs are correct but when it comes to actual sensors, it always gives zero percent result. BTW, I'm using Arduino Uno.

@alvesoaj
Copy link
Owner

alvesoaj commented Jun 5, 2019

Hello sorry for late reply, I am in vacations. It is very very strange, I wish to test your code and I will do it as soon as possible, I am going back to my home in the next week. I have some temperature and moisture sensors to reproduce your problem.

@ryanescorial
Copy link
Author

Hi, I am also wondering about it. I can't trace the real problem if it is in the circuits or codes. I will wait for your feedback about your testing. Thank you so much for taking time in my problem.

@alvesoaj
Copy link
Owner

alvesoaj commented Jun 6, 2019 via email

@vinayakfaraday
Copy link

can we provide digital data instead of analog data from sensors. For example you can take BH1750 light sensor which gives digital data. Will the library work?

@alvesoaj
Copy link
Owner

Yes, it should work!

@ryanescorial
Copy link
Author

Hi, do you have any update about your testing?

@alvesoaj
Copy link
Owner

Hello @ryanescorial, sorry for a very late response, after my vacation i found looots of work.

I tested your code, I found the "question". There are a lack of FuzzyRules to cover all possibilities, I modified your code to check which FuzzeRules is fired in each interaction, take a look! I made small modifications in your code (The sensor version and I included a unique identifier to each FuzzyRule)

#include <DHT.h>
#include <DHT_U.h>
#include <Fuzzy.h>

#define DHTPIN 2      //d2 pin
#define DHTTYPE DHT11 // it was DHT22

DHT dht(DHTPIN, DHTTYPE);

float sm;
float humid;
float temp;
int sensorPin = A0;

// Fuzzy
Fuzzy *fuzzy = new Fuzzy();

// FuzzyInput - Soil Moisture
FuzzySet *dry = new FuzzySet(-87, -60, -60, -34);
FuzzySet *normal = new FuzzySet(-44, -17, -17, 9);
FuzzySet *adeWet = new FuzzySet(0, 26, 26, 52);
FuzzySet *saturated = new FuzzySet(44, 66, 66, 87);

// FuzzyInput - Humidity
FuzzySet *low = new FuzzySet(0, 15, 15, 30);
FuzzySet *medium = new FuzzySet(25, 40, 40, 55);
FuzzySet *high = new FuzzySet(50, 65, 65, 80);
FuzzySet *exhigh = new FuzzySet(75, 88, 88, 100);

// FuzzyInput - Temperature
FuzzySet *cold = new FuzzySet(-30, -5, -5, 20);
FuzzySet *normal1 = new FuzzySet(10, 30, 30, 50);
FuzzySet *hot = new FuzzySet(40, 60, 60, 80);
FuzzySet *vhot = new FuzzySet(70, 95, 95, 120);

// FuzzyOutput - Pump
FuzzySet *ten = new FuzzySet(0, 10, 10, 20);
FuzzySet *tfive = new FuzzySet(15, 30, 30, 45);
FuzzySet *fifty = new FuzzySet(40, 55, 55, 70);
FuzzySet *sfive = new FuzzySet(65, 75, 75, 85);
FuzzySet *full = new FuzzySet(80, 90, 90, 100);

void setup()
{
  // Set the Serial output
  Serial.begin(9600);
  // Set a random seed
  randomSeed(analogRead(0));
  Serial.print("Data from Sensors \n");
  pinMode(DHTPIN, INPUT); //DHT sensor
  dht.begin();

  // FuzzyInput
  FuzzyInput *soilM = new FuzzyInput(1);

  soilM->addFuzzySet(dry);
  soilM->addFuzzySet(normal);
  soilM->addFuzzySet(adeWet);
  soilM->addFuzzySet(saturated);
  fuzzy->addFuzzyInput(soilM);

  // FuzzyInput
  FuzzyInput *humid = new FuzzyInput(2);

  humid->addFuzzySet(low);
  humid->addFuzzySet(medium);
  humid->addFuzzySet(high);
  humid->addFuzzySet(exhigh);
  fuzzy->addFuzzyInput(humid);

  // FuzzyInput
  FuzzyInput *temp = new FuzzyInput(3);

  temp->addFuzzySet(cold);
  temp->addFuzzySet(normal1);
  temp->addFuzzySet(hot);
  temp->addFuzzySet(vhot);
  fuzzy->addFuzzyInput(temp);

  // FuzzyOutput
  FuzzyOutput *valve = new FuzzyOutput(1);

  valve->addFuzzySet(ten);
  valve->addFuzzySet(tfive);
  valve->addFuzzySet(fifty);
  valve->addFuzzySet(sfive);
  valve->addFuzzySet(full);
  fuzzy->addFuzzyOutput(valve);

  // Building FuzzyRule - 1
  FuzzyRuleAntecedent *hlowAndtvhot = new FuzzyRuleAntecedent();
  hlowAndtvhot->joinWithAND(low, vhot);
  FuzzyRuleAntecedent *smdry1 = new FuzzyRuleAntecedent();
  smdry1->joinSingle(dry);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndvhot = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndvhot->joinWithAND(hlowAndtvhot, smdry1);
  FuzzyRuleConsequent *thenValveFull = new FuzzyRuleConsequent();
  thenValveFull->addOutput(full);
  FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifsmdryAndhlowAndvhot, thenValveFull);
  fuzzy->addFuzzyRule(fuzzyRule1);

  // Building FuzzyRule - 2
  FuzzyRuleAntecedent *hlowAndthot = new FuzzyRuleAntecedent();
  hlowAndthot->joinWithAND(low, hot);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndthot = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndthot->joinWithAND(hlowAndthot, smdry1);
  FuzzyRuleConsequent *thenValveSfive2 = new FuzzyRuleConsequent();
  thenValveSfive2->addOutput(sfive);
  FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifsmdryAndhlowAndthot, thenValveSfive2);
  fuzzy->addFuzzyRule(fuzzyRule2);

  // Building FuzzyRule - 11
  FuzzyRuleAntecedent *smsaturated11 = new FuzzyRuleAntecedent();
  smsaturated11->joinSingle(saturated);
  FuzzyRuleAntecedent *ifsmsaturated11AndhlowAndthot11 = new FuzzyRuleAntecedent();
  ifsmsaturated11AndhlowAndthot11->joinWithAND(hlowAndthot, smsaturated11);
  FuzzyRuleConsequent *thenValveTfive11 = new FuzzyRuleConsequent();
  thenValveTfive11->addOutput(tfive);
  FuzzyRule *fuzzyRule11 = new FuzzyRule(11, ifsmsaturated11AndhlowAndthot11, thenValveTfive11);
  fuzzy->addFuzzyRule(fuzzyRule11);

  // Building FuzzyRule - 8
  FuzzyRuleAntecedent *smadeWet8 = new FuzzyRuleAntecedent();
  smadeWet8->joinSingle(adeWet);
  FuzzyRuleAntecedent *ifsmadeWetAndhlowAndthot8 = new FuzzyRuleAntecedent();
  ifsmadeWetAndhlowAndthot8->joinWithAND(hlowAndthot, smadeWet8);
  FuzzyRuleConsequent *thenValveTfive8 = new FuzzyRuleConsequent();
  thenValveTfive8->addOutput(tfive);
  FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifsmadeWetAndhlowAndthot8, thenValveTfive8);
  fuzzy->addFuzzyRule(fuzzyRule8);

  // Building FuzzyRule -3
  FuzzyRuleAntecedent *hlowAndtnormal = new FuzzyRuleAntecedent();
  hlowAndtnormal->joinWithAND(low, normal1);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndtnormal = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndtnormal->joinWithAND(hlowAndtnormal, smdry1);
  FuzzyRuleConsequent *thenValveSfive3 = new FuzzyRuleConsequent();
  thenValveSfive3->addOutput(sfive);
  FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifsmdryAndhlowAndtnormal, thenValveSfive3);
  fuzzy->addFuzzyRule(fuzzyRule3);

  // Building FuzzyRule -4
  FuzzyRuleAntecedent *hlowAndtcold = new FuzzyRuleAntecedent();
  hlowAndtcold->joinWithAND(low, cold);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndtcold = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndtcold->joinWithAND(hlowAndtcold, smdry1);
  FuzzyRuleConsequent *thenValveFifty = new FuzzyRuleConsequent();
  thenValveFifty->addOutput(fifty);
  FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifsmdryAndhlowAndtcold, thenValveFifty);
  fuzzy->addFuzzyRule(fuzzyRule4);

  // Building FuzzyRule -13
  FuzzyRuleAntecedent *smsaturated13 = new FuzzyRuleAntecedent();
  smsaturated13->joinSingle(saturated);
  FuzzyRuleAntecedent *ifsmsaturated13AndhlowAndtcold13 = new FuzzyRuleAntecedent();
  ifsmsaturated13AndhlowAndtcold13->joinWithAND(hlowAndtcold, smsaturated13);
  FuzzyRuleConsequent *thenValveTen13 = new FuzzyRuleConsequent();
  thenValveTen13->addOutput(ten);
  FuzzyRule *fuzzyRule13 = new FuzzyRule(13, ifsmsaturated13AndhlowAndtcold13, thenValveTen13);
  fuzzy->addFuzzyRule(fuzzyRule13);

  // Building FuzzyRule -5
  FuzzyRuleAntecedent *hmediumAndtcold = new FuzzyRuleAntecedent();
  hmediumAndtcold->joinWithAND(medium, cold);
  FuzzyRuleAntecedent *smnormal = new FuzzyRuleAntecedent();
  smnormal->joinSingle(normal);
  FuzzyRuleAntecedent *ifsmnormalAndhmediumAndtcold = new FuzzyRuleAntecedent();
  ifsmnormalAndhmediumAndtcold->joinWithAND(hmediumAndtcold, smnormal);
  FuzzyRuleConsequent *thenValveSfive5 = new FuzzyRuleConsequent();
  thenValveSfive5->addOutput(sfive);
  FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifsmnormalAndhmediumAndtcold, thenValveSfive5);
  fuzzy->addFuzzyRule(fuzzyRule5);

  // Building FuzzyRule -6
  FuzzyRuleAntecedent *hhighAndtnormal6 = new FuzzyRuleAntecedent();
  hhighAndtnormal6->joinWithAND(high, normal1);
  FuzzyRuleAntecedent *ifsmnormalAndhhighAndtnormal6 = new FuzzyRuleAntecedent();
  ifsmnormalAndhhighAndtnormal6->joinWithAND(hhighAndtnormal6, smnormal);
  FuzzyRuleConsequent *thenValveFifty6 = new FuzzyRuleConsequent();
  thenValveFifty6->addOutput(fifty);
  FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifsmnormalAndhhighAndtnormal6, thenValveFifty6);
  fuzzy->addFuzzyRule(fuzzyRule6);

  // Building FuzzyRule -7
  FuzzyRuleAntecedent *hexhighAndthot7 = new FuzzyRuleAntecedent();
  hexhighAndthot7->joinWithAND(exhigh, hot);
  FuzzyRuleAntecedent *ifsmnormalAndhexhighAndthot7 = new FuzzyRuleAntecedent();
  ifsmnormalAndhexhighAndthot7->joinWithAND(hexhighAndthot7, smnormal);
  FuzzyRuleConsequent *thenValveSfive7 = new FuzzyRuleConsequent();
  thenValveSfive7->addOutput(sfive);
  FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifsmnormalAndhexhighAndthot7, thenValveSfive7);
  fuzzy->addFuzzyRule(fuzzyRule7);

  // Building FuzzyRule -17
  FuzzyRuleAntecedent *ifsmsaturated17AndhexhighAndthot17 = new FuzzyRuleAntecedent();
  ifsmsaturated17AndhexhighAndthot17->joinWithAND(hexhighAndthot7, smsaturated11);
  FuzzyRuleConsequent *thenValveTfive17 = new FuzzyRuleConsequent();
  thenValveTfive17->addOutput(tfive);
  FuzzyRule *fuzzyRule17 = new FuzzyRule(17, ifsmsaturated17AndhexhighAndthot17, thenValveTfive17);
  fuzzy->addFuzzyRule(fuzzyRule17);

  // Building FuzzyRule -9
  FuzzyRuleAntecedent *hmediumAndthot9 = new FuzzyRuleAntecedent();
  hmediumAndthot9->joinWithAND(medium, hot);
  FuzzyRuleAntecedent *ifsmadeWetAndhmediumAndthot9 = new FuzzyRuleAntecedent();
  ifsmadeWetAndhmediumAndthot9->joinWithAND(hmediumAndthot9, smadeWet8);
  FuzzyRuleConsequent *thenValveTfive9 = new FuzzyRuleConsequent();
  thenValveTfive9->addOutput(tfive);
  FuzzyRule *fuzzyRule9 = new FuzzyRule(9, ifsmadeWetAndhmediumAndthot9, thenValveTfive9);
  fuzzy->addFuzzyRule(fuzzyRule9);

  // Building FuzzyRule -12

  FuzzyRuleAntecedent *ifsmsaturated12AndhmediumAndthot12 = new FuzzyRuleAntecedent();
  ifsmsaturated12AndhmediumAndthot12->joinWithAND(hmediumAndthot9, smsaturated11);
  FuzzyRuleConsequent *thenValveTfive12 = new FuzzyRuleConsequent();
  thenValveTfive12->addOutput(tfive);
  FuzzyRule *fuzzyRule12 = new FuzzyRule(12, ifsmsaturated12AndhmediumAndthot12, thenValveTfive12);
  fuzzy->addFuzzyRule(fuzzyRule12);

  // Building FuzzyRule -10
  FuzzyRuleAntecedent *hhighAndthot10 = new FuzzyRuleAntecedent();
  hhighAndthot10->joinWithAND(high, hot);
  FuzzyRuleAntecedent *ifsmadeWetAndhhighAndthot10 = new FuzzyRuleAntecedent();
  ifsmadeWetAndhhighAndthot10->joinWithAND(hhighAndthot10, smadeWet8);
  FuzzyRuleConsequent *thenValveTfive10 = new FuzzyRuleConsequent();
  thenValveTfive10->addOutput(tfive);
  FuzzyRule *fuzzyRule10 = new FuzzyRule(10, ifsmadeWetAndhhighAndthot10, thenValveTfive10);
  fuzzy->addFuzzyRule(fuzzyRule10);

  // Building FuzzyRule -14
  FuzzyRuleAntecedent *hexhighAndtcold14 = new FuzzyRuleAntecedent();
  hexhighAndtcold14->joinWithAND(exhigh, cold);
  FuzzyRuleAntecedent *ifsmsaturated14AndhexhighAndtcold14 = new FuzzyRuleAntecedent();
  ifsmsaturated14AndhexhighAndtcold14->joinWithAND(hexhighAndtcold14, smsaturated11);
  FuzzyRuleConsequent *thenValveTen14 = new FuzzyRuleConsequent();
  thenValveTen14->addOutput(ten);
  FuzzyRule *fuzzyRule14 = new FuzzyRule(14, ifsmsaturated14AndhexhighAndtcold14, thenValveTen14);
  fuzzy->addFuzzyRule(fuzzyRule14);

  // Building FuzzyRule -16

  FuzzyRuleAntecedent *ifsmsaturated16AndhexhighAndtcold16 = new FuzzyRuleAntecedent();
  ifsmsaturated16AndhexhighAndtcold16->joinWithAND(hexhighAndtcold14, smsaturated11);
  FuzzyRuleConsequent *thenValveTen16 = new FuzzyRuleConsequent();
  thenValveTen16->addOutput(ten);
  FuzzyRule *fuzzyRule16 = new FuzzyRule(16, ifsmsaturated16AndhexhighAndtcold16, thenValveTen16);
  fuzzy->addFuzzyRule(fuzzyRule16);

  // Building FuzzyRule -15
  FuzzyRuleAntecedent *hhighAndtcold15 = new FuzzyRuleAntecedent();
  hhighAndtcold15->joinWithAND(high, cold);
  FuzzyRuleAntecedent *ifsmadeWet15AndhhighAndtcold15 = new FuzzyRuleAntecedent();
  ifsmadeWet15AndhhighAndtcold15->joinWithAND(hhighAndtcold15, smadeWet8);
  FuzzyRuleConsequent *thenValveTen15 = new FuzzyRuleConsequent();
  thenValveTen15->addOutput(ten);
  FuzzyRule *fuzzyRule15 = new FuzzyRule(15, ifsmadeWet15AndhhighAndtcold15, thenValveTen15);
  fuzzy->addFuzzyRule(fuzzyRule15);
}

void loop()
{
  // get random input
  // sm = random(-87, 87);
  // humid = random(0, 100);
  // temp = random(-30, 120);

  sm = map(analogRead(sensorPin), 550, 10, 0, 100);
  humid = dht.readHumidity();
  temp = dht.readTemperature();

  Serial.print("Soil Moisture: ");
  Serial.print(sm);
  Serial.print("%");
  Serial.print(", Humidity: ");
  Serial.print(humid);
  Serial.print("%");
  Serial.print(", and Temperature: ");
  Serial.print(temp);
  Serial.println("C");

  fuzzy->setInput(1, sm);
  fuzzy->setInput(2, humid);
  fuzzy->setInput(3, temp);

  fuzzy->fuzzify();
  float output1 = fuzzy->defuzzify(1);

  Serial.print("\t\t\t\nPumping: ");
  Serial.print(output1);
  Serial.print("\n");

  Serial.print("Fired Rules: ");
  for (size_t i = 1; i <= 17; i++)
  {
    if (fuzzy->isFiredRule(i) == 1)
    {
      Serial.print(i);
      Serial.print(", ");
    }
  }
  Serial.print("\n");

  delay(10000);
}

I hope it still can help you!

@alvesoaj alvesoaj closed this as completed Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants