Blynk example for RedBearLab BLE Nano

Dependencies:   BLE_API Blynk mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*************************************************************
00002   Blynk is a platform with iOS and Android apps to control
00003   Arduino, Raspberry Pi and the likes over the Internet.
00004   You can easily build graphic interfaces for all your
00005   projects by simply dragging and dropping widgets.
00006 
00007     Downloads, docs, tutorials: http://www.blynk.cc
00008     Sketch generator:           http://examples.blynk.cc
00009     Blynk community:            http://community.blynk.cc
00010     Follow us:                  http://www.fb.com/blynkapp
00011                                 http://twitter.com/blynk_app
00012 
00013   Blynk library is licensed under MIT license
00014   This example code is in public domain.
00015 
00016  *************************************************************
00017   This example shows how to use RedBearLab BLE Nano
00018   to connect your project to Blynk.
00019 
00020   Warning: Bluetooth support is in beta!
00021 
00022  *************************************************************/
00023 #include <mbed.h>
00024 Serial pc(USBTX, USBRX);
00025 
00026 // TODO: these defines are included as a workaround.
00027 // I'm investigating further.
00028 #define BLYNK_NO_INFO
00029 #define BLYNK_USE_DIRECT_CONNECT
00030 // end of workaround
00031 
00032 //#define BLYNK_DEBUG
00033 #define BLYNK_PRINT pc
00034 
00035 #include <BlynkSimpleRedBearLab_BLE_Nano.h>
00036 
00037 // You should get Auth Token in the Blynk App.
00038 // Go to the Project Settings (nut icon).
00039 char auth[] = "YourAuthToken";
00040 
00041 uint8_t device_name[] = "Blynk";
00042 
00043 void setup() {
00044   ble.init();
00045 
00046   ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00047   ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00048                                         device_name, sizeof(device_name) - 1);
00049                                         
00050   ble.gap().setDeviceName(device_name);
00051   ble.gap().setTxPower(4);
00052   
00053   // Add Blynk service...
00054   Blynk.begin(auth);
00055 
00056   ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00057   ble.gap().setAdvertisingInterval(Gap::MSEC_TO_GAP_DURATION_UNITS(1000));
00058   ble.gap().setAdvertisingTimeout(0);
00059   ble.startAdvertising();
00060 
00061   printf("Waiting for connections...\n");
00062 }
00063 
00064 void loop() {
00065     ble.waitForEvent();
00066     Blynk.run();
00067 }
00068 
00069 int main(void) {
00070     setup();
00071 
00072     while(1) {
00073         loop();
00074     }
00075 }