I have some code here on an ESP32 devkit 1 that is sending out some simple data using BLE.
I have been all day trying to get some kind of receiver working in MIT App inventor 2 and had zero luck.
Tried a couple of different methods and libraries on the Arduino, but none seem to work.
BUT... pretty sure the problem is the MIT App inventor end. I have managed to connect using some BLE scanners and seen some kind of data changes (but not what I expect).
I either get nothing, or a whole pile of Android exception errors.
Anyone had any luck with this stuff?
I'll try posting what I have so far when I get a second
Your first order of business is to get this sketch working reliably with a standard app like Light Blue or nrfConnect. When it is solid, then move on to making your custom app.
The trouble I am having is literally every tutorial or example I have found in a days worth of Googling as some permissions issue, no longer supported or simply doesn't work.
//---------------------- BLE server -----------------------
BLEDevice::init("Voltage_tester");
pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID); // Create the BLE Service
pCharacteristic = pService->createCharacteristic( // Create a BLE Characteristic
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // Start advertising
pAdvertising->addServiceUUID(SERVICE_UUID);
BLEDevice::startAdvertising();
And then called on a milli's timer every 500ms:
if (currentMillis - ble_tx_Millis > 500) { // Transmit the BLE value
ble_tx_Millis = currentMillis;
String btdata = (String) ble_voltage; // Convert the BLE data to a string
Serial.println(btdata);
//pCharacteristic->setValue(btdata.c_str()); // Send string
pCharacteristic->setValue((uint8_t*)&ble_voltage, 4); // Send bytes
pCharacteristic->notify();
}
I can see and connect to this in both Light Blue and EFRconnect. BUT... I cannot see any changing data (or in fact, any data).
I also downloaded the MIT app inventor code and the Arduino code from this example.
Neither work. The Arduino code uploads fine, but the APP inventor stuff is broken.
The slider doesn't show on my Samsung S22+ at all (it's just missing for some odd reason), and pressing connect just crashes the app.
Clearly BLE isn't very reliable. Been on this all day.
I will go back to using a HC-06 as I have working code for that.
Therefore.... no real reason to use the crappy ESP32 (I really don't like these things).
I may as well just use a Pro-Mini + HC-06.
Well had to walk away from this. Whether it's my phone or I am just thick )probably the later).... nothing works
I tried the BLE_UART example in the ESP32 examples in the IDE and even that doesn't work.
It's supposed to increment a value that I can read on my phone, but I have 4 different BLE apps and none of them show any data exchange.
The example shows the SERVICE_UUID, CHARACTERISTIC_UUID_RX and CHARACTERISTIC_UUID_RX all as the same address. Is that correct? (or does that even matter?)
I thought they were supposed to be different.
Anyway, far too much time gone on this. Back to square one. Thank God I didn't get the PCB made that I designed around the ESP32. This is why I breadboard these things first.
I don't confirm that. When I use Light Blue, I can connect to the example, subscribe to the Tx characteristic, set the data format as unsigned little endian, and see the values 0-255 being received. When I go to the Rx characteristic, set the data format as UTF-8 string I can read in the serial monitor connected to the esp32 a text string from the phone.
The example shows the SERVICE_UUID, CHARACTERISTIC_UUID_RX and CHARACTERISTIC_UUID_RX all as the same address. Is that correct? (or does that even matter?)
Notice the different lead values 6E400001 6E400002 6E400003
EDIT: If you mean the MAC device address of the ESP32, then yes, it is based on the board, and is common to the service and characteristics.
This is the library example code I ran. My phone is a Samsung Galaxy A53.
/*
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini
Create a BLE server that, once we receive a connection, will send periodic notifications.
The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY"
The design of creating the BLE server is:
1. Create a BLE Server
2. Create a BLE Service
3. Create a BLE Characteristic on the Service
4. Create a BLE Descriptor on the characteristic
5. Start the service.
6. Start advertising.
In this example rxValue is the data received (only accessible inside that function).
And txValue is the data to be sent, in this example just a byte incremented every second.
*/
#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) {
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[i]);
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;
}
}
OK. After A LOT of faffing, I managed to get the Arduino side of things working.
I can now send and receive by BLE to my Bluetooth apps.
But.... the MIT App inventor 2 stuff just fails miserably. I downloaded probably 7 or 8 different examples and they all crash when you try to connect. I allowed location, access to Bluetooth etc.
I just don't think the MIT stuff keeps up with the more modern phones.
So.... Wifi is next then
How the hell do I do that.....