CCCPaste

Climate Monitor rev_11

/********* 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: Climate Monitor
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-10-02 21:47:47

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Initialize internal Bluetooth device as master, */
	/* receive BLE data, and show it on a 16x2 LCD using */
	/* LiquidCrystal library for Arduino. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* read temperature and send it via bluetooth. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include  //https://github.com/arduino-libraries/LiquidCrystal
#include  //https://github.com/matmunk/DS18B20
#include  //https://github.com/adafruit/DHT-sensor-library
#include  // Library for Bluetooth serial communication
#include "LiquidCrystalDisplay.h" // Include custom library for LCD functionality
#include "DS18B20Sensor.h" // Include custom library for DS18B20 sensor functionality
#include "Adafruit_SSD1306_OLED.h" // Include OLED library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void sendTemperature(); // Function to send the temperature via Bluetooth
void updateBluetoothDisplay(); // Function to update LCD with Bluetooth data

/***** DIGITAL INPUT/OUTPUT PINS *****/
const uint8_t temp_DS18B20_DQ_PIN_D8 = 8; // DS18B20 sensor pin
const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9; // DHT22 sensor pin
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHTTYPE); // Create a DHT sensor object

// LCD display pins
const uint8_t display_LCD1602_RS_PIN_D2 = 2;
const uint8_t display_LCD1602_E_PIN_D3 = 3;
const uint8_t display_LCD1602_D4_PIN_D4 = 4;
const uint8_t display_LCD1602_D5_PIN_D5 = 5;
const uint8_t display_LCD1602_D6_PIN_D6 = 6;
const uint8_t display_LCD1602_D7_PIN_D7 = 7;

// Create an instance for Bluetooth
BluetoothSerial SerialBT;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Include and initialize the sensor libraries 
DS18B20 ds(temp_DS18B20_DQ_PIN_D8); //create an instance for DS18B20

void setup(void) {
    Serial.begin(9600); // Initialize serial communication
    dht.begin(); // Initialize the DHT sensor
    SerialBT.begin("ESP32_Bluetooth"); // Set Bluetooth device name
    initializeLCD(); // Initialize the LCD display
}

void loop(void) {
    sendTemperature(); // Send temperature data
    updateBluetoothDisplay(); // Display data on LCD
    delay(2000); // Wait before next loop to avoid flooding data
}

// Function to read temperature and send it via Bluetooth
void sendTemperature() {
    float humidity = dht.readHumidity();
    float temperature = dht.readTemperature();
    if (isnan(humidity) || isnan(temperature)) {
        Serial.println("Failed to read from DHT sensor!");
        return; // Exit function if read failed
    }

    // Prepare message for Bluetooth
    String message = String("Temp: ") + temperature + "°C Humidity: " + humidity + "%";
    SerialBT.println(message); // Send message via Bluetooth
}

// Function to update the displayed message on the LCD
void updateBluetoothDisplay() {
    if (SerialBT.available()) {
        String inputMessage = SerialBT.readString(); // Read incoming message
        displayData(inputMessage.c_str()); // Display message on the LCD
    }
}

void displayData(const char* message) {
    lcd.clear(); // Clear LCD
    lcd.print(message); // Print Bluetooth message on LCD
}

/* END CODE */