Advertisement
pleasedontcode

"Temperature Monitoring" rev_05

Oct 2nd, 2024
48
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 Monitoring"
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-02 20:37:17
  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.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void initializeLCD(); // Function to initialize the LCD
  39. void updateLCDDisplay(); // Function to update LCD display with temperature
  40. void sendTemperature(); // Function to send temperature via Bluetooth
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8; // Pin for DS18B20
  44. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9; // Pin for DHT22
  45.  
  46. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  47. const uint8_t display_LCD1602_RS_PIN_D2 = 2;
  48. const uint8_t display_LCD1602_E_PIN_D3 = 3;
  49. const uint8_t display_LCD1602_D4_PIN_D4 = 4;
  50. const uint8_t display_LCD1602_D5_PIN_D5 = 5;
  51. const uint8_t display_LCD1602_D6_PIN_D6 = 6;
  52. const uint8_t display_LCD1602_D7_PIN_D7 = 7;
  53.  
  54. /***** DEFINITION OF I2C PINS *****/
  55. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  56. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  57. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60.  
  61. // Initialize the LCD
  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. // Initialize the OLED display with the correct I2C address
  65. #define OLED_RESET -1 // Define reset pin as -1 since it is not used
  66. Adafruit_SSD1306 display(OLED_RESET); // Create an instance of the Adafruit_SSD1306 class
  67.  
  68. // Initialize U8g2_for_Adafruit_GFX object with the display instance
  69. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create an instance of the U8G2_FOR_ADAFRUIT_GFX class
  70.  
  71. // Initialize DS18B20 object with the correct pin
  72. DS18B20 ds(temp_DS18B20_DQ_PIN_D8); // Create an instance of DS18B20 on pin D8
  73.  
  74. // Initialize DHT object with the correct pin and type
  75. #define DHTTYPE DHT22   // Define the type of DHT sensor
  76. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHTTYPE); // Create an instance of the DHT class
  77.  
  78. void setup(void)
  79. {
  80.     // put your setup code here, to run once:
  81.  
  82.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  83.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  84.  
  85.     pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
  86.     pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
  87.     pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
  88.     pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
  89.     pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
  90.     pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
  91.  
  92.     // Initialize the OLED display
  93.     display.begin(SSD1306_SWITCHCAPVCC, myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  94.     display.display(); // Show initial display
  95.     delay(2000);
  96.     display.clearDisplay(); // Clear the display
  97.  
  98.     // Initialize the U8g2_for_Adafruit_GFX library
  99.     u8g2_for_adafruit_gfx.begin(display); // Initialize U8g2 with the Adafruit display
  100.  
  101.     initializeLCD(); // Initialize the LCD display
  102.  
  103.     // Initialize the DHT sensor
  104.     dht.begin(); // Start the DHT sensor
  105.  
  106.     // Initialize Bluetooth communication
  107.     Serial.begin(9600); // Start serial communication for Bluetooth
  108. }
  109.  
  110. void loop(void)
  111. {
  112.     // put your main code here, to run repeatedly:
  113.  
  114.     updateLCDDisplay(); // Update LCD display with temperature
  115.     sendTemperature(); // Send temperature data via Bluetooth
  116. }
  117.  
  118. void updateLCDDisplay()
  119. {
  120.     // Read temperature from DHT sensor
  121.     float temperature = dht.readTemperature();
  122.     if (!isnan(temperature)) {
  123.         // Update the LCD display with the temperature
  124.         lcd.clear();
  125.         lcd.print("Temp: ");
  126.         lcd.print(temperature);
  127.         lcd.print(" C");
  128.         lcd.display();
  129.     }
  130. }
  131.  
  132. void sendTemperature()
  133. {
  134.     // Read temperature from DHT sensor
  135.     float temperature = dht.readTemperature();
  136.     if (!isnan(temperature)) {
  137.         // Send temperature via Bluetooth (assuming Serial is used for Bluetooth)
  138.         Serial.print("Temperature: ");
  139.         Serial.println(temperature);
  140.     }
  141. }
  142.  
  143. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement