Advertisement
pleasedontcode

**Sensor Monitor** rev_38

Oct 3rd, 2024
79
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: **Sensor Monitor**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-03 14:19:16
  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> // Include Bluetooth library
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void);
  38. void readTemperatureAndSend(void;
  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 OUTPUT PHYSICAL VARIABLES *****/
  61. float display_LCD1602_RS_PIN_D2_phyData = 0.0;
  62. float display_LCD1602_E_PIN_D3_phyData = 0.0;
  63. float display_LCD1602_D4_PIN_D4_phyData = 0.0;
  64. float display_LCD1602_D5_PIN_D5_phyData = 0.0;
  65. float display_LCD1602_D6_PIN_D6_phyData = 0.0;
  66. float display_LCD1602_D7_PIN_D7_phyData = 0.0;
  67.  
  68. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  69. // Initialize the LiquidCrystal object
  70. LiquidCrystal lcd(display_LCD1602_RS_PIN_D2, display_LCD1602_E_PIN_D3,
  71.                   display_LCD1602_D4_PIN_D4, display_LCD1602_D5_PIN_D5,
  72.                   display_LCD1602_D6_PIN_D6, display_LCD1602_D7_PIN_D7);
  73.  
  74. // Initialize the DHT sensor
  75. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22);
  76.  
  77. // Initialize Bluetooth Serial
  78. BluetoothSerial SerialBT;
  79.  
  80. void setup(void)
  81. {
  82.     // Initialize the LCD
  83.     lcd.begin(16, 2);
  84.     lcd.clear();
  85.  
  86.     // Initialize the DHT sensor
  87.     dht.begin();
  88.  
  89.     // Initialize Bluetooth
  90.     SerialBT.begin("ESP32_Bluetooth"); // Bluetooth device name
  91.  
  92.     // Set pin modes
  93.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  94. }
  95.  
  96. void loop(void)
  97. {
  98.     // Update outputs
  99.     updateOutputs();
  100.  
  101.     // Read temperature and send via Bluetooth
  102.     readTemperatureAndSend();
  103.  
  104.     delay(2000); // Delay for stability
  105. }
  106.  
  107. void updateOutputs()
  108. {
  109.     // Update LCD display with raw data
  110.     lcd.setCursor(0, 0);
  111.     lcd.print("LCD Data:");
  112.     lcd.setCursor(0, 1);
  113.     lcd.print("Temperature: ");
  114. }
  115.  
  116. void readTemperatureAndSend()
  117. {
  118.     // Read temperature from DHT sensor
  119.     float temperature = dht.readTemperature(); // Read temperature as Celsius
  120.  
  121.     // Check if the reading is valid
  122.     if (isnan(temperature)) {
  123.         lcd.print("Err");
  124.         SerialBT.println("Failed to read temperature");
  125.     } else {
  126.         lcd.print(temperature);
  127.         SerialBT.print("Temperature: ");
  128.         SerialBT.println(temperature);
  129.     }
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement