Skip to content

Arduino nano IoT 33 switch bewtween BLE and WiFi issue on read BLE characteristic value #298

Open
@ibexnet

Description

@ibexnet

Hi everybody,
I have a strange issue when I switch between WiFi and BLE.
I written a program that start BLE and every hour send data over WiFi to an http server for logging purpose.
Before I start to send data over WiFi I stop BLE service and start again it when the http connection is completed.
The issue appear when I start the BLE from second time.
The issue is that when I read values of every characteristics, the value is repeated twice like if it was appended every time the program start the BLE service, for example the value of characteristic should be in hex 52000000, if I read it after the BLE stop and start the value became 5200000052000000, if I read it the third time BLE stop and start the value became 520000005200000052000000 and so on until the max length of the characteristic is reached.
The strange part is that if I subscribe on the characteristic change, the value is read correctly.
If I remove the code that start and stop the BLE everything work properly, so I'm sure that I do something wrong when start BLE but I don't understand what I doing wrong.

This is my code

bool startBle(  ){
  if ( !BLE.begin() ){
    return false;
  }

  BLE.setLocalName("name");
  BLE.setDeviceName("name");
  byte mData[6] = { 0xe0, 0x0, 0x0, 0x0, 0x0, 0x0 };
  BLE.setManufacturerData(mData, 6);
  
  BLE.setAdvertisedService(BLE_S_default);

  BLE_S_default.addCharacteristic(BLE_C_rtc);
  BLE_S_default.addCharacteristic(BLE_C_pumpStat);

  BLE.addService(BLE_S_default);
  BLE.advertise();

  BLE.setEventHandler(BLEConnected, onBLEConnected);
  BLE.setEventHandler(BLEDisconnected, onBLEDisconnected);

  BLE_C_rtc.setEventHandler(BLEWritten, BLEEvWritten);
  BLE_C_pumpStat.setEventHandler(BLEWritten, BLEEvWritten);

  return true;
}

void stopBle(){
    BLE.stopAdvertise();
    if(debug){
      Serial.println("BLE advertise stopped");
    }
    BLE.end();
    if(debug){
      Serial.println("BLE stopped");
    }
    ble_disabled = true;
}

void setup() {
...omitted code

 if(!startBle(true)){
    Serial.println("starting BLE failed!");
  }
}


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

...omitted code

 if (elapsed_time_logger >= send_interval) {
      previousSendData = currentMillis;
      
      stopBle();
      if(connectToAP() == WL_CONNECTED){
        httpRequest(data);
      }else{
        errorHandler("Unable to send data, WiFi not available");
      }
      WiFi.end();
      
      if (ble_disabled){
        if(!startBle()){
          errorHandler("unable to switch on ble");
        }else{
          if(debug){
            Serial.println("BLE enabled");
          }
          ble_disabled=false;
        }
      }
    }

  if (central){
...omitted code
  }

}

regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions