I keep getting the message "Not connected from central " when I run my code. What should I look for when I am having a situation like this? Please bear with me if my question is too broad, as I am a beginner to hardware programming.
My code is below (Its the line BLECentral central = blePeripheral.central(); that is causing trouble)
#include "MutichannelGasSensor.h"
//#include <SPI.h> // Required to have support for signed/unsigned long type
#include <CurieBLE.h>
//#include <BLEPeripheral.h>
// Create my own UUIDs; used https://www.uuidgenerator.net/
#define DATA_SERVICE_UUID "5badc414-a8f6-4692-849e-23aa5d97aa3c"
#define DEVICEID_CHAR_UUID "935626b3-6996-4b09-b6dc-02695f307f7a"
#define NH3_CHAR_UUID "fbd5e235-8892-4f51-9d52-1450c94f722d"
#define CO_CHAR_UUID "80c50a3f-663f-4a3b-ab54-f73d7e7699f8"
#define NO2_CHAR_UUID "b3cb7efc-1352-498f-a4f0-5397ae648b70"
#define C3H8_CHAR_UUID "e002c096-a23e-42b8-a340-571dbded7c47"
#define C4H10_CHAR_UUID "6e02eb88-332d-4a70-920e-fb302169b4b0"
#define CH4_CHAR_UUID "f3bacc86-caac-4db3-ae24-47f424dc22e8"
#define H2_CHAR_UUID "c56089e6-682d-44c4-9e10-4a8beafbe4da"
#define C2H5OH_CHAR_UUID "86599cc0-e642-43c8-86c8-0b97738d5e96"
// Arduino 101 acts as a BLE peripheral
BLEPeripheral blePeripheral;
// data is registered as a BLE service
BLEService dataService(DATA_SERVICE_UUID);
// Each data point is its own characteristic
BLEIntCharacteristic deviceChar(DEVICEID_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic nh3Char(NH3_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic coChar(CO_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic no2Char(NO2_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic c3h8Char(C3H8_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic c4h10Char(C4H10_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic ch4Char(CH4_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic h2Char(H2_CHAR_UUID, BLERead | BLENotify);
BLEFloatCharacteristic c2h5ohChar(C2H5OH_CHAR_UUID, BLERead | BLENotify);
// Assign pin to indicate BLE connection
const int INDICATOR_PIN = 13;
int deviceID = 1; float nh3 = 0; float co = 0; float no2 = 0; float c3h8 = 0;
float c4h10 = 0; float ch4 = 0; float h2 = 0; float c2h5oh = 0;
// the variables for each gas element detected by the gas sensor
long previousMillis = 0;
void setup()
{
/*
* setup() sets up the baud rate, the gas sensor (connected to the board via I2C pin, and
* the wifi connection
*/
delay(10000);
Serial.println("start!");
Serial.begin(115200);
Serial.println("1");
//blePeripheral = new BLEPeripheral();
// Initialize BLE peripheral
blePeripheral.setLocalName("SENSOR_DATA");
blePeripheral.setAdvertisedServiceUuid(dataService.uuid());
blePeripheral.addAttribute(dataService);
blePeripheral.addAttribute(deviceChar);
blePeripheral.addAttribute(nh3Char);
blePeripheral.addAttribute(coChar);
blePeripheral.addAttribute(no2Char);
blePeripheral.addAttribute(c3h8Char);
blePeripheral.addAttribute(c4h10Char);
blePeripheral.addAttribute(ch4Char);
blePeripheral.addAttribute(h2Char);
blePeripheral.addAttribute(c2h5ohChar);
Serial.println("2");
// Set initial values
deviceChar.setValue(deviceID);
nh3Char.setValue(nh3);
coChar.setValue(co);
no2Char.setValue(no2);
c3h8Char.setValue(c3h8);
c4h10Char.setValue(c4h10);
ch4Char.setValue(ch4);
h2Char.setValue(h2);
c2h5ohChar.setValue(c2h5oh);
Serial.println("3");
// Now, activate the BLE peripheral
blePeripheral.begin();
Serial.println("Bluetooth device active, waiting for connections...");
if (blePeripheral.connected()){
Serial.println("peripheral connected!");
}else{
Serial.println("peripheral NOT connected!");
}
// configure the gas sensor
gas.begin(0x04);//the default I2C address of the slave is 0x04
gas.powerOn();
}
void loop()
{
/**
* loop() retrieves data read from the gas sensor using the instance gas from MutichannelGasSensor.h,
* parses them such that it creates the json string, and make a API call (insertRawData)
* to the microservice app (the Sptring Boot project) to add the data to the data repository (NoSQL)
**/
// Check if the connection to the central is active or not
BLECentral central = blePeripheral.central();
if (central){
Serial.print("Connected to central: ");
Serial.println(central.address());
while(central.connected()) {
updateSensorData();
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}else{
Serial.println("Not connected from central ");
}
delay(1000);
}
void updateSensorData(){
/* updateSensorData()
*
*/
// call the functions that measures the gases read by the sensor
nh3 = gas.measure_NH3();
co = gas.measure_CO();
no2 = gas.measure_NO2();
c3h8 = gas.measure_C3H8();
c4h10 = gas.measure_C4H10();
ch4 = gas.measure_CH4();
h2 = gas.measure_H2();
c2h5oh = gas.measure_C2H5OH();
nh3Char.setValue(nh3);
coChar.setValue(co);
no2Char.setValue(no2);
c3h8Char.setValue(c3h8);
c4h10Char.setValue(c4h10);
ch4Char.setValue(ch4);
h2Char.setValue(h2);
c2h5ohChar.setValue(c2h5oh);
Serial.println("Update complete!");
delay(1000);
}