Advertisement
pleasedontcode

Climate Monitor rev_09

Oct 2nd, 2024
39
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:26:13
  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 <Adafruit_SSD1306.h> // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  32. #include <U8g2_for_Adafruit_GFX.h> // https://github.com/olikraus/U8g2_for_Adafruit_GFX
  33. #include <DHT.h>              // https://github.com/adafruit/DHT-sensor-library
  34. #include <BluetoothSerial.h>   // Include Bluetooth Serial library
  35. #include "LiquidCrystalDisplay.h" // Include the header file for LCD
  36. #include "AdafruitSSD1306Display.h" // Include the header file for OLED display
  37. #include "DHTSensor.h" // Include the header file for DHT sensor
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42.  
  43. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  44. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8;
  45. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t display_LCD1602_RS_PIN_D2 = 2;
  49. const uint8_t display_LCD1602_E_PIN_D3 = 3;
  50. const uint8_t display_LCD1602_D4_PIN_D4 = 4;
  51. const uint8_t display_LCD1602_D5_PIN_D5 = 5;
  52. const uint8_t display_LCD1602_D6_PIN_D6 = 6;
  53. const uint8_t display_LCD1602_D7_PIN_D7 = 7;
  54.  
  55. /***** DEFINITION OF I2C PINS *****/
  56. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  57. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  58. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
  59.  
  60. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  61.  
  62. // Create an instance of the DS18B20 class using the appropriate pin
  63. DS18B20 ds(temp_DS18B20_DQ_PIN_D8);
  64.  
  65. // Initialize the OLED display using the Adafruit_SSD1306 library
  66. Adafruit_SSD1306 display(myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4, myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5);
  67.  
  68. // Create an instance of the DHT class
  69. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22); // Initialize DHT sensor
  70.  
  71. // Create an instance of the LiquidCrystal class
  72. 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);
  73.  
  74. // Create an instance of the BluetoothSerial class
  75. BluetoothSerial SerialBT;
  76.  
  77. void setup(void)
  78. {
  79.     Serial.begin(9600); // Initialize serial communication for debugging
  80.     SerialBT.begin("ESP32_Bluetooth"); // Initialize Bluetooth with device name
  81.  
  82.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  83.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  84.  
  85.     // Initialize the LCD
  86.     lcd.begin(16, 2); // Set the dimensions of the LCD (16 columns and 2 rows)
  87.     lcd.print("Initializing..."); // Print a message to the LCD
  88.  
  89.     // Initialize the OLED display
  90.     display.begin(SSD1306_SWITCHCAPVCC, myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  91.     display.clearDisplay(); // Clear the display
  92.     display.display(); // Update the display
  93.  
  94.     // Initialize the DHT sensor
  95.     dht.begin(); // Start the DHT sensor
  96. }
  97.  
  98. void loop(void)
  99. {
  100.     // Read temperature and humidity from DHT sensor
  101.     float h = dht.readHumidity();
  102.     float t = dht.readTemperature();
  103.  
  104.     // Check if any reads failed and exit early (to try again).
  105.     if (isnan(h) || isnan(t)) {
  106.         Serial.println("Failed to read from DHT sensor!");
  107.         return;
  108.     }
  109.  
  110.     // Display temperature and humidity on the LCD
  111.     lcd.clear();
  112.     lcd.setCursor(0, 0);
  113.     lcd.print("Temp: ");
  114.     lcd.print(t);
  115.     lcd.print(" C");
  116.     lcd.setCursor(0, 1);
  117.     lcd.print("Humidity: ");
  118.     lcd.print(h);
  119.     lcd.print(" %");
  120.  
  121.     // Send temperature data via Bluetooth
  122.     SerialBT.print("Temperature: ");
  123.     SerialBT.print(t);
  124.     SerialBT.println(" C");
  125.  
  126.     // Delay for a while before the next loop
  127.     delay(2000); // Delay for 2 seconds
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement