Uploading value with arduino nano 33 ble

Hi everyone, I hope I'm not making any mistakes in publishing this post.
I searched the forum but couldn't find a solution to my problem.
I want to create a code that sends a continuously changing variable via BLE, reading this variable from a phone.
At the moment the variable read by the phone remains fixed at the value it had at the time of connection and I can't resolve it.
I hope someone can help me!

#include <ArduinoBLE.h>

int var = 0;

BLEService greetingService("0000180C-0000-1000-8000-00805F9B34FB");  // User defined service
BLEStringCharacteristic greetingCharacteristic("00002A56-0000-1000-8000-00805F9B34FB",  // standard 16-bit characteristic UUID
    BLERead | BLEWrite, 128); // remote clients will be able to read and write up to 20 bytes

void setup() {
  Serial.begin(9600);    // initialize serial communication
  while (!Serial);

  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin

  if (!BLE.begin()) {   // initialize BLE
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName("Nano33BLE");  // Set name for connection
  BLE.setAdvertisedService(greetingService); // Advertise service
  greetingService.addCharacteristic(greetingCharacteristic); // Add characteristic to service
  BLE.addService(greetingService); // Add service

  BLE.advertise();  // Start advertising
  Serial.print("Peripheral device MAC: ");
  Serial.println(BLE.address());
  Serial.println("Waiting for connections...");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central MAC: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);

    while (central.connected()){
      var++;
      greetingCharacteristic.setValue(String(var));
      delay(50);
      greetingCharacteristic.broadcast();
      delay(500);
      Serial.println(var);
    }
    
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Disconnected from central MAC: ");
    Serial.println(central.address());
  }
}

I can not confirm your findings.

What phone app are you using?

When I connect to your peripheral with Light Blue I can read the changing values.

I'm using BLE scanner (on iphone) and Serial Bluetooth Terminal (on android, but despite using the app's ble service it don't find the Arduino).
Now i'm using like you Light Blue, but how do you see the variable on the app?

problem solved, now I see the variable on LightBlue.
I need to fix the code on Thunkable to read the variable correctly now.
Thanks for your help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.