Skip to content

Commit

Permalink
Initial conversion to NimBLE
Browse files Browse the repository at this point in the history
  • Loading branch information
thorrak committed Apr 29, 2020
1 parent c199cbd commit fc51dc0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# The `CMakeListsUser.txt` will not be overwritten by PlatformIO.

cmake_minimum_required(VERSION 3.2)

project("tiltbridge")

include(CMakeListsPrivate.txt)
Expand Down
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ build_flags =

lib_deps =
https://github.com/tzapu/WiFiManager#development
https://github.com/h2zero/NimBLE-Arduino.git



Expand Down Expand Up @@ -75,6 +76,7 @@ board_build.partitions = ${common.board_build.partitions}
build_flags =
${common.build_flags}
-DLCD_TFT
; -DCORE_DEBUG_LEVEL=5
lib_deps =
${common.lib_deps}
Adafruit GFX Library
Expand Down
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//


//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
//#include "esp_log.h"

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -70,6 +72,10 @@ void setup() {
// enough as-is.
// lcd.display_logo(); // Display the Fermentrack logo

// esp_log_level_set("*", ESP_LOG_DEBUG); // set all components to DEBUG level
// esp_log_level_set("wifi", ESP_LOG_WARN); // enable WARN logs from WiFi stack
// esp_log_level_set("dhcpc", ESP_LOG_WARN); // enable WARN logs from DHCP client

// Initialize the BLE scanner
tilt_scanner.init();
tilt_scanner.scan();
Expand Down
1 change: 1 addition & 0 deletions src/sendData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ bool dataSendHandler::send_to_url_https(const char *url, const char *apiKey, con
Serial.printf("[HTTPS] Pre-deinit RAM left %d\r\n", esp_get_free_heap_size());
#endif
// We're severely memory starved. Deinitialize bluetooth and free the related memory
// NOTE - This is not strictly true under NimBLE. Deinit now only waits for a scan to complete before
tilt_scanner.deinit();
yield();

Expand Down
48 changes: 33 additions & 15 deletions src/tilt/tiltScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
// Created by John Beeler on 5/12/18.
//

#include "tiltBridge.h"
#include "tiltHydrometer.h"
#include "tiltScanner.h"

#ifdef BLE_PRINT_ALL_DEVICES
#include <Arduino.h>
#endif

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <NimBLEDevice.h>
#include <NimBLEUtils.h>
#include <NimBLEScan.h>
#include <NimBLEAdvertisedDevice.h>


// Create the scanner
Expand All @@ -26,15 +27,23 @@ tiltScanner tilt_scanner;
// BLE Scanner Callbacks/Code
////////////////////////////

void MyAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice advertisedDevice) {
void MyAdvertisedDeviceCallbacks::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
// uint8_t color = tilt_scanner.load_tilt_from_advert_hex(advertisedDevice.getManufacturerData());
tilt_scanner.load_tilt_from_advert_hex(advertisedDevice.getManufacturerData());

if(advertisedDevice->getManufacturerData().length() > 4) {
if(advertisedDevice->getManufacturerData()[0] == 0x4c && advertisedDevice->getManufacturerData()[1] == 0x00 &&
advertisedDevice->getManufacturerData()[2] == 0x02 && advertisedDevice->getManufacturerData()[3] == 0x15) {
#if defined(BLE_PRINT_ALL_DEVICES) && defined(DEBUG_PRINTS)
Serial.printf("Advertised Device: %s \r\n", advertisedDevice.toString().c_str());
Serial.printf("Advertised iBeacon Device: %s ", advertisedDevice->toString().c_str());
Serial.println();
#endif
tilt_scanner.load_tilt_from_advert_hex(advertisedDevice->getManufacturerData());
}
}
}

static void ble_scan_complete(BLEScanResults scanResults) {
static void ble_scan_complete(NimBLEScanResults scanResults) {
// Todo - Decide if it makes sense to process scan results here rather than on a callback
tilt_scanner.set_scan_active_flag(false);
}

Expand All @@ -57,18 +66,23 @@ tiltScanner::tiltScanner() {


void tiltScanner::init() {
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
NimBLEDevice::init("");
pBLEScan = NimBLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(callbacks);
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
// CHANGE IN NIMBLE - Active Scan must be set false to read new (Gen 2/3) Tilts
// See https://github.com/h2zero/NimBLE-Arduino/issues/22
pBLEScan->setActiveScan(false); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}

void tiltScanner::deinit() {
//pBLEScan->stop();
wait_until_scan_complete();
BLEDevice::deinit(false); // Deinitialize the scanner & release memory
// NimBLE fails to reinitialize after a call to deinit() (but thankfully it's light enough weight that we don't
// have to call deinit to use https any longer)
// https://github.com/h2zero/NimBLE-Arduino/issues/23
//NimBLEDevice::deinit(); // Deinitialize the scanner & release memory
}


Expand All @@ -81,7 +95,7 @@ bool tiltScanner::scan() {
// Set a flag when we start asynchronously scanning to prevent multiple scans from being launched
if(!m_scan_active) {
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
pBLEScan->start(BLE_SCAN_TIME, ble_scan_complete);
pBLEScan->start(BLE_SCAN_TIME, ble_scan_complete, true);
m_scan_active = true;
return true;
}
Expand Down Expand Up @@ -110,9 +124,12 @@ uint8_t tiltScanner::load_tilt_from_advert_hex(const std::string& advert_string_
// std::string m_end_string;
uint8_t m_color;

// Check that this is an iBeacon packet
if(advert_string_hex[0] != 0x4c || advert_string_hex[1] != 0x00 || advert_string_hex[2] != 0x02 || advert_string_hex[3] != 0x15)
return TILT_NONE;

// There is almost certainly a better way to do this
// TODO - Rewrite this to not cast the grav/temp as a string & then recast it as an int
char *hex_cstr = BLEUtils::buildHexData(nullptr, (uint8_t*)advert_string_hex.data(), advert_string_hex.length());
char *hex_cstr = NimBLEUtils::buildHexData(nullptr, (uint8_t*)advert_string_hex.data(), advert_string_hex.length());
ss.str(hex_cstr);
free(hex_cstr);
advert_string = ss.str();
Expand All @@ -133,6 +150,7 @@ uint8_t tiltScanner::load_tilt_from_advert_hex(const std::string& advert_string_
if(m_color == TILT_NONE) // We didn't match the uuid to a color (should only happen if new colors are released)
return TILT_NONE;

// TODO - Rewrite this to not cast the grav/temp as a string & then recast it as an int
uint32_t temp = std::stoul(advert_string.substr(40,4), nullptr, 16);
uint32_t gravity = std::stoul(advert_string.substr(44,4), nullptr, 16);
// m_end_string = advert_string.substr(48,2); // first byte is txpower, second is RSSI
Expand Down
6 changes: 3 additions & 3 deletions src/tilt/tiltScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "tiltHydrometer.h"
#include <nlohmann/json.hpp>
#include <BLEAdvertisedDevice.h>
#include <NimBLEAdvertisedDevice.h>



Expand All @@ -18,8 +18,8 @@
using json = nlohmann::json;


class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) override;
class MyAdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
};


Expand Down
1 change: 1 addition & 0 deletions src/tiltBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

// Enable this for testing
//#define DEBUG_PRINTS 1
//#define BLE_PRINT_ALL_DEVICES 1

// This is for a "heartbeat" checkin to fermentrack.com. Unless you are me (thorrak) don't enable this, please.
//#define ENABLE_TEST_CHECKINS 1
Expand Down

0 comments on commit fc51dc0

Please sign in to comment.