|
| 1 | +/********************************************************************* |
| 2 | + This is an example for our nRF52 based Bluefruit LE modules |
| 3 | +
|
| 4 | + Pick one up today in the adafruit shop! |
| 5 | +
|
| 6 | + Adafruit invests time and resources providing this open source code, |
| 7 | + please support Adafruit and open-source hardware by purchasing |
| 8 | + products from Adafruit! |
| 9 | +
|
| 10 | + MIT license, check LICENSE for more information |
| 11 | + All text above, and the splash screen below must be included in |
| 12 | + any redistribution |
| 13 | +*********************************************************************/ |
| 14 | +#include <bluefruit.h> |
| 15 | +#include <Adafruit_LittleFS.h> |
| 16 | +#include <InternalFileSystem.h> |
| 17 | +#include <Adafruit_CircuitPlayground.h> |
| 18 | + |
| 19 | +// BLE Service |
| 20 | +BLEDfu bledfu; // OTA DFU service |
| 21 | +BLEDis bledis; // device information |
| 22 | +BLEUart bleuart; // uart over ble |
| 23 | +BLEBas blebas; // battery |
| 24 | + |
| 25 | +// Base UUID : ADAF0000-C332-42A8-93BD-25E905756CB8 |
| 26 | + |
| 27 | +/* CPB Temperature |
| 28 | + * - Service: ADAF-0001-C332-42A8-93BD-25E905756CB8 |
| 29 | + * - Temperature Celsius : 0x2A6E |
| 30 | + * - Measurement Interval : 0x2A21 |
| 31 | + */ |
| 32 | +BLEAdafruitTemperature bleTemp; |
| 33 | + |
| 34 | +/* Adafruit NeoPixel Service |
| 35 | + * - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8 |
| 36 | + * - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8 |
| 37 | + * - Type : ADAF-0004-C332-42A8-93BD-25E905756CB8 |
| 38 | + * - Data : ADAF-0005-C332-42A8-93BD-25E905756CB8 |
| 39 | + */ |
| 40 | +BLEAdafruitNeopixel bleNeopixel; |
| 41 | + |
| 42 | +/* Adafruit Accelerometer Service |
| 43 | + * using micro:bit Accelerometer Service definition |
| 44 | + * https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html |
| 45 | + * |
| 46 | + * - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8 |
| 47 | + * - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8 |
| 48 | + * - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8 |
| 49 | + */ |
| 50 | +BLEAdafruitAccel bleAccel; |
| 51 | + |
| 52 | +void setup() |
| 53 | +{ |
| 54 | + Serial.begin(115200); |
| 55 | + while ( !Serial ) delay(10); // for nrf52840 with native usb |
| 56 | + |
| 57 | + Serial.println("Bluefruit52 BLEUART Example"); |
| 58 | + Serial.println("---------------------------\n"); |
| 59 | + |
| 60 | + CircuitPlayground.begin(); |
| 61 | + |
| 62 | + // Setup the BLE LED to be enabled on CONNECT |
| 63 | + // Note: This is actually the default behaviour, but provided |
| 64 | + // here in case you want to control this LED manually via PIN 19 |
| 65 | + Bluefruit.autoConnLed(true); |
| 66 | + |
| 67 | + // Config the peripheral connection with maximum bandwidth |
| 68 | + // more SRAM required by SoftDevice |
| 69 | + // Note: All config***() function must be called before begin() |
| 70 | + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
| 71 | + |
| 72 | + Bluefruit.begin(); |
| 73 | + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values |
| 74 | + Bluefruit.setName("Bluefruit52"); |
| 75 | + //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections |
| 76 | + Bluefruit.Periph.setConnectCallback(connect_callback); |
| 77 | + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
| 78 | + |
| 79 | + // To be consistent OTA DFU should be added first if it exists |
| 80 | + bledfu.begin(); |
| 81 | + |
| 82 | + // Configure and Start Device Information Service |
| 83 | + bledis.setManufacturer("Adafruit Industries"); |
| 84 | + bledis.setModel("Bluefruit Feather52"); |
| 85 | + bledis.begin(); |
| 86 | + |
| 87 | + // Configure and Start BLE Uart Service |
| 88 | + bleuart.begin(); |
| 89 | + |
| 90 | + // Start BLE Battery Service |
| 91 | + blebas.begin(); |
| 92 | + blebas.write(100); |
| 93 | + |
| 94 | + // Adafruit service |
| 95 | + bleTemp.begin(); |
| 96 | + bleNeopixel.begin(); |
| 97 | + bleAccel.begin(); |
| 98 | + |
| 99 | + |
| 100 | + // Set up and start advertising |
| 101 | + startAdv(); |
| 102 | + |
| 103 | + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); |
| 104 | + Serial.println("Once connected, enter character(s) that you wish to send"); |
| 105 | +} |
| 106 | + |
| 107 | +void startAdv(void) |
| 108 | +{ |
| 109 | + // Advertising packet |
| 110 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 111 | + Bluefruit.Advertising.addTxPower(); |
| 112 | + |
| 113 | + // Include bleuart 128-bit uuid |
| 114 | + Bluefruit.Advertising.addService(bleuart); |
| 115 | + |
| 116 | + // Secondary Scan Response packet (optional) |
| 117 | + // Since there is no room for 'Name' in Advertising packet |
| 118 | + Bluefruit.ScanResponse.addName(); |
| 119 | + |
| 120 | + /* Start Advertising |
| 121 | + * - Enable auto advertising if disconnected |
| 122 | + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms |
| 123 | + * - Timeout for fast mode is 30 seconds |
| 124 | + * - Start(timeout) with timeout = 0 will advertise forever (until connected) |
| 125 | + * |
| 126 | + * For recommended advertising interval |
| 127 | + * https://developer.apple.com/library/content/qa/qa1931/_index.html |
| 128 | + */ |
| 129 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 130 | + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms |
| 131 | + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode |
| 132 | + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds |
| 133 | +} |
| 134 | + |
| 135 | +void loop() |
| 136 | +{ |
| 137 | + if ( !Bluefruit.connected() ) return; |
| 138 | + if ( !bleTemp.Temperature.notifyEnabled() ) return; |
| 139 | + |
| 140 | + int16_t temp = (int16_t) (100*CircuitPlayground.temperature()); |
| 141 | + bleTemp.Temperature.notify16((uint16_t) temp); |
| 142 | + |
| 143 | + delay(1000*bleTemp.MeasurementInterval.read16()); |
| 144 | +} |
| 145 | + |
| 146 | +// callback invoked when central connects |
| 147 | +void connect_callback(uint16_t conn_handle) |
| 148 | +{ |
| 149 | + // Get the reference to current connection |
| 150 | + BLEConnection* connection = Bluefruit.Connection(conn_handle); |
| 151 | + |
| 152 | + char central_name[32] = { 0 }; |
| 153 | + connection->getPeerName(central_name, sizeof(central_name)); |
| 154 | + |
| 155 | + Serial.print("Connected to "); |
| 156 | + Serial.println(central_name); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Callback invoked when a connection is dropped |
| 161 | + * @param conn_handle connection where this event happens |
| 162 | + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h |
| 163 | + */ |
| 164 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 165 | +{ |
| 166 | + (void) conn_handle; |
| 167 | + (void) reason; |
| 168 | + |
| 169 | + Serial.println(); |
| 170 | + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); |
| 171 | +} |
0 commit comments