Ok, I have simplified my original sketch to a much shorter and readable version, this is what I’m currently trying on, both interfaces gets connected the ble streams the data and I can also receive from lora, but they got blocked by each one.
#include "BLEDevice.h"
#include "BLERemoteCharacteristic.h"
#include <Arduino.h>
#include "LoRaWan_APP.h"
TaskHandle_t lora_task1;
TaskHandle_t lora_task2;
TaskHandle_t ble_task;
/*
LORA CONFIGURATION
*/
#define RF_FREQUENCY 915000000 // Hz
#define TX_OUTPUT_POWER 14 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 30 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
int16_t txNumber;
int16_t rssi,rxSize;
bool lora_idle = true;
/*
LORA CONFIGURATION END
*/
#define ppgData
#define BLE_server "PPG" // starts with PPG
// the sensor service UUID
static BLEUUID FFserviceUUID("0000f0f0-0000-1000-8000-008000000000");
// The characteristic of the remote service we are interested in.
static BLEUUID FFcharUUID("0000f0f1-0000-1000-8000-00805f9b34fb");
static boolean doConnect = false;
static boolean connected = false;
//static BLEAddress* pServerAddress;
static BLEAdvertisedDevice* pServerAddress;
// last time we scanned for devices
static uint32_t last_scan = 0;
static void notifyCallback(
BLERemoteCharacteristic * pBLERemoteCharacteristic,
uint8_t * pData,
size_t length,
bool isNotify) {
String string_data;
for (size_t i = 0; i < length; i++) {
if (pData[i] < 0x10) {
string_data += "0";
}
string_data += String(pData[i], HEX);
}
// print the string_data
Serial.print(string_data);
Serial.println("");
}
bool connect_ble_erver() {
Serial.println("Connecting to server..");
bool bt_status = false;
BLEClient* pClient = BLEDevice::createClient();
pClient->connect(pServerAddress);
// If we are not connected to the server, return false
// wait 5 seconds before exiting the loop
for (int i = 0; i < 50; i++) {
if (pClient->isConnected()) {
bt_status = true;
break;
}
delay(100); // wait 100ms before checking again, so 50 checkings will be 5 seconds
}
if (!bt_status) {
Serial.println("Failed to connect to the ble server!");
return false;
} else {
Serial.println("Connected successfully to server");
pClient->setMTU(28); //set client to request maximum MTU from server (default is 23 otherwise)
Serial.println(" MTU set");
BLERemoteService* pRemoteServiceFF = pClient->getService(FFserviceUUID);
Serial.println(" Services Found!");
BLERemoteCharacteristic* ppgDataCharacteristicFF = pRemoteServiceFF->getCharacteristic(FFcharUUID);
if (ppgDataCharacteristicFF == nullptr) {
Serial.print(" Failed to find our characteristic UUID");
return false;
}
Serial.println(" Characteristics Found!");
std::string value = ppgDataCharacteristicFF -> toString().c_str();
Serial.print("The FF characteristic value was: ");
Serial.println(value.c_str());
// ppgDataCharacteristic->registerForNotify(ppgDataNotifyCallback);
ppgDataCharacteristicFF -> registerForNotify(notifyCallback);
connected = true;
return true;
}
}
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
String name = advertisedDevice.getName().c_str();
if (name.startsWith(BLE_server) && advertisedDevice.isAdvertisingService(FFserviceUUID)) {
Serial.print("Found our device: " + name + ".. preparing tasks");
ble_device_name = name.c_str();
advertisedDevice.getScan()->stop();
//pServerAddress = new BLEAddress(advertisedDevice.getAddress());
pServerAddress = new BLEAdvertisedDevice(advertisedDevice);
ble_device_found = true;
doConnect = true;
if (connect_to_ble_cloud_server){
connect_cloud_ble_server();
}
Serial.println("Device found. Connecting to: "+ String(ble_device_name));
} else {
Serial.println("Detected unknown device: " + name);
}
}
};
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
rssi=rssi;
rxSize=size;
memcpy(rxpacket, payload, size );
rxpacket[size]='\0';
Radio.Sleep( );
Serial.printf("\r\nreceived packet \"%s\" with rssi %d , length %d\r\n",rxpacket,rssi,rxSize);
//postData(rxpacket);
lora_idle = true;
}
void init_lora(void *dummy_param){
txNumber=0;
rssi=0;
RadioEvents.RxDone = OnRxDone;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
}
void handling_lora(void *dummy_param){
while(1){
if(lora_idle)
{
lora_idle = false;
Serial.println("into RX mode");
Radio.Rx(0);
}
Radio.IrqProcess();
}
}
// scan for devices
void scan_ble_devices() {
Serial.println("Scanning for ble devices...");
/*
if (ble_interface_initiated) {
// deinit
BLEDevice::deinit(false);
}
*/
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
// Serial.println("debug1");
pBLEScan->setActiveScan(true);
// Serial.println("debug2");
pBLEScan->start(5, false);
// Serial.println("debug3");
// Serial.println("Client ready");
last_scan = millis();
ble_interface_initiated = true;
Serial.println("Ble scanner active");
}
void setup() {
Serial.begin(115200);
delay(5000);
Mcu.begin(HELTEC_BOARD,SLOW_CLK_TPYE);
// if (ssid != "" || password != ""){
if (setup_mode == false) {
// working lora at core 1
init_lora(NULL);
xTaskCreatePinnedToCore(
handling_lora,
"handling_lora",
10000,
NULL,
1,
&lora_task2,
0);
//*/
scan_ble_devices();
}
}
void loop() {
if (! setup_mode){
///*
if (doConnect == true) {
if (connect_ble_erver()) {
Serial.println("We are now connected to the BLE Server.");
Serial.println("Streaming BLE Device: " + String(ble_device_name));
connected = true;
} else {
Serial.println("Failed to connect to the BLE server!");
}
doConnect = false;
} else {
// if 10 seconds has passed since the last scan, scan again
if (millis() - last_scan > 10000) {
scan_ble_devices();
}
}
}
}