Arduino Nano 33 iot - BLE Connection Alert "Timeout interrogating peripheral"

Hi everyone,

I’m working to measure and track my heat pumps coefficient of performance (COP). To start, I’m utilizing (2) CT sensors and (1) temperature sensor to track the following: power input to air hander fan, power input to outdoor unit, discharge air temperature. For now, I’m making assumptions on flow rate and entering air temperature. I’m utilizing Bluetooth to communicate the readings back to a central device.

//Libraries
#include <DHT.h>
#include <ArduinoBLE.h>
#include "EmonLib.h"




//Variables


EnergyMonitor emon1, emon2;


#define DHTPIN 2
#define DHTTYPE DHT22


float oldtemp = 0;
float FanCoiloldAmp=0;
float OutdoorUnitoldAmp=0;
long previousMillis = 0;




// BLE Service
BLEService HeatPumpService("280B");


// BLE Battery Level Characteristic
BLEUnsignedCharCharacteristic EnteringAirTemp("3A18",
    BLERead | BLENotify);
BLEFloatCharacteristic FanCoilAmp("3A19",
    BLERead | BLENotify);
BLEFloatCharacteristic OutdoorUnitAmp("4D80",
    BLERead | BLENotify);
    
void setup()
{
  Serial.begin(9600);


  emon1.current(1, 90.909);
  emon2.current(3, 90.909);


  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
      
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  BLE.setLocalName("HVAC_Monitor");
  BLE.setAdvertisedService(HeatPumpService);
  HeatPumpService.addCharacteristic(EnteringAirTemp);
  HeatPumpService.addCharacteristic(FanCoilAmp);
  HeatPumpService.addCharacteristic(OutdoorUnitAmp);
  BLE.addService(HeatPumpService);
  EnteringAirTemp.writeValue(oldtemp);
  FanCoilAmp.writeValue(FanCoiloldAmp);
  OutdoorUnitAmp.writeValue(OutdoorUnitoldAmp);


  // start advertising
  BLE.advertise();


  Serial.println("Bluetooth device active, waiting for connections...");


}


void loop()
{


    // wait for a BLE central
    BLEDevice central = BLE.central();


    // if a central is connected to the peripheral:
    if (central) {
      Serial.print("Connected to central: ");
     // print the central's BT address:
      Serial.println(central.address());
     // turn on the LED to indicate the connection:
      digitalWrite(LED_BUILTIN, HIGH);


    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check sensor(s):
      if (currentMillis - previousMillis >= 2000) {
        previousMillis = currentMillis;
        updateTemp();
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
    }
}
void updateTemp() {


DHT dht(DHTPIN, DHTTYPE);
  float temp;


    dht.begin();
      temp = (dht.readTemperature()*9/5)+32;
            Serial.print(" %, Temp: ");
            Serial.print(temp);
            Serial.println(" Farenhiet");
            EnteringAirTemp.writeValue(temp);
            oldtemp = temp;
            
           
  float Irms1 = emon1.calcIrms(1480);
    Serial.print("Air Handler Running Amperage: ");
    Serial.print(Irms1);
    Serial.println(" amps ");
    FanCoilAmp.writeValue(Irms1);
    FanCoiloldAmp = Irms1;


  float Irms2 = emon2.calcIrms(1480);
    Serial.print("Air Handler Running Amperage: ");
    Serial.print(Irms1);
    Serial.println(" amps ");
    FanCoilAmp.writeValue(Irms1);
    FanCoiloldAmp = Irms1;
}

I’m utilizing LightBlue to test the devices bluetooth connection. When running this code, Lightblue gives me a connection timeout alert. Things however work as expected when I remove one of the CT sensors and the associated code. Example code I removed below to get the central device to connect normally:

  float Irms2 = emon2.calcIrms(1480);
    Serial.print("Air Handler Running Amperage: ");
    Serial.print(Irms1);
    Serial.println(" amps ");
    FanCoilAmp.writeValue(Irms1);
    FanCoiloldAmp = Irms1;

Any ideas what might be causing this?

Looks like you are not calling connected() often enough

while (central.connected()) {

includes a call the HCI.poll().
It seems your call to

emon2.calcIrms(1480);

is taking too long
I suggest you move the code for calcIrms into you sketch and add extra calls to central.connected()
Check out my tutorial on Multi-tasking in Arduino that includes a loopTimer you can use to see how often connected()is called.

Using the loopTimer was just what I needed. It was taking about 2.5 seconds to run with the temperature sensor and both CT's, causing problems with me telling it to run every 2 seconds.

As an interim fix, I adjusted the sampling rate from every 2 seconds to every 3 seconds which solved my original problem

    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check sensor(s):
      if (currentMillis - previousMillis >= 3000) 
        previousMillis = currentMillis;
        updateTemp();

}

I would like a higher sampling rate so I'm going to work to implement some of the other things you noted below to improve multi-tasking flow.

Thanks!

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