Advertisement
pleasedontcode

Temperature Display rev_17

Oct 2nd, 2024
44
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 Display
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-02 22:30:42
  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.  
  28. /********* User code review feedback **********
  29. #### Feedback 1 ####
  30. - put all code only in ino file.
  31. ********* User code review feedback **********/
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <Wire.h>
  35. #include <LiquidCrystal.h>  //https://github.com/arduino-libraries/LiquidCrystal
  36. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  37. #include <Adafruit_SSD1306.h>   //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  38. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  39. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44. void updateOutputs();
  45. void readTemperatureAndSend();
  46.  
  47. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  48. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8;
  49. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  50.  
  51. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  52. const uint8_t display_LCD1602_RS_PIN_D2 = 2;
  53. const uint8_t display_LCD1602_E_PIN_D3 = 3;
  54. const uint8_t display_LCD1602_D4_PIN_D4 = 4;
  55. const uint8_t display_LCD1602_D5_PIN_D5 = 5;
  56. const uint8_t display_LCD1602_D6_PIN_D6 = 6;
  57. const uint8_t display_LCD1602_D7_PIN_D7 = 7;
  58.  
  59. /***** DEFINITION OF I2C PINS *****/
  60. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  61. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  62. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
  63.  
  64. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  65. // Initialize the Adafruit SSD1306 display object with the correct I2C address
  66. Adafruit_SSD1306 display(myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4, myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5);
  67.  
  68. // Initialize U8g2_for_Adafruit_GFX object
  69. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create an instance of U8g2_for_Adafruit_GFX
  70.  
  71. // Initialize DS18B20 object with the correct pin
  72. DS18B20 ds(temp_DS18B20_DQ_PIN_D8); // Using the defined pin for DS18B20
  73.  
  74. // Initialize DHT sensor 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); // Initialize DHT sensor
  77.  
  78. // Initialize the LiquidCrystal display object
  79. 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);
  80.  
  81. void setup(void)
  82. {
  83.     // Initialize Serial for Bluetooth communication
  84.     Serial.begin(9600);
  85.  
  86.     // Set pin modes
  87.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  88.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  89.  
  90.     // Initialize the LCD display
  91.     lcd.begin(16, 2); // Set up the LCD's number of columns and rows
  92.     lcd.print("Initializing...");
  93.  
  94.     // Initialize the OLED display
  95.     display.begin(SSD1306_SWITCHCAPVCC, myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  96.     display.display();
  97.     delay(2000);
  98.     display.clearDisplay();
  99.  
  100.     // Initialize U8g2_for_Adafruit_GFX with the display
  101.     u8g2_for_adafruit_gfx.begin(display); // Initialize the U8g2_for_Adafruit_GFX library
  102.  
  103.     // Initialize the DHT sensor
  104.     dht.begin(); // Start the DHT sensor
  105. }
  106.  
  107. void loop(void)
  108. {
  109.     // Update outputs and read temperature to send via Bluetooth
  110.     updateOutputs(); // Refresh output data
  111.     readTemperatureAndSend(); // Read temperature and send via Bluetooth
  112. }
  113.  
  114. void updateOutputs()
  115. {
  116.     // Update the LCD display with the latest data
  117.     lcd.clear();
  118.     lcd.setCursor(0, 0);
  119.     lcd.print("Temp: ");
  120.     lcd.print(dht.readTemperature());
  121.     lcd.print(" C");
  122.     lcd.setCursor(0, 1);
  123.     lcd.print("Humidity: ");
  124.     lcd.print(dht.readHumidity());
  125.     lcd.print(" %");
  126. }
  127.  
  128. void readTemperatureAndSend()
  129. {
  130.     // Read temperature from DHT22 sensor
  131.     float temperature = dht.readTemperature();
  132.     float humidity = dht.readHumidity();
  133.  
  134.     // Check if the readings are valid
  135.     if (isnan(temperature) || isnan(humidity)) {
  136.         Serial.println("Failed to read from DHT sensor!");
  137.         return;
  138.     }
  139.  
  140.     // Send temperature data via Bluetooth
  141.     Serial.print("Temperature: ");
  142.     Serial.println(temperature);
  143.     Serial.print("Humidity: ");
  144.     Serial.println(humidity);
  145. }
  146.  
  147. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement