Juan,
This is great work! I am looking for a way to modify this so that instead of printing the result to the serial monitor it prints it to a 16x2 I2C LCD screen. I am able to get this working in part by adding lcd.print(valor); to the main loop, but of course then it just loops that text on the screen. What I would like to be able to do is to send a bit of text, have it stay on the screen until another string of text is sent. Could you point me in the right direction?
Sorry for the bother…
Is this a simple example for receive a command from Esp32 (or Arduino)?
I tried one.
I mean: We can turn on/off a led on Esp32 (Arduino) from cell phone.
But I am not able to turn on/off a “led” on cell phone from Esp32 (Arduino)…
I tried… But it works only if you repress on/off from cell phone…
So I am trying to make a simple app with a simple “ON” or “OFF” and in Esp32 (Arduino) building simple button… (simply writing on terminal)…
How do it???
“When BluetoothLE1.StringsReceived” doesn’t work…
/* 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 updated by chegewara
Create a BLE server that, once we receive a connection, will send periodic notifications.
A connect hander associated with the server starts a background task that performs notification
every couple of seconds. */
// Modificado por Juan A. Villalpando
// http://kio4.com/arduino/160i_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define pin12 12 // pin12 PushButton
#define pin2 2 // pin2 LED2
int valor12; // Status PushButton
String output ="LOW"; // Output to App. Notify.
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
pinMode(pin12, INPUT); // pin12 PushButton
pinMode(pin2, OUTPUT); // pin2 LED2
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("MyESP32");
// 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
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// notify changed value
if (deviceConnected) {
pCharacteristic->setValue(output.c_str()); // Output
pCharacteristic->notify();
valor12 = digitalRead(pin12); // Read pin12 --> valor12. ( 0 or 1)
if (valor12 == HIGH) {
digitalWrite(pin2, HIGH); // if valor12 is HIGH, set pin2 HIGH
Serial.println("Pulsado");
output ="HIGH"; // Notify HIGH
}
if (valor12 == LOW) {
digitalWrite(pin2, LOW); // If valor12 is LOW, set pin2 LOW
Serial.println("No Pulsado");
output ="LOW"; // Notify LOW
}
delay(100); // 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;
}
}
@Juan_Antonio, I’m getting started with esp32 BLE connection and MIT app inventor. I don’t know what is happening, but I got this error when I try to use any off the apps. Can someone help me?
Note: I’m just copying and pasting both code and app
Important: each mobile device must have a CHARACTERISTIC_UUID. If you have multiple mobiles, set a CHARACTERISTIC_UUID for each one. You can generate UUID: https://www.uuidgenerator.net/
// Modified Juan A. Villalpando.
// http://kio4.com/arduino/160_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// Each client, mobile with a CHARACTERISTIC_UUID.
// Generate UUID: https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
//#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define CHARACTERISTIC_UUID "d6f841fa-a37d-11ea-bb37-0242ac130002"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
int value = 0;
String valor = "";
// These functions will detect when the device is connected or disconnected.
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MyESP32");
// Create BLE Server.
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service with properties READ, WRITE, NOTIFY, INDICATE
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// BLE descriptor.
pCharacteristic->addDescriptor(new BLE2902());
// Start BLE service.
pService->start();
// Start BLE advertising.
pServer->getAdvertising()->start();
}
void loop() {
if (deviceConnected) {
Serial.printf("*** NOTIFY: %d ***\n", value);
valor = (String) value;
pCharacteristic->setValue(valor.c_str()); // Set value.
pCharacteristic->notify(); // Notify value.
//pCharacteristic->indicate();
value++;
delay(800);
}
}
See below, June 1st, Notifications. You can use “Register for Strings” to receive data when it is available (every time it is available), rather than having to request it.
Thanks for this detailed tutorial. I am using the 6.- ESP32 sends automatically (Notify) a random number to App.. But my use requires me to get multiple analog readings and display them in different labels. Could you guide me on how it can be done? I am attaching the desired app screenshot.
int aleatorio_1 = random(1,100); // Crea el numero random.
int aleatorio_2 = random(100,200); // Crea el numero random.
int aleatorio_3 = random(200,300); // Crea el numero random.
String alea = (String) aleatorio_1 + "," + (String) aleatorio_2 + "," + (String) aleatorio_3; // Lo convierte en String.
pCharacteristic->setValue(alea.c_str()); // Pone el numero random
pCharacteristic->notify();
Heey i followed the guide to send and receive data but i get this error everytime i run companion.
Error from Companion: java.lang.NoClassDefFoundError: edu.mit.appinventor.ble.BluetoothLEint$1
Then i get this when i press enable BT
invoke: no method named `EnableBluetooth' in class java.lang.Boolean
and goes like that .
Seems its the extensions causing it , i know for sure bluetoothLE is causing me issues cuz i can run the app without it ,but i need the extensions to read and send sensor data .
any ideas what this could be ?