CCCPaste

"Water Monitor" 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: "Water Monitor"
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-06-14 12:06:51

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Design an Arduino project for water level */
	/* detection and control using SoftwareSerial, Relay, */
	/* and Sim800L libraries. The system should read */
	/* float sensor data, manage a relay, and communicate */
	/* water levels via GSM using the Sim800L module. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include  //https://github.com/plerup/espsoftwareserial
#include  //https://github.com/rafaelnsantos/Relay
#include  //https://github.com/vittorioexp/Sim800L-Arduino-Library-revised

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t floatSensorPin = 7; // Pin connected to the float sensor
const uint8_t gsm_SIM800L_RING_PIN_D4 = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t gsm_SIM800L_RST_PIN_D3 = 3;
const uint8_t gsm_SIM800L_DTR_PIN_D5 = 5;

/***** DEFINITION OF Software Serial *****/
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_A0 = A0;
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_A1 = A1;
SoftwareSerial gsm_SIM800L_Serial(gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool gsm_SIM800L_RST_PIN_D3_rawData = 0;
bool gsm_SIM800L_DTR_PIN_D5_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float gsm_SIM800L_RST_PIN_D3_phyData = 0.0;
float gsm_SIM800L_DTR_PIN_D5_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Relay relay(2, true); // Initialize relay on pin 2, Normally Open
Sim800L GSM(gsm_SIM800L_Serial); // Initialize Sim800L with SoftwareSerial

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(floatSensorPin, INPUT); // Set float sensor pin as input
  pinMode(gsm_SIM800L_RING_PIN_D4, INPUT_PULLUP);

  pinMode(gsm_SIM800L_RST_PIN_D3, OUTPUT);
  pinMode(gsm_SIM800L_DTR_PIN_D5, OUTPUT);

  gsm_SIM800L_Serial.begin(9600); // Initialize SoftwareSerial for GSM module

  relay.begin(); // Initialize the relay

  GSM.begin(9600); // Initialize the GSM module with a baud rate of 9600
  char* text = "System Initialized";
  char* number = "2926451386";
  bool error = GSM.sendSms(number, text); // Send an initialization SMS
}

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

  updateOutputs(); // Refresh output data
  relay.loop(); // Update relay state

  // Read float sensor data
  bool waterLevelHigh = digitalRead(floatSensorPin);

  if (waterLevelHigh) {
    relay.turnOn(); // Turn relay on if water level is high
    GSM.sendSms("2926451386", "Water level is high!"); // Send SMS alert
  } else {
    relay.turnOff(); // Turn relay off if water level is low
  }

  delay(1000); // Delay for a second before next loop iteration
}

void updateOutputs()
{
  digitalWrite(gsm_SIM800L_RST_PIN_D3, gsm_SIM800L_RST_PIN_D3_rawData);
  digitalWrite(gsm_SIM800L_DTR_PIN_D5, gsm_SIM800L_DTR_PIN_D5_rawData);
}

/* END CODE */