Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Sensor Communication
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-07-05 00:56:58
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Initialize internal Bluetooth device as master, */
- /* receive BLE data, and show it on a 16x2 LCD using */
- /* LiquidCrystal library for Arduino. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* read temperature and send it via bluetooth. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <LiquidCrystal.h> //https://github.com/arduino-libraries/LiquidCrystal
- #include <Adafruit_SSD1306.h> //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
- #include <U8g2_for_Adafruit_GFX.h> //https://github.com/olikraus/U8g2_for_Adafruit_GFX
- #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
- #include <BluetoothSerial.h> // For Bluetooth
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void Bluetooth_receiveData();
- void Bluetooth_sendTemperature();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t temp_DS18B20_DQ_PIN_D8 = 8;
- const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t display_LCD1602_RS_PIN_D2 = 2;
- const uint8_t display_LCD1602_E_PIN_D3 = 3;
- const uint8_t display_LCD1602_D4_PIN_D4 = 4;
- const uint8_t display_LCD1602_D5_PIN_D5 = 5;
- const uint8_t display_LCD1602_D6_PIN_D6 = 6;
- const uint8_t display_LCD1602_D7_PIN_D7 = 7;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
- const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
- const uint8_t myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
- /***** DECLARATION OF OBJECT INSTANCES *****/
- // Instantiate the Bluetooth serial object
- BluetoothSerial SerialBT;
- // Instantiate DHT sensor object; assuming DHT22 at pin 9
- DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT::DHT22);
- // Instantiate SSD1306 OLED display object (using I2C)
- Adafruit_SSD1306 display(A4, A5);
- /****** SETUP PROCEDURE ******/
- void setup(void)
- {
- // Initialize Bluetooth as master, and enable receive mode
- SerialBT.begin("NanoESP32_BLE_Master"); // Name of Bluetooth device
- // Setup pin modes for digital I/O
- pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
- pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
- pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
- pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
- pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
- pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
- pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
- pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
- // Initialize DHT sensor
- dht.begin();
- // Initialize OLED display
- display.begin(SSD1306_SWITCHCAPVCC, myOledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
- display.display();
- delay(2000);
- display.clearDisplay();
- // Send initial temperature via Bluetooth every cycle
- Bluetooth_sendTemperature();
- // Display WiFi message on LCD
- // For placeholder, just show message
- // The sequence functionality is minimal to focus on core system
- }
- /****** LOOP PROCEDURE ******/
- void loop(void)
- {
- // Receive data over BLE (e.g., screen commands)
- Bluetooth_receiveData();
- // Read temperature sensors
- float tempC = dht.readTemperature(); // Celsius
- // Send temperature data via BLE
- Bluetooth_sendTemperature();
- // Show temperature on LCD
- // Using a simplified display update, when data is available
- // Update LCD with temperature
- String tempString = "Temp:" + String(tempC,2) + " C";
- // Write to LCD considering system architecture if needed
- // For simplicity, assume 16x2 LCD
- for (uint8_t i = 0; i < 16 && i < tempString.length(); i++) {
- digitalWrite(display_LCD1602_RS_PIN_D2, HIGH); // set register select high for data
- digitalWrite(display_LCD1602_E_PIN_D3, LOW);
- // set data pins based on character bits
- // Implemented depends on actual LCD wiring; here incar placeholder
- // For dynamics, we'll assume the RS/ E signal directly controls the display and leave far PIDs unspecified
- // Alternatively, pre-implemented functions would be invoked here.
- updateOutputs(); // placeholder for display update
- }
- delay(1000);
- }
- /****** BLE Callback Function: Receive Data ******/
- void Bluetooth_receiveData()
- {
- if (SerialBT.available()) {
- String incomingData = SerialBT.readString();
- // Process incoming BLE data, e.g., change display message, toggle LEDs etc.
- // For demonstration, show it on OLED
- display.clearDisplay();
- display.setCursor(0,0);
- display.println(incomingData);
- display.display();
- }
- }
- /****** Bluetooth SEND Temperature Function ******/
- void Bluetooth_sendTemperature()
- {
- float tempF = dht.readTemperature(true); // Fahrenheit
- String tempMsg = "Temp:" + String(tempF, 2) + "F";
- SerialBT.println(tempMsg);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement