Here I am showcasing BLE Robot Car Controlling With Bluefruit Connect App
This Code Is Modified From BLE Example.
Working
I am controlling a robot car using the Bluefruit connect app. The app sends the Particular commands when I press the control pad on the app. on the receiver side, I have used Realtek Ameba RTL8722DM (AMB21) Board. The RTL8722DM receives the Bluetooth data and processes the received data and sends the Contro signal to the Motor Driver. The motor Driver Controls the Four motors according to the control signals.
Block Diagram and Connection
Hardware and Software Requirements
Hardware
- RTL8722DM Board
- Motors (4 no)
- l298 Motor Driver
- Battery
- wheels( 4 no)
- chassis
- USB cable
- Jumper wires
Software
- Arduino IDE
- Bluefruit Connect App
CODE
#include "BLEDevice.h"
const int ml1 = 3; //Motor Driver IN1
const int ml2 = 4; //Motor Driver IN2
const int mr1 = 5; //Motor Driver IN3
const int mr2 = 6; //Motor Driver IN4
#define UART_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
#define STRING_BUF_SIZE 1000
BLEService UartService(UART_SERVICE_UUID);
BLECharacteristic Rx(CHARACTERISTIC_UUID_RX);
BLECharacteristic Tx(CHARACTERISTIC_UUID_TX);
BLEAdvertData advdata;
BLEAdvertData scndata;
bool notify = false;
void readCB (BLECharacteristic* chr, uint8_t connID) {
printf("Characteristic %s read by connection %d \n", chr->getUUID().str(), connID);
}
void writeCB (BLECharacteristic* chr, uint8_t connID) {
printf("Characteristic %s write by connection %d :\n", chr->getUUID().str(), connID);
uint16_t datalen = chr->getDataLen();
if (datalen > 0) {
if (chr->readData8() == '!') {
uint8_t command[datalen];
chr->getData(command, datalen);
// " B" IS PREFIX TO IDENTIFY THAT RECEIVED DATA IS CONTROL PAD DATA FROM THE BLUEFRUIT APP
if (command[1] == 'B')
{
Serial.print("Command 2:");
Serial.print(command[2]);
Serial.println();
Serial.print("Command 3:");
Serial.print(command[3]);
Serial.println();
Serial.print("Command 4:");
Serial.print(command[4]);
Serial.println();
if((command[4] == 54) && (command[3] == 49)) //FORWARD COMMAND
{
Serial.println("forward");
digitalWrite(ml1, HIGH);
digitalWrite(ml2,LOW);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,LOW);
}
if((command[4] == 55) && (command[3] == 48))//STOP COMMAND
{
Serial.println("STOP");
digitalWrite(ml1, HIGH);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,HIGH);
}
if((command[4] == 53) && (command[3] == 49))//REVERSE COMMAND
{
Serial.println("Reverse");
digitalWrite(ml1, LOW);//REVERS
digitalWrite(ml2,HIGH);
digitalWrite(mr1,LOW);
digitalWrite(mr2,HIGH);
}
if((command[4] == 54) && (command[3] == 48))//STOP COMMAND
{
Serial.println("STOP");
digitalWrite(ml1, HIGH);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,HIGH);
}
if((command[4] == 52) && (command[3] == 49))//LEFT COMMAND
{
Serial.println("LEFT");
digitalWrite(ml1, HIGH);//LEFT
digitalWrite(ml2,LOW);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,HIGH);
}
if((command[4] == 53) && (command[3] == 48))//STOP COMMAND
{
Serial.println("STOP");
digitalWrite(ml1,HIGH);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,HIGH);
}
if((command[4] == 51) && (command[3] == 49))//RIGHT COMMAND
{
Serial.println("RIGHT");
digitalWrite(ml1, HIGH);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,LOW);
}
if((command[4] == 52) && (command[3] == 48))//STOP COMMAND
{
Serial.println("STOP");
digitalWrite(ml1, HIGH);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,HIGH);
}
delay(100);
}
} else {
Serial.print("Received string: ");
Serial.print(chr->readString());
Serial.println();
}
}
void notifCB (BLECharacteristic* chr, uint8_t connID, uint16_t cccd) {
if (cccd & GATT_CLIENT_CHAR_CONFIG_NOTIFY) {
printf("Notifications enabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
notify = true;
} else {
printf("Notifications disabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
notify = false;
}
}
void setup() {
Serial.begin(115200);
pinMode(ml1, OUTPUT);
pinMode(ml2,OUTPUT);
pinMode(mr1, OUTPUT);
pinMode(mr2,OUTPUT);
advdata.addFlags(GAP_ADTYPE_FLAGS_LIMITED | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED);
advdata.addCompleteName("AMEBA_BLE_DEV");
scndata.addCompleteServices(BLEUUID(UART_SERVICE_UUID));
Rx.setWriteProperty(true);
Rx.setWriteCallback(writeCB);
Rx.setBufferLen(STRING_BUF_SIZE);
Tx.setReadProperty(true);
Tx.setReadCallback(readCB);
Tx.setNotifyProperty(true);
Tx.setCCCDCallback(notifCB);
Tx.setBufferLen(STRING_BUF_SIZE);
UartService.addCharacteristic(Rx);
UartService.addCharacteristic(Tx);
BLE.init();
BLE.configAdvert()->setAdvData(advdata);
BLE.configAdvert()->setScanRspData(scndata);
BLE.configServer(1);
BLE.addService(UartService);
BLE.beginPeripheral();
}
void loop() {
if (Serial.available()) {
Tx.writeString(Serial.readString());
if (BLE.connected(0) && notify) {
Tx.notify(0);
}
}
delay(100);
}
Demo Video