BLE ESP32. Bluetooth. Send. Receive. Arduino IDE. Multitouch

19.- Through a Slider the app sends data to ESP32. ESP32 sends two random numbers from time to time to the app. Notify.

p110i_esp32_ble_notifica_Desliza.aia (197.6 KB)

- This is a similar example to the previous one, but @Xolo has improved it with the help of ChatGPT.

  • Tested with Android 9 and Android 13.
  • MIT Companion and Installed.
  • ESP-WROOM-32, ESP32-WROOM-32 and ESP32-WROOM-32D
  • BLE Extension Version: 20230728

  • By moving the Slider in the App, ESP32 receives the number and displays it on the Serial Monitor.

  • ESP32 creates two random numbers at random times, Notifies (sends) them to the App.

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

String valor;
long previousMillis_BLE1 = 0;
int interval_BLE1 = 1000;

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

#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) {
        valor = "";
        for (int i = 0; i < value.length(); i++) {
          valor += value[i];
        }

        Serial.println("*********");
        Serial.print("Received value: ");
        Serial.println(valor);
      }
    }
};

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
        Serial.println("Client Connected");
    }

    void onDisconnect(BLEServer* pServer) {
        Serial.println("Client Disconnected");
        pServer->startAdvertising();  // restart advertising
    }
};

void setup() {
  Serial.begin(115200);

  // Initialize the BLE device
  BLEDevice::init("MyESP32_BLE1");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis_BLE1 > interval_BLE1) {
    float TemperatureBLE1 = random(10,60000)/1000.0; // simulate temperature reading with 3 decimals
    float HumidityBLE1 = random(5,99000)/1000.0;     // simulate humidity reading with 3 decimals

    String temperatureBLE1 = String(TemperatureBLE1,3);
    String humidityBLE1 = String(HumidityBLE1,3);
    String tem_hum_BLE1 = temperatureBLE1 + "," + humidityBLE1;

    pCharacteristic->setValue(tem_hum_BLE1.c_str()); // Notify connected client
    pCharacteristic->notify();

    previousMillis_BLE1 = currentMillis;
    interval_BLE1 = random(500,1000);
  }
}
1 Like

Hi, I am a newbie to build the app with MIT APP Inventor. I currently doing my project with the actuator and relay board. Below is my coding using the Arduino IDE platform. But now I am stuck on the build the block at MIT. Can any expert help me look block that I build really can work or not with coding?

sketch_nov10b.ino (1.5 KB)

Look here for an explanation how to connect to a BLE device: The Internet of Things: Data Acquisition and Analysis
It is written for a micro:bit, but it should work similarly for your Arduino.

For anyone using these .aia apps as a starting point, the BluetoothLE extension should be updated and then if you are using Android 12 or above, delete the LocationSensor component if present and enable the NoLocationNeeded option in the BluetoothLE properties.

image

1 Like