Advertisement
pleasedontcode

Climate Monitor rev_30

Oct 3rd, 2024
76
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-03 11:35:20
  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 LCDs
  30. #include <DS18B20.h>         // Library for DS18B20 temperature sensor
  31. #include <DHT.h>             // Library for DHT temperature and humidity sensor
  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 readTemperatureAndSend();
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8; // DS18B20 data pin
  42. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9; // DHT22 data pin
  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 OUTPUT RAW VARIABLES *****/
  53. bool display_LCD1602_RS_PIN_D2_rawData = 0;
  54. bool display_LCD1602_E_PIN_D3_rawData = 0;
  55. bool display_LCD1602_D4_PIN_D4_rawData = 0;
  56. bool display_LCD1602_D5_PIN_D5_rawData = 0;
  57. bool display_LCD1602_D6_PIN_D6_rawData = 0;
  58. bool display_LCD1602_D7_PIN_D7_rawData = 0;
  59.  
  60. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  61. // Create an instance of the LiquidCrystal class
  62. 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);
  63.  
  64. // Create an instance of the DS18B20 class
  65. DS18B20 tempSensor(temp_DS18B20_DQ_PIN_D8);
  66.  
  67. // Create an instance of the DHT class
  68. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22); // DHT22 sensor
  69.  
  70. // Create an instance of the BluetoothSerial class
  71. BluetoothSerial SerialBT;
  72.  
  73. void setup(void)
  74. {
  75.     // Initialize the LCD
  76.     lcd.begin(16, 2); // Set up the LCD's number of columns and rows
  77.     lcd.print("Initializing...");
  78.  
  79.     // Initialize the temperature sensors
  80.     dht.begin();
  81.     tempSensor.begin();
  82.  
  83.     // Initialize Bluetooth
  84.     SerialBT.begin("ESP32_Master"); // Name of the Bluetooth device
  85.  
  86.     // Set pin modes
  87.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  88.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  89.     pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
  90.     pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
  91.     pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
  92.     pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
  93.     pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
  94.     pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
  95. }
  96.  
  97. void loop(void)
  98. {
  99.     // Update outputs and read temperature
  100.     updateOutputs();
  101.     readTemperatureAndSend();
  102.     delay(2000); // Delay for stability
  103. }
  104.  
  105. void updateOutputs()
  106. {
  107.     digitalWrite(display_LCD1602_RS_PIN_D2, display_LCD1602_RS_PIN_D2_rawData);
  108.     digitalWrite(display_LCD1602_E_PIN_D3, display_LCD1602_E_PIN_D3_rawData);
  109.     digitalWrite(display_LCD1602_D4_PIN_D4, display_LCD1602_D4_PIN_D4_rawData);
  110.     digitalWrite(display_LCD1602_D5_PIN_D5, display_LCD1602_D5_PIN_D5_rawData);
  111.     digitalWrite(display_LCD1602_D6_PIN_D6, display_LCD1602_D6_PIN_D6_rawData);
  112.     digitalWrite(display_LCD1602_D7_PIN_D7, display_LCD1602_D7_PIN_D7_rawData);
  113. }
  114.  
  115. void readTemperatureAndSend()
  116. {
  117.     // Read temperature from DHT sensor
  118.     float temperature = dht.readTemperature();
  119.     if (isnan(temperature)) {
  120.         lcd.print("Failed to read temp");
  121.     } else {
  122.         lcd.setCursor(0, 1); // Move to the second line
  123.         lcd.print("Temp: ");
  124.         lcd.print(temperature);
  125.         lcd.print(" C");
  126.  
  127.         // Send temperature via Bluetooth
  128.         SerialBT.print("Temperature: ");
  129.         SerialBT.print(temperature);
  130.         SerialBT.println(" C");
  131.     }
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement