Advertisement
pleasedontcode

Temperature Bluetooth rev_23

Oct 3rd, 2024
68
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 Bluetooth
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-03 09:50:23
  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 readTemperatureAndSend(); // Function to read temperature and send via Bluetooth
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8;
  44. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  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 OUTPUT RAW VARIABLES *****/
  55. bool display_LCD1602_RS_PIN_D2_rawData = 0;
  56. bool display_LCD1602_E_PIN_D3_rawData = 0;
  57. bool display_LCD1602_D4_PIN_D4_rawData = 0;
  58. bool display_LCD1602_D5_PIN_D5_rawData = 0;
  59. bool display_LCD1602_D6_PIN_D6_rawData = 0;
  60. bool display_LCD1602_D7_PIN_D7_rawData = 0;
  61.  
  62. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  63. 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);
  64. BluetoothSerial SerialBT; // Bluetooth Serial object
  65.  
  66. void setup(void)
  67. {
  68.     // Initialize the LCD
  69.     lcd.begin(16, 2);
  70.     lcd.print("Initializing...");
  71.  
  72.     // Initialize Bluetooth
  73.     SerialBT.begin("ESP32_Bluetooth"); // Bluetooth device name
  74.     lcd.clear();
  75.     lcd.print("Bluetooth Ready");
  76.  
  77.     // Set pin modes
  78.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  79.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  80.     pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
  81.     pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
  82.     pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
  83.     pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
  84.     pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
  85.     pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
  86. }
  87.  
  88. void loop(void)
  89. {
  90.     // Refresh output data
  91.     updateOutputs();
  92.    
  93.     // Read temperature and send via Bluetooth
  94.     readTemperatureAndSend();
  95. }
  96.  
  97. void updateOutputs()
  98. {
  99.     digitalWrite(display_LCD1602_RS_PIN_D2, display_LCD1602_RS_PIN_D2_rawData);
  100.     digitalWrite(display_LCD1602_E_PIN_D3, display_LCD1602_E_PIN_D3_rawData);
  101.     digitalWrite(display_LCD1602_D4_PIN_D4, display_LCD1602_D4_PIN_D4_rawData);
  102.     digitalWrite(display_LCD1602_D5_PIN_D5, display_LCD1602_D5_PIN_D5_rawData);
  103.     digitalWrite(display_LCD1602_D6_PIN_D6, display_LCD1602_D6_PIN_D6_rawData);
  104.     digitalWrite(display_LCD1602_D7_PIN_D7, display_LCD1602_D7_PIN_D7_rawData);
  105. }
  106.  
  107. void readTemperatureAndSend()
  108. {
  109.     // Read temperature from DHT22 sensor
  110.     float temperature = dht.readTemperature(); // Assuming dht is an instance of DHT class
  111.     if (isnan(temperature)) {
  112.         lcd.print("Failed to read");
  113.         return;
  114.     }
  115.    
  116.     // Display temperature on LCD
  117.     lcd.setCursor(0, 1);
  118.     lcd.print("Temp: ");
  119.     lcd.print(temperature);
  120.     lcd.print(" C");
  121.  
  122.     // Send temperature via Bluetooth
  123.     SerialBT.println(temperature);
  124. }
  125.  
  126. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement