Browse Source

changed on pairing stuff

master
Niklas 3 years ago
parent
commit
078765c06a
  1. 7
      .gitignore
  2. 33
      src/SBNetwork.cpp
  3. 10
      src/SBNetwork.h
  4. 8
      src/SBNetwork_config.h
  5. 1
      src/SBTypes.h

7
.gitignore vendored

@ -0,0 +1,7 @@
.pio
.pioenvs
.piolibdeps
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/

33
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;
}

10
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

8
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

1
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))

Loading…
Cancel
Save