clone
Dependencies: BLE_API Blynk mbed nRF51822
Fork of Blynk_RBL_BLE_Nano by
Diff: main.cpp
- Revision:
- 1:6b5c74442c7a
- Parent:
- 0:cf8e8a71c9c3
- Child:
- 2:56696744060e
--- a/main.cpp Mon Aug 31 01:48:49 2015 +0000 +++ b/main.cpp Thu Jun 23 23:09:33 2016 +0000 @@ -1,160 +1,65 @@ -#include "mbed.h" -#include "ble/BLE.h" - -#define TXRX_BUF_LEN 20 - -Timer millis_timer; - -static -void delay(unsigned long ms) -{ - wait_ms(ms); -} - -static -unsigned long millis(void) -{ - return millis_timer.read_ms(); -} - -BLE ble; - -Serial pc(USBTX, USBRX); +/************************************************************** + * Blynk is a platform with iOS and Android apps to control + * Arduino, Raspberry Pi and the likes over the Internet. + * You can easily build graphic interfaces for all your + * projects by simply dragging and dropping widgets. + * + * Downloads, docs, tutorials: http://www.blynk.cc + * Blynk community: http://community.blynk.cc + * Social networks: http://www.fb.com/blynkapp + * http://twitter.com/blynk_app + * + * Blynk library is licensed under MIT license + * This example code is in public domain. + * + ************************************************************** + * + * This example shows how to use RedBearLab BLE Nano + * to connect your project to Blynk. + * + * NOTE: BLE support is in beta! + * + **************************************************************/ #define BLYNK_DEBUG -#define BLYNK_PRINT pc -#include <Blynk/BlynkSimpleUserDefined.h> -#include <Blynk/utility/BlynkFifo.h> - - -// The Nordic UART Service -static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; -static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; -static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; -static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; -static const uint8_t uart_dev_name[] = "Blynk"; - -uint8_t txPayload[TXRX_BUF_LEN] = {0,}; -uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; - -GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, - TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); - -GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - -GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; +#define BLYNK_PRINT stdout -GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); - - - -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) -{ - pc.printf("Disconnected \r\n"); - pc.printf("Restart advertising \r\n"); - ble.startAdvertising(); -} +//#define BLYNK_USE_DIRECT_CONNECT -BlynkFifo<uint8_t, 128> rx_fifo; - -void WrittenHandler(const GattWriteCallbackParams *Handler) -{ - if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) - { - uint8_t buf[TXRX_BUF_LEN]; - uint16_t bytesRead; - ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); - pc.printf("Got data\r\n"); - rx_fifo.write(buf, bytesRead); - } -} +#include <BlynkSimpleRedBearLab_BLE_Nano.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). -char auth[] = "e3918e9a5fbd4739a8c973bcfc4e12b8"; +char auth[] = "338b85a6c7aa451689ee3590cde0ad33"; -// This function is used by Blynk to receive data -size_t BlynkStreamRead(void* buf, size_t len) -{ - uint32_t start = millis(); - while ((rx_fifo.getOccupied() < len) && (millis() - start < 1500)) { - ble.waitForEvent(); - } - return rx_fifo.read((uint8_t*)buf, len); -} +uint8_t device_name[] = "Blynk1"; + +void setup() { + ble.init(); -// This function is used by Blynk to send data -size_t BlynkStreamWrite(const void* buf, size_t len) -{ - int i = 0; - uint8_t* buff = (uint8_t*)buf; - while (i < len) { - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buff+i, BlynkMin(size_t(TXRX_BUF_LEN), len-i)); - i += TXRX_BUF_LEN; - } - return len; -} + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, + device_name, sizeof(device_name) - 1); + + ble.gap().setDeviceName(device_name); + ble.gap().setTxPower(4); + + // Add Blynk service... + Blynk.begin(auth); -//DigitalOut led(LED1); - -BLYNK_READ(V1) { - //led = !led; - Blynk.virtualWrite(V1, millis()/1000); -} + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.gap().setAdvertisingInterval(Gap::MSEC_TO_GAP_DURATION_UNITS(1000)); + ble.gap().setAdvertisingTimeout(0); + ble.startAdvertising(); -Timeout flipper; - -//DigitalOut led2(LED2); - -void flip() { - //led2 = !led2; - flipper.attach(&flip, 2.0); + printf("Waiting for connections...\n"); } -void setup() -{ - flipper.attach(&flip, 2.0); - - pc.baud(9600); - millis_timer.start(); - Blynk.begin(auth); - - ble.init(); - ble.onDisconnection(disconnectionCallback); - ble.onDataWritten(WrittenHandler); - - // setup advertising - ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); - ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - uart_dev_name, sizeof(uart_dev_name) - 1); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - uart_base_uuid_rev, sizeof(uart_base_uuid)); - - ble.setAdvertisingInterval(Gap::MSEC_TO_GAP_DURATION_UNITS(1000)); - - ble.addService(uartService); - - ble.startAdvertising(); - pc.printf("Advertising Start \r\n"); +void loop() { + Blynk.run(); } -void loop() -{ - ble.waitForEvent(); - // Okay, handle Blynk protocol - int hasIncomingData = rx_fifo.getOccupied(); - //BLYNK_LOG("Readable: %d", hasIncomingData); - // Tell Blynk if it has incoming data - // (this allows to skip unneeded BlynkStreamRead calls) - if (!Blynk.run(hasIncomingData)) { - // Error happened. No action for serial. - } -} - -int main() { +int main(void) { setup(); while(1) { loop();