Advertisement
pleasedontcode

Bluetooth Temperature rev_07

Oct 2nd, 2024
41
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: Bluetooth Temperature
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-02 21:08:33
  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 BluetoothSerial library
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8;
  43. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t display_LCD1602_RS_PIN_D2 = 2;
  47. const uint8_t display_LCD1602_E_PIN_D3 = 3;
  48. const uint8_t display_LCD1602_D4_PIN_D4 = 4;
  49. const uint8_t display_LCD1602_D5_PIN_D5 = 5;
  50. const uint8_t display_LCD1602_D6_PIN_D6 = 6;
  51. const uint8_t display_LCD1602_D7_PIN_D7 = 7;
  52.  
  53. /***** DEFINITION OF I2C PINS *****/
  54. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  55. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  56. const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. // Initialize the LiquidCrystal object with the defined pins
  60. 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);
  61. DS18B20 ds(temp_DS18B20_DQ_PIN_D8); // Initialize DS18B20 object with the defined pin
  62.  
  63. // Initialize the Adafruit SSD1306 object with the I2C address
  64. Adafruit_SSD1306 display(myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4, myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5);
  65.  
  66. // Initialize the U8g2_for_Adafruit_GFX object with the display object
  67. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  68.  
  69. // Initialize the DHT object with the defined pin and type
  70. #define DHTTYPE DHT22
  71. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHTTYPE); // Initialize DHT object
  72.  
  73. // Initialize Bluetooth Serial
  74. BluetoothSerial SerialBT;
  75.  
  76. void setup(void)
  77. {
  78.     // put your setup code here, to run once:
  79.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  80.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  81.  
  82.     pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
  83.     pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
  84.     pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
  85.     pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
  86.     pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
  87.     pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
  88.  
  89.     // Initialize the LCD
  90.     lcd.begin(16, 2); // Set up the LCD's number of columns and rows
  91.     lcd.print("Hello, World!"); // Print a message to the LCD
  92.    
  93.     // Initialize the OLED display
  94.     display.begin(SSD1306_SWITCHCAPVCC, myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  95.     display.clearDisplay(); // Clear the display buffer
  96.    
  97.     // Initialize U8g2_for_Adafruit_GFX with the display object
  98.     u8g2_for_adafruit_gfx.begin(display);
  99.    
  100.     // Initialize the DHT sensor
  101.     dht.begin(); // Start the DHT sensor
  102.    
  103.     // Initialize Bluetooth
  104.     SerialBT.begin("ESP32_Device"); // Name of the Bluetooth device
  105. }
  106.  
  107. void loop(void)
  108. {
  109.     // put your main code here, to run repeatedly:
  110.     updateOutputs(); // Refresh output data
  111.  
  112.     // Read temperature and send it via Bluetooth
  113.     float temperature = dht.readTemperature();
  114.     if (!isnan(temperature)) {
  115.         SerialBT.println("Temperature: " + String(temperature) + " °C");
  116.         lcd.setCursor(0, 0);
  117.         lcd.print("Temp: ");
  118.         lcd.print(temperature);
  119.         lcd.print(" C");
  120.     } else {
  121.         lcd.setCursor(0, 0);
  122.         lcd.print("Temp: Err");
  123.     }
  124.     delay(2000); // Delay for 2 seconds before the next reading
  125. }
  126.  
  127. void updateOutputs()
  128. {
  129.     // Update the output states based on the raw data
  130.     digitalWrite(display_LCD1602_RS_PIN_D2, LOW); // Example value
  131.     digitalWrite(display_LCD1602_E_PIN_D3, LOW); // Example value
  132.     digitalWrite(display_LCD1602_D4_PIN_D4, LOW); // Example value
  133.     digitalWrite(display_LCD1602_D5_PIN_D5, LOW); // Example value
  134.     digitalWrite(display_LCD1602_D6_PIN_D6, LOW); // Example value
  135.     digitalWrite(display_LCD1602_D7_PIN_D7, LOW); // Example value
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement