Skip to content

Instantly share code, notes, and snippets.

@eely22
Created July 24, 2016 03:09
Show Gist options
  • Select an option

  • Save eely22/3e65a5053e29afa26cddf76096ee9d82 to your computer and use it in GitHub Desktop.

Select an option

Save eely22/3e65a5053e29afa26cddf76096ee9d82 to your computer and use it in GitHub Desktop.
// This #include statement was automatically added by the Particle IDE.
#include "bluz_gateway.h"
#define POLL_CONNECTIONS_INTERVAL 10000
SYSTEM_MODE(SEMI_AUTOMATIC);
bluz_gateway gateway;
int bluz_peripherals_available = 0;
int bluz_preipherals_reported = 0;
void handle_custom_data(uint8_t *data, uint16_t length) {
//if you use BLE.send from any connected DK, the data will end up here
Particle.publish("Bluz Custom Data", String((char*)data));
bluz_preipherals_reported++;
if (bluz_preipherals_reported == bluz_peripherals_available) {
delay(1000); //wait, just in case, for the publish to go through
//now go to sleep for 5 minutes, only wake up again when we need to poll
System.sleep(SLEEP_MODE_DEEP, 300);
}
}
void handle_gateway_event(uint8_t event, uint8_t *data, uint16_t length) {
//will return any polling queries from the gateway here
uint8_t rsp[2] = {'H', 'i'};
switch (event) {
case CONNECTION_RESULTS:
String online_devices = "";
for (int i = 0; i < length; i++) {
if (data[i] == 0) {
online_devices +="O ";
} else {
online_devices +="X ";
gateway.send_peripheral_data(i, rsp, 2);
bluz_peripherals_available++;
}
}
Particle.publish("Bluz Devices Online", online_devices);
break;
}
}
void setup() {
gateway.init();
//only set this if you want the nrf51 central to not connect, recomended for Electron gateway
gateway.set_ble_local(true);
//register the callback functions
gateway.register_data_callback(handle_custom_data);
gateway.register_gateway_event(handle_gateway_event);
}
long timeToNextPoll = POLL_CONNECTIONS_INTERVAL;
void loop() {
gateway.loop();
if (millis() > timeToNextPoll) {
timeToNextPoll = POLL_CONNECTIONS_INTERVAL + millis();
gateway.poll_connections();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment