Good day gentle people. I seek Your collective wisdom.
For three days i have been trying to include ble into my app. I tried all the example apps, tinkered alot with my own but neither reveals esp32 ble. However it is discoverable under phone’s bluetooth settings. Also nRF Toolbox connects to esp32 ble uart and sends commands like a charm.
Any advices or working examples would be much appreciated.
ABG
December 30, 2019, 3:52pm
2
Did you load the BLE Extension?
For BLE samples, see the BLE sections of FAQ
https://sites.google.com/view/ai2-faq-view/home
I did use the extention, but there is probably something with notification and or advertising. Will take a closer look at provided link. If no luck, will upload my logs. Tanks
I think i’ve finally got the clue. Before starting any app with ble extension location services must be turned on the phone in order to get it to see any Bluetooth devices. Now I can start working from here. Thanks for the links.
Ok, now i have a new and very strange issue. I chose BLE write example for ESP32. Now my app sees BLE device and manages to send string (“arm”) to it which can be seen on Serial.print via serial monitor.
However it seems that ESP32 recieves text but does nothing more.
When i send the same command as text via nRF Connect i get the same on Serial.print but ESP32 executes as written in the code
What is it in value marked in red before “arm” ?
p
Ok, it is ascII for “arm”, but that does not help
ABG
January 3, 2020, 7:10pm
7
Posting your code and blocks would be helpful.
Here’s a blind guess - you need to send a \n (line feed)?
sure, was not allowed to attach more than 1 pic
serial monitor.
18:54:11.221 “arm” -sent through app - no consecquences
18:54:44.628 “arm” - sent through nRF Connect - lights blinking & everything as suspected.
arduino code (ESP32 BLE Arduino - BLE write exaple ,only with some "If"s added ):
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <Servo.h>
Servo motor1, motor2;
int pos = 1500;
int f = 1590;
int b = 1350;
int maxf = 2000;
int minf = 1550;
int maxb = 1000;
int minb = 1450;
int steps = 45;
int p;
#define led 2
#define ledG 13
#define ledR 12
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.print("fwd:");
Serial.println(f);
Serial.print("bck:");
Serial.println(b);
if (value=="arm") { //arm
motor1.writeMicroseconds(pos);
motor2.writeMicroseconds(pos);
for (p = p; p <= 5; p++) {
digitalWrite(ledG, 1);
digitalWrite(ledR, 1);
delay(100);
digitalWrite(ledG, 0);
digitalWrite(ledR, 0);
delay(500);
}
}
if (value=="5") { //stop
motor1.writeMicroseconds(pos);
motor2.writeMicroseconds(pos);
digitalWrite(led, 0);
digitalWrite(ledG, 0);
digitalWrite(ledR, 0);
}
if (value=="2") { //forward
motor1.writeMicroseconds(f);
motor2.writeMicroseconds(f);
digitalWrite(led, 1);
digitalWrite(ledG, 1);
digitalWrite(ledR, 1);
}
/*
if (rxValue=="8") { //BACK
motor1.writeMicroseconds(1300);
motor2.writeMicroseconds(1300);
delay(50);
motor1.writeMicroseconds(pos);
motor2.writeMicroseconds(pos);
delay(100);
motor1.writeMicroseconds(b);
motor2.writeMicroseconds(b);
digitalWrite(ledR, 1);
}
if (rxValue=="4") { //LEFT
motor2.writeMicroseconds(1300);
delay(50);
motor2.writeMicroseconds(pos);
delay(100);
motor1.writeMicroseconds(f);
motor2.writeMicroseconds(b);
}
if (rxValue=="1") { //left_fwd
motor2.writeMicroseconds(1300);
delay(50);
motor2.writeMicroseconds(pos);
delay(100);
if (f > 1850) {
f = 1850;
}
motor1.writeMicroseconds(f+50);
motor2.writeMicroseconds(f-50);
}
if (rxValue=="7") { //left_bck
motor2.writeMicroseconds(1300);
delay(50);
motor2.writeMicroseconds(pos);
delay(100);
if (b < 1150) {
b = 1150;
}
motor1.writeMicroseconds(b+50);
motor2.writeMicroseconds(b-90);
}
if (rxValue=="6") { //right
motor1.writeMicroseconds(1300);
delay(50);
motor1.writeMicroseconds(pos);
delay(100);
motor1.writeMicroseconds(b);
motor2.writeMicroseconds(f);
}
if (rxValue=="3") { //right_fwd
motor2.writeMicroseconds(1300);
delay(50);
motor2.writeMicroseconds(pos);
delay(100);
if (f > 1850) {
f = 1850;
}
motor1.writeMicroseconds(f-50);
motor2.writeMicroseconds(f+50);
}
if (rxValue=="9") { //right_bck
motor2.writeMicroseconds(1300);
delay(50);
motor2.writeMicroseconds(pos);
delay(100);
if (b < 1150) {
b = 1150;
}
motor1.writeMicroseconds(b-90);
motor2.writeMicroseconds(b+50);
}
if (rxValue=="a") { //add power
f = f + steps; //buvo 74
if (f > maxf) {
f = maxf;
}
b = b - steps; // buvo 60
if (b < maxb) {
b = maxb;
}
}
if (rxValue=="d") { //drop power
f = f - steps;
if (f < minf) {
f = minf;
}
b = b + steps;
if (b > minb) {
b = minb;
}
}
*/
}
}
};
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledR,OUTPUT);
motor1.attach(32);
motor2.attach(33);
Serial.println("1- Download and install an BLE scanner app in your phone");
Serial.println("2- Scan for BLE devices in the app");
Serial.println("3- Connect to MyESP32");
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
Serial.println("5- See the magic =)");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
delay(2000);
}
Would you mind posting the solution here so that others might learn from it in the future?
Will do. Had to change the arduino code. Will post changes later
So instead of:
if (value==“arm”) {
changed to:
if (value.find(“arm”) != -1) {