My app (Open IRIS) :: ESP32 -BLE extension / slider to modify my servo angle ? how?

Hi can you help me to finish my app.
I'm a beginner but I managed to connect to my esp32. I would like to change my angle with a slider :
But I can't do it.

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <ESP32Servo.h>
#include <Adafruit_NeoPixel.h>

// UUIDs BLE
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID_OUVRIR "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define CHARACTERISTIC_UUID_FERMER "beb5483e-36e1-4688-b7f5-ea07361b26a9"
#define CHARACTERISTIC_UUID_ANGLE_MAX "beb5483e-36e1-4688-b7f5-ea07361b26ae"  // Nouvelle caractéristique pour l'angle maximum
#define CHARACTERISTIC_UUID_TEMPS "beb5483e-36e1-4688-b7f5-ea07361b26aa"
#define CHARACTERISTIC_UUID_EFFET "beb5483e-36e1-4688-b7f5-ea07361b26ab"
#define CHARACTERISTIC_UUID_MODE_AUTO "beb5483e-36e1-4688-b7f5-ea07361b26ac"  // Nouveau UUID pour mode auto

// Définition des pins
#define SERVO_PIN 13
#define LED_PIN 12
#define NUM_LEDS 8

// Variables d'état
bool irisOuverte = false;
unsigned long tempsOuverture = 2000;
unsigned long tempsFermeture = 2000;
String effetLED = "degrade";

// Mode automatique
bool modeAuto = false;
unsigned long lastChangeTime = 0;
bool isCycleOpen = true;
const unsigned long AUTO_OPEN_TIME = 8000;  // 8s ouvert
const unsigned long AUTO_CLOSE_TIME = 3000; // 3s fermé

// Servo et LEDs
Servo irisServo;
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

// Angle max initialisé à 180
int angleMax = 180;

// Fonction pour gérer la couleur des LEDs
void setStripColor(uint8_t r, uint8_t g, uint8_t b) {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(r, g, b));
  }
  strip.show();
}

// Fonctions d'ouverture/fermeture
void ouvrirIris() {
  Serial.println("Ouverture de l'iris...");
  irisServo.write(angleMax);  // Utilise l'angle maximum défini
  irisOuverte = true;
  setStripColor(0, 255, 0); // Vert
}

void fermerIris() {
  Serial.println("Fermeture de l'iris...");
  irisServo.write(0);  // Angle 0 pour fermer
  irisOuverte = false;
  setStripColor(255, 0, 0); // Rouge
}

// Callback BLE
class MyCallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *pCharacteristic) override {
    String rawValue = pCharacteristic->getValue().c_str();
    String rawUUID = pCharacteristic->getUUID().toString().c_str();

    if (rawUUID == CHARACTERISTIC_UUID_OUVRIR) {
      if (rawValue == "ouvrir") ouvrirIris();
    } else if (rawUUID == CHARACTERISTIC_UUID_FERMER) {
      if (rawValue == "fermer") fermerIris();
    } else if (rawUUID == CHARACTERISTIC_UUID_ANGLE_MAX) {
      angleMax = rawValue.toInt();
      Serial.println("Angle maximum mis à jour : " + String(angleMax));
    } else if (rawUUID == CHARACTERISTIC_UUID_TEMPS) {
      int separatorIndex = rawValue.indexOf('|');
      if (separatorIndex != -1) {
        tempsOuverture = rawValue.substring(0, separatorIndex).toInt();
        tempsFermeture = rawValue.substring(separatorIndex + 1).toInt();
        Serial.println("Temps mis à jour : Ouverture = " + String(tempsOuverture) + " ms, Fermeture = " + String(tempsFermeture) + " ms");
      }
    } else if (rawUUID == CHARACTERISTIC_UUID_EFFET) {
      effetLED = rawValue;
      Serial.println("Effet LED mis à jour : " + effetLED);
    } else if (rawUUID == CHARACTERISTIC_UUID_MODE_AUTO) {
      modeAuto = (rawValue == "on");
      Serial.println("Mode automatique : " + String(modeAuto ? "Activé" : "Désactivé"));
    }
  }
};

void setup() {
  Serial.begin(115200);
  
  // Initialisation du Servo
  irisServo.attach(SERVO_PIN);
  
  // Initialisation LEDs WS2812
  strip.begin();
  strip.show();
  strip.setBrightness(50);

  // Initialisation BLE
  BLEDevice::init("IrisController");
  Serial.println("Adresse MAC ESP32 : " + String(BLEDevice::getAddress().toString().c_str()));

  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Création des caractéristiques BLE
  BLECharacteristic *pCharacteristicOuvrir = pService->createCharacteristic(
    CHARACTERISTIC_UUID_OUVRIR, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicOuvrir->setCallbacks(new MyCallbacks());

  BLECharacteristic *pCharacteristicFermer = pService->createCharacteristic(
    CHARACTERISTIC_UUID_FERMER, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicFermer->setCallbacks(new MyCallbacks());

  BLECharacteristic *pCharacteristicAngleMax = pService->createCharacteristic(
    CHARACTERISTIC_UUID_ANGLE_MAX, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicAngleMax->setCallbacks(new MyCallbacks());

  BLECharacteristic *pCharacteristicTemps = pService->createCharacteristic(
    CHARACTERISTIC_UUID_TEMPS, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicTemps->setCallbacks(new MyCallbacks());

  BLECharacteristic *pCharacteristicEffet = pService->createCharacteristic(
    CHARACTERISTIC_UUID_EFFET, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicEffet->setCallbacks(new MyCallbacks());

  BLECharacteristic *pCharacteristicModeAuto = pService->createCharacteristic(
    CHARACTERISTIC_UUID_MODE_AUTO, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicModeAuto->setCallbacks(new MyCallbacks());

  // Démarrer le service BLE
  pService->start();
  pServer->getAdvertising()->start();
  Serial.println("BLE prêt !");
}

void loop() {
  if (modeAuto) {
    unsigned long currentTime = millis();
    
    if (isCycleOpen && (currentTime - lastChangeTime >= AUTO_OPEN_TIME)) {
      fermerIris();
      lastChangeTime = currentTime;
      isCycleOpen = false;
    } 
    else if (!isCycleOpen && (currentTime - lastChangeTime >= AUTO_CLOSE_TIME)) {
      ouvrirIris();
      lastChangeTime = currentTime;
      isCycleOpen = true;
    }
  }
}

IRIS_SMR_GATE_BLE.aia (201.3 KB)

i try this block but its not good :

anybody for my help ?

How to slow down a slider to avoid machine gunning update messages

Init global lastTransmissionMS to 0

When slider changes

  • If Clock1.SystemTime > (500 + lastTransmissionMS) then
    • Send update
    • Set lastTransmissionMS to Clock1.SystemTime
  • Else
    • Do nothing

The aia you annexed is not the right one. It doesn't contain the Slider1 blocks and most of the movement buttons are not implemented yet.
In addition, the characteristicUuid aren't consistent, for example:

Where is defined this one ?

and to which of the characteristic_uuid defined on the ESP32 (here below) is referred ?

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID_OUVRIR "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define CHARACTERISTIC_UUID_FERMER "beb5483e-36e1-4688-b7f5-ea07361b26a9"
#define CHARACTERISTIC_UUID_ANGLE_MAX "beb5483e-36e1-4688-b7f5-ea07361b26ae" // Nouvelle caractéristique pour l'angle maximum
#define CHARACTERISTIC_UUID_TEMPS "beb5483e-36e1-4688-b7f5-ea07361b26aa"
#define CHARACTERISTIC_UUID_EFFET "beb5483e-36e1-4688-b7f5-ea07361b26ab"
#define CHARACTERISTIC_UUID_MODE_AUTO "beb5483e-36e1-4688-b7f5-ea07361b26ac".

So, in addition to what @ABG has already suggested you to avoid a continuous sending from the app to the ESP, please check whether the Uuid's are the right ones.

hi and thank you for your response. i don't have this block when.slider change

thank you I saw after my post the error I had corrected
IRIS_SMR_GATE_BLE1.aia (405.2 KB)

I was talking generally, from my phone.

You have that same block in

I see from your aia that you don't have a Clock component yet.
You can pull one in from the Sensors Drawer.

Once you have a Clock1, you can drag in


blocks (3)

Thank you for your response and the captures.
I added the new component and performed the update and build. however when I change the angle with the slider on the application my servo no longer works.
I specify that before touching the slider, the servo opens and closes by pressing the open and close buttons.

That sounds like it's time to show your blocks again.

Are you adding the requirements that the slider only work when the device is "open"?

Yes , ive test : change angle by slider 45 and click open... and the serdo don't move.
I upload the new project in the previous post.

There are a lot of previous posts.

For consistency and for the ability to see progress, please post both .ino and .aia files in a new post in this thread.

We have plenty of room but little tolerance for wasted time.

IRIS_SMR_GATE_BLE.ino.ino (5.5 KB)
IRIS_SMR_GATE_BLE.aia (405.6 KB)
see attach my 2 files ino and aia

I see a conflict between your expectations of data types in the sketch and app for the Angle:

// Callback BLE
class MyCallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *pCharacteristic) override {
    String rawValue = pCharacteristic->getValue().c_str();
    String rawUUID = pCharacteristic->getUUID().toString().c_str();

    if (rawUUID == CHARACTERISTIC_UUID_OUVRIR) {
      if (rawValue == "ouvrir") ouvrirIris();
    } else if (rawUUID == CHARACTERISTIC_UUID_FERMER) {
      if (rawValue == "fermer") fermerIris();
    } else if (rawUUID == CHARACTERISTIC_UUID_ANGLE_MAX) {
      angleMax = rawValue.toInt();
      Serial.println("Angle maximum mis à jour : " + String(angleMax));

#define CHARACTERISTIC_UUID_ANGLE_MAX "beb5483e-36e1-4688-b7f5-ea07361b26ae" // Nouvelle caractéristique pour l'angle maximum

BLECharacteristic *pCharacteristicAngleMax = pService->createCharacteristic(
CHARACTERISTIC_UUID_ANGLE_MAX, BLECharacteristic::PROPERTY_WRITE);
pCharacteristicAngleMax->setCallbacks(new MyCallbacks());

From what I see in the sketch, it is expecting a piece of text for the angle, based on that toInt conversion.

But you don't send text. You send a single byte:



That's what WriteBytes does.

Look for another BLE block that writes text.

P.S. I get confused about the declaration of BLE Characteristic properties in the sketch. Does PROPERTY_WRITE mean the sketch can write to the characteristic, or that it can read from the characteristic?

HI, I'm sorry but I don't understand your explanation, well not in its entirety. I understand that I can send text (write.string block?) I am a complete beginner sorry