Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

cakraawijaya/Aquaponic-pH-Control-Monitoring-with-Type-2-Fuzzy-Method-Based-on-IoT-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

560 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Open Source Love License: MIT GitHub last commit Project Type

Aquaponic pH Control-Monitoring with Type 2 Fuzzy Method-Based on IoT-Bot

Undergraduate Thesis Project Documentation - Informatics UPN Veteran Jatim

An aquaponic system is a combined farming system between fish with vegetables where the activities are mutually beneficial. On the other hand, crop failure can also be a concern for aquaponic farmers as this can happen at any time. Harvest failure can be influenced by many factors, but is usually caused by the high pH ambiguity of the water around the scope of cultivation. The aquaponic farmers are worried that if the crop failure continues it will harm their food security. This project was created in the hope of solving the pH problem. This project has been carried out and took approximately 1 year. The system created can control and monitor changes in water pH at any time. This system is based on the Internet of Things (IoT), using MQTT as the communication protocol. This system is also equipped with artificial intelligence, which uses IT2FL (Interval Type-2 Fuzzy Logic) as its decision support. In addition, the system interface uses Telegram Bot, making it easier for users to interact.



Project Requirements

Part Description
Development Board DOIT ESP32 DEVKIT V1
Supporting Board Arduino Uno R3
Code Editor Arduino IDE 1.8.19 (Stable Legacy Version)
Application Support • Telegram Bot
• Matlab R2020
Driver CP210X USB Driver
IoT Platform io-t.net
Communications Protocol • Inter Integrated Circuit (I2C)
• Message Queuing Telemetry Transport (MQTT)
• MTProto
IoT Architecture 3 Layer
Matlab Fuzzy Interface System
Programming Language C/C++
Arduino Library • WiFi (default)
• PubSubClient by Nick O'Leary (Version: 2.8)
• LiquidCrystal_I2C by Frank de Brabander (Version: 1.1.2)
• CTBot by Stefano Ledda (Version: 2.1.11)
• ArduinoJson by Benoit Blanchon (Version: 6.19.4)
• RTClib by Adafruit (Version: 2.0.2)
Actuators • Submersible pump aquarium (x1)
• Pneumatic solenoid valve (x2)
• Piezoelectric (x1)
Sensor • pH Sensor (x1)
• RTC (x1)
Display LCD I2C (x1)
Experimental Object • Pakcoy mustard seeds
• Dumbo catfish fry
Other Components • Micro USB cable - USB type A (x1)
• Jumper cable (1 set)
• Switching power supply 12V 1A (x1)
• Electromechanical relay 2-channel (x1)
• Round switch (x1)
• ESP32 expansion board (x1)
• PCB Dot Matrix (x1)
• Terminal PCB block screw (x10)
• Socket female jack DC (x1)
• Connector male jack DC (x3)
• Probe Elektroda pH (x1)
• Pipes (1 set)
• Netpot (1 set)
• Rockwool (1 set)
• Flannel fabric (1 set)
• Water filter (x1)
• Glass hubcap (1 set)
• Bottle (x2)
• Aquarium wheeled placemat (x1)
• Aquarium (x1)
• Acrylic box (x1)
• Skun (1 set)
• Galvanized plate (x1)
• Bolts plus (1 set)
• Nuts (1 set)



Download & Install

  1. Arduino IDE

    https://www.arduino.cc/en/software
    

  2. CP210X USB Driver

    https://bit.ly/CP210X_USBdriver
    

  3. Matlab R2020

    https://bit.ly/MATLAB_R2020a_Installer
    



Project Designs

Infrastructure
infrastructure
Pictorial Diagram Prototype Design Main Box Design
pictorial-diagram prototype-design mainBox-design
Fuzzy Interface System IT2FL IT2FL Input Variable IT2FL Output Variable
fis-it2fl varin-it2fl varout-it2fl



Scanning the I2C Address on the LCD

/*
  =====================================================
  I2C Scanner for Arduino / ESP32 / ESP8266
  by: Devan Cakra Mudra Wijaya, S.Kom.
  =====================================================

  Functions:
  - Detects all connected I2C devices
  - Displays device addresses in HEX format
  - Displays the total number of detected devices


  =====================================================
  SDA and SCL Pins for Arduino / ESP32 / ESP8266
  =====================================================
  Arduino I2C Connection (default):
  - Arduino Uno / Nano (ATmega328P)
    SDA -> A4
    SCL -> A5

  - Arduino Mega 2560
    SDA -> D20
    SCL -> D21

  - Other Arduino boards
    SDA -> SDA pin
    SCL -> SCL pin
    (Refer to the datasheet or board pinout)

  ESP32 I2C Connection (default):
  SDA -> GPIO 21
  SCL -> GPIO 22

  ESP8266 I2C Connection (default):
  SDA -> GPIO 4 (D2)
  SCL -> GPIO 5 (D1)
*/

// Include the Wire library for I2C communication
#include <Wire.h>

// Constant that defines the delay between scans (5000 ms = 5 seconds)
const uint32_t SCAN_INTERVAL = 5000;


// Function to initialize I2C communication
// SDA and SCL pin configuration will be adjusted automatically based on the board being used
void initI2C() {

  // If the board being used is ESP32:
  #if defined(ESP32)

    // Enable I2C communication
    // SDA = GPIO21
    // SCL = GPIO22
    Wire.begin(21, 22);

  // If the board being used is ESP8266:
  #elif defined(ESP8266)

    // Enable I2C communication
    // SDA = D2 (GPIO4)
    // SCL = D1 (GPIO5)
    Wire.begin(D2, D1);

  // If the board is neither ESP32 nor ESP8266
  // Examples: Arduino Uno, Nano, Mega, Leonardo, etc.
  #else

    // Enable I2C communication using the board's built-in hardware pins
    Wire.begin();

  #endif

}


// The setup() function runs once when the board is powered on or reset
// It is used to initialize hardware, serial communication, sensors, modules, and the program's initial configuration
void setup() {

  // Start Serial communication at 115200 baud rate
  Serial.begin(115200);

  // Check whether the board uses native USB
  // Examples: Arduino Leonardo, Arduino Micro, some ESP32-S2/S3 boards
  #if defined(USBCON) || defined(ARDUINO_USB_CDC_ON_BOOT)

    // If yes:
    // The program will wait until the Serial Monitor is connected before continuing execution
    while (!Serial);

  #endif

  // Wait for 2 seconds before starting the program
  delay(2000);

  // Display program header
  Serial.println("====================================");
  Serial.println("         I2C DEVICE SCANNER         ");
  Serial.println("by: Devan Cakra Mudra Wijaya, S.Kom.");
  Serial.println("====================================");

  // Print an empty line
  Serial.println();

  // Initialize I2C communication
  initI2C();
}


// The loop() function runs continuously after setup() has finished
// The main program logic is typically placed inside this function
void loop() {

  // Variable to store the error code returned from I2C communication
  uint8_t error;

  // Variable to store the I2C address currently being checked
  uint8_t address;

  // Counter variable for the number of detected devices
  uint8_t deviceCount = 0;

  // Display information indicating that the scan process has started
  Serial.println("------------------------------------");
  Serial.println("Scanning I2C bus...");
  Serial.println("------------------------------------");

  // Loop through addresses from 1 to 126
  // Valid I2C addresses range from 0x01 to 0x7E
  for (address = 1; address < 127; address++) {

    // Start communication with the address currently being tested
    Wire.beginTransmission(address);

    // End the transmission and store the result
    // 0 = success
    // 1 = data too long
    // 2 = NACK received when address was sent
    // 3 = NACK received when data was sent
    // 4 = other error
    error = Wire.endTransmission();

    // If no error occurs:
    if (error == 0) {

      // Display information that a device was found
      Serial.print("[FOUND] Device at address 0x");

      // If the address is less than 16:
      // Add a leading zero to keep HEX formatting aligned
      if (address < 16) {
        Serial.print("0");
      }

      // Display the address in HEX format
      Serial.println(address, HEX);

      // Increment the detected device count
      deviceCount++;
    }

    // If an unknown error occurs:
    else if (error == 4) {

      // Display an error message
      Serial.print("[ERROR] Unknown error at address 0x");

      // If the address is less than 16:
      // Add a leading zero to keep HEX formatting aligned
      if (address < 16) {
        Serial.print("0");
      }

      // Display the problematic address in HEX format
      Serial.println(address, HEX);
    }

    // If the error is neither 0 nor 4:
    // Ignore it, as this usually means no device exists at that address
  }

  // Print an empty line
  Serial.println();

  // If no devices were found:
  if (deviceCount == 0) {

    // Display a message indicating that no devices were found
    Serial.println("No I2C devices found.");
  }
  else { // If at least one device was found:

    // Display the total number of detected devices
    Serial.print("Total devices found: ");

    // Display the value of deviceCount
    Serial.println(deviceCount);
  }

  // Display information about the next scan
  Serial.print("Next scan in ");

  // Convert milliseconds to seconds
  Serial.print(SCAN_INTERVAL / 1000);

  // Display the unit in seconds
  Serial.println(" seconds.");

  // Empty line
  Serial.println("\n");

  // Wait 5 seconds before performing the next scan
  delay(SCAN_INTERVAL);
}


pH Sensor Calibration

The pH sensor can be calibrated using the results of a linear regression calculation. You can see the linear regression equation as follows.

I

$\ Y = a+b.X $


Explanation of the symbols in equation I, you can see more details in the following table.

Symbol Description
𝑌 pH buffer value
𝑋 voltage value obtained based on Y
𝑎 and 𝑏 linear regression determination value

The linear regression determination values (a and b) can be obtained through equations II and III.

II

$\ a = \frac{\sum Y.\sum X^{2}-\sum X.\sum XY}{n.\sum X^{2}-(\sum X)^{2}} $

III

$\ b = \frac{n.\sum XY-\sum X.\sum Y}{n.\sum X^{2}-(\sum X)^{2}} $


Example of pH sensor calibration: Click Here



RTC Sensor Calibration

This RTC sensor can be calibrated using the following program code :

#include <RTClib.h> // Calling the RTC library
RTC_DS3231 rtc; // Constructor

void setup() {
   RTCinit(); // Calling the RTCinit method
}

void loop() {}

void RTCinit() {
   // Starting up the RTC
   rtc.begin();

   // Set Time Now
   // If calibrated, please close with a comment
   rtc.adjust(DateTime(YYYY, MM, DD, HH, MM, SS));
}


Arduino IDE Setup

  1. Open the Arduino IDE first, then open the project by clicking File -> Open :

    PH_IT2FL.ino


  2. Fill in the Additional Board Manager URLs in Arduino IDE

    Click File -> Preferences -> enter the Boards Manager Url by copying the following link :

    https://dl.espressif.com/dl/package_esp32_index.json
    

  3. Board Setup in Arduino IDE

    How to setup the DOIT ESP32 DEVKIT V1 board

    • Click Tools -> Board -> Boards Manager -> Install esp32.

    • Then selecting a Board by clicking: Tools -> Board -> ESP32 Arduino -> DOIT ESP32 DEVKIT V1.

    Arduino Uno board in this project is only used as a filter or voltage divider.

    • You don't need to configure the Arduino Uno board, just focus on the ESP32.


  4. Change the Board Speed in Arduino IDE

    Click Tools -> Upload Speed -> 9600


  5. Install Library in Arduino IDE

    Download all the library zip files. Then paste it in the: C:\Users\Computer_Username\Documents\Arduino\libraries


  6. Port Setup in Arduino IDE

    Click Port -> Choose according to your device port (you can see in device manager)


  7. Change the WiFi Name, WiFi Password, and so on according to what you are currently using.

  8. Before uploading the program please click: Verify.

  9. If there is no error in the program code, then please click: Upload.

  10. Some things you need to do when using the ESP32 board :

    • If ESP32 board cannot process Source Code totally -> Press EN (RST) button -> Restart.

    • If ESP32 board cannot process Source Code automatically then :

    • When information: Uploading... has appeared -> immediately press and hold the BOOT button.

    • When information: Writing at .... (%) has appeared -> release the BOOT button.

    • If message: Done Uploading has appeared -> The previously entered program can already be operated.

    • Do not press the BOOT and EN buttons at the same time as this may switch to Upload Firmware mode.


  11. If there is still a problem when uploading the program, then try checking the driver / port / others section.



Io-t.net Setup

  1. Getting started with io-t.net :

    • Go to the official website at the following link : io-t.net.

    • If you do not have an account, please Register first -> activate your account via email.

    • If you already have an account, please Sign In to be able to access io-t.net services.


  2. Create a node :

    • Go to Instance menu -> Set Node.

    • Then give the node a unique name that you use.


  3. Create a device :

    • Go to Devices menu.

    • Select Add Devices -> fill in the Client ID, Access, Topic sections as needed. For example :

    • Client ID -> Phiotnet_v1.

    • Access -> Publish & Subscribe.

    • Topic -> detect.



Telegram Bot Setup

  1. Open @BotFather.

  2. Type /newbot.

  3. Type the desired bot name, for example: phiotnet_bot.

  4. Type the desired bot username, for example: phiotnet_bot.

  5. Also do it for bot image settings, bot descriptions, and so on according to your needs.

  6. Copy your telegram bot API token -> then paste it into the #define BOTtoken "YOUR_API_BOT_TOKEN" section.

    For example :

    #define BOTtoken "2006772150:AAE6Fdjk3KbiySkzV6CLbd6ClJDzgTfJ5y0"



Matlab Setup

  1. Open the Matlab.

  2. Open the Set Path dialog box using the command :

    pathtool
    

    set-path-matlab

  3. Select Add Folder... -> search the FIS-IT2FLS-Toolbox-MATLAB folder -> Select Folder.

  4. Click Save and then click Close.

  5. Open the IT2FL Toolbox using the command :

    fuzzyt2
    

    it2fl-toolbox

  6. The rest you can customize according to your needs.



Get Started

  1. Download and extract this repository.

  2. Make sure you have the necessary electronic components.

  3. Make sure your components are designed according to the diagram.

  4. Configure your device according to the settings above.

  5. Please enjoy [Done].



Demonstration of Application

Via Telegram: @phiotnet_bot



Highlights

Product IT2FL Decision Support System Telegram Bot
product it2fl-spk telegram-bot



More information:

• Undergraduate Thesis : Access 1 or Access 2

• Journals : Article 1 or Article 2



Notes

  • Hardware Repairs Suggestions :

    1. The power supply in the microcontroller and actuator circuit needs to be reset separately so that the system can function properly and safely.
    2. Use a standard PSU with a capacity of 3A to supply multiple attached devices, such as actuators.
    3. Add a Step Down Converter to get a more stable voltage and in accordance with the needs of the device. With this component, there is no need for an Arduino Uno.
    4. Change the Electromechanical relay 2-channel setting from NO (Normally Open) to NC (Normally Close) to reduce overheating.
    5. Add insulators to electrical components to prevent leakage current.
    6. Add fans or vents to increase airflow and prevent overheating.
    7. Add Heat Sinks to heat-prone components.
    8. Add an MCB (Miniature Circuit Breaker) to prevent overcurrent, keeping the device safe and secure.
    9. Add a special case to protect the pH Sensor Probe from damage, especially from impact.
    10. Consider choosing more than 1 Development Board to improve system optimization.
    11. Consider replacing the 2-Channel Electromechanical Relay with the SSR DD Relay for smoother control (no noise), faster response, lower power consumption, higher efficiency, and better durability for long-term use.
    12. Consider replacing the acrylic box with a standard panel box to make the device much more secure and presentable.
    13. Consider replacing the cables according to the standard of use. For DC electricity, use AVS 1 x 0.5mm² cables, which are more durable than ribbon cables. Meanwhile, for AC electricity, use NYY-HY 2 x 1.5mm² cables, which are more resistant to water, weather, and physical disturbances such as rat bites.

  • Firmware Repairs Suggestions :

    1. Optimize the algorithm to reduce latency in the decision-making process. It is recommended to use RTOS (Real-Time Operating System) in order to better prioritize the functions.
    2. Add OTA (Over The Air) methods to improve network security, including Telegram Bot.
    3. Add EEPROM to permanently store the pH sensor calibration results, so that the calibration does not need to be written in the loop() method again. This can save time and improve device efficiency.
    4. Add the ArduLite library and use its syntax to lighten the load of the ESP32, as it can reduce the memory usage of the ESP32. This ArduLite is ideal for projects with limited resources that prioritize efficiency and simplicity. Link: https://github.com/ArduLite/ArduLite.

  • Automation Feature Improvement Suggestions :

    1. Automatic pump integrated with a level sensor to control liquid filling in the form of pH and AB Mix.
    2. Time-based automatic feeding for scheduled feeding.
    3. Automatic pump controlled via telegram bot to simplify the process of draining and replenishing water.
    4. Monitoring water quality in ponds or aquariums using a combination of pH, water temperature, and ammonia sensors.
    5. Plant irradiation with growth lamps to grow plants optimally even in low light conditions (as a substitute for sunlight).

  • Planting Media Optimization Suggestions :

    1. Increase the number of hydroponic modules to accommodate more plants.
    2. Exploration of alternative growing media such as husk charcoal or cocopeat to improve plant growth efficiency.



Appreciation

If this work is useful to you, then support this work as a form of appreciation to the author by clicking the ⭐Star button at the top of the repository.



Disclaimer

This application is my own work and is not the result of plagiarism from other people's research or work, except those related to third party services which include: libraries, frameworks, and so on.



LICENSE

MIT License - Copyright © 2022 - Devan C. M. Wijaya, S.Kom

Permission is hereby granted without charge to any person obtaining a copy of this software and the software-related documentation files to deal in them without restriction, including without limitation the right to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons receiving the Software to be furnished therewith on the following terms:

The above copyright notice and this permission notice must accompany all copies or substantial portions of the Software.

IN ANY EVENT, THE AUTHOR OR COPYRIGHT HOLDER HEREIN RETAINS FULL OWNERSHIP RIGHTS. THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, THEREFORE IF ANY DAMAGE, LOSS, OR OTHERWISE ARISES FROM THE USE OR OTHER DEALINGS IN THE SOFTWARE, THE AUTHOR OR COPYRIGHT HOLDER SHALL NOT BE LIABLE, AS THE USE OF THE SOFTWARE IS NOT COMPELLED AT ALL, SO THE RISK IS YOUR OWN.