Skip to content

Commit 16339ac

Browse files
committed
WIP
1 parent d075d12 commit 16339ac

File tree

3 files changed

+115
-73
lines changed

3 files changed

+115
-73
lines changed

examples/OneOpenAir/OneOpenAir.ino

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
8787
#define I2C_SCL_PIN 6
8888
#define OLED_I2C_ADDR 0x3C
8989

90-
#include <Arduino.h>
91-
#include <NimBLEDevice.h>
92-
93-
static NimBLEServer *pServer;
94-
9590
/** Power pin */
9691
#define GPIO_POWER_MODULE_PIN 5
9792
#define GPIO_EXPANSION_CARD_POWER 4
@@ -173,8 +168,6 @@ AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, checkForFirmware
173168
AgSchedule networkSignalCheckSchedule(10000, networkSignalCheck);
174169
AgSchedule printMeasurementsSchedule(6000, printMeasurements);
175170

176-
static void setupBLE();
177-
178171
void setup() {
179172
/** Serial for print debug message */
180173
Serial.begin(115200);
@@ -221,11 +214,6 @@ void setup() {
221214
boardInit();
222215
setMeasurementMaxPeriod();
223216

224-
setupBLE();
225-
oledDisplay.setText("BT", "ON", "");
226-
Serial.println("Bluetooth server ready");
227-
while(1) {delay(100);}
228-
229217
bool connectToNetwork = true;
230218
if (ag->isOne()) { // Offline mode only available for indoor monitor
231219
/** Show message confirm offline mode, should me perform if LED bar button
@@ -1677,63 +1665,3 @@ void newMeasurementCycle() {
16771665
Serial.printf("Free heap: %u\n", ESP.getFreeHeap());
16781666
}
16791667
}
1680-
1681-
class ServerCallbacks : public NimBLEServerCallbacks {
1682-
void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) override {
1683-
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
1684-
}
1685-
1686-
void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) override {
1687-
Serial.printf("Client disconnected - start advertising\n");
1688-
NimBLEDevice::startAdvertising();
1689-
}
1690-
1691-
void onAuthenticationComplete(NimBLEConnInfo &connInfo) override {
1692-
Serial.println("\n========== PAIRING COMPLETE ==========");
1693-
Serial.printf("Peer Address: %s\n", connInfo.getAddress().toString().c_str());
1694-
1695-
Serial.printf("Encrypted: %s\n", connInfo.isEncrypted() ? "YES" : "NO");
1696-
Serial.printf("Authenticated: %s\n", connInfo.isAuthenticated() ? "YES" : "NO");
1697-
Serial.printf("Key Size: %d bits\n", connInfo.getSecKeySize() * 8);
1698-
1699-
Serial.println("======================================\n");
1700-
}
1701-
};
1702-
1703-
/** Handler class for characteristic actions */
1704-
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
1705-
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
1706-
Serial.printf("%s : onRead(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
1707-
pCharacteristic->getValue().c_str());
1708-
}
1709-
1710-
void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
1711-
Serial.printf("%s : onWrite(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
1712-
pCharacteristic->getValue().c_str());
1713-
}
1714-
};
1715-
1716-
void setupBLE() {
1717-
NimBLEDevice::init("AirGradient");
1718-
NimBLEDevice::setPower(3); /** +3db */
1719-
1720-
/** bonding, MITM, don't need BLE secure connections as we are using passkey pairing */
1721-
NimBLEDevice::setSecurityAuth(false, false, true);
1722-
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);
1723-
1724-
NimBLEServer *pServer = NimBLEDevice::createServer();
1725-
pServer->setCallbacks(new ServerCallbacks());
1726-
1727-
NimBLEService *pService = pServer->createService("acbcfea8-e541-4c40-9bfd-17820f16c95c");
1728-
NimBLECharacteristic *pSecureCharacteristic =
1729-
pService->createCharacteristic("703fa252-3d2a-4da9-a05c-83b0d9cacb8e",
1730-
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC |
1731-
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_ENC);
1732-
pSecureCharacteristic->setCallbacks(new CharacteristicCallbacks());
1733-
1734-
pService->start();
1735-
1736-
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
1737-
pAdvertising->addServiceUUID(pService->getUUID());
1738-
pAdvertising->start();
1739-
}

src/AgWiFiConnector.cpp

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,52 @@
11
#include "AgWiFiConnector.h"
2+
#include "Arduino.h"
23
#include "Libraries/WiFiManager/WiFiManager.h"
34

45
#define WIFI_CONNECT_COUNTDOWN_MAX 180
56
#define WIFI_HOTSPOT_PASSWORD_DEFAULT "cleanair"
67

78
#define WIFI() ((WiFiManager *)(this->wifi))
89

10+
static bool g_isBLEConnect = false;
11+
12+
13+
class ServerCallbacks : public NimBLEServerCallbacks {
14+
void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) override {
15+
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
16+
g_isBLEConnect = true;
17+
}
18+
19+
void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) override {
20+
Serial.printf("Client disconnected - start advertising\n");
21+
NimBLEDevice::startAdvertising();
22+
g_isBLEConnect = false;
23+
}
24+
25+
void onAuthenticationComplete(NimBLEConnInfo &connInfo) override {
26+
Serial.println("\n========== PAIRING COMPLETE ==========");
27+
Serial.printf("Peer Address: %s\n", connInfo.getAddress().toString().c_str());
28+
29+
Serial.printf("Encrypted: %s\n", connInfo.isEncrypted() ? "YES" : "NO");
30+
Serial.printf("Authenticated: %s\n", connInfo.isAuthenticated() ? "YES" : "NO");
31+
Serial.printf("Key Size: %d bits\n", connInfo.getSecKeySize() * 8);
32+
33+
Serial.println("======================================\n");
34+
}
35+
};
36+
37+
/** Handler class for characteristic actions */
38+
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
39+
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
40+
Serial.printf("%s : onRead(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
41+
pCharacteristic->getValue().c_str());
42+
}
43+
44+
void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
45+
Serial.printf("%s : onWrite(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
46+
pCharacteristic->getValue().c_str());
47+
}
48+
};
49+
950
/**
1051
* @brief Set reference AirGradient instance
1152
*
@@ -101,6 +142,11 @@ bool WifiConnector::connect(void) {
101142
[](void *obj) {
102143
WifiConnector *connector = (WifiConnector *)obj;
103144
while (connector->_wifiConfigPortalActive()) {
145+
if (g_isBLEConnect) {
146+
Serial.println("Stopping portal because BLE connected");
147+
connector->_wifiStop();
148+
break;
149+
}
104150
connector->_wifiProcess();
105151
vTaskDelay(1);
106152
}
@@ -113,9 +159,11 @@ bool WifiConnector::connect(void) {
113159
uint32_t ledPeriod = millis();
114160
bool clientConnectChanged = false;
115161

162+
setupBLE();
163+
116164
AgStateMachineState stateOld = sm.getDisplayState();
117165
while (WIFI()->getConfigPortalActive()) {
118-
/** LED animatoin and display update content */
166+
/** LED animation and display update content */
119167
if (WiFi.isConnected() == false) {
120168
/** Display countdown */
121169
uint32_t ms;
@@ -145,6 +193,8 @@ bool WifiConnector::connect(void) {
145193
clientConnectChanged = clientConnected;
146194
if (clientConnectChanged) {
147195
sm.handleLeds(AgStateMachineWiFiManagerPortalActive);
196+
Serial.println("Stopping BLE since wifi is connected");
197+
stopBLE();
148198
} else {
149199
sm.ledAnimationInit();
150200
sm.handleLeds(AgStateMachineWiFiManagerMode);
@@ -161,6 +211,26 @@ bool WifiConnector::connect(void) {
161211
_wifiProcess();
162212
#endif
163213

214+
while (1) {
215+
delay(1000);
216+
}
217+
218+
219+
/** Set wifi connect */
220+
WiFi.begin("hbonfam", "51burian");
221+
222+
/** Wait for wifi connect to AP */
223+
int count = 0;
224+
while (WiFi.status() != WL_CONNECTED) {
225+
delay(1000);
226+
count++;
227+
if (count >= 15) {
228+
logError("Try connect to default wifi \"" + String(this->defaultSsid) +
229+
String("\" failed"));
230+
break;
231+
}
232+
}
233+
164234
/** Show display wifi connect result failed */
165235
if (WiFi.isConnected() == false) {
166236
sm.handleLeds(AgStateMachineWiFiManagerConnectFailed);
@@ -248,6 +318,10 @@ bool WifiConnector::_wifiConfigPortalActive(void) {
248318
}
249319
void WifiConnector::_wifiTimeoutCallback(void) { connectorTimeout = true; }
250320

321+
void WifiConnector::_wifiStop() {
322+
WIFI()->stopConfigPortal();
323+
}
324+
251325
/**
252326
* @brief Process WiFiManager connection
253327
*
@@ -411,3 +485,34 @@ bool WifiConnector::isConfigurePorttalTimeout(void) { return connectorTimeout; }
411485
void WifiConnector::setDefault(void) {
412486
WiFi.begin("airgradient", "cleanair");
413487
}
488+
489+
490+
void WifiConnector::setupBLE() {
491+
NimBLEDevice::init("AirGradient");
492+
NimBLEDevice::setPower(3); /** +3db */
493+
494+
/** bonding, MITM, don't need BLE secure connections as we are using passkey pairing */
495+
NimBLEDevice::setSecurityAuth(false, false, true);
496+
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);
497+
498+
NimBLEServer *pServer = NimBLEDevice::createServer();
499+
pServer->setCallbacks(new ServerCallbacks());
500+
501+
NimBLEService *pService = pServer->createService("acbcfea8-e541-4c40-9bfd-17820f16c95c");
502+
NimBLECharacteristic *pSecureCharacteristic =
503+
pService->createCharacteristic("703fa252-3d2a-4da9-a05c-83b0d9cacb8e",
504+
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC |
505+
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_ENC);
506+
pSecureCharacteristic->setCallbacks(new CharacteristicCallbacks());
507+
508+
pService->start();
509+
510+
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
511+
pAdvertising->addServiceUUID(pService->getUUID());
512+
pAdvertising->start();
513+
}
514+
515+
void WifiConnector::stopBLE() {
516+
NimBLEDevice::deinit();
517+
518+
}

src/AgWiFiConnector.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
#include "AirGradient.h"
77
#include "AgConfigure.h"
88
#include "Main/PrintLog.h"
9+
#include "NimBLECharacteristic.h"
10+
#include "NimBLEService.h"
911

1012
#include <Arduino.h>
13+
#include <NimBLEDevice.h>
1114

1215
class WifiConnector : public PrintLog {
1316
private:
@@ -25,6 +28,11 @@ class WifiConnector : public PrintLog {
2528

2629
bool wifiClientConnected(void);
2730

31+
void setupBLE();
32+
void stopBLE();
33+
34+
NimBLEServer *pServer;
35+
2836
public:
2937
void setAirGradient(AirGradient *ag);
3038

@@ -39,6 +47,7 @@ class WifiConnector : public PrintLog {
3947
void _wifiSaveParamCallback(void);
4048
bool _wifiConfigPortalActive(void);
4149
void _wifiTimeoutCallback(void);
50+
void _wifiStop();
4251
void _wifiProcess();
4352
bool isConnected(void);
4453
void reset(void);

0 commit comments

Comments
 (0)