数码之家

 找回密码
 立即注册
搜索
查看: 559|回复: 18

[Arduino] 旧手机除了换盆,还能点灯

[复制链接]
发表于 2024-2-28 23:55:13 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 lyy-cy 于 2024-2-28 23:59 编辑



相信大家手里都有几个旧手机,换脸盆不甘心
其实要是能连接上ESP32,就可以当个屏幕来用
下面的方案,供大家参考


封面4.png






点击下面可以看视频,不会发视频,有知道的老铁能分享下么


【手搓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/

国外的网站,有点慢



  1. /*
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/esp32-web-bluetooth/
  4.   
  5.   Permission is hereby granted, free of charge, to any person obtaining a copy
  6.   of this software and associated documentation files.
  7.   
  8.   The above copyright notice and this permission notice shall be included in all
  9.   copies or substantial portions of the Software.
  10. */

  11. #include <BLEDevice.h>
  12. #include <BLEServer.h>
  13. #include <BLEUtils.h>
  14. #include <BLE2902.h>

  15. BLEServer* pServer = NULL;
  16. BLECharacteristic* pSensorCharacteristic = NULL;
  17. BLECharacteristic* pLedCharacteristic = NULL;
  18. bool deviceConnected = false;
  19. bool oldDeviceConnected = false;
  20. uint32_t value = 0;

  21. const int ledPin = 2; // Use the appropriate GPIO pin for your setup

  22. // See the following for generating UUIDs:
  23. // https://www.uuidgenerator.net/

  24. #define SERVICE_UUID        "19b10000-e8f2-537e-4f6c-d104768a1214"
  25. #define SENSOR_CHARACTERISTIC_UUID "19b10001-e8f2-537e-4f6c-d104768a1214"
  26. #define LED_CHARACTERISTIC_UUID "19b10002-e8f2-537e-4f6c-d104768a1214"

  27. class MyServerCallbacks: public BLEServerCallbacks {
  28.     void onConnect(BLEServer* pServer) {
  29.       deviceConnected = true;
  30.     };

  31.     void onDisconnect(BLEServer* pServer) {
  32.       deviceConnected = false;
  33.     }
  34. };

  35. class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
  36.     void onWrite(BLECharacteristic* pLedCharacteristic) {
  37.         std::string value = pLedCharacteristic->getValue();
  38.         if (value.length() > 0) {
  39.             Serial.print("Characteristic event, written: ");
  40.             Serial.println(static_cast<int>(value[0])); // Print the integer value

  41.             int receivedValue = static_cast<int>(value[0]);
  42.             if (receivedValue == 1) {
  43.                 digitalWrite(ledPin, HIGH);
  44.             } else {
  45.                 digitalWrite(ledPin, LOW);
  46.             }
  47.         }
  48.     }
  49. };

  50. void setup() {
  51.   Serial.begin(115200);
  52.   pinMode(ledPin, OUTPUT);

  53.   // Create the BLE Device
  54.   BLEDevice::init("ESP32");

  55.   // Create the BLE Server
  56.   pServer = BLEDevice::createServer();
  57.   pServer->setCallbacks(new MyServerCallbacks());

  58.   // Create the BLE Service
  59.   BLEService *pService = pServer->createService(SERVICE_UUID);

  60.   // Create a BLE Characteristic
  61.   pSensorCharacteristic = pService->createCharacteristic(
  62.                       SENSOR_CHARACTERISTIC_UUID,
  63.                       BLECharacteristic::PROPERTY_READ   |
  64.                       BLECharacteristic::PROPERTY_WRITE  |
  65.                       BLECharacteristic::PROPERTY_NOTIFY |
  66.                       BLECharacteristic::PROPERTY_INDICATE
  67.                     );

  68.   // Create the ON button Characteristic
  69.   pLedCharacteristic = pService->createCharacteristic(
  70.                       LED_CHARACTERISTIC_UUID,
  71.                       BLECharacteristic::PROPERTY_WRITE
  72.                     );

  73.   // Register the callback for the ON button characteristic
  74.   pLedCharacteristic->setCallbacks(new MyCharacteristicCallbacks());

  75.   // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  76.   // Create a BLE Descriptor
  77.   pSensorCharacteristic->addDescriptor(new BLE2902());
  78.   pLedCharacteristic->addDescriptor(new BLE2902());

  79.   // Start the service
  80.   pService->start();

  81.   // Start advertising
  82.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  83.   pAdvertising->addServiceUUID(SERVICE_UUID);
  84.   pAdvertising->setScanResponse(false);
  85.   pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
  86.   BLEDevice::startAdvertising();
  87.   Serial.println("Waiting a client connection to notify...");
  88. }

  89. void loop() {
  90.     // notify changed value
  91.     if (deviceConnected) {
  92.         pSensorCharacteristic->setValue(String(value).c_str());
  93.         pSensorCharacteristic->notify();
  94.         value++;
  95.         Serial.print("New value notified: ");
  96.         Serial.println(value);
  97.         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
  98.     }
  99.     // disconnecting
  100.     if (!deviceConnected && oldDeviceConnected) {
  101.         Serial.println("Device disconnected.");
  102.         delay(500); // give the bluetooth stack the chance to get things ready
  103.         pServer->startAdvertising(); // restart advertising
  104.         Serial.println("Start advertising");
  105.         oldDeviceConnected = deviceConnected;
  106.     }
  107.     // connecting
  108.     if (deviceConnected && !oldDeviceConnected) {
  109.         // do stuff here on connecting
  110.         oldDeviceConnected = deviceConnected;
  111.         Serial.println("Device Connected");
  112.     }
  113. }
复制代码



打赏

参与人数 1家元 +30 收起 理由
200birds + 30

查看全部打赏

发表于 2024-2-29 00:41:49 | 显示全部楼层
没意思,你要控制自己的家电,你觉得是用你常用的手机好还是另外拿一台淘汰机来搞合适?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-29 00:56:06 | 显示全部楼层
本帖最后由 lyy-cy 于 2024-2-29 01:01 编辑
200birds 发表于 2024-2-29 00:41
没意思,你要控制自己的家电,你觉得是用你常用的手机好还是另外拿一台淘汰机来搞合适? ...

写代码的时候需要一台手机作为蓝牙的接收端,来连接flutter,这个不能用模拟器.如果用主力手机,来个电话或微信,会打断写代码,等会还要重新连接(比较耗时),而且调试过程可能会把机器搞死机.

等APP完成了,你觉得我不会安装自己写的APP么?

况且我还可以把手机修改外观后,固定到墙上,当个控制中心,这样大家都能用,这不就是智能家居的中控屏么?
回复 支持 反对

使用道具 举报

发表于 2024-2-29 09:59:23 来自手机浏览器 | 显示全部楼层
太高深玩不了这个。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-29 10:44:31 | 显示全部楼层
壹筒江湖 发表于 2024-2-29 09:59
太高深玩不了这个。

难度两个51.业余一个月时间.
回复 支持 反对

使用道具 举报

发表于 2024-2-29 11:04:33 | 显示全部楼层
旧还有很多可玩之处,


如 安装 termux,像一台 linux 电脑,可安装多种开发编程语言,做小型的开发没有问题。像python、node.js、c/c++、php等,而数据库有MariaDB(MySQL的分支)或使用sqlite3。添加SSH后,使用电脑编写,很方便。
————限制是linux界面的开发(但可以使用web做界面)、不适合大型的开发,要求android版本较高。




qpython,也是开源的,也可以自己编译、在编译打包时加入预装的python代码,编写python代码也是不错选择。纯python的库,可以直接使用pip安装,缺点是限制是要求编译的库,安装失败。要求android版本比较低。
回复 支持 1 反对 0

使用道具 举报

发表于 2024-2-29 11:28:51 | 显示全部楼层
太高深玩不了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-29 11:34:19 | 显示全部楼层
devcang 发表于 2024-2-29 11:04
旧还有很多可玩之处,

我以前没有搞懂APP的时候   就是用手机上的Python编译器   来控制MQTT的
回复 支持 反对

使用道具 举报

发表于 2024-2-29 14:13:16 | 显示全部楼层
高手,厉害了,
回复 支持 反对

使用道具 举报

发表于 2024-2-29 21:52:38 | 显示全部楼层
lyy-cy 发表于 2024-2-29 11:34
我以前没有搞懂APP的时候   就是用手机上的Python编译器   来控制MQTT的

嫌app麻烦,,,,,要是自己用,使用termux比较方便,支持比较广泛,MQTT太小意思了,,,,,有ssh,在电脑上使用winscp+putty,很方便,传文件也快,上班不背电脑、用手机用作“U盘”搬文件、资料了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-1 09:16:39 | 显示全部楼层
本帖最后由 lyy-cy 于 2024-3-1 09:19 编辑
devcang 发表于 2024-2-29 21:52
嫌app麻烦,,,,,要是自己用,使用termux比较方便,支持比较广泛,MQTT太小意思了,,,,,有ssh,在 ...

termux好像很有意思,能跑HA么? 我看网上有人说bug多.


  1. termux、aidlux直接安装:
  2. 推荐度:中
  3. 投入:有闲置的情况下0
  4. 难度:一般
  5. 通用性:高
  6. 优点:大部分人都有闲置安卓,设备容易获取,技术上要求也相对不高。
  7. 缺点:可能存在莫名其妙的bug,例如本人遇到的:dhcp一直报错,HomeKit无法正常连接等。同时更新起来也非常麻烦,随着ha更新,需要同时更新相应环境。
  8. 使用termux或者aidlux,在其提供的环境下安装Home Assistant Core。我最早尝试的方法,后面发现bug较多而舍弃。有条件可以自行尝试,现在安装好像简单得多。对手机没有特殊需求,安卓手机即可。
复制代码


回复 支持 反对

使用道具 举报

发表于 2024-3-1 17:53:10 来自手机浏览器 | 显示全部楼层
devcang 发表于 2024-2-29 21:52
嫌app麻烦,,,,,要是自己用,使用termux比较方便,支持比较广泛,MQTT太小意思了,,,,,有ssh,在 ...

高手         

回复 支持 反对

使用道具 举报

发表于 2024-3-2 00:04:47 来自手机浏览器 | 显示全部楼层
我想问问,这个无线模块能不能一直访问互联网进行POST操作。我想搞个模块然后一直访问互联网挂网页文字游戏。
回复 支持 反对

使用道具 举报

发表于 2024-3-2 09:37:01 来自手机浏览器 | 显示全部楼层
这个厉害了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-2 19:22:42 | 显示全部楼层
xiaohui888 发表于 2024-3-2 00:04
我想问问,这个无线模块能不能一直访问互联网进行POST操作。我想搞个模块然后一直访问互联网挂网页文字游戏 ...

可以,HTTP有库.arduino和micropython都可以,业务逻辑不复杂推荐micropython,代码简单.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-2 19:27:47 | 显示全部楼层

蓝牙和APP(告别各种助手和控制台 命令行)一直以来都是我想要的翻过的两座大山,
以前pyQT和51822都半途而废了,没有坚持下来.
arduino(ESP32)和flutter相对简单点.推荐从这里入手.
回复 支持 反对

使用道具 举报

发表于 2024-3-15 08:52:10 | 显示全部楼层
高手,厉害了
回复 支持 反对

使用道具 举报

发表于 2024-3-18 20:13:06 来自手机浏览器 | 显示全部楼层
旧手机的屏幕能不能利用起来?这个屏幕可是高级多了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-5-3 03:52 , Processed in 0.156000 second(s), 13 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表