From 078765c06a8497825482c1a6f08f2652f603f13c Mon Sep 17 00:00:00 2001 From: Gurkengewuerz Date: Wed, 17 Apr 2019 21:41:37 +0200 Subject: [PATCH] changed on pairing stuff --- .gitignore | 7 +++++++ src/SBNetwork.cpp | 33 ++++++++++++++++++++++++++------- src/SBNetwork.h | 10 ++++++++-- src/SBNetwork_config.h | 8 +++++--- src/SBTypes.h | 1 + 5 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..777ec13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.pio +.pioenvs +.piolibdeps +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ \ No newline at end of file diff --git a/src/SBNetwork.cpp b/src/SBNetwork.cpp index 69e57a4..cbda39b 100644 --- a/src/SBNetwork.cpp +++ b/src/SBNetwork.cpp @@ -22,17 +22,17 @@ void printDeviceData(SBNetworkDevice &device){ Serial.println(); Serial.print(F("Master MAC = ")); printAddress(device.MasterMAC.Bytes); - Serial.println(""); + Serial.println(); Serial.print(F("NetKey = ")); Serial.print(device.NetworkKey, DEC); - Serial.println(""); + Serial.println(); } SBNetwork::SBNetwork(bool client, uint8_t cePin, uint8_t csPin) : radio(cePin, csPin){ RunAsClient = client; } -void SBNetwork::initialize(SBMacAddress mac){ +int8_t SBNetwork::initialize(SBMacAddress mac){ Serial.print(F("SBNetwork Version ")); Serial.println(F(SB_VERSION)); Serial.println(F("====================")); @@ -58,7 +58,7 @@ void SBNetwork::initialize(SBMacAddress mac){ // Set the PA Level low to prevent power supply related issues since this is a // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. - this->radio.setPALevel(RF24_PA_HIGH); + this->radio.setPALevel(RF24_PA_LOW); this->radio.enableDynamicPayloads(); //this->radio.enableDynamicAck(); @@ -75,15 +75,24 @@ void SBNetwork::initialize(SBMacAddress mac){ // Start the listening phase this->radio.startListening(); Serial.println(F("Done")); + + unsigned long startConnect = millis(); if (this->RunAsClient) { // Connect to a master _Connected = false; - while (!_Connected) { + while (!_Connected && millis() - startConnect <= CONNECT_TIMEOUT) { _Connected = connectToNetwork(); delay(500); // This can be an endless loop in case of no connection to master is available } + + if(!_Connected) { + Serial.println(F("Timeout on connect to network...")); + return 1; + } } + + return 0; } void SBNetwork::initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac){ @@ -219,7 +228,9 @@ bool SBNetwork::sendToDeviceInternal(SBNetworkFrame frame) { Serial.print(millis()); Serial.println(F(" Sending physical data")); #endif + bSuccess = radio.write(buffer, bufferSize); + radio.openReadingPipe(0, this->NetworkDevice.MAC); radio.startListening(); return bSuccess; @@ -361,7 +372,7 @@ bool SBNetwork::connectToNetwork(){ unsigned long started_waiting_at = millis(); boolean timeout = false; while (!this->receive(&frame)) { - if ((millis() - started_waiting_at) > 1000) { + if ((millis() - started_waiting_at) > 2500) { timeout = true; break; } @@ -394,12 +405,13 @@ bool SBNetwork::connectToNetwork(){ conFrame.Header.PackageId = millis(); conFrame.Header.ToAddress = frame.Header.FromAddress; conFrame.MessageSize = 0; + started_waiting_at = millis(); if (!this->sendToDeviceInternal(conFrame)) { Serial.println(F("Failed - Sending pairing request")); } else { while (!this->receive(&frame)) { - if (millis() - started_waiting_at > 1000) { + if (millis() - started_waiting_at > 5000) { timeout = true; break; } @@ -686,3 +698,10 @@ uint8_t SBNetwork::removeMac(SBMacAddress mac){ return -1; } } + +bool SBNetwork::shutdown() { + radio.stopListening(); + delay(10); + radio.powerDown(); + return true; +} \ No newline at end of file diff --git a/src/SBNetwork.h b/src/SBNetwork.h index e87b3ef..067cb28 100644 --- a/src/SBNetwork.h +++ b/src/SBNetwork.h @@ -115,9 +115,10 @@ public: /* * Initializes the sensor / master + * Return 0 if success and 1 on timeout */ - void initialize(SBMacAddress mac); - void initialize(byte mac[]) { initialize(SBMacAddress(mac[0], mac[1], mac[2], mac[3], mac[4])); } + int8_t initialize(SBMacAddress mac); + int8_t initialize(byte mac[]) { initialize(SBMacAddress(mac[0], mac[1], mac[2], mac[3], mac[4])); } /* @@ -195,5 +196,10 @@ public: return SB_NETWORK_FLASH_SIZE; } + /* + Stops Listening and power down NRF Device. + */ + bool shutdown(); + }; #endif diff --git a/src/SBNetwork_config.h b/src/SBNetwork_config.h index e6437cb..d68d039 100644 --- a/src/SBNetwork_config.h +++ b/src/SBNetwork_config.h @@ -4,7 +4,7 @@ // Generates details debug messages about sending and receiving data packages -//#define _DEBUG +#define _DEBUG // All slaves will ping the master every xxx milliseconds. if set to 0, they will not ping the master #define MASTER_CHECK_INTERVAL 0 @@ -13,10 +13,12 @@ #define CLIENT_TIMEOUT 60000 +#define CONNECT_TIMEOUT 10000 + // Milliseconds to wait for fragmented packages -#define FRAGMENT_TIMEOUT 80 +#define FRAGMENT_TIMEOUT 150 // Milliseconds to wait between two fragment sendings -#define FRAGMENT_DELAY 10 +#define FRAGMENT_DELAY 20 #endif diff --git a/src/SBTypes.h b/src/SBTypes.h index e83d2c5..0fdedbc 100644 --- a/src/SBTypes.h +++ b/src/SBTypes.h @@ -37,6 +37,7 @@ typedef struct { uint8_t PackageId; // The unique ID of the package. Will be uint8_t FragmentCount; // How many fragments will be sent uint8_t FragmentNr; // Which fragment is this package + bool writeFast; } SBNetworkHeader; #define MAX_PACKAGE_SIZE (32 - sizeof(SBNetworkHeader))