Advertisement
pleasedontcode

"Moisture Control" rev_01

Feb 20th, 2025
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Moisture Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-02-20 13:24:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Odczytuje wilgotność gleby co 2 sekundy. */
  21.     /* Sprawdza, czy w zbiorniku jest woda.  Jeśli ziemia */
  22.     /* jest zbyt sucha – włącza pompę.   Wyświetla dane */
  23.     /* na LCD i OLED.   Umożliwia zmianę progu */
  24.     /* wilgotności przez WiFi. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <LCDIC2.h> //https://github.com/offcircuit/LCDIC2
  32. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  33. #include <LiquidCrystal_I2C.h> // Added for LCD functionality
  34. #include <WiFi.h> // Include WiFi library for WiFi functionality
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void checkMoisture(void); // Function to check moisture and control pump
  40. void setupWiFi(void); // Function to setup WiFi
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t myDHT22_DHT22_DOUT_PIN_D4     = 4;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t LCD_LCD1602I2C_I2C_PIN_SDA_D21        = 21;
  47. const uint8_t LCD_LCD1602I2C_I2C_PIN_SCL_D22        = 22;
  48. const uint8_t LCD_LCD1602I2C_I2C_SLAVE_ADDRESS      = 39;
  49.  
  50. /***** DEFINITION OF ADDITIONAL PINS *****/
  51. const int potPin = 34; // Pin for moisture sensor
  52. const int pumpPin = 26; // Pin for pump control
  53. int threshold = 50; // Default moisture threshold
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address 0x27
  57.  
  58. // WiFi credentials
  59. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  60. const char* password = "your_PASSWORD"; // Replace with your WiFi password
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(myDHT22_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
  66.     pinMode(pumpPin, OUTPUT); // Set pump pin as output
  67.     digitalWrite(pumpPin, LOW); // Ensure pump is off initially
  68.  
  69.     // initialize LCD
  70.     lcd.init();
  71.     // turn on LCD backlight
  72.     lcd.backlight();
  73.     delay(1000);
  74.     Serial.begin(115200); // Initialize serial communication
  75.  
  76.     setupWiFi(); // Setup WiFi connection
  77. }
  78.  
  79. int calculateMoisture(int inputValue) {
  80.     int value = (-100 * inputValue) / 3295 + 409500 / 3295;
  81.     if (value < 0) {
  82.         return 0;
  83.     } else if (value > 100) {
  84.         return 100;
  85.     }
  86.     return value;
  87. }
  88.  
  89. void checkMoisture() {
  90.     int potValue = analogRead(potPin); // Read moisture sensor value
  91.     int moisturePrecent = calculateMoisture(potValue); // Calculate moisture percentage
  92.  
  93.     if (moisturePrecent < threshold) {
  94.         digitalWrite(pumpPin, HIGH); // Turn on pump if moisture is below threshold
  95.     } else {
  96.         digitalWrite(pumpPin, LOW); // Turn off pump if moisture is above threshold
  97.     }
  98.  
  99.     Serial.print(potValue);
  100.     Serial.print(" - ");
  101.     Serial.print(moisturePrecent);
  102.     Serial.println();
  103.  
  104.     lcd.clear();
  105.     // set cursor to first column, first row
  106.     lcd.setCursor(0, 0);
  107.     // print message
  108.     lcd.print("Wilgotnosc: ");
  109.     lcd.print(moisturePrecent);
  110.     lcd.print("%");
  111.  
  112.     // set cursor to first column, second row
  113.     lcd.setCursor(0, 1);
  114.     lcd.print("Pompa: ");
  115.     lcd.print(digitalRead(pumpPin) ? "ON " : "OFF"); // Check pump state
  116. }
  117.  
  118. void setupWiFi() {
  119.     WiFi.begin(ssid, password); // Connect to WiFi
  120.     while (WiFi.status() != WL_CONNECTED) {
  121.         delay(1000);
  122.         Serial.println("Connecting to WiFi...");
  123.     }
  124.     Serial.println("Connected to WiFi");
  125. }
  126.  
  127. void loop(void)
  128. {
  129.     // put your main code here, to run repeatedly:
  130.     checkMoisture(); // Check moisture and control pump
  131.     delay(2000); // Wait for 2 seconds before the next reading
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement