Blynk example for RedBearLab BLE Nano

Dependencies:   BLE_API Blynk mbed nRF51822

source/main.cpp

Committer:
vshymanskyy
Date:
2017-11-27
Revision:
4:27632dd38be6
Parent:
3:1a1b4df54a17
Child:
5:c52894060941

File content as of revision 4:27632dd38be6:

/**************************************************************
 * 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!
 *
 **************************************************************/
#include <mbed.h>
Serial pc(USBTX, USBRX);

//#define BLYNK_DEBUG
#define BLYNK_PRINT pc

#include <BlynkSimpleRedBearLab_BLE_Nano.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

uint8_t device_name[] = "Blynk";

PwmOut speaker(p30);
BLYNK_WRITE(V1) {
    if (param.asInt()) {
        speaker.period(1.0/param.asInt());
        speaker = 0.5;
    } else {
        speaker = 0;
    }
}

PwmOut ledR(p15);
BLYNK_WRITE(V2) {
    ledR = 1.0 - (param.asDouble()/1024.0f);
}

void setup() {
  ble.init();

  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);

  ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
  ble.gap().setAdvertisingInterval(Gap::MSEC_TO_GAP_DURATION_UNITS(1000));
  ble.gap().setAdvertisingTimeout(0);
  ble.startAdvertising();

  printf("Waiting for connections...\n");
  
  ledR = 1.0;
  speaker = 0.0;
}

void loop() {
  Blynk.run();
}

int main(void) {
    setup();
    while(1) {
        loop();
    }
}