Switching between networks

Started by brolly759, August 21, 2017, 12:07:59 PM

brolly759

I am trying to have 2 separate networks, a pairing network and the live network and switch between the 2.  Here is my function that I call but I get inconsistent results (sometimes switches, sometimes does not) on the network switching, what am I missing?

Thanks!

bool start_Wireless_Module(byte node_id, byte network_id, char encrpy_key[]) {

	byte init_OK = 0;

	delay(100);
	if (radio.initialize(FREQUENCY, node_id, network_id)) {
		init_OK = 1;

		delay(100);
		radio.encrypt(encrpy_key);
		delay(100);
		radio.setHighPower();
		delay(1000);
	}

	else if (radio.initialize(FREQUENCY, node_id, network_id)) {
		init_OK = 1;

		delay(100);
		radio.encrypt(encrpy_key);
		delay(100);
		radio.setHighPower();
		delay(1000);
	}

	return init_OK;
}

ChemE

It just feels wrong that you would initialize the radio twice 2 clocks apart and all those delays seem really needless.  Would it not be cleaner to initialize the radio once depending on the network ID passed?  And even at the fastest (8MHz) SPI bus I cannot change a register on the radio and then read back the old value.  I don't think you need any of those delays after you twiddle bits in the radio's registers.

There is some time needed for the oscillator to change frequency but that is tens of milliseconds and depends on how much you shift frequency. 1 second it still vastly more than is needed.  I assume all this is just there while you debug.

ChemE

Actually, that if else is doing nothing.  You have the same code in both branches.  You are initializing once and perhaps twice back to back and then setting the same things regardless of which initialize happens to return a true return value.  This collapses to:

if (!init1) init2;
encrypt;
highpower;

brolly759

the if statement returns true if it passed and init only runs once, if not it tries once more in the elseif. I can remove the delays, I was just using them in testing.

What I am finding is running an init() and then running another at another point of time does not always work.

brolly759

#4
I am trying to do something differently, I am just trying to change the encrypt key and network id but still not working properly. The init() is ran one time on an initial startup state routine. Is there anything I am missing?



char PAIR_ENCRYPTKEY[] = "TEMP1234TEMP1234";
uint8_t  PAIR_NETWORKID = 255;


radio.encrypt(PAIR_ENCRYPTKEY);
delay(5);
radio.setNetwork(NETWORKID);
delay(1000);




I have a sensor on a separate network spamming once a second but those packets dont come in, even if I switch the credentials.... Any thoughts?

ChemE

A longshot but node 255 is special since node 255 = everyone respond regardless of your ID.  Perhaps network ID also behaves the same.  I would avoid 255 and 0 for testing both on node ID and network ID.

ChemE

I would look into why the first init isn't working.  Rather than doing it again, run readAllRegsCompact and see which if any register values do not contain the values they should contain.  Do the same after a successful init and see if that is the difference.

brolly759

#7
The first init always worked, it was switching that failed. I got it to work consistently now and here is the code:


Function to change network info:

bool start_Wireless_Module(byte node_id, byte network_id, char encrpy_key[]) {

	if (radio.initialize(FREQUENCY, node_id, network_id)) {
		radio.setHighPower();
		radio.encrypt(encrpy_key);
		return true;
	}
	return false;
}



If Statement for pairing network.

if (start_Wireless_Module(NODEID, PAIR_NETWORKID, PAIR_ENCRYPTKEY)) {				// Go into pairing mode

					unsigned int pairing_timeout_duration = 30000;
					unsigned long current_Time = millis();
					unsigned long pairing_timeout = current_Time + pairing_timeout_duration;

					while (millis() <= pairing_timeout) {

						checkUSB();
						if (checkWirelessData()) {
							break;
						}
					}
				}



After pairing mode, switching to live network:

start_Wireless_Module(NODEID, NETWORKID, ENCRYPTKEY);