Advertisement
pleasedontcode

Temperature Monitor rev_24

Oct 3rd, 2024
67
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 09:55:41
  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.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs();
  40. void readAndDisplayTemperature();
  41. void sendTemperatureViaBluetooth(float temperature);
  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 OUTPUT RAW VARIABLES *****/
  61. bool display_LCD1602_RS_PIN_D2_rawData = 0;
  62. bool display_LCD1602_E_PIN_D3_rawData = 0;
  63. bool display_LCD1602_D4_PIN_D4_rawData = 0;
  64. bool display_LCD1602_D5_PIN_D5_rawData = 0;
  65. bool display_LCD1602_D6_PIN_D6_rawData = 0;
  66. bool display_LCD1602_D7_PIN_D7_rawData = 0;
  67.  
  68. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  69. float display_LCD1602_RS_PIN_D2_phyData = 0.0;
  70. float display_LCD1602_E_PIN_D3_phyData = 0.0;
  71. float display_LCD1602_D4_PIN_D4_phyData = 0.0;
  72. float display_LCD1602_D5_PIN_D5_phyData = 0.0;
  73. float display_LCD1602_D6_PIN_D6_phyData = 0.0;
  74. float display_LCD1602_D7_PIN_D7_phyData = 0.0;
  75.  
  76. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  77. 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);
  78. DS18B20 tempSensor(temp_DS18B20_DQ_PIN_D8);
  79. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22);
  80. BluetoothSerial SerialBT; // Create Bluetooth Serial object
  81.  
  82. void setup(void)
  83. {
  84.     // Initialize pins
  85.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  86.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  87.     pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
  88.     pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
  89.     pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
  90.     pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
  91.     pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
  92.     pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
  93.  
  94.     // Initialize the LCD and sensors
  95.     lcd.begin(16, 2); // Initialize the LCD
  96.     dht.begin(); // Initialize the DHT sensor
  97.     tempSensor.begin(); // Initialize the DS18B20 sensor
  98.  
  99.     // Initialize Bluetooth
  100.     SerialBT.begin("ESP32_Bluetooth"); // Name of the Bluetooth device
  101. }
  102.  
  103. void loop(void)
  104. {
  105.     // Refresh output data
  106.     updateOutputs();
  107.     // Read temperature and display it
  108.     readAndDisplayTemperature();
  109. }
  110.  
  111. void updateOutputs()
  112. {
  113.     digitalWrite(display_LCD1602_RS_PIN_D2, display_LCD1602_RS_PIN_D2_rawData);
  114.     digitalWrite(display_LCD1602_E_PIN_D3, display_LCD1602_E_PIN_D3_rawData);
  115.     digitalWrite(display_LCD1602_D4_PIN_D4, display_LCD1602_D4_PIN_D4_rawData);
  116.     digitalWrite(display_LCD1602_D5_PIN_D5, display_LCD1602_D5_PIN_D5_rawData);
  117.     digitalWrite(display_LCD1602_D6_PIN_D6, display_LCD1602_D6_PIN_D6_rawData);
  118.     digitalWrite(display_LCD1602_D7_PIN_D7, display_LCD1602_D7_PIN_D7_rawData);
  119. }
  120.  
  121. void readAndDisplayTemperature()
  122. {
  123.     float temperature = dht.readTemperature(); // Read temperature from DHT sensor
  124.     if (isnan(temperature)) {
  125.         lcd.print("Error reading temp");
  126.     } else {
  127.         lcd.setCursor(0, 0);
  128.         lcd.print("Temp: ");
  129.         lcd.print(temperature);
  130.         lcd.print(" C");
  131.         sendTemperatureViaBluetooth(temperature); // Send temperature via Bluetooth
  132.     }
  133. }
  134.  
  135. void sendTemperatureViaBluetooth(float temperature)
  136. {
  137.     // Send temperature data via Bluetooth
  138.     SerialBT.print("Temperature: ");
  139.     SerialBT.print(temperature);
  140.     SerialBT.println(" C");
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement