2157浏览
查看: 2157|回复: 1

[项目] Arduino Nano ESP32 体感控车

[复制链接]
本帖最后由 云天 于 2023-10-1 13:23 编辑

Arduino Nano ESP32 体感控车图1

Arduino Nano ESP32 体感控车图3

【项目背景】


最近在DF商城发现一款主板Arduino Nano ESP32开发板, Arduino Nano ESP32: 小巧便携,功能强大。集成了Arduino和ESP32的优势,支持WiFi和蓝牙通信,适用于物联网和智能设备开发,Arduino Nano ESP32将Arduino品牌的易用性和支持与ESP32-S3的强大功能
Arduino Nano ESP32 体感控车图4

个人认为设备特点:1.输入标准电压:5~18V DC。2.USB连接:USB-C。3.支持WiFi和低功耗蓝牙
【项目设计】
1.本体感遥控车,通过Arduino Nano ESP32的低功耗蓝牙实现与手机APP进行无线连接通信。
2.Arduino Nano ESP32主控编程使用Arduino IDE。
3.手机APP编写使用WxBit的App Inventor2图形化编程。
4.电机使用“金属齿轮减速电机 减速比30:1”
5.电机驱动模块使用“微型双路1.5A直流电机驱动模块”
6.供电电源使用2个9V无汞电池。
7.电路板使用嘉立创免费打板。
【电路板】
Arduino Nano ESP32 体感控车图11


Arduino Nano ESP32 体感控车图10


Arduino Nano ESP32 体感控车图12

【硬件组装】
1.焊接电机、电源排针,主控排母
Arduino Nano ESP32 体感控车图9

2.焊接电机杜邦线
Arduino Nano ESP32 体感控车图13

3.安装主控、电机驱动模块、电机
Arduino Nano ESP32 体感控车图14

4.安装电源
Arduino Nano ESP32 体感控车图15


【编写手机APP
1.界面设计
Arduino Nano ESP32 体感控车图5

Arduino Nano ESP32 体感控车图2

2.逻辑设计
首先通过蓝牙扫描,找到名称为“UART Service”(Arduino IDE编程设置)的蓝牙地址。找到后修改程序——蓝牙扫描找到设备后对蓝牙地址进行检查,如匹配进行连接。
Arduino Nano ESP32 体感控车图7

发送数据时,填写服务UUID及特征UUID(Arduino IDE编程设置)
Arduino Nano ESP32 体感控车图8

完整程序
Arduino Nano ESP32 体感控车图6


【Arduino Nano ESP32主控编程】
1.测试电机驱动模块,确定电机转动方向
  1. void setup() {
  2.   // initialize digital pin LED_BUILTIN as an output.
  3.   pinMode(LED_BUILTIN, OUTPUT);
  4.   pinMode(D2, OUTPUT);
  5.   pinMode(D3, OUTPUT);
  6.   pinMode(D4, OUTPUT);
  7.   pinMode(D5, OUTPUT);
  8. }
  9. // the loop function runs over and over again forever
  10. void loop() {
  11.   //正转
  12.   digitalWrite(D2,LOW);
  13.   analogWrite(D3, 50);
  14.   digitalWrite(D5,LOW);
  15.   analogWrite(D4, 50);
  16.   delay(5000);
  17.   digitalWrite(D2,LOW);
  18.   analogWrite(D3, 0);
  19.   digitalWrite(D5,LOW);
  20.   analogWrite(D4, 0);
  21.   delay(5000);
  22.   //反转
  23.   digitalWrite(D3,LOW);
  24.   analogWrite(D2, 50);
  25.   digitalWrite(D4,LOW);
  26.   analogWrite(D5, 50);
  27.   delay(5000);
  28.   digitalWrite(D3,LOW);
  29.   analogWrite(D2, 0);
  30.   digitalWrite(D4,LOW);
  31.   analogWrite(D5, 0);
  32.   delay(5000);         
  33. }
复制代码


2.蓝牙连接测试
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5. BLEServer *pServer = NULL;
  6. BLECharacteristic * pTxCharacteristic;
  7. bool deviceConnected = false;
  8. bool oldDeviceConnected = false;
  9. uint8_t txValue = 0;
  10. // See the following for generating UUIDs:
  11. // [url]https://www.uuidgenerator.net/[/url]
  12. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  13. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  14. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  15. class MyServerCallbacks: public BLEServerCallbacks {
  16.     void onConnect(BLEServer* pServer) {
  17.       deviceConnected = true;
  18.     };
  19.     void onDisconnect(BLEServer* pServer) {
  20.       deviceConnected = false;
  21.     }
  22. };
  23. class MyCallbacks: public BLECharacteristicCallbacks {
  24.     void onWrite(BLECharacteristic *pCharacteristic) {
  25.       std::string rxValue = pCharacteristic->getValue();
  26.       if (rxValue.length() > 0) {
  27.         Serial.println("*********");
  28.         Serial.print("Received Value: ");
  29.         for (int i = 0; i < rxValue.length(); i++)
  30.           Serial.print(rxValue[i]);
  31.         Serial.println();
  32.         Serial.println("*********");
  33.       }
  34.     }
  35. };
  36. void setup() {
  37.   Serial.begin(115200);
  38.   // Create the BLE Device
  39.   BLEDevice::init("UART Service");
  40.   // Create the BLE Server
  41.   pServer = BLEDevice::createServer();
  42.   pServer->setCallbacks(new MyServerCallbacks());
  43.   // Create the BLE Service
  44.   BLEService *pService = pServer->createService(SERVICE_UUID);
  45.   // Create a BLE Characteristic
  46.   pTxCharacteristic = pService->createCharacteristic(
  47.                                                                                 CHARACTERISTIC_UUID_TX,
  48.                                                                                 BLECharacteristic::PROPERTY_NOTIFY
  49.                                                                         );
  50.                      
  51.   pTxCharacteristic->addDescriptor(new BLE2902());
  52.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  53.                                                                                          CHARACTERISTIC_UUID_RX,
  54.                                                                                         BLECharacteristic::PROPERTY_WRITE
  55.                                                                                 );
  56.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  57.   // Start the service
  58.   pService->start();
  59.   // Start advertising
  60.   pServer->getAdvertising()->start();
  61.   Serial.println("Waiting a client connection to notify...");
  62. }
  63. void loop() {
  64.     if (deviceConnected) {
  65.         pTxCharacteristic->setValue(&txValue, 1);
  66.         pTxCharacteristic->notify();
  67.         txValue++;
  68.                 delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  69.         }
  70.     // disconnecting
  71.     if (!deviceConnected && oldDeviceConnected) {
  72.         delay(500); // give the bluetooth stack the chance to get things ready
  73.         pServer->startAdvertising(); // restart advertising
  74.         Serial.println("start advertising");
  75.         oldDeviceConnected = deviceConnected;
  76.     }
  77.     // connecting
  78.     if (deviceConnected && !oldDeviceConnected) {
  79.                 // do stuff here on connecting
  80.         oldDeviceConnected = deviceConnected;
  81.     }
  82. }
复制代码

3.蓝牙连接与电机控制结合(完整程序)
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5. BLEServer *pServer = NULL;
  6. BLECharacteristic * pTxCharacteristic;
  7. bool deviceConnected = false;
  8. bool oldDeviceConnected = false;
  9. uint8_t txValue = 0;
  10. uint8_t bs = 0;
  11. // See the following for generating UUIDs:
  12. // [url]https://www.uuidgenerator.net/[/url]
  13. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  14. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  15. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  16. class MyServerCallbacks: public BLEServerCallbacks {
  17.     void onConnect(BLEServer* pServer) {
  18.       deviceConnected = true;
  19.     };
  20.     void onDisconnect(BLEServer* pServer) {
  21.       deviceConnected = false;
  22.     }
  23. };
  24. class MyCallbacks: public BLECharacteristicCallbacks {
  25.     void onWrite(BLECharacteristic *pCharacteristic) {
  26.       std::string rxValue = pCharacteristic->getValue();
  27.       int speed=80;
  28.       if (rxValue.length() > 0) {
  29.         if (rxValue[0]=='G'){
  30.           bs=1;
  31.           GO(speed);
  32.         }
  33.         if (rxValue[0]=='L'){
  34.           bs=1;
  35.           LEFT(speed);
  36.         }
  37.         if (rxValue[0]=='R'){
  38.           bs=1;
  39.           RIGHT(speed);
  40.         }
  41.         if (rxValue[0]=='B'){
  42.           bs=2;
  43.           BACK(speed);
  44.         }
  45.         if (rxValue[0]=='S'){
  46.           if(bs==1){
  47.           STOPG();
  48.           }
  49.           if(bs==2){
  50.           STOPB();
  51.           }
  52.         }
  53.       }
  54.     };
  55.     void GO(int speed){
  56.     //正转
  57.        digitalWrite(D2,LOW);
  58.        analogWrite(D3, speed);
  59.        digitalWrite(D5,LOW);
  60.        analogWrite(D4, speed+50);
  61.      };
  62.      void LEFT(int speed){
  63.     //左转
  64.        digitalWrite(D2,LOW);
  65.        analogWrite(D3, speed);
  66.        digitalWrite(D5,LOW);
  67.        analogWrite(D4, 0);
  68.      };
  69.      void RIGHT(int speed){
  70.     //右转
  71.        digitalWrite(D2,LOW);
  72.        analogWrite(D3, 0);
  73.        digitalWrite(D5,LOW);
  74.        analogWrite(D4, speed);
  75.      };
  76.      void BACK(int speed){
  77.     //反转
  78.        digitalWrite(D3,LOW);
  79.        analogWrite(D2, speed);
  80.        digitalWrite(D4,LOW);
  81.        analogWrite(D5, speed+50);
  82.      };
  83.      void STOPG(){
  84.        //停止前进
  85.        digitalWrite(D2,LOW);
  86.        analogWrite(D3, 0);
  87.        digitalWrite(D5,LOW);
  88.        analogWrite(D4, 0);
  89.      };
  90.      void STOPB(){
  91.        //停止后退
  92.        digitalWrite(D3,LOW);
  93.        analogWrite(D2, 0);
  94.        digitalWrite(D4,LOW);
  95.        analogWrite(D5, 0);
  96.      };
  97. };
  98. void setup() {
  99.   Serial.begin(115200);
  100.   // Create the BLE Device
  101.   BLEDevice::init("UART Service");
  102.   // Create the BLE Server
  103.   pServer = BLEDevice::createServer();
  104.   pServer->setCallbacks(new MyServerCallbacks());
  105.   // Create the BLE Service
  106.   BLEService *pService = pServer->createService(SERVICE_UUID);
  107.   // Create a BLE Characteristic
  108.   pTxCharacteristic = pService->createCharacteristic(
  109.                                                                                 CHARACTERISTIC_UUID_TX,
  110.                                                                                 BLECharacteristic::PROPERTY_NOTIFY
  111.                                                                         );
  112.                      
  113.   pTxCharacteristic->addDescriptor(new BLE2902());
  114.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  115.                                                                                          CHARACTERISTIC_UUID_RX,
  116.                                                                                         BLECharacteristic::PROPERTY_WRITE
  117.                                                                                 );
  118.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  119.   // Start the service
  120.   pService->start();
  121.   // Start advertising
  122.   pServer->getAdvertising()->start();
  123.   Serial.println("Waiting a client connection to notify...");
  124.   pinMode(D2, OUTPUT);
  125.   pinMode(D3, OUTPUT);
  126.   pinMode(D4, OUTPUT);
  127.   pinMode(D5, OUTPUT);
  128. }
  129. void loop() {
  130.     if (deviceConnected) {
  131.         pTxCharacteristic->setValue(&txValue, 1);
  132.         pTxCharacteristic->notify();
  133.         txValue++;
  134.                 delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  135.         }
  136.     // disconnecting
  137.     if (!deviceConnected && oldDeviceConnected) {
  138.         delay(500); // give the bluetooth stack the chance to get things ready
  139.         pServer->startAdvertising(); // restart advertising
  140.         Serial.println("start advertising");
  141.         oldDeviceConnected = deviceConnected;
  142.     }
  143.     // connecting
  144.     if (deviceConnected && !oldDeviceConnected) {
  145.                 // do stuff here on connecting
  146.         oldDeviceConnected = deviceConnected;
  147.     }
  148. }
复制代码
【演示视频】



easy猿  初级技师 来自手机

发表于 2023-12-7 11:23:55

老师能分享下app不,现在wxbit进不去啊
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail