赞
踩
摘要: 本文详细介绍了如何使用 ESP32 构建强大的蓝牙网关,实现蓝牙设备与 Wi-Fi/互联网之间的无缝连接和数据桥接。文章涵盖了连接和桥接功能、数据处理和分析能力,并提供了详细的代码示例和 Mermaid 生成的图表,助您轻松构建自己的蓝牙网关解决方案。
随着物联网 (IoT) 的快速发展,蓝牙技术凭借其低功耗、低成本和易用性,在连接各种传感器、设备和智能家居产品方面发挥着至关重要的作用。然而,蓝牙设备通常通信范围有限,且无法直接连接互联网。ESP32 是一款功能强大的微控制器,集成了 Wi-Fi 和蓝牙功能,使其成为构建蓝牙网关的理想选择,可以将蓝牙设备连接到互联网,实现远程监控和控制。
ESP32 蓝牙网关可以实现以下关键功能:
1. 连接和桥接:
2. 数据处理和分析:
下图展示了 ESP32 蓝牙网关的典型系统架构:
架构说明:
以下是一个简单的 ESP32 蓝牙网关代码示例,演示了如何使用 BLE 连接到蓝牙传感器并通过 Wi-Fi 将数据发送到 MQTT broker:
- #include <Arduino.h>
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEScan.h>
- #include <BLEAdvertisedDevice.h>
- #include <WiFi.h>
- #include <PubSubClient.h>
-
- // 设置 Wi-Fi 和 MQTT broker 信息
- const char* ssid = "your_wifi_ssid";
- const char* password = "your_wifi_password";
- const char* mqtt_server = "your_mqtt_broker_ip";
- const int mqtt_port = 1883;
-
- // 设置蓝牙服务和特征 UUID
- static BLEUUID serviceUUID("your_service_uuid");
- static BLEUUID characteristicUUID("your_characteristic_uuid");
-
- // 创建 BLE 和 MQTT 客户端
- BLEAdvertisedDevice* myDevice;
- WiFiClient espClient;
- PubSubClient client(espClient);
-
- // 连接到 Wi-Fi
- void setupWifi() {
- delay(10);
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
-
- WiFi.begin(ssid, password);
-
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
-
- Serial.println("");
- Serial.println("WiFi connected!");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
-
- // 连接到 MQTT broker
- void reconnect() {
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (client.connect("ESP32Client")) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- delay(5000);
- }
- }
- }
-
- // 扫描蓝牙设备
- class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
- void onResult(BLEAdvertisedDevice advertisedDevice) {
- if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
- BLEDevice::getScan()->stop();
- myDevice = new BLEAdvertisedDevice(advertisedDevice);
- }
- }
- };
-
- // 连接到蓝牙设备
- bool connectToSensor() {
- BLEClient* pClient = BLEDevice::createClient();
- Serial.println(" - Created client");
-
- // 连接到 GATT 服务器
- Serial.println(" - Connecting to server...");
- if (!pClient->connect(myDevice)) {
- Serial.println(" - Failed to connect to server");
- return false;
- }
- Serial.println(" - Connected to server");
-
- // 获取服务
- BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
- if (pRemoteService == nullptr) {
- Serial.print(" - Failed to find our service UUID: ");
- Serial.println(serviceUUID.toString().c_str());
- return false;
- }
- Serial.println(" - Found our service");
-
- // 获取特征
- BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(characteristicUUID);
- if (pRemoteCharacteristic == nullptr) {
- Serial.print(" - Failed to find our characteristic UUID: ");
- Serial.println(characteristicUUID.toString().c_str());
- return false;
- }
- Serial.println(" - Found our characteristic");
-
- // 读取特征值
- std::string value = pRemoteCharacteristic->readValue();
- Serial.print(" - The characteristic value was: ");
- Serial.println(value.c_str());
-
- // 发送数据到 MQTT broker
- if (client.connected()) {
- client.publish("sensor/data", value.c_str());
- }
-
- return true;
- }
-
- void setup() {
- Serial.begin(115200);
- setupWifi();
- client.setServer(mqtt_server, mqtt_port);
- client.setCallback(callback);
-
- // 初始化蓝牙
- BLEDevice::init("ESP32_Gateway");
- BLEScan* pBLEScan = BLEDevice::getScan();
- pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
- pBLEScan->setInterval(2000);
- pBLEScan->start(60, false);
- }
-
- void loop() {
- if (!client.connected())
- {
- reconnect();
- }
- client.loop();
-
- if (myDevice) {
- if (connectToSensor()) {
- Serial.println("Data sent to MQTT broker!");
- } else {
- Serial.println("Failed to connect to sensor!");
- }
- myDevice = nullptr;
- delay(5000); // 等待 5 秒后再次扫描
- BLEDevice::getScan()->start(60, false);
- }
- }
代码说明:
ssid
、password
、mqtt_server
和 mqtt_port
为您的实际网络和 MQTT broker 信息。serviceUUID
和 characteristicUUID
为您要连接的蓝牙设备的服务和特征 UUID。BLEScan
类扫描附近的蓝牙设备,并查找与指定服务 UUID 匹配的设备。BLEClient
类连接到找到的蓝牙设备,并获取指定特征的值。PubSubClient
库将获取的蓝牙传感器数据发布到指定的 MQTT 主题。本文介绍了如何使用 ESP32 构建蓝牙网关,实现蓝牙设备与 Wi-Fi/互联网之间的连接和数据桥接。通过结合 ESP32 强大的硬件功能和灵活的软件库,您可以轻松构建自定义的蓝牙网关解决方案,满足各种物联网应用的需求。
注意:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。