|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lyy-cy 于 2024-2-28 23:59 编辑
相信大家手里都有几个旧手机,换脸盆不甘心
其实要是能连接上ESP32,就可以当个屏幕来用
下面的方案,供大家参考
点击下面可以看视频,不会发视频,有知道的老铁能分享下么
【手搓APP ,ESP32 用MQTT 和 BLE 通过Flutter 点灯】
[url=https://www.bilibili.com/video/B ... 8e8f9b1011a86083422]B站视频[/url]
参考这个网
https://randomnerdtutorials.com/esp32-web-bluetooth/
刷入下面的代码,然后用电脑访问这个网址,就能控制,电脑要有蓝牙,最好是笔记本
https://ruisantosdotme.github.io/esp32-web-ble/
国外的网站,有点慢
- /*
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp32-web-bluetooth/
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files.
-
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- */
- #include <BLEDevice.h>
- #include <BLEServer.h>
- #include <BLEUtils.h>
- #include <BLE2902.h>
- BLEServer* pServer = NULL;
- BLECharacteristic* pSensorCharacteristic = NULL;
- BLECharacteristic* pLedCharacteristic = NULL;
- bool deviceConnected = false;
- bool oldDeviceConnected = false;
- uint32_t value = 0;
- const int ledPin = 2; // Use the appropriate GPIO pin for your setup
- // See the following for generating UUIDs:
- // https://www.uuidgenerator.net/
- #define SERVICE_UUID "19b10000-e8f2-537e-4f6c-d104768a1214"
- #define SENSOR_CHARACTERISTIC_UUID "19b10001-e8f2-537e-4f6c-d104768a1214"
- #define LED_CHARACTERISTIC_UUID "19b10002-e8f2-537e-4f6c-d104768a1214"
- class MyServerCallbacks: public BLEServerCallbacks {
- void onConnect(BLEServer* pServer) {
- deviceConnected = true;
- };
- void onDisconnect(BLEServer* pServer) {
- deviceConnected = false;
- }
- };
- class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
- void onWrite(BLECharacteristic* pLedCharacteristic) {
- std::string value = pLedCharacteristic->getValue();
- if (value.length() > 0) {
- Serial.print("Characteristic event, written: ");
- Serial.println(static_cast<int>(value[0])); // Print the integer value
- int receivedValue = static_cast<int>(value[0]);
- if (receivedValue == 1) {
- digitalWrite(ledPin, HIGH);
- } else {
- digitalWrite(ledPin, LOW);
- }
- }
- }
- };
- void setup() {
- Serial.begin(115200);
- pinMode(ledPin, OUTPUT);
- // Create the BLE Device
- BLEDevice::init("ESP32");
- // Create the BLE Server
- pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- // Create the BLE Service
- BLEService *pService = pServer->createService(SERVICE_UUID);
- // Create a BLE Characteristic
- pSensorCharacteristic = pService->createCharacteristic(
- SENSOR_CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE |
- BLECharacteristic::PROPERTY_NOTIFY |
- BLECharacteristic::PROPERTY_INDICATE
- );
- // Create the ON button Characteristic
- pLedCharacteristic = pService->createCharacteristic(
- LED_CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_WRITE
- );
- // Register the callback for the ON button characteristic
- pLedCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
- // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
- // Create a BLE Descriptor
- pSensorCharacteristic->addDescriptor(new BLE2902());
- pLedCharacteristic->addDescriptor(new BLE2902());
- // Start the service
- pService->start();
- // Start advertising
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
- pAdvertising->addServiceUUID(SERVICE_UUID);
- pAdvertising->setScanResponse(false);
- pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
- BLEDevice::startAdvertising();
- Serial.println("Waiting a client connection to notify...");
- }
- void loop() {
- // notify changed value
- if (deviceConnected) {
- pSensorCharacteristic->setValue(String(value).c_str());
- pSensorCharacteristic->notify();
- value++;
- Serial.print("New value notified: ");
- Serial.println(value);
- delay(3000); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
- }
- // disconnecting
- if (!deviceConnected && oldDeviceConnected) {
- Serial.println("Device disconnected.");
- delay(500); // give the bluetooth stack the chance to get things ready
- pServer->startAdvertising(); // restart advertising
- Serial.println("Start advertising");
- oldDeviceConnected = deviceConnected;
- }
- // connecting
- if (deviceConnected && !oldDeviceConnected) {
- // do stuff here on connecting
- oldDeviceConnected = deviceConnected;
- Serial.println("Device Connected");
- }
- }
复制代码
|
打赏
-
查看全部打赏
|