testing BLE discovery
Some checks failed
Intercity Express Build / build (ubuntu-latest) (push) Successful in 8m36s
Intercity Express Build / build (macos-latest) (push) Has been cancelled

This commit is contained in:
2023-08-19 18:23:36 +02:00
parent 121dec906c
commit 01ec69bb0c
13 changed files with 296 additions and 3 deletions

View File

@@ -21,7 +21,7 @@ monitor_speed = 115200
[env:lilygo-t-amoled] [env:lilygo-t-amoled]
board = lilygo-t-amoled board = lilygo-t-amoled
build_flags = build_flags =
-D CORE_DEBUG_LEVEL=5 -D CORE_DEBUG_LEVEL=4
-D ESP32S3 -D ESP32S3
-D LILYGOTAMOLED -D LILYGOTAMOLED
-D BOARD_HAS_PSRAM -D BOARD_HAS_PSRAM
@@ -33,3 +33,4 @@ lib_deps =
me-no-dev/AsyncTCP @ ^1.1.1 me-no-dev/AsyncTCP @ ^1.1.1
lovyan03/LovyanGFX @ ^1.1.8 lovyan03/LovyanGFX @ ^1.1.8
lvgl/lvgl @ ^8.3.9 lvgl/lvgl @ ^8.3.9
rpolitex/ArduinoNvs @ ^2.5.0

View File

@@ -19,7 +19,7 @@
#pragma once #pragma once
#include "stdint.h" #include "stdint.h"
#include "pins_config.h" #include "pins.h"
#define TFT_MADCTL 0x36 #define TFT_MADCTL 0x36
#define TFT_MAD_MY 0x80 #define TFT_MAD_MY 0x80

View File

@@ -16,13 +16,22 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
#include <Arduino.h> #include "main.h"
#include <Arduino.h>
#ifdef LILYGOTAMOLED #ifdef LILYGOTAMOLED
#include "gfx/lv_setup.h" #include "gfx/lv_setup.h"
#include <lvgl.h> #include <lvgl.h>
#endif #endif
#include <ArduinoNvs.h>
#include <services/ble/discovery_service.h>
void loadSettings()
{
/// @todo load settings from NVS
}
void setup() void setup()
{ {
#ifdef LILYGOTAMOLED #ifdef LILYGOTAMOLED
@@ -43,6 +52,11 @@ void setup()
lv_label_set_text(label2, "It is a circularly scrolling text. "); lv_label_set_text(label2, "It is a circularly scrolling text. ");
lv_obj_align(label2, LV_ALIGN_CENTER, 0, 40); lv_obj_align(label2, LV_ALIGN_CENTER, 0, 40);
#endif #endif
NVS.begin();
loadSettings();
BLE::startAdvertising();
} }
void loop() void loop()

1
src/main.h Normal file
View File

@@ -0,0 +1 @@
#pragma once

View File

@@ -0,0 +1,34 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#include "battery_service.h"
namespace BLE
{
BLEService *createBatteryService(BLEServer *pServer)
{
auto *pService = pServer->createService(BLEUUID((uint16_t)0x180F));
auto *pBatteryLevel = pService->createCharacteristic(BLEUUID((uint16_t)0x2A19), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
/// TODO: Add battery level
auto value = esp_random() % 100;
pBatteryLevel->setValue(value);
pService->start();
return pService;
}
} // namespace BLE

View File

@@ -0,0 +1,26 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#pragma once
#include <BLEServer.h>
namespace BLE
{
BLEService *createBatteryService(BLEServer *pServer);
} // namespace BLE

View File

@@ -0,0 +1,56 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#include "discovery_service.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <string>
#include <WiFi.h>
#include <BLE2902.h>
#include "battery_service.h"
#include "osrw_service.h"
#include "server_callback.h"
namespace BLE
{
void startAdvertising()
{
uint8_t mac[6];
auto pMac = WiFi.macAddress(mac);
char *name = new char[18];
sprintf(name, "OSRW-%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
BLEDevice::init(name);
auto *pServer = BLEDevice::createServer();
pServer->setCallbacks(new ServerCallbacks());
auto *pService = createOSRWService(pServer);
auto *pBatteryService = createBatteryService(pServer);
auto *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(pService->getUUID());
pAdvertising->addServiceUUID(pBatteryService->getUUID());
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
}
} // namespace BLE

View File

@@ -0,0 +1,24 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#pragma once
namespace BLE
{
void startAdvertising();
} // namespace BLE

View File

@@ -0,0 +1,42 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#include "osrw_service.h"
#include <BLE2902.h>
#define OSRW_SERVICE_UUID "B7C28FEE-ABD3-4841-ABC8-A686B68C82E2"
#define NAME_CHARACTERISTIC_UUID "5A3973F9-79B5-4D24-A76C-BBA81986A26B"
// 5A3973F9-79B5-4D24-A76C-BBA81986A26B
// 7D616E83-645F-4D2B-B9D5-1151F3CE4133
// 51B849E4-C15E-4E13-8D42-9A642862DBF4
// C2690528-B2D2-4E3E-861C-B371D4852FE4
namespace BLE
{
BLEService *createOSRWService(BLEServer *pServer)
{
auto *pService = pServer->createService(OSRW_SERVICE_UUID);
auto *pName = pService->createCharacteristic(NAME_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ);
pName->setValue("ICE 3");
pService->start();
return pService;
}
} // namespace BLE

View File

@@ -0,0 +1,26 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#pragma once
#include <BLEServer.h>
namespace BLE
{
BLEService *createOSRWService(BLEServer *pServer);
} // namespace BLE

View File

@@ -0,0 +1,38 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#include "server_callback.h"
#include <esp_log.h>
namespace BLE
{
void ServerCallbacks::onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param)
{
ESP_LOGD("ble", "BLE client connected");
}
void ServerCallbacks::onDisconnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param)
{
ESP_LOGD("ble", "BLE client disconnected");
}
void ServerCallbacks::onMtuChanged(BLEServer *pServer, esp_ble_gatts_cb_param_t *param)
{
ESP_LOGD("ble", "BLE client MTU changed");
}
} // namespace BLE

View File

@@ -0,0 +1,31 @@
//----------------------------------------------------------------------------
// OS-Railway - Intercity Express Train
// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//----------------------------------------------------------------------------
#pragma once
#include <BLEServer.h>
namespace BLE
{
class ServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;
void onDisconnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;
void onMtuChanged(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;
};
} // namespace BLE