/********* 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: Temp Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-10-13 15:37:09

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Power Management System for Smart Homes: Build a */
	/* power management system that automatically turns */
	/* off unused devices or circuits to conserve energy */
	/* based on usage patterns detected by sensors. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <DS18B20.h>	//https://github.com/matmunk/DS18B20
#include <Relay.h>	//https://github.com/rafaelnsantos/Relay

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t temp_sens_DS18B20_DQ_PIN_D2		= 2;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t led_LED_PIN_D3		= 3;
const uint8_t relay_RelayModule_Signal_PIN_D4		= 4;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize DS18B20 temperature sensor with the address of the device
uint8_t address[] = {40, 250, 31, 218, 4, 0, 0, 52}; // Example address
DS18B20 ds(address); // Create DS18B20 object with the specified address

// Initialize Relay object on pin 4, Normally Open (NO)
Relay relay(relay_RelayModule_Signal_PIN_D4, true); // Use true for Normally Open configuration

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

	pinMode(temp_sens_DS18B20_DQ_PIN_D2, INPUT); // Set temperature sensor pin as input

	pinMode(led_LED_PIN_D3, OUTPUT); // Set LED pin as output
	pinMode(relay_RelayModule_Signal_PIN_D4, OUTPUT); // Set relay pin as output

	// Initialize the relay
	relay.begin(); // Initialize the relay pin
}

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

	updateOutputs(); // Refresh output data

	// Update the DS18B20 temperature sensor
	ds.Update(); // Call the update method to read temperature
	int16_t temperature = ds.GetTemperature<int16_t>(); // Get temperature in 1/16 degrees Celsius

	// Check if temperature exceeds certain thresholds
	if (temperature > 250) { // Example high threshold (25.0°C)
		relay.turnOn(); // Turn relay on if temperature is too high
		led_LED_PIN_D3_rawData = true; // Turn on LED to indicate high temperature
	} else {
		relay.turnOff(); // Turn relay off if temperature is normal
		led_LED_PIN_D3_rawData = false; // Turn off LED
	}

	delay(1000); // Delay for stability
}

void updateOutputs()
{
	digitalWrite(led_LED_PIN_D3, led_LED_PIN_D3_rawData); // Update LED state
	digitalWrite(relay_RelayModule_Signal_PIN_D4, relay_RelayModule_Signal_PIN_D4_rawData); // Update relay state
}

/* END CODE */