CCCPaste

"Moisture Control" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Moisture Control"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2025-02-20 13:24:03

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Odczytuje wilgotność gleby co 2 sekundy. */
	/* Sprawdza, czy w zbiorniku jest woda.  Jeśli ziemia */
	/* jest zbyt sucha – włącza pompę.   Wyświetla dane */
	/* na LCD i OLED.   Umożliwia zmianę progu */
	/* wilgotności przez WiFi. */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 	//https://github.com/offcircuit/LCDIC2
#include 	//https://github.com/adafruit/DHT-sensor-library
#include  // Added for LCD functionality
#include  // Include WiFi library for WiFi functionality

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void checkMoisture(void); // Function to check moisture and control pump
void setupWiFi(void); // Function to setup WiFi

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t myDHT22_DHT22_DOUT_PIN_D4		= 4;

/***** DEFINITION OF I2C PINS *****/
const uint8_t LCD_LCD1602I2C_I2C_PIN_SDA_D21		= 21;
const uint8_t LCD_LCD1602I2C_I2C_PIN_SCL_D22		= 22;
const uint8_t LCD_LCD1602I2C_I2C_SLAVE_ADDRESS		= 39;

/***** DEFINITION OF ADDITIONAL PINS *****/
const int potPin = 34; // Pin for moisture sensor
const int pumpPin = 26; // Pin for pump control
int threshold = 50; // Default moisture threshold

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address 0x27

// WiFi credentials
const char* ssid = "your_SSID"; // Replace with your WiFi SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi password

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(myDHT22_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
	pinMode(pumpPin, OUTPUT); // Set pump pin as output
	digitalWrite(pumpPin, LOW); // Ensure pump is off initially

	// initialize LCD
	lcd.init();
	// turn on LCD backlight
	lcd.backlight();
	delay(1000);
	Serial.begin(115200); // Initialize serial communication

	setupWiFi(); // Setup WiFi connection
}

int calculateMoisture(int inputValue) {
	int value = (-100 * inputValue) / 3295 + 409500 / 3295;
	if (value < 0) {
		return 0;
	} else if (value > 100) {
		return 100;
	}
	return value;
}

void checkMoisture() {
	int potValue = analogRead(potPin); // Read moisture sensor value
	int moisturePrecent = calculateMoisture(potValue); // Calculate moisture percentage

	if (moisturePrecent < threshold) {
		digitalWrite(pumpPin, HIGH); // Turn on pump if moisture is below threshold
	} else {
		digitalWrite(pumpPin, LOW); // Turn off pump if moisture is above threshold
	}

	Serial.print(potValue);
	Serial.print(" - ");
	Serial.print(moisturePrecent);
	Serial.println();

	lcd.clear();
	// set cursor to first column, first row
	lcd.setCursor(0, 0);
	// print message
	lcd.print("Wilgotnosc: ");
	lcd.print(moisturePrecent);
	lcd.print("%");

	// set cursor to first column, second row
	lcd.setCursor(0, 1);
	lcd.print("Pompa: ");
	lcd.print(digitalRead(pumpPin) ? "ON " : "OFF"); // Check pump state
}

void setupWiFi() {
	WiFi.begin(ssid, password); // Connect to WiFi
	while (WiFi.status() != WL_CONNECTED) {
		delay(1000);
		Serial.println("Connecting to WiFi...");
	}
	Serial.println("Connected to WiFi");
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	checkMoisture(); // Check moisture and control pump
	delay(2000); // Wait for 2 seconds before the next reading
}

/* END CODE */