#include #include //pin declarations #define BATTERY_VOLTAGE_SENSE_PIN A0 #define POWER_AVAILABILITY_PIN 2 int oldBatteryLevel = 0; // last battery level reading from analog input long previousMillis = 0; // last time the battery level was checked, in ms int initialPowerAvailabilityState = 0; BLEService batteryService("180F"); // BLE Battery Service BLEService powerAvailableService("BD1CA63F-50E3-4A05-9294-0A1443CF5B72"); BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); BLEUnsignedCharCharacteristic powerAvailabilityChar("BD1CA63E-50E3-4A05-9294-0A1443CF5B72", BLERead | BLENotify); void setup() { Serial.begin(9600); // initialize serial communication pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected // begin initialization BLE.begin(); /* Set a local name for the BLE device This name will appear in advertising packets and can be used by remote devices to identify this BLE device The name can be changed but maybe be truncated based on space left in advertisement packet */ BLE.setLocalName("PowerStatus"); BLE.setLocalName("BatteryMonitor"); BLE.setAdvertisedService(powerAvailableService); BLE.setAdvertisedService(batteryService); // add the service UUID powerAvailableService.addCharacteristic(powerAvailabilityChar); batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic BLE.addService(batteryService); // Add the BLE Battery service BLE.addService(powerAvailableService); powerAvailabilityChar.setValue(initialPowerAvailabilityState); batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic // start advertising BLE.advertise(); BLE.advertise(); Serial.println("Bluetooth device active, waiting for connections..."); } void loop() { initialiseInterruptReading(); } void initialiseInterruptReading(){ // 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()); // turn on the LED to indicate the connection: digitalWrite(13, HIGH); // check the battery level every 200ms // as long as the central is still connected: while (central.connected()) { long currentMillis = millis(); // if 200ms have passed, check the battery level: if (currentMillis - previousMillis >= 200) { previousMillis = currentMillis; updateBatteryLevel(); GeneratorPowerAvailabilityStatus(); } } // when the central disconnects, turn off the LED: digitalWrite(13, LOW); Serial.print("Disconnected from central: "); Serial.println(central.address()); } } void updateBatteryLevel() { /* Read the current voltage level on the A0 analog input pin. This is used here to simulate the charge level of a battery. */ int battery = analogRead(BATTERY_VOLTAGE_SENSE_PIN); int batteryLevel = map(battery, 0, 1023, 0, 100); if (batteryLevel != oldBatteryLevel) { // if the battery level has changed Serial.print("Battery Level % is now: "); // print it Serial.println(batteryLevel); batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic oldBatteryLevel = batteryLevel; // save the level for next comparison } } void PowerAvailabilityStatus() { int powerAvailableStatus = digitalRead(POWER_AVAILABILITY_PIN); if (powerAvailableStatus == HIGH){ Serial.print("power is Available on Generator1"); powerAvailabilityChar.setValue(powerAvailableStatus); initialPowerAvailabilityState = powerAvailableStatus; }else{ Serial.println("power is Unavailable on Generator1"); powerAvailabilityChar.setValue(powerAvailableStatus); initialPowerAvailabilityState = powerAvailableStatus; } }