Send multiple values with BLE between RP2040 Connect

Hello All,

I'm substantially a new entry in the Arduino's club but, due to my past esperience (while rather old) with microcontrollers, I was able to easily set up a sort o multifunction system, with a 7" Nextion display. One of these functions is to read and archive data from multiple environmental sensors. For this I'm using two Arduino MKR Wifi 1010, one manages sensors and display inside the house, the other is outside. Both are connected to the Arduino cloud and each have a dashboard. Now I need to transfer data (integer and floating point values) from the externa device to the internal and I'd like to do it through BLE.
I'm testing this using two RP2040 Connect with basic sketches (see attached) but in no way I'm able to transfer more than one integer value. In the attached examples I try to make available two integers from a sensor on the peripheral device and gather them from the central device but the result is that only the first value is printed out twice on the central device. I tried to add multiple services but, when the device are able to connect I get always the same result. Can someone point out what I'm doing wrong? Thank you.

Simone

//Code for peripheral
#include <WiFiNINA.h>
#include <ArduinoBLE.h>
#include <DFRobot_ENS160.h>

uint16_t ECO2;
DFRobot_ENS160_I2C ENS160(&Wire, 0x53);


float timer1;

BLEService ENVService("19B10000-E8F2-537E-4F6C-D104768A1214");  
BLEIntCharacteristic CO2Characteristic("19B10001-E8F2-537E-4F6C-A104768A1214", BLERead | BLENotify);
BLEIntCharacteristic TVOCCharacteristic("19B10001-E8F2-537E-4F6C-B104768A1215", BLERead | BLENotify);
void setup() {
  Serial.begin(9600);

  // Init the sensor
  while (NO_ERR != ENS160.begin()) {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");

  /**
   * Set power mode
   * mode Configurable power mode:
   *   ENS160_SLEEP_MODE: DEEP SLEEP mode (low power standby)
   *   ENS160_IDLE_MODE: IDLE mode (low-power)
   *   ENS160_STANDARD_MODE: STANDARD Gas Sensing Modes
   */
  ENS160.setPWRMode(ENS160_STANDARD_MODE);

  /**
   * Users write ambient temperature and relative humidity into ENS160 for calibration and compensation of the measured gas data.
   * ambientTemp Compensate the current ambient temperature, float type, unit: C
   * relativeHumidity Compensate the current ambient humidity, float type, unit: %rH
   */
  ENS160.setTempAndHum(/*temperature=*/25.0, /*humidity=*/50.0);


  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy failed!");
  }
  // set advertised local name and service UUID:
  BLE.setLocalName("EnvSensor");
  BLE.setAdvertisedService(ENVService);
  // add the characteristic to the service
  ENVService.addCharacteristic(CO2Characteristic);
  ENVService.addCharacteristic(TVOCCharacteristic);
  // add service
  BLE.addService(ENVService);
  // start advertising
  BLE.advertise();
  Serial.println("Peripheral, waiting for connections....");
}
void loop() {
  /**
   * Get CO2 equivalent concentration calculated according to the detected data of VOCs and hydrogen (eCO2 – Equivalent CO2)
   * Return value range: 400–65000, unit: ppm
   * Five levels: Excellent(400 - 600), Good(600 - 800), Moderate(800 - 1000), 
   *               Poor(1000 - 1500), Unhealthy(> 1500)
   */


  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());
    // while the central is still connected to peripheral:
    while (central.connected()) {

      if (timer1 > millis()) { timer1 = millis(); }
      if (millis() - timer1 >= 1000) {
        timer1 = millis();
        ECO2 = ENS160.getECO2();
        Serial.print("Carbon dioxide equivalent concentration : ");
        Serial.print(ECO2);
        Serial.println(" ppm");

        CO2Characteristic.writeValue(ECO2);

        uint16_t TVOC = ENS160.getTVOC();
        Serial.print("Concentration of total volatile organic compounds : ");
        Serial.print(TVOC);
        Serial.println(" ppb");
        TVOCCharacteristic.writeValue(TVOC);
      }
    }
  }

  // when the central disconnects, print it out:
  Serial.print(F("Disconnected from central: "));
  Serial.println(central.address());
}
//Code for central
#include <WiFiNINA.h>
#include <ArduinoBLE.h>

float timer1;

void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;
  // initialize the BLE hardware
  BLE.begin();
  Serial.println("BLE Central");
  // start scanning for Button Device BLE peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();
  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();
    if (peripheral.localName().indexOf("EnvSensor") < 0) {
      Serial.println("No 'Env Sensor' in name");
      return;  // If the name doesn't have "Button Device" in it then ignore it
    }
    // stop scanning
    BLE.stopScan();
    controlLed(peripheral);
    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");
  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }
  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }
  // retrieve the characteristics
BLECharacteristic CO2Characteristic = peripheral.characteristic("19B10001-E8F2-537E-4F6C-A104768A1214");
BLECharacteristic TVOCCharacteristic = peripheral.characteristic("19B10001-E8F2-537E-4F6C-B104768A1215");
 // if (!CO2Characteristic) {
 //   Serial.println("Peripheral does not have CO2 characteristic!");
 //   peripheral.disconnect();
  //  return;
 // }
  while (peripheral.connected()) {
    if (timer1 > millis()) { timer1 = millis(); }
    if (millis() - timer1 >= 1000) {
      timer1 = millis();
      // while the peripheral is connected
      if (CO2Characteristic.canRead()) {
        uint16_t value = CO2Characteristic.read();
        CO2Characteristic.readValue(&value, 4);
 

        Serial.println(value);
      }
      // while the peripheral is connected
      if (TVOCCharacteristic.canRead()) {
        uint16_t value2 = TVOCCharacteristic.read();
        CO2Characteristic.readValue(&value2, 4);


        Serial.println(value2);
      }
    }
  }
  Serial.println("Peripheral disconnected");
}

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