0

Can somebody help me , when i run this code separately , no problem was found , but when i fuse it together , only fuzzy work and the data can't be sent to my server, sorry for my bad english

i already tried to separate them with function and give delay to make time for posting data , but maybe that not the real problem

#include "SoftwareSerial.h"
#include <dht.h>
#include <Fuzzy.h>
#define DEBUG true
#define dht_apin A1 // Analog Pin sensor is connected to
SoftwareSerial esp(6, 7);// RX, TX
String ssid = "BU SIS";
String password = "Bismillah";
dht DHT;
// Fuzzy
Fuzzy *fuzzy = new Fuzzy();
// FuzzyInputKudara
FuzzySet *urendah = new FuzzySet(0, 50, 50, 50);
FuzzySet *usedang = new FuzzySet(45, 60, 60, 75);
FuzzySet *utinggi = new FuzzySet(70, 70, 100, 100);
// FuzzyInputKtanah
FuzzySet *trendah = new FuzzySet(0, 50, 50, 50);
FuzzySet *tsedang = new FuzzySet(45, 60, 60, 75);
FuzzySet *ttinggi = new FuzzySet(70, 70, 100, 100);
// FuzzyInputSudara
FuzzySet *srendah = new FuzzySet(20, 27, 27, 31);
FuzzySet *stinggi = new FuzzySet(30, 35, 40, 40);
// FuzzyOutputKondisi
FuzzySet *tidak = new FuzzySet(0, 25, 25, 50);
FuzzySet *siram = new FuzzySet(50, 80, 100, 100);
String data;
String server = "192.168.100.11";
String uri = "/web/insertx.php";
String temp , hum;
int ftemp, fhum;
int tanah;
void setup() {
  Serial.begin(9600);
  esp.begin(9600);
  // Reset modul
  sendCommand("AT+RST\r\n", 1000, DEBUG);
  // Konfigurasikan sebagai Akses poin & Wifi
  sendCommand("AT+CWMODE=3\r\n", 1000, DEBUG);
  // Sesuaikan dengan SSID dan Password
  sendCommand("AT+CWJAP=\"BU SIS\",\"Bismillah\"\r\n", 10000, DEBUG);
  // Mendapatkan IP adress
  sendCommand("AT+CIFSR\r\n", 1000, DEBUG);
  // Konfigurasikan untuk multiple connection
  sendCommand("AT+CIPMUX=1\r\n", 1000, DEBUG);
    //   FuzzyInput
    FuzzyInput *kudara = new FuzzyInput(1);
    kudara->addFuzzySet(urendah);
    kudara->addFuzzySet(usedang);
    kudara->addFuzzySet(utinggi);
    fuzzy->addFuzzyInput(kudara);
    // FuzzyInput
    FuzzyInput *ktanah = new FuzzyInput(2);
    ktanah->addFuzzySet(trendah);
    ktanah->addFuzzySet(tsedang);
    ktanah->addFuzzySet(ttinggi);
    fuzzy->addFuzzyInput(ktanah);
    // FuzzyInput
    FuzzyInput *sudara = new FuzzyInput(3);
    sudara->addFuzzySet(srendah);
    sudara->addFuzzySet(stinggi);
    fuzzy->addFuzzyInput(sudara);
    // FuzzyOutput
    FuzzyOutput *kondisi = new FuzzyOutput(1);
    kondisi->addFuzzySet(tidak);
    kondisi->addFuzzySet(siram);
    fuzzy->addFuzzyOutput(kondisi);
    // Building FuzzyRule 3
    FuzzyRuleAntecedent *a3 = new FuzzyRuleAntecedent();
    a3->joinWithAND(urendah, tsedang);
    FuzzyRuleAntecedent *ktanah3 = new FuzzyRuleAntecedent();
    ktanah3->joinSingle(srendah);
    FuzzyRuleAntecedent *aa3 = new FuzzyRuleAntecedent();
    aa3->joinWithAND(a3, ktanah3);
    FuzzyRuleConsequent *maka3 = new FuzzyRuleConsequent();
    maka3->addOutput(siram);
    FuzzyRule *fuzzyRule3 = new FuzzyRule(3, aa3, maka3);
    fuzzy->addFuzzyRule(fuzzyRule3);
    // Building FuzzyRule 5
    FuzzyRuleAntecedent *a5 = new FuzzyRuleAntecedent();
    a5->joinWithAND(urendah, ttinggi);
    FuzzyRuleAntecedent *ktanah5 = new FuzzyRuleAntecedent();
    ktanah5->joinSingle(srendah);
    FuzzyRuleAntecedent *aa5 = new FuzzyRuleAntecedent();
    aa5->joinWithAND(a5, ktanah5);
    FuzzyRuleConsequent *maka5 = new FuzzyRuleConsequent();
    maka5->addOutput(siram);
    FuzzyRule *fuzzyRule5 = new FuzzyRule(5, aa5, maka5);
    fuzzy->addFuzzyRule(fuzzyRule5);
    // Building FuzzyRule 9
    FuzzyRuleAntecedent *a9 = new FuzzyRuleAntecedent();
    a9->joinWithAND(usedang, tsedang);
    FuzzyRuleAntecedent *ktanah9 = new FuzzyRuleAntecedent();
    ktanah9->joinSingle(srendah);
    FuzzyRuleAntecedent *aa9 = new FuzzyRuleAntecedent();
    aa9->joinWithAND(a9, ktanah9);
    FuzzyRuleConsequent *maka9 = new FuzzyRuleConsequent();
    maka9->addOutput(siram);
    FuzzyRule *fuzzyRule9 = new FuzzyRule(9, aa9, maka9);
    fuzzy->addFuzzyRule(fuzzyRule9);
    //   Building FuzzyRule 11
    FuzzyRuleAntecedent *a11 = new FuzzyRuleAntecedent();
    a11->joinWithAND(usedang, ttinggi);
    FuzzyRuleAntecedent *ktanaha11 = new FuzzyRuleAntecedent();
    ktanaha11->joinSingle(srendah);
    FuzzyRuleAntecedent *aa11 = new FuzzyRuleAntecedent();
    aa11->joinWithAND(a11, ktanaha11);
    FuzzyRuleConsequent *maka11 = new FuzzyRuleConsequent();
    maka11->addOutput(siram);
    FuzzyRule *fuzzyRule11 = new FuzzyRule(11, aa11, maka11);
    fuzzy->addFuzzyRule(fuzzyRule11);
    // Building FuzzyRule 12
    FuzzyRuleAntecedent *a12 = new FuzzyRuleAntecedent();
    a12->joinWithAND(usedang, stinggi);
    FuzzyRuleAntecedent *ktanaha2 = new FuzzyRuleAntecedent();
    ktanaha2->joinSingle(ttinggi);
    FuzzyRuleAntecedent *aa12 = new FuzzyRuleAntecedent();
    aa12->joinWithAND(a12, ktanaha2);
    FuzzyRuleConsequent *makaa2 = new FuzzyRuleConsequent();
    makaa2->addOutput(siram);
    FuzzyRule *fuzzyRule12 = new FuzzyRule(12, aa12, makaa2);
    fuzzy->addFuzzyRule(fuzzyRule12);
    // Building FuzzyRule 15
    FuzzyRuleAntecedent *a15 = new FuzzyRuleAntecedent();
    a15->joinWithAND(utinggi, srendah);
    FuzzyRuleAntecedent *ktanaha5 = new FuzzyRuleAntecedent();
    ktanaha5->joinSingle(tsedang);
    FuzzyRuleAntecedent *aa15 = new FuzzyRuleAntecedent();
    aa15->joinWithAND(a15, ktanaha5);
    FuzzyRuleConsequent *makaa5 = new FuzzyRuleConsequent();
    makaa5->addOutput(siram);
    FuzzyRule *fuzzyRule15 = new FuzzyRule(15, aa15, makaa5);
    fuzzy->addFuzzyRule(fuzzyRule15);
    // Building FuzzyRule 17
    FuzzyRuleAntecedent *a17 = new FuzzyRuleAntecedent();
    a17->joinWithAND(utinggi, ttinggi);
    FuzzyRuleAntecedent *ktanaha7 = new FuzzyRuleAntecedent();
    ktanaha7->joinSingle(srendah);
    FuzzyRuleAntecedent *aa17 = new FuzzyRuleAntecedent();
    aa17->joinWithAND(a17, ktanaha7);
    FuzzyRuleConsequent *makaa7 = new FuzzyRuleConsequent();
    makaa7->addOutput(siram);
    FuzzyRule *fuzzyRule17 = new FuzzyRule(17, aa17, makaa7);
    fuzzy->addFuzzyRule(fuzzyRule17);
  delay(10000);
}
void dataku() {
  DHT.read11(dht_apin);
  fhum = DHT.humidity;
  ftemp = DHT.temperature;
  tanah = analogRead(A0);
  tanah = map(tanah, 1023, 36, 100, 0);
  hum = String(fhum);
  temp = String(ftemp);
  fuzzy->setInput(1, fhum);
  fuzzy->setInput(2, tanah);
  fuzzy->setInput(3, ftemp);
  fuzzy->fuzzify();
  Serial.println(fhum);
  Serial.println(ftemp);
  Serial.println(tanah);
  Serial.print("\n\nKondisi: ");
  if (siram->getPertinence() > tidak->getPertinence()) {
    Serial.println("Siram\n");
    pinMode(10, OUTPUT);
    digitalWrite(10, LOW);
  } else {
    Serial.println("Tidak Siram\n");
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);
  };
  float output1 = fuzzy->defuzzify(1);
  data = "suhu=" + temp + " & udara=" + hum + " & tanah=" + tanah;// data sent must be under this form //name1=value1&name2=value2.
//    data = "suhu=10 & udara=20 & tanah=30";
  Serial.println(data);
}
void loop() {
  esp.println("AT+CIPSTART=0,\"TCP\",\"" + server + "\",80");//start a TCP connection.
  if ( esp.find("OK")) {
    Serial.println("Koneksi siap");
  };
  Serial.println("=>======================================");
  dataku();
  httppost();
  delay(5000);
}
void httppost () {
  String postRequest =
    "POST " + uri + " HTTP/1.1\r\n" +
    "Host: " + server + "\r\n" +
    "Connection: close\r\n" +
    "Content-Type: application/x-www-form-urlencoded\r\n" +
    //    "Content-Type: application/json;charset=UTF-8\r\n"+
    "Content-Length: " + data.length() + "\r\n" +
    "\r\n" + data;
  String sendCmd = "AT+CIPSEND=0";//determine the number of caracters to be sent.
  //Serial.println(postRequest);
  esp.print(sendCmd);
  esp.print(",");
  esp.println(postRequest.length());
  delay(200);
  if (esp.find(">")) {
    Serial.println("Sending.."); esp.print(postRequest);
    if ( esp.find("SEND OK")) {
      Serial.println("Packet sent");
      while (esp.available()) {
        String tmpResp = esp.readString();
        Serial.println(tmpResp);
      }
      // close the connection
      delay(3000);
      esp.println("AT+CIPCLOSE");
      Serial.println("PUTUS");
    }
    //    Serial.println(postRequest);
  }
  delay(4000);
  //  Serial.println(sendCmd + postRequest.length() );
}

String sendCommand(String command, const int timeout, boolean debug) {
  String response = "";
  esp.print(command);
  long int time = millis();
  while ( (time + timeout) > millis())  {
    while (esp.available())    {
      char c = esp.read();
      response += c;
    }
  }
  if (debug) {
    Serial.print(response);
  }
  return response;
}

Sketch uses 17524 bytes (54%) of program storage space. Maximum is 32256 bytes. Global variables use 832 bytes (40%) of dynamic memory, leaving 1216 bytes for local variables. Maximum is 2048 bytes.

  • 2
    Hint: Measure free memory after setup() i.e. call freeMemory() and print last in setup. learn.adafruit.com/memories-of-an-arduino/measuring-free-memory – Mikael Patel Jul 10 '19 at 10:08
  • 06:39:14.727 -> AT+RST 06:39:14.727 -> busy p... 06:39:15.745 -> AT+CWMODE=3 06:39:15.745 -> busy p... 06:39:25.809 -> AT+CWJAP="BU SIS","Bismillah" 06:39:25.844 -> busy p... 06:39:25.844 -> OK 06:39:26.825 -> AT+CIFSR 06:39:26.825 -> 192.168.4.1 06:39:26.858 -> 192.168.100.30 06:39:26.858 -> OK 06:39:27.846 -> AT+CIPMUX=1 06:39:27.879 -> OK 06:39:27.879 -> 238 06:39:37.850 -> 238 06:39:38.019 -> Koneksi siap 06:39:38.019 -> =>====================================== – Ridhuwan Robbi Jul 10 '19 at 23:42
  • 06:39:38.053 -> 69 06:39:38.053 -> 28 06:39:38.087 -> 96 06:39:38.087 -> 06:39:38.087 -> 06:39:38.087 -> Kondisi: Siram 06:39:38.087 -> 06:39:38.087 -> 186 06:39:38.087 -> 96 06:39:38.123 -> 181 06:39:38.295 -> Sending.. 06:39:48.283 -> 181 06:39:49.297 -> =>====================================== 06:39:49.331 -> 71 06:39:49.331 -> 28 06:39:49.365 -> 96 – Ridhuwan Robbi Jul 10 '19 at 23:42
  • did it means , my arduino can't handle that code together ? – Ridhuwan Robbi Jul 10 '19 at 23:45
  • The Arduino Uno has only 2K byte. That is one million times smaller than todays PCs. It is important to understand memory allocation and how to write code that does not consume as much memory. My guess was that you are very close to the limit and need to rewrite some sections to reduce the memory footprint. But first of all it is important to understand how much memory is used and where in the code. – Mikael Patel Jul 11 '19 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.