Created
July 5, 2019 20:08
-
-
Save cpq/d01d503814db7baec9b2effd72ffe17d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mDash.h> | |
#include <BLEDevice.h> | |
#include <BLEUtils.h> | |
#include <BLEScan.h> | |
#include <BLEAdvertisedDevice.h> | |
#define WIFI_NAME "XX" | |
#define WIFI_PASS "YY" | |
#define DEVICE_ID "d1" | |
#define DEVICE_TOKEN "ZZ" | |
int scanTime = 5; //In seconds | |
BLEScan* pBLEScan; | |
static const char *s_eir[] = { | |
"??", /* 0x00 */ | |
"flags", /* 0x01 */ | |
"uuid16_incomplete", /* 0x02 */ | |
"uuid16_complete", /* 0x03 */ | |
"uuid32_incomplete", /* 0x04 */ | |
"uuid32_complete", /* 0x05 */ | |
"uuid128_incomplete", /* 0x06 */ | |
"uuid128_complete", /* 0x07 */ | |
"short_name", /* 0x08 */ | |
"full_name", /* 0x09 */ | |
"tx_power_level", /* 0x0a */ | |
"??", /* 0x0b */ | |
"??", /* 0x0c */ | |
"device_class", /* 0x0d */ | |
"pairing_hash", /* 0x0e */ | |
"pairing_randomizer", /* 0x0f */ | |
"device_id", /* 0x10 */ | |
"oob_flags", /* 0x11 */ | |
"slave_conn_interval_range", /* 0x12 */ | |
"??", /* 0x13 */ | |
"solicitation_uuid16", /* 0x14 */ | |
"solicitation_uuid128", /* 0x15 */ | |
"service_data", /* 0x16 */ | |
"public_target_address", /* 0x17 */ | |
"random_target_address", /* 0x18 */ | |
"appearance", /* 0x19 */ | |
"adv_interval", /* 0x1a */ | |
"ble_device_address", /* 0x1b */ | |
"le_role", /* 0x1c */ | |
"paring_hash_256", /* 0x1d */ | |
"paring_rand_256", /* 0x1e */ | |
"solicitation_uuid32", /* 0x1f */ | |
"service_data_uuid32", /* 0x20 */ | |
"service_data_uuid128", /* 0x21 */ | |
"le_secure_confirm_value", /* 0x22 */ | |
"le_secure_random_value", /* 0x23 */ | |
"URI", /* 0x24 */ | |
"indoor_position", /* 0x25 */ | |
"transport_discovery_data", /* 0x26 */ | |
"le_supported_features", /* 0x27 */ | |
"channel_map_update_ind", /* 0x28 */ | |
"pb-adv", /* 0x29 */ | |
"mesh_message", /* 0x2a */ | |
"mesh_beacon", /* 0x2b */ | |
}; | |
static int print_parsed_adv_data(struct mjson_out *out, va_list *ap) { | |
int n = 0, datalen = va_arg(*ap, int); | |
const unsigned char *data = va_arg(*ap, const unsigned char *); | |
const char *comma = ""; | |
for (int i = 0; i < datalen && data[i] > 0; i += data[i] + 1) { | |
int type = data[i + 1], len = data[i]; | |
const char *name = "??"; | |
if (len + i + 2 > datalen) len = datalen - (i + 2); | |
if (type == 0x3d) name = "3d_information_data"; | |
if (type == 0xff) name = "manufacturer_specific_data"; | |
if (type < sizeof(s_eir) / sizeof(s_eir[0])) name = s_eir[type]; | |
if (type == 8 || type == 9) { | |
n += mjson_printf(out, "%s{\"type\":%d,\"name\":%Q,\"value\":%.*Q}", | |
comma, type, name, len - 1, data + i + 2); | |
} else { | |
n += mjson_printf(out, "%s{\"type\":%d,\"name\":%Q,\"value\":%H}", comma, | |
type, name, len - 1, data + i + 2); | |
} | |
comma = ","; | |
} | |
return n; | |
} | |
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { | |
void onResult(BLEAdvertisedDevice d) { | |
Serial.printf("Advertised Device: %s \n", d.toString().c_str()); | |
mDashPublish("data/" DEVICE_ID, "{\"mac\":%Q,\"adv\":%H,\"parsed\":[%M]}", | |
d.getAddress().toString().c_str(), | |
(int) d.getPayloadLength(), d.getPayload(), | |
print_parsed_adv_data, (int) d.getPayloadLength(), | |
d.getPayload()); | |
} | |
}; | |
void setup() { | |
Serial.begin(115200); | |
mDashSetServer("mdash.net", 1883); | |
mDashStartWithWifi(WIFI_NAME, WIFI_PASS, DEVICE_ID, DEVICE_TOKEN); | |
BLEDevice::init(""); | |
pBLEScan = BLEDevice::getScan(); //create new scan | |
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); | |
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster | |
pBLEScan->setInterval(100); | |
pBLEScan->setWindow(99); // less or equal setInterval value | |
} | |
void loop() { | |
BLEScanResults foundDevices = pBLEScan->start(scanTime, false); | |
pBLEScan->clearResults(); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment