Advertisement
pleasedontcode

Climate Monitor rev_11

Oct 2nd, 2024
37
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: Climate Monitor
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-02 21:47:47
  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> //https://github.com/arduino-libraries/LiquidCrystal
  30. #include <DS18B20.h> //https://github.com/matmunk/DS18B20
  31. #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
  32. #include <BluetoothSerial.h> // Library for Bluetooth serial communication
  33. #include "LiquidCrystalDisplay.h" // Include custom library for LCD functionality
  34. #include "DS18B20Sensor.h" // Include custom library for DS18B20 sensor functionality
  35. #include "Adafruit_SSD1306_OLED.h" // Include OLED library
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void sendTemperature(); // Function to send the temperature via Bluetooth
  41. void updateBluetoothDisplay(); // Function to update LCD with Bluetooth data
  42.  
  43. /***** DIGITAL INPUT/OUTPUT PINS *****/
  44. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8; // DS18B20 sensor pin
  45. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9; // DHT22 sensor pin
  46. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  47. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHTTYPE); // Create a DHT sensor object
  48.  
  49. // LCD display pins
  50. const uint8_t display_LCD1602_RS_PIN_D2 = 2;
  51. const uint8_t display_LCD1602_E_PIN_D3 = 3;
  52. const uint8_t display_LCD1602_D4_PIN_D4 = 4;
  53. const uint8_t display_LCD1602_D5_PIN_D5 = 5;
  54. const uint8_t display_LCD1602_D6_PIN_D6 = 6;
  55. const uint8_t display_LCD1602_D7_PIN_D7 = 7;
  56.  
  57. // Create an instance for Bluetooth
  58. BluetoothSerial SerialBT;
  59.  
  60. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  61. // Include and initialize the sensor libraries
  62. DS18B20 ds(temp_DS18B20_DQ_PIN_D8); //create an instance for DS18B20
  63.  
  64. void setup(void) {
  65.     Serial.begin(9600); // Initialize serial communication
  66.     dht.begin(); // Initialize the DHT sensor
  67.     SerialBT.begin("ESP32_Bluetooth"); // Set Bluetooth device name
  68.     initializeLCD(); // Initialize the LCD display
  69. }
  70.  
  71. void loop(void) {
  72.     sendTemperature(); // Send temperature data
  73.     updateBluetoothDisplay(); // Display data on LCD
  74.     delay(2000); // Wait before next loop to avoid flooding data
  75. }
  76.  
  77. // Function to read temperature and send it via Bluetooth
  78. void sendTemperature() {
  79.     float humidity = dht.readHumidity();
  80.     float temperature = dht.readTemperature();
  81.     if (isnan(humidity) || isnan(temperature)) {
  82.         Serial.println("Failed to read from DHT sensor!");
  83.         return; // Exit function if read failed
  84.     }
  85.  
  86.     // Prepare message for Bluetooth
  87.     String message = String("Temp: ") + temperature + "°C Humidity: " + humidity + "%";
  88.     SerialBT.println(message); // Send message via Bluetooth
  89. }
  90.  
  91. // Function to update the displayed message on the LCD
  92. void updateBluetoothDisplay() {
  93.     if (SerialBT.available()) {
  94.         String inputMessage = SerialBT.readString(); // Read incoming message
  95.         displayData(inputMessage.c_str()); // Display message on the LCD
  96.     }
  97. }
  98.  
  99. void displayData(const char* message) {
  100.     lcd.clear(); // Clear LCD
  101.     lcd.print(message); // Print Bluetooth message on LCD
  102. }
  103.  
  104. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement