EasyManuals Logo

SunFounder ESP32 User Manual

Default Icon
771 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #145 background imageLoading...
Page #145 background image
SunFounder ESP32 Starter Kit
The following is a brief summary of the code:
Include necessary libraries: The code begins by including necessary libraries for working with Bluetooth Low
Energy (BLE) on the ESP32.
#include "BLEDevice.h"
#include "BLEServer.h"
#include "BLEUtils.h"
#include "BLE2902.h"
Global Variables: The code defines a set of global variables including the Bluetooth device name (bleName),
variables to keep track of received text and the time of the last message, UUIDs for the service and characteristics,
and a BLECharacteristic object (pCharacteristic).
// Define the Bluetooth device name
const char *bleName = "ESP32_Bluetooth";
// Define the received text and the time of the last message
String receivedText = "";
unsigned long lastMessageTime = 0;
// Define the UUIDs of the service and characteristics
#define SERVICE_UUID "your_service_uuid_here"
#define CHARACTERISTIC_UUID_RX "your_rx_characteristic_uuid_here"
#define CHARACTERISTIC_UUID_TX "your_tx_characteristic_uuid_here"
// Define the Bluetooth characteristic
BLECharacteristic *pCharacteristic;
Setup: In the setup() function, the serial port is initialized with a baud rate of 115200 and the setupBLE()
function is called to set up the Bluetooth BLE.
void setup() {
Serial.begin(115200); // Initialize the serial port
setupBLE(); // Initialize the Bluetooth BLE
}
Main Loop: In the loop() function, if a string was received over BLE (i.e., receivedText is not empty) and
at least 1 second has passed since the last message, the code prints the received string to the serial monitor, sets
the characteristic value to the received string, sends a notification, and then clears the received string. If data
is available on the serial port, it reads the string until a newline character is encountered, sets the characteristic
value to this string, and sends a notification.
void loop() {
// When the received text is not empty and the time since the last
˓message is over 1 second
// Send a notification and print the received text
if (receivedText.length() > 0 && millis() - lastMessageTime > 1000) {
Serial.print("Received message: ");
Serial.println(receivedText);
pCharacteristic->setValue(receivedText.c_str());
pCharacteristic->notify();
receivedText = "";
}
(continues on next page)
1.38. 7.1 Bluetooth 139

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the SunFounder ESP32 and is the answer not in the manual?

SunFounder ESP32 Specifications

General IconGeneral
BrandSunFounder
ModelESP32
CategoryController
LanguageEnglish