Advertisement
pleasedontcode

Temperature Monitor rev_28

Oct 3rd, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Temperature Monitor
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-03 10:56:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Initialize internal Bluetooth device as master, */
  21.     /* receive BLE data, and show it on a 16x2 LCD using */
  22.     /* LiquidCrystal library for Arduino. */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* read temperature and send it via bluetooth. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal.h>  // Library for controlling LCD displays
  30. #include <DS18B20.h>        // Library for DS18B20 temperature sensor
  31. #include <DHT.h>            // Library for DHT temperature and humidity sensors
  32. #include <BluetoothSerial.h> // Library for Bluetooth communication
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs();
  38. void sendTemperature(float temperature);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8; // Pin for DS18B20
  42. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9; // Pin for DHT22
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t display_LCD1602_RS_PIN_D2 = 2; // LCD RS pin
  46. const uint8_t display_LCD1602_E_PIN_D3 = 3;  // LCD Enable pin
  47. const uint8_t display_LCD1602_D4_PIN_D4 = 4;  // LCD D4 pin
  48. const uint8_t display_LCD1602_D5_PIN_D5 = 5;  // LCD D5 pin
  49. const uint8_t display_LCD1602_D6_PIN_D6 = 6;  // LCD D6 pin
  50. const uint8_t display_LCD1602_D7_PIN_D7 = 7;  // LCD D7 pin
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. // Create instances for the libraries
  54. 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);
  55. DS18B20 temperatureSensor(temp_DS18B20_DQ_PIN_D8); // DS18B20 instance
  56. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22); // DHT22 instance
  57. BluetoothSerial SerialBT; // Bluetooth Serial instance
  58.  
  59. void setup(void)
  60. {
  61.     // Initialize serial communication for debugging
  62.     Serial.begin(115200);
  63.     SerialBT.begin("ESP32_BT"); // Initialize Bluetooth with device name
  64.  
  65.     // Initialize the LCD
  66.     lcd.begin(16, 2);
  67.     lcd.print("Initializing...");
  68.  
  69.     // Initialize temperature sensors
  70.     temperatureSensor.begin();
  71.     dht.begin();
  72.  
  73.     // Display initial message on LCD
  74.     lcd.clear();
  75.     lcd.print("Ready");
  76. }
  77.  
  78. void loop(void)
  79. {
  80.     // Read temperature from DHT sensor
  81.     float temperature = dht.readTemperature();
  82.     if (isnan(temperature)) {
  83.         lcd.print("DHT Error");
  84.     } else {
  85.         // Display temperature on LCD
  86.         lcd.setCursor(0, 0);
  87.         lcd.print("Temp: ");
  88.         lcd.print(temperature);
  89.         lcd.print(" C");
  90.  
  91.         // Send temperature via Bluetooth
  92.         sendTemperature(temperature);
  93.     }
  94.  
  95.     // Read temperature from DS18B20 sensor
  96.     float dsTemp = temperatureSensor.getTempC();
  97.     if (!isnan(dsTemp)) {
  98.         // Display DS18B20 temperature on LCD
  99.         lcd.setCursor(0, 1);
  100.         lcd.print("DS Temp: ");
  101.         lcd.print(dsTemp);
  102.         lcd.print(" C");
  103.        
  104.         // Send DS18B20 temperature via Bluetooth
  105.         sendTemperature(dsTemp);
  106.     } else {
  107.         lcd.setCursor(0, 1);
  108.         lcd.print("DS Error");
  109.     }
  110.  
  111.     delay(2000); // Wait for 2 seconds before next reading
  112. }
  113.  
  114. void updateOutputs()
  115. {
  116.     // Update the LCD display with the latest readings
  117.     lcd.display();
  118. }
  119.  
  120. void sendTemperature(float temperature) {
  121.     // Format temperature data as a string
  122.     String tempData = "Temperature: " + String(temperature) + " C";
  123.     SerialBT.println(tempData); // Send temperature data via Bluetooth
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement