Arduino Nano ESP32 体感控车
本帖最后由 云天 于 2023-10-1 13:23 编辑【项目背景】
https://ws.dfrobot.com.cn/Fp0WhSj8KM-rGdGzdbCvzjHpcgis
最近在DF商城发现一款主板Arduino Nano ESP32开发板, Arduino Nano ESP32: 小巧便携,功能强大。集成了Arduino和ESP32的优势,支持WiFi和蓝牙通信,适用于物联网和智能设备开发,Arduino Nano ESP32将Arduino品牌的易用性和支持与ESP32-S3的强大功能。
个人认为设备特点: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.电路板使用嘉立创免费打板。
【电路板】
【硬件组装】
1.焊接电机、电源排针,主控排母
2.焊接电机杜邦线
3.安装主控、电机驱动模块、电机
4.安装电源
【编写手机APP】
1.界面设计
2.逻辑设计
首先通过蓝牙扫描,找到名称为“UART Service”(Arduino IDE编程设置)的蓝牙地址。找到后修改程序——蓝牙扫描找到设备后对蓝牙地址进行检查,如匹配进行连接。
发送数据时,填写服务UUID及特征UUID(Arduino IDE编程设置)
完整程序
【Arduino Nano ESP32主控编程】
1.测试电机驱动模块,确定电机转动方向
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
//正转
digitalWrite(D2,LOW);
analogWrite(D3, 50);
digitalWrite(D5,LOW);
analogWrite(D4, 50);
delay(5000);
digitalWrite(D2,LOW);
analogWrite(D3, 0);
digitalWrite(D5,LOW);
analogWrite(D4, 0);
delay(5000);
//反转
digitalWrite(D3,LOW);
analogWrite(D2, 50);
digitalWrite(D4,LOW);
analogWrite(D5, 50);
delay(5000);
digitalWrite(D3,LOW);
analogWrite(D2, 0);
digitalWrite(D4,LOW);
analogWrite(D5, 0);
delay(5000);
}
2.蓝牙连接测试
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++)
Serial.print(rxValue);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("UART Service");
// 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
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
if (deviceConnected) {
pTxCharacteristic->setValue(&txValue, 1);
pTxCharacteristic->notify();
txValue++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
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;
}
}
3.蓝牙连接与电机控制结合(完整程序)
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;
uint8_t bs = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
int speed=80;
if (rxValue.length() > 0) {
if (rxValue=='G'){
bs=1;
GO(speed);
}
if (rxValue=='L'){
bs=1;
LEFT(speed);
}
if (rxValue=='R'){
bs=1;
RIGHT(speed);
}
if (rxValue=='B'){
bs=2;
BACK(speed);
}
if (rxValue=='S'){
if(bs==1){
STOPG();
}
if(bs==2){
STOPB();
}
}
}
};
void GO(int speed){
//正转
digitalWrite(D2,LOW);
analogWrite(D3, speed);
digitalWrite(D5,LOW);
analogWrite(D4, speed+50);
};
void LEFT(int speed){
//左转
digitalWrite(D2,LOW);
analogWrite(D3, speed);
digitalWrite(D5,LOW);
analogWrite(D4, 0);
};
void RIGHT(int speed){
//右转
digitalWrite(D2,LOW);
analogWrite(D3, 0);
digitalWrite(D5,LOW);
analogWrite(D4, speed);
};
void BACK(int speed){
//反转
digitalWrite(D3,LOW);
analogWrite(D2, speed);
digitalWrite(D4,LOW);
analogWrite(D5, speed+50);
};
void STOPG(){
//停止前进
digitalWrite(D2,LOW);
analogWrite(D3, 0);
digitalWrite(D5,LOW);
analogWrite(D4, 0);
};
void STOPB(){
//停止后退
digitalWrite(D3,LOW);
analogWrite(D2, 0);
digitalWrite(D4,LOW);
analogWrite(D5, 0);
};
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("UART Service");
// 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
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
}
void loop() {
if (deviceConnected) {
pTxCharacteristic->setValue(&txValue, 1);
pTxCharacteristic->notify();
txValue++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
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;
}
}
【演示视频】
https://www.bilibili.com/video/BV1mz4y1G7h2/?share_source=copy_web&vd_source=98855d5b99ff76982639c5ca6ff6f528
老师能分享下app不,现在wxbit进不去啊
页:
[1]