nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.37k stars 710 forks source link

Cannot BLE advertise with different data #1117

Open vk3drb opened 2 years ago

vk3drb commented 2 years ago

Hello.

I have a simple issue. I have an ESP32-C3 and am using Arduino IDE. I have to transmit some data from a weighing machine periodically.

I have connections working fine. The problem is when a user connects and receives the weighing data on his phone, and walks way, and forgets to disconnect, he "locks out" the next user from connecting.

I think the solution could be:

  1. To transmit the data in the advertising string. eg: "Weigher xxxx 12.34kg" where xxxx is an incremental counter, so that the phone app checks there was a new weighing event.
  2. Alternatively, maybe set up a beacon that transmits the varying data.

Fortunately, the phone does not provide commands. Hence, any or all phones can receive the data at the same time which is not a use case issue.

I am using nRF Connect for testing.

So, to test option 1, I wrote some very simple code below. The result is "weight 1" is advertised, but after the 5+1 seconds, "Weight 2" is not advertised. If I close nRf Connect app on the phone and open it up again, I see do not see any advertising.

What is wrong with this code? Is using beacon a better way to broadcast this data, or is there some other better and easier solution?

=========================================

include

include

include

include

BLEServer pServer = NULL; BLECharacteristic pTxCharacteristic;

define SERVICE_UUID "2b593b0f-658a-49c8-8f7f-a70a872e47d2"

define CHARACTERISTIC_UUID_RX "2b593b0f-658a-49c8-8f7f-a70a872e47d2"

define CHARACTERISTIC_UUID_TX "2b593b0f-658a-49c8-8f7f-a70a872e47d2"

void setup() { // Create the BLE Device BLEDevice::init("Weight 1"); // Start advertising pServer->getAdvertising()->start(); // Advertise "Weight 1.0kg"

delay(5000); // Just sit there for 5 seconds pServer->getAdvertising()->stop();
BLEDevice::deinit(true); delay(1000);
BLEDevice::init("Weight 2");
pServer->getAdvertising()->start(); // Advertise "Weight 2.0kg"
}

void loop() { }

Any help is much appreciated. regards, Dave