mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-12-17 02:58:44 +01:00
WIP
This commit is contained in:
@@ -87,11 +87,6 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
|||||||
#define I2C_SCL_PIN 6
|
#define I2C_SCL_PIN 6
|
||||||
#define OLED_I2C_ADDR 0x3C
|
#define OLED_I2C_ADDR 0x3C
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <NimBLEDevice.h>
|
|
||||||
|
|
||||||
static NimBLEServer *pServer;
|
|
||||||
|
|
||||||
/** Power pin */
|
/** Power pin */
|
||||||
#define GPIO_POWER_MODULE_PIN 5
|
#define GPIO_POWER_MODULE_PIN 5
|
||||||
#define GPIO_EXPANSION_CARD_POWER 4
|
#define GPIO_EXPANSION_CARD_POWER 4
|
||||||
@@ -173,8 +168,6 @@ AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, checkForFirmware
|
|||||||
AgSchedule networkSignalCheckSchedule(10000, networkSignalCheck);
|
AgSchedule networkSignalCheckSchedule(10000, networkSignalCheck);
|
||||||
AgSchedule printMeasurementsSchedule(6000, printMeasurements);
|
AgSchedule printMeasurementsSchedule(6000, printMeasurements);
|
||||||
|
|
||||||
static void setupBLE();
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
/** Serial for print debug message */
|
/** Serial for print debug message */
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -221,11 +214,6 @@ void setup() {
|
|||||||
boardInit();
|
boardInit();
|
||||||
setMeasurementMaxPeriod();
|
setMeasurementMaxPeriod();
|
||||||
|
|
||||||
setupBLE();
|
|
||||||
oledDisplay.setText("BT", "ON", "");
|
|
||||||
Serial.println("Bluetooth server ready");
|
|
||||||
while(1) {delay(100);}
|
|
||||||
|
|
||||||
bool connectToNetwork = true;
|
bool connectToNetwork = true;
|
||||||
if (ag->isOne()) { // Offline mode only available for indoor monitor
|
if (ag->isOne()) { // Offline mode only available for indoor monitor
|
||||||
/** Show message confirm offline mode, should me perform if LED bar button
|
/** Show message confirm offline mode, should me perform if LED bar button
|
||||||
@@ -1677,63 +1665,3 @@ void newMeasurementCycle() {
|
|||||||
Serial.printf("Free heap: %u\n", ESP.getFreeHeap());
|
Serial.printf("Free heap: %u\n", ESP.getFreeHeap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ServerCallbacks : public NimBLEServerCallbacks {
|
|
||||||
void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) override {
|
|
||||||
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) override {
|
|
||||||
Serial.printf("Client disconnected - start advertising\n");
|
|
||||||
NimBLEDevice::startAdvertising();
|
|
||||||
}
|
|
||||||
|
|
||||||
void onAuthenticationComplete(NimBLEConnInfo &connInfo) override {
|
|
||||||
Serial.println("\n========== PAIRING COMPLETE ==========");
|
|
||||||
Serial.printf("Peer Address: %s\n", connInfo.getAddress().toString().c_str());
|
|
||||||
|
|
||||||
Serial.printf("Encrypted: %s\n", connInfo.isEncrypted() ? "YES" : "NO");
|
|
||||||
Serial.printf("Authenticated: %s\n", connInfo.isAuthenticated() ? "YES" : "NO");
|
|
||||||
Serial.printf("Key Size: %d bits\n", connInfo.getSecKeySize() * 8);
|
|
||||||
|
|
||||||
Serial.println("======================================\n");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Handler class for characteristic actions */
|
|
||||||
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
|
|
||||||
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
|
|
||||||
Serial.printf("%s : onRead(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
|
|
||||||
pCharacteristic->getValue().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
|
|
||||||
Serial.printf("%s : onWrite(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
|
|
||||||
pCharacteristic->getValue().c_str());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void setupBLE() {
|
|
||||||
NimBLEDevice::init("AirGradient");
|
|
||||||
NimBLEDevice::setPower(3); /** +3db */
|
|
||||||
|
|
||||||
/** bonding, MITM, don't need BLE secure connections as we are using passkey pairing */
|
|
||||||
NimBLEDevice::setSecurityAuth(false, false, true);
|
|
||||||
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);
|
|
||||||
|
|
||||||
NimBLEServer *pServer = NimBLEDevice::createServer();
|
|
||||||
pServer->setCallbacks(new ServerCallbacks());
|
|
||||||
|
|
||||||
NimBLEService *pService = pServer->createService("acbcfea8-e541-4c40-9bfd-17820f16c95c");
|
|
||||||
NimBLECharacteristic *pSecureCharacteristic =
|
|
||||||
pService->createCharacteristic("703fa252-3d2a-4da9-a05c-83b0d9cacb8e",
|
|
||||||
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC |
|
|
||||||
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_ENC);
|
|
||||||
pSecureCharacteristic->setCallbacks(new CharacteristicCallbacks());
|
|
||||||
|
|
||||||
pService->start();
|
|
||||||
|
|
||||||
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
|
||||||
pAdvertising->addServiceUUID(pService->getUUID());
|
|
||||||
pAdvertising->start();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "AgWiFiConnector.h"
|
#include "AgWiFiConnector.h"
|
||||||
|
#include "Arduino.h"
|
||||||
#include "Libraries/WiFiManager/WiFiManager.h"
|
#include "Libraries/WiFiManager/WiFiManager.h"
|
||||||
|
|
||||||
#define WIFI_CONNECT_COUNTDOWN_MAX 180
|
#define WIFI_CONNECT_COUNTDOWN_MAX 180
|
||||||
@@ -6,6 +7,46 @@
|
|||||||
|
|
||||||
#define WIFI() ((WiFiManager *)(this->wifi))
|
#define WIFI() ((WiFiManager *)(this->wifi))
|
||||||
|
|
||||||
|
static bool g_isBLEConnect = false;
|
||||||
|
|
||||||
|
|
||||||
|
class ServerCallbacks : public NimBLEServerCallbacks {
|
||||||
|
void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) override {
|
||||||
|
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
|
||||||
|
g_isBLEConnect = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) override {
|
||||||
|
Serial.printf("Client disconnected - start advertising\n");
|
||||||
|
NimBLEDevice::startAdvertising();
|
||||||
|
g_isBLEConnect = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onAuthenticationComplete(NimBLEConnInfo &connInfo) override {
|
||||||
|
Serial.println("\n========== PAIRING COMPLETE ==========");
|
||||||
|
Serial.printf("Peer Address: %s\n", connInfo.getAddress().toString().c_str());
|
||||||
|
|
||||||
|
Serial.printf("Encrypted: %s\n", connInfo.isEncrypted() ? "YES" : "NO");
|
||||||
|
Serial.printf("Authenticated: %s\n", connInfo.isAuthenticated() ? "YES" : "NO");
|
||||||
|
Serial.printf("Key Size: %d bits\n", connInfo.getSecKeySize() * 8);
|
||||||
|
|
||||||
|
Serial.println("======================================\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Handler class for characteristic actions */
|
||||||
|
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
|
||||||
|
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
|
||||||
|
Serial.printf("%s : onRead(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
|
||||||
|
pCharacteristic->getValue().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
|
||||||
|
Serial.printf("%s : onWrite(), value: %s\n", pCharacteristic->getUUID().toString().c_str(),
|
||||||
|
pCharacteristic->getValue().c_str());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set reference AirGradient instance
|
* @brief Set reference AirGradient instance
|
||||||
*
|
*
|
||||||
@@ -101,6 +142,11 @@ bool WifiConnector::connect(void) {
|
|||||||
[](void *obj) {
|
[](void *obj) {
|
||||||
WifiConnector *connector = (WifiConnector *)obj;
|
WifiConnector *connector = (WifiConnector *)obj;
|
||||||
while (connector->_wifiConfigPortalActive()) {
|
while (connector->_wifiConfigPortalActive()) {
|
||||||
|
if (g_isBLEConnect) {
|
||||||
|
Serial.println("Stopping portal because BLE connected");
|
||||||
|
connector->_wifiStop();
|
||||||
|
break;
|
||||||
|
}
|
||||||
connector->_wifiProcess();
|
connector->_wifiProcess();
|
||||||
vTaskDelay(1);
|
vTaskDelay(1);
|
||||||
}
|
}
|
||||||
@@ -113,9 +159,11 @@ bool WifiConnector::connect(void) {
|
|||||||
uint32_t ledPeriod = millis();
|
uint32_t ledPeriod = millis();
|
||||||
bool clientConnectChanged = false;
|
bool clientConnectChanged = false;
|
||||||
|
|
||||||
|
setupBLE();
|
||||||
|
|
||||||
AgStateMachineState stateOld = sm.getDisplayState();
|
AgStateMachineState stateOld = sm.getDisplayState();
|
||||||
while (WIFI()->getConfigPortalActive()) {
|
while (WIFI()->getConfigPortalActive()) {
|
||||||
/** LED animatoin and display update content */
|
/** LED animation and display update content */
|
||||||
if (WiFi.isConnected() == false) {
|
if (WiFi.isConnected() == false) {
|
||||||
/** Display countdown */
|
/** Display countdown */
|
||||||
uint32_t ms;
|
uint32_t ms;
|
||||||
@@ -145,6 +193,8 @@ bool WifiConnector::connect(void) {
|
|||||||
clientConnectChanged = clientConnected;
|
clientConnectChanged = clientConnected;
|
||||||
if (clientConnectChanged) {
|
if (clientConnectChanged) {
|
||||||
sm.handleLeds(AgStateMachineWiFiManagerPortalActive);
|
sm.handleLeds(AgStateMachineWiFiManagerPortalActive);
|
||||||
|
Serial.println("Stopping BLE since wifi is connected");
|
||||||
|
stopBLE();
|
||||||
} else {
|
} else {
|
||||||
sm.ledAnimationInit();
|
sm.ledAnimationInit();
|
||||||
sm.handleLeds(AgStateMachineWiFiManagerMode);
|
sm.handleLeds(AgStateMachineWiFiManagerMode);
|
||||||
@@ -161,6 +211,26 @@ bool WifiConnector::connect(void) {
|
|||||||
_wifiProcess();
|
_wifiProcess();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Set wifi connect */
|
||||||
|
WiFi.begin("hbonfam", "51burian");
|
||||||
|
|
||||||
|
/** Wait for wifi connect to AP */
|
||||||
|
int count = 0;
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(1000);
|
||||||
|
count++;
|
||||||
|
if (count >= 15) {
|
||||||
|
logError("Try connect to default wifi \"" + String(this->defaultSsid) +
|
||||||
|
String("\" failed"));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Show display wifi connect result failed */
|
/** Show display wifi connect result failed */
|
||||||
if (WiFi.isConnected() == false) {
|
if (WiFi.isConnected() == false) {
|
||||||
sm.handleLeds(AgStateMachineWiFiManagerConnectFailed);
|
sm.handleLeds(AgStateMachineWiFiManagerConnectFailed);
|
||||||
@@ -248,6 +318,10 @@ bool WifiConnector::_wifiConfigPortalActive(void) {
|
|||||||
}
|
}
|
||||||
void WifiConnector::_wifiTimeoutCallback(void) { connectorTimeout = true; }
|
void WifiConnector::_wifiTimeoutCallback(void) { connectorTimeout = true; }
|
||||||
|
|
||||||
|
void WifiConnector::_wifiStop() {
|
||||||
|
WIFI()->stopConfigPortal();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Process WiFiManager connection
|
* @brief Process WiFiManager connection
|
||||||
*
|
*
|
||||||
@@ -411,3 +485,34 @@ bool WifiConnector::isConfigurePorttalTimeout(void) { return connectorTimeout; }
|
|||||||
void WifiConnector::setDefault(void) {
|
void WifiConnector::setDefault(void) {
|
||||||
WiFi.begin("airgradient", "cleanair");
|
WiFi.begin("airgradient", "cleanair");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void WifiConnector::setupBLE() {
|
||||||
|
NimBLEDevice::init("AirGradient");
|
||||||
|
NimBLEDevice::setPower(3); /** +3db */
|
||||||
|
|
||||||
|
/** bonding, MITM, don't need BLE secure connections as we are using passkey pairing */
|
||||||
|
NimBLEDevice::setSecurityAuth(false, false, true);
|
||||||
|
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);
|
||||||
|
|
||||||
|
NimBLEServer *pServer = NimBLEDevice::createServer();
|
||||||
|
pServer->setCallbacks(new ServerCallbacks());
|
||||||
|
|
||||||
|
NimBLEService *pService = pServer->createService("acbcfea8-e541-4c40-9bfd-17820f16c95c");
|
||||||
|
NimBLECharacteristic *pSecureCharacteristic =
|
||||||
|
pService->createCharacteristic("703fa252-3d2a-4da9-a05c-83b0d9cacb8e",
|
||||||
|
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC |
|
||||||
|
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_ENC);
|
||||||
|
pSecureCharacteristic->setCallbacks(new CharacteristicCallbacks());
|
||||||
|
|
||||||
|
pService->start();
|
||||||
|
|
||||||
|
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
||||||
|
pAdvertising->addServiceUUID(pService->getUUID());
|
||||||
|
pAdvertising->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiConnector::stopBLE() {
|
||||||
|
NimBLEDevice::deinit();
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,8 +6,11 @@
|
|||||||
#include "AirGradient.h"
|
#include "AirGradient.h"
|
||||||
#include "AgConfigure.h"
|
#include "AgConfigure.h"
|
||||||
#include "Main/PrintLog.h"
|
#include "Main/PrintLog.h"
|
||||||
|
#include "NimBLECharacteristic.h"
|
||||||
|
#include "NimBLEService.h"
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <NimBLEDevice.h>
|
||||||
|
|
||||||
class WifiConnector : public PrintLog {
|
class WifiConnector : public PrintLog {
|
||||||
private:
|
private:
|
||||||
@@ -25,6 +28,11 @@ private:
|
|||||||
|
|
||||||
bool wifiClientConnected(void);
|
bool wifiClientConnected(void);
|
||||||
|
|
||||||
|
void setupBLE();
|
||||||
|
void stopBLE();
|
||||||
|
|
||||||
|
NimBLEServer *pServer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setAirGradient(AirGradient *ag);
|
void setAirGradient(AirGradient *ag);
|
||||||
|
|
||||||
@@ -39,6 +47,7 @@ public:
|
|||||||
void _wifiSaveParamCallback(void);
|
void _wifiSaveParamCallback(void);
|
||||||
bool _wifiConfigPortalActive(void);
|
bool _wifiConfigPortalActive(void);
|
||||||
void _wifiTimeoutCallback(void);
|
void _wifiTimeoutCallback(void);
|
||||||
|
void _wifiStop();
|
||||||
void _wifiProcess();
|
void _wifiProcess();
|
||||||
bool isConnected(void);
|
bool isConnected(void);
|
||||||
void reset(void);
|
void reset(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user