/********* 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: Temperature Monitor
- Source Code NOT compiled for: Arduino Nano ESP32
- Source Code created on: 2024-10-03 10:56:08
********* 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 // Library for controlling LCD displays
#include // Library for DS18B20 temperature sensor
#include // Library for DHT temperature and humidity sensors
#include // Library for Bluetooth communication
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void sendTemperature(float temperature);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t temp_DS18B20_DQ_PIN_D8 = 8; // Pin for DS18B20
const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9; // Pin for DHT22
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t display_LCD1602_RS_PIN_D2 = 2; // LCD RS pin
const uint8_t display_LCD1602_E_PIN_D3 = 3; // LCD Enable pin
const uint8_t display_LCD1602_D4_PIN_D4 = 4; // LCD D4 pin
const uint8_t display_LCD1602_D5_PIN_D5 = 5; // LCD D5 pin
const uint8_t display_LCD1602_D6_PIN_D6 = 6; // LCD D6 pin
const uint8_t display_LCD1602_D7_PIN_D7 = 7; // LCD D7 pin
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create instances for the libraries
LiquidCrystal lcd(display_LCD1602_RS_PIN_D2, display_LCD1602_E_PIN_D3, display_LCD1602_D4_PIN_D4, display_LCD1602_D5_PIN_D5, display_LCD1602_D6_PIN_D6, display_LCD1602_D7_PIN_D7);
DS18B20 temperatureSensor(temp_DS18B20_DQ_PIN_D8); // DS18B20 instance
DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22); // DHT22 instance
BluetoothSerial SerialBT; // Bluetooth Serial instance
void setup(void)
{
// Initialize serial communication for debugging
Serial.begin(115200);
SerialBT.begin("ESP32_BT"); // Initialize Bluetooth with device name
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Initializing...");
// Initialize temperature sensors
temperatureSensor.begin();
dht.begin();
// Display initial message on LCD
lcd.clear();
lcd.print("Ready");
}
void loop(void)
{
// Read temperature from DHT sensor
float temperature = dht.readTemperature();
if (isnan(temperature)) {
lcd.print("DHT Error");
} else {
// Display temperature on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
// Send temperature via Bluetooth
sendTemperature(temperature);
}
// Read temperature from DS18B20 sensor
float dsTemp = temperatureSensor.getTempC();
if (!isnan(dsTemp)) {
// Display DS18B20 temperature on LCD
lcd.setCursor(0, 1);
lcd.print("DS Temp: ");
lcd.print(dsTemp);
lcd.print(" C");
// Send DS18B20 temperature via Bluetooth
sendTemperature(dsTemp);
} else {
lcd.setCursor(0, 1);
lcd.print("DS Error");
}
delay(2000); // Wait for 2 seconds before next reading
}
void updateOutputs()
{
// Update the LCD display with the latest readings
lcd.display();
}
void sendTemperature(float temperature) {
// Format temperature data as a string
String tempData = "Temperature: " + String(temperature) + " C";
SerialBT.println(tempData); // Send temperature data via Bluetooth
}