From 795d86c104c8f67ac2fe93275091cc1701f9b370 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Mon, 13 Nov 2017 22:58:11 +0100 Subject: [PATCH 01/10] Added point to point communication --- .../SodaqExplorer-p2p/SodaqExplorer-p2p.ino | 134 ++++++++++++++++++ src/rn2xx3.cpp | 109 +++++++++++++- src/rn2xx3.h | 18 ++- src/src.ino | 1 + 4 files changed, 258 insertions(+), 4 deletions(-) create mode 100644 examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino create mode 100644 src/src.ino diff --git a/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino new file mode 100644 index 0000000..5e9ebca --- /dev/null +++ b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino @@ -0,0 +1,134 @@ +/* + * Basic sketch for connecting + * a Sodaq Explorer to another Sodaq Explorer + * + * Author: Dennis Ruigrok + */ + +#include + +// Explorer Serial port definitions. +#define debugSerial SerialUSB +#define loraSerial Serial2 + + +// create an instance of the Library. +rn2xx3 myLora(loraSerial); + + +void setup() +{ + // built_in led + pinMode(LED_BUILTIN, OUTPUT); + led_on(); + + // make sure usb serial connection is available, + // or after 10s go on anyway for 'headless' use of the + // node. + while ((!debugSerial) && (millis() < 10000)); + + // beginning serial connections. + debugSerial.begin(57600); + loraSerial.begin(57600); + + // + debugSerial.println(F("--------------------------------")); + debugSerial.println(F("Basic sketch for communicating ")); + debugSerial.println(F("with another Sodaq Explorer")); + debugSerial.println(F("--------------------------------")); + led_off(); + + initialize_radio(); + +} + +void initialize_radio() +{ + + myLora.autobaud(); + + debugSerial.println("DevEUI? ");debugSerial.print(F("> ")); + debugSerial.println(myLora.hweui()); + debugSerial.println("Version?");debugSerial.print(F("> ")); + debugSerial.println(myLora.sysver()); + debugSerial.println(F("--------------------------------")); + + debugSerial.println(F("Setting up for listening for another explorer")); + bool join_result = false; + + + // point to point + join_result = myLora.initP2P(); + + + debugSerial.println("\u2713 Successfully Activated radio 2 radio"); + + +} + + + + +void loop() +{ + debugSerial.print("TXing"); + myLora.txCnf("Can you hear me???"); //one byte, blocking function + + switch(myLora.txCnf("!")) //one byte, blocking function + { + case TX_FAIL: + { + debugSerial.println("TX unsuccessful or not acknowledged"); + break; + } + case TX_SUCCESS: + { + debugSerial.println("TX successful and acknowledged"); + break; + } + case TX_WITH_RX: + { + String received = myLora.getRx(); + received = myLora.base16decode(received); + debugSerial.print("Received downlink immediately: " + received); + break; + } + default: + { + debugSerial.println("Unknown response from TX function"); + } + } + + led_off(); + + for(int i = 0; i < 3; i++) + switch(myLora.listenP2P()) { + case TX_WITH_RX: + { + String received = myLora.getRx(); + received = myLora.base16decode(received); + debugSerial.print("Received downlink: " + received); + break; + } + case RADIO_LISTEN_WITHOUT_RX: + { + debugSerial.println("Listened timeout but no downlink"); + break; + } + + + } + + +} + + +void led_on() +{ + digitalWrite(LED_BUILTIN, 1); +} + +void led_off() +{ + digitalWrite(LED_BUILTIN, 0); +} diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 0e7aaad..1a9c0e6 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -92,6 +92,8 @@ String rn2xx3::deveui() bool rn2xx3::init() { + _radio2radio = false; + if(_appskey=="0") //appskey variable is set by both OTAA and ABP { return false; @@ -323,6 +325,82 @@ bool rn2xx3::initABP(String devAddr, String AppSKey, String NwkSKey) } } +bool rn2xx3::initP2P() { + _radio2radio = true; + + String receivedData; + + //clear serial buffer + while(_serial.available()) + _serial.read(); + + configureModuleType(); + + sendRawCommand(F("mac pause")); + + sendRawCommand(F("radio set mod lora")); + + switch (_moduleType) { + case RN2903: + sendRawCommand(F("radio set freq 869100000")); + break; + case RN2483: + sendRawCommand(F("radio set freq 869100000")); + + break; + default: + // we shouldn't go forward with the init + return false; + } + + sendRawCommand(F("radio set pwr 14")); + + sendRawCommand(F("radio set sf sf7")); + + sendRawCommand(F("radio set afcbw 41.7")); + + sendRawCommand(F("radio set rxbw 125")); + + sendRawCommand(F("radio set prlen 8")); + + sendRawCommand(F("radio set crc on")); + + sendRawCommand(F("radio set iqi off")); + + sendRawCommand(F("radio set cr 4/5")); + + sendRawCommand(F("radio set sync 12")); + + sendRawCommand(F("radio set bw 125")); + + return true; +} + +TX_RETURN_TYPE rn2xx3::listenP2P() { + String receivedData; + bool mustStop = false; + + receivedData = sendRawCommand(F("radio rx 0")); // don't put this in receiveddata we want to ignore the first ok + while(!mustStop) { + receivedData = _serial.readStringUntil('\n'); + + if(receivedData.startsWith("radio_err")) { + return RADIO_LISTEN_WITHOUT_RX; // timeout + } else if(receivedData.startsWith("busy")) { + // just wait + } else if(receivedData.startsWith("radio_rx")) { + //example: radio_rx 54657374696E6720313233 + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + + return TX_WITH_RX; + } + + } + + + +} + TX_RETURN_TYPE rn2xx3::tx(String data) { return txUncnf(data); //we are unsure which mode we're in. Better not to wait for acks. @@ -371,10 +449,18 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) return TX_FAIL; } + if(_radio2radio) { + command.replace("mac", "radio"); + command.replace("uncnf 1 ", ""); + command.replace("cnf 1 ", ""); + } + + + _serial.print(command); if(shouldEncode) { - sendEncoded(data); + sendEncoded(data); } else { @@ -384,7 +470,9 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) String receivedData = _serial.readStringUntil('\n'); //TODO: Debug print on receivedData - + + + if(receivedData.startsWith("ok")) { _serial.setTimeout(30000); @@ -422,14 +510,23 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) else if(receivedData.startsWith("radio_tx_ok")) { + //SUCCESS!! send_success = true; return TX_SUCCESS; } - + else if(receivedData.startsWith("radio_rx")) { + //example: radio_rx 54657374696E6720313233 + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + send_success = true; + return TX_WITH_RX; + } + else if(receivedData.startsWith("radio_err")) { //This should never happen. If it does, something major is wrong. + // or someone added radio 2 radio support and did it wrong ;-) + init(); } @@ -439,6 +536,12 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) //init(); } } + else if(receivedData.startsWith("radio_rx")) { + //example: radio_rx 54657374696E6720313233 + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + send_success = true; + return TX_WITH_RX; + } else if(receivedData.startsWith("invalid_param")) { diff --git a/src/rn2xx3.h b/src/rn2xx3.h index 9cd4940..3ae63f0 100644 --- a/src/rn2xx3.h +++ b/src/rn2xx3.h @@ -33,8 +33,10 @@ enum TX_RETURN_TYPE { TX_SUCCESS = 1, // The transmission was successful. // Also the case when a confirmed message was acked. - TX_WITH_RX = 2 // A downlink message was received after the transmission. + TX_WITH_RX = 2, // A downlink message was received after the transmission. // This also implies that a confirmed message is acked. + RADIO_LISTEN_WITHOUT_RX = 3 // listened to radio 2 radio but nothing came back + }; class rn2xx3 @@ -136,6 +138,17 @@ class rn2xx3 * DevEui: Device EUI as a uint8_t buffer (optional - set to 0 to use Hardware EUI) */ bool initOTAA(uint8_t * AppEUI, uint8_t * AppKey, uint8_t * DevEui); + + /* + * Sets the rn2xx3 to listen/send transmissions from/to other rn2xx3's + */ + + bool initP2P(); + + /* + * Listen to + */ + TX_RETURN_TYPE listenP2P(); /* * Transmit the provided data. The data is hex-encoded by this library, @@ -239,6 +252,9 @@ class rn2xx3 RN2xx3_t _moduleType = RN_NA; + // mac pause and radio to radio transmissions + bool _radio2radio = false; + //Flags to switch code paths. Default is to use OTAA. bool _otaa = true; diff --git a/src/src.ino b/src/src.ino new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/src.ino @@ -0,0 +1 @@ + From 0707ab4c553ef2aef0e3402f35325a5375a3ec92 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Mon, 13 Nov 2017 23:08:13 +0100 Subject: [PATCH 02/10] Added point to point communication. set rn2903 frequency --- src/rn2xx3.cpp | 51 +++++++------------------------------------------- 1 file changed, 7 insertions(+), 44 deletions(-) diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 1a9c0e6..feea32a 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -342,7 +342,7 @@ bool rn2xx3::initP2P() { switch (_moduleType) { case RN2903: - sendRawCommand(F("radio set freq 869100000")); + sendRawCommand(F("radio set freq 923300000")); break; case RN2483: sendRawCommand(F("radio set freq 869100000")); @@ -369,6 +369,8 @@ bool rn2xx3::initP2P() { sendRawCommand(F("radio set cr 4/5")); + sendRawCommand(F("radio set wdt 0"));// continues reception + sendRawCommand(F("radio set sync 12")); sendRawCommand(F("radio set bw 125")); @@ -376,30 +378,6 @@ bool rn2xx3::initP2P() { return true; } -TX_RETURN_TYPE rn2xx3::listenP2P() { - String receivedData; - bool mustStop = false; - - receivedData = sendRawCommand(F("radio rx 0")); // don't put this in receiveddata we want to ignore the first ok - while(!mustStop) { - receivedData = _serial.readStringUntil('\n'); - - if(receivedData.startsWith("radio_err")) { - return RADIO_LISTEN_WITHOUT_RX; // timeout - } else if(receivedData.startsWith("busy")) { - // just wait - } else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); - - return TX_WITH_RX; - } - - } - - - -} TX_RETURN_TYPE rn2xx3::tx(String data) { @@ -449,18 +427,12 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) return TX_FAIL; } - if(_radio2radio) { - command.replace("mac", "radio"); - command.replace("uncnf 1 ", ""); - command.replace("cnf 1 ", ""); - } - - + if(_radio2radio) command.replace("mac", "radio"); _serial.print(command); if(shouldEncode) { - sendEncoded(data); + sendEncoded(data); } else { @@ -470,9 +442,7 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) String receivedData = _serial.readStringUntil('\n'); //TODO: Debug print on receivedData - - - + if(receivedData.startsWith("ok")) { _serial.setTimeout(30000); @@ -510,7 +480,6 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) else if(receivedData.startsWith("radio_tx_ok")) { - //SUCCESS!! send_success = true; return TX_SUCCESS; @@ -526,7 +495,7 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) { //This should never happen. If it does, something major is wrong. // or someone added radio 2 radio support and did it wrong ;-) - + SerialUSB.println("Radio error"); // remove me, debugging init(); } @@ -536,12 +505,6 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) //init(); } } - else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); - send_success = true; - return TX_WITH_RX; - } else if(receivedData.startsWith("invalid_param")) { From c332ca3785390e1875a23ac11c09a34e26917e78 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Wed, 22 Nov 2017 17:36:02 +0100 Subject: [PATCH 03/10] Trash --- .../SodaqExplorer-p2p/SodaqExplorer-p2p.ino | 134 ------------------ src/rn2xx3.cpp | 19 +-- src/rn2xx3.h | 18 +-- src/src.ino | 1 - 4 files changed, 11 insertions(+), 161 deletions(-) delete mode 100644 examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino delete mode 100644 src/src.ino diff --git a/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino deleted file mode 100644 index 5e9ebca..0000000 --- a/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Basic sketch for connecting - * a Sodaq Explorer to another Sodaq Explorer - * - * Author: Dennis Ruigrok - */ - -#include - -// Explorer Serial port definitions. -#define debugSerial SerialUSB -#define loraSerial Serial2 - - -// create an instance of the Library. -rn2xx3 myLora(loraSerial); - - -void setup() -{ - // built_in led - pinMode(LED_BUILTIN, OUTPUT); - led_on(); - - // make sure usb serial connection is available, - // or after 10s go on anyway for 'headless' use of the - // node. - while ((!debugSerial) && (millis() < 10000)); - - // beginning serial connections. - debugSerial.begin(57600); - loraSerial.begin(57600); - - // - debugSerial.println(F("--------------------------------")); - debugSerial.println(F("Basic sketch for communicating ")); - debugSerial.println(F("with another Sodaq Explorer")); - debugSerial.println(F("--------------------------------")); - led_off(); - - initialize_radio(); - -} - -void initialize_radio() -{ - - myLora.autobaud(); - - debugSerial.println("DevEUI? ");debugSerial.print(F("> ")); - debugSerial.println(myLora.hweui()); - debugSerial.println("Version?");debugSerial.print(F("> ")); - debugSerial.println(myLora.sysver()); - debugSerial.println(F("--------------------------------")); - - debugSerial.println(F("Setting up for listening for another explorer")); - bool join_result = false; - - - // point to point - join_result = myLora.initP2P(); - - - debugSerial.println("\u2713 Successfully Activated radio 2 radio"); - - -} - - - - -void loop() -{ - debugSerial.print("TXing"); - myLora.txCnf("Can you hear me???"); //one byte, blocking function - - switch(myLora.txCnf("!")) //one byte, blocking function - { - case TX_FAIL: - { - debugSerial.println("TX unsuccessful or not acknowledged"); - break; - } - case TX_SUCCESS: - { - debugSerial.println("TX successful and acknowledged"); - break; - } - case TX_WITH_RX: - { - String received = myLora.getRx(); - received = myLora.base16decode(received); - debugSerial.print("Received downlink immediately: " + received); - break; - } - default: - { - debugSerial.println("Unknown response from TX function"); - } - } - - led_off(); - - for(int i = 0; i < 3; i++) - switch(myLora.listenP2P()) { - case TX_WITH_RX: - { - String received = myLora.getRx(); - received = myLora.base16decode(received); - debugSerial.print("Received downlink: " + received); - break; - } - case RADIO_LISTEN_WITHOUT_RX: - { - debugSerial.println("Listened timeout but no downlink"); - break; - } - - - } - - -} - - -void led_on() -{ - digitalWrite(LED_BUILTIN, 1); -} - -void led_off() -{ - digitalWrite(LED_BUILTIN, 0); -} diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index feea32a..d33b16d 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -92,8 +92,6 @@ String rn2xx3::deveui() bool rn2xx3::init() { - _radio2radio = false; - if(_appskey=="0") //appskey variable is set by both OTAA and ABP { return false; @@ -325,6 +323,7 @@ bool rn2xx3::initABP(String devAddr, String AppSKey, String NwkSKey) } } +<<<<<<< HEAD bool rn2xx3::initP2P() { _radio2radio = true; @@ -379,6 +378,8 @@ bool rn2xx3::initP2P() { } +======= +>>>>>>> parent of 795d86c... Added point to point communication TX_RETURN_TYPE rn2xx3::tx(String data) { return txUncnf(data); //we are unsure which mode we're in. Better not to wait for acks. @@ -427,8 +428,11 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) return TX_FAIL; } +<<<<<<< HEAD if(_radio2radio) command.replace("mac", "radio"); +======= +>>>>>>> parent of 795d86c... Added point to point communication _serial.print(command); if(shouldEncode) { @@ -484,18 +488,15 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) send_success = true; return TX_SUCCESS; } - else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); - send_success = true; - return TX_WITH_RX; - } - + else if(receivedData.startsWith("radio_err")) { //This should never happen. If it does, something major is wrong. +<<<<<<< HEAD // or someone added radio 2 radio support and did it wrong ;-) SerialUSB.println("Radio error"); // remove me, debugging +======= +>>>>>>> parent of 795d86c... Added point to point communication init(); } diff --git a/src/rn2xx3.h b/src/rn2xx3.h index 3ae63f0..9cd4940 100644 --- a/src/rn2xx3.h +++ b/src/rn2xx3.h @@ -33,10 +33,8 @@ enum TX_RETURN_TYPE { TX_SUCCESS = 1, // The transmission was successful. // Also the case when a confirmed message was acked. - TX_WITH_RX = 2, // A downlink message was received after the transmission. + TX_WITH_RX = 2 // A downlink message was received after the transmission. // This also implies that a confirmed message is acked. - RADIO_LISTEN_WITHOUT_RX = 3 // listened to radio 2 radio but nothing came back - }; class rn2xx3 @@ -138,17 +136,6 @@ class rn2xx3 * DevEui: Device EUI as a uint8_t buffer (optional - set to 0 to use Hardware EUI) */ bool initOTAA(uint8_t * AppEUI, uint8_t * AppKey, uint8_t * DevEui); - - /* - * Sets the rn2xx3 to listen/send transmissions from/to other rn2xx3's - */ - - bool initP2P(); - - /* - * Listen to - */ - TX_RETURN_TYPE listenP2P(); /* * Transmit the provided data. The data is hex-encoded by this library, @@ -252,9 +239,6 @@ class rn2xx3 RN2xx3_t _moduleType = RN_NA; - // mac pause and radio to radio transmissions - bool _radio2radio = false; - //Flags to switch code paths. Default is to use OTAA. bool _otaa = true; diff --git a/src/src.ino b/src/src.ino deleted file mode 100644 index 8b13789..0000000 --- a/src/src.ino +++ /dev/null @@ -1 +0,0 @@ - From 17aa8123b0cc9fbf64dae8254b514ef3adcfe650 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Wed, 22 Nov 2017 17:37:12 +0100 Subject: [PATCH 04/10] Accent removed some --- .../SodaqExplorer-p2p/SodaqExplorer-p2p.ino | 134 ++++++++++++++++++ src/rn2xx3.cpp | 70 ++++++--- src/rn2xx3.h | 18 ++- src/src.ino | 1 + 4 files changed, 205 insertions(+), 18 deletions(-) create mode 100644 examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino create mode 100644 src/src.ino diff --git a/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino new file mode 100644 index 0000000..5e9ebca --- /dev/null +++ b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino @@ -0,0 +1,134 @@ +/* + * Basic sketch for connecting + * a Sodaq Explorer to another Sodaq Explorer + * + * Author: Dennis Ruigrok + */ + +#include + +// Explorer Serial port definitions. +#define debugSerial SerialUSB +#define loraSerial Serial2 + + +// create an instance of the Library. +rn2xx3 myLora(loraSerial); + + +void setup() +{ + // built_in led + pinMode(LED_BUILTIN, OUTPUT); + led_on(); + + // make sure usb serial connection is available, + // or after 10s go on anyway for 'headless' use of the + // node. + while ((!debugSerial) && (millis() < 10000)); + + // beginning serial connections. + debugSerial.begin(57600); + loraSerial.begin(57600); + + // + debugSerial.println(F("--------------------------------")); + debugSerial.println(F("Basic sketch for communicating ")); + debugSerial.println(F("with another Sodaq Explorer")); + debugSerial.println(F("--------------------------------")); + led_off(); + + initialize_radio(); + +} + +void initialize_radio() +{ + + myLora.autobaud(); + + debugSerial.println("DevEUI? ");debugSerial.print(F("> ")); + debugSerial.println(myLora.hweui()); + debugSerial.println("Version?");debugSerial.print(F("> ")); + debugSerial.println(myLora.sysver()); + debugSerial.println(F("--------------------------------")); + + debugSerial.println(F("Setting up for listening for another explorer")); + bool join_result = false; + + + // point to point + join_result = myLora.initP2P(); + + + debugSerial.println("\u2713 Successfully Activated radio 2 radio"); + + +} + + + + +void loop() +{ + debugSerial.print("TXing"); + myLora.txCnf("Can you hear me???"); //one byte, blocking function + + switch(myLora.txCnf("!")) //one byte, blocking function + { + case TX_FAIL: + { + debugSerial.println("TX unsuccessful or not acknowledged"); + break; + } + case TX_SUCCESS: + { + debugSerial.println("TX successful and acknowledged"); + break; + } + case TX_WITH_RX: + { + String received = myLora.getRx(); + received = myLora.base16decode(received); + debugSerial.print("Received downlink immediately: " + received); + break; + } + default: + { + debugSerial.println("Unknown response from TX function"); + } + } + + led_off(); + + for(int i = 0; i < 3; i++) + switch(myLora.listenP2P()) { + case TX_WITH_RX: + { + String received = myLora.getRx(); + received = myLora.base16decode(received); + debugSerial.print("Received downlink: " + received); + break; + } + case RADIO_LISTEN_WITHOUT_RX: + { + debugSerial.println("Listened timeout but no downlink"); + break; + } + + + } + + +} + + +void led_on() +{ + digitalWrite(LED_BUILTIN, 1); +} + +void led_off() +{ + digitalWrite(LED_BUILTIN, 0); +} diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index d33b16d..1a9c0e6 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -92,6 +92,8 @@ String rn2xx3::deveui() bool rn2xx3::init() { + _radio2radio = false; + if(_appskey=="0") //appskey variable is set by both OTAA and ABP { return false; @@ -323,7 +325,6 @@ bool rn2xx3::initABP(String devAddr, String AppSKey, String NwkSKey) } } -<<<<<<< HEAD bool rn2xx3::initP2P() { _radio2radio = true; @@ -341,7 +342,7 @@ bool rn2xx3::initP2P() { switch (_moduleType) { case RN2903: - sendRawCommand(F("radio set freq 923300000")); + sendRawCommand(F("radio set freq 869100000")); break; case RN2483: sendRawCommand(F("radio set freq 869100000")); @@ -368,8 +369,6 @@ bool rn2xx3::initP2P() { sendRawCommand(F("radio set cr 4/5")); - sendRawCommand(F("radio set wdt 0"));// continues reception - sendRawCommand(F("radio set sync 12")); sendRawCommand(F("radio set bw 125")); @@ -377,9 +376,31 @@ bool rn2xx3::initP2P() { return true; } +TX_RETURN_TYPE rn2xx3::listenP2P() { + String receivedData; + bool mustStop = false; + + receivedData = sendRawCommand(F("radio rx 0")); // don't put this in receiveddata we want to ignore the first ok + while(!mustStop) { + receivedData = _serial.readStringUntil('\n'); + + if(receivedData.startsWith("radio_err")) { + return RADIO_LISTEN_WITHOUT_RX; // timeout + } else if(receivedData.startsWith("busy")) { + // just wait + } else if(receivedData.startsWith("radio_rx")) { + //example: radio_rx 54657374696E6720313233 + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + + return TX_WITH_RX; + } + + } + + + +} -======= ->>>>>>> parent of 795d86c... Added point to point communication TX_RETURN_TYPE rn2xx3::tx(String data) { return txUncnf(data); //we are unsure which mode we're in. Better not to wait for acks. @@ -428,15 +449,18 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) return TX_FAIL; } -<<<<<<< HEAD - if(_radio2radio) command.replace("mac", "radio"); + if(_radio2radio) { + command.replace("mac", "radio"); + command.replace("uncnf 1 ", ""); + command.replace("cnf 1 ", ""); + } + + -======= ->>>>>>> parent of 795d86c... Added point to point communication _serial.print(command); if(shouldEncode) { - sendEncoded(data); + sendEncoded(data); } else { @@ -446,7 +470,9 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) String receivedData = _serial.readStringUntil('\n'); //TODO: Debug print on receivedData - + + + if(receivedData.startsWith("ok")) { _serial.setTimeout(30000); @@ -484,19 +510,23 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) else if(receivedData.startsWith("radio_tx_ok")) { + //SUCCESS!! send_success = true; return TX_SUCCESS; } - + else if(receivedData.startsWith("radio_rx")) { + //example: radio_rx 54657374696E6720313233 + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + send_success = true; + return TX_WITH_RX; + } + else if(receivedData.startsWith("radio_err")) { //This should never happen. If it does, something major is wrong. -<<<<<<< HEAD // or someone added radio 2 radio support and did it wrong ;-) - SerialUSB.println("Radio error"); // remove me, debugging -======= ->>>>>>> parent of 795d86c... Added point to point communication + init(); } @@ -506,6 +536,12 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) //init(); } } + else if(receivedData.startsWith("radio_rx")) { + //example: radio_rx 54657374696E6720313233 + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + send_success = true; + return TX_WITH_RX; + } else if(receivedData.startsWith("invalid_param")) { diff --git a/src/rn2xx3.h b/src/rn2xx3.h index 9cd4940..3ae63f0 100644 --- a/src/rn2xx3.h +++ b/src/rn2xx3.h @@ -33,8 +33,10 @@ enum TX_RETURN_TYPE { TX_SUCCESS = 1, // The transmission was successful. // Also the case when a confirmed message was acked. - TX_WITH_RX = 2 // A downlink message was received after the transmission. + TX_WITH_RX = 2, // A downlink message was received after the transmission. // This also implies that a confirmed message is acked. + RADIO_LISTEN_WITHOUT_RX = 3 // listened to radio 2 radio but nothing came back + }; class rn2xx3 @@ -136,6 +138,17 @@ class rn2xx3 * DevEui: Device EUI as a uint8_t buffer (optional - set to 0 to use Hardware EUI) */ bool initOTAA(uint8_t * AppEUI, uint8_t * AppKey, uint8_t * DevEui); + + /* + * Sets the rn2xx3 to listen/send transmissions from/to other rn2xx3's + */ + + bool initP2P(); + + /* + * Listen to + */ + TX_RETURN_TYPE listenP2P(); /* * Transmit the provided data. The data is hex-encoded by this library, @@ -239,6 +252,9 @@ class rn2xx3 RN2xx3_t _moduleType = RN_NA; + // mac pause and radio to radio transmissions + bool _radio2radio = false; + //Flags to switch code paths. Default is to use OTAA. bool _otaa = true; diff --git a/src/src.ino b/src/src.ino new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/src.ino @@ -0,0 +1 @@ + From 07451eec7c2b4e607fc8f53ffede35e19fe4a3cf Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Sun, 3 Dec 2017 00:48:09 +0100 Subject: [PATCH 05/10] decode bytes --- src/rn2xx3.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++----- src/rn2xx3.h | 5 ++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 1a9c0e6..1d14d14 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -92,6 +92,7 @@ String rn2xx3::deveui() bool rn2xx3::init() { + if(_radio2radio)sendRawCommand(F("mac resume")); _radio2radio = false; if(_appskey=="0") //appskey variable is set by both OTAA and ABP @@ -389,8 +390,11 @@ TX_RETURN_TYPE rn2xx3::listenP2P() { } else if(receivedData.startsWith("busy")) { // just wait } else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + //example: radio_rx 54657374696E6720313233 + SerialUSB.print("receivedData:"); + SerialUSB.println(receivedData); + + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); return TX_WITH_RX; } @@ -458,12 +462,22 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) _serial.print(command); + SerialUSB.print("Command: "); + SerialUSB.print(command); + SerialUSB.println("."); if(shouldEncode) { sendEncoded(data); + SerialUSB.print("encoded: "); + SerialUSB.print(data); + SerialUSB.println("."); } else { + SerialUSB.print("plain: "); + SerialUSB.print(data); + SerialUSB.println("."); + _serial.print(data); } _serial.println(); @@ -516,8 +530,11 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) return TX_SUCCESS; } else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + //example: radio_rx 54657374696E6720313233 + SerialUSB.print("receivedData:"); + SerialUSB.println(receivedData); + + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); send_success = true; return TX_WITH_RX; } @@ -537,8 +554,11 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) } } else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 1)+1); + //example: radio_rx 54657374696E6720313233 + SerialUSB.print("receivedData:"); + SerialUSB.println(receivedData); + + _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); send_success = true; return TX_WITH_RX; } @@ -649,6 +669,8 @@ String rn2xx3::base16encode(String input) } String rn2xx3::getRx() { + SerialUSB.print("Get rx: "); + SerialUSB.println(_rxMessenge); return _rxMessenge; } @@ -667,6 +689,7 @@ String rn2xx3::base16decode(String input) input.toCharArray(charsIn, input.length()+1); unsigned i = 0; + for(i = 0; i 0) { + uint8_t* bytesOut = (uint8_t*) malloc(input.length() * sizeof(uint8_t)); + + char charsIn[input.length()+1]; + + input.trim(); + input.toCharArray(charsIn, input.length()+1); + + unsigned i = 0; + SerialUSB.print("out: "); + for(i = 0; i=0 && dr<=5) diff --git a/src/rn2xx3.h b/src/rn2xx3.h index 3ae63f0..ac90e4c 100644 --- a/src/rn2xx3.h +++ b/src/rn2xx3.h @@ -246,6 +246,11 @@ class rn2xx3 * string received from the RN2xx3. */ String base16decode(String); + /* + * Decode a HEX string to an Byte array. Useful to receive + * bytes from the RN2xx3. + */ + uint8_t * base16decodeBytes(String); private: Stream& _serial; From 9b5e0487badb9fd23f8807735e06a3d8d5da6026 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Sun, 3 Dec 2017 18:25:14 +0100 Subject: [PATCH 06/10] Array fault repaired --- src/rn2xx3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 1d14d14..e161bb6 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -731,7 +731,7 @@ uint8_t * rn2xx3::base16decodeBytes(String input) toDo[1] = charsIn[i*2+1]; int out = strtoul(toDo, 0, 16); - *bytesOut++ = (uint8_t) out; + bytesOut[i] = (uint8_t) out; } return bytesOut; } From 57ed651c3e3f174da4a11e7439153823e0085a76 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Sun, 3 Dec 2017 20:42:16 +0100 Subject: [PATCH 07/10] Remove sodaq test data --- src/rn2xx3.cpp | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index e161bb6..795895f 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -390,10 +390,7 @@ TX_RETURN_TYPE rn2xx3::listenP2P() { } else if(receivedData.startsWith("busy")) { // just wait } else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - SerialUSB.print("receivedData:"); - SerialUSB.println(receivedData); - + //example: radio_rx 54657374696E6720313233 _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); return TX_WITH_RX; @@ -461,23 +458,13 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) - _serial.print(command); - SerialUSB.print("Command: "); - SerialUSB.print(command); - SerialUSB.println("."); + _serial.print(command); if(shouldEncode) { sendEncoded(data); - SerialUSB.print("encoded: "); - SerialUSB.print(data); - SerialUSB.println("."); } else { - SerialUSB.print("plain: "); - SerialUSB.print(data); - SerialUSB.println("."); - _serial.print(data); } _serial.println(); @@ -530,10 +517,7 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) return TX_SUCCESS; } else if(receivedData.startsWith("radio_rx")) { - //example: radio_rx 54657374696E6720313233 - SerialUSB.print("receivedData:"); - SerialUSB.println(receivedData); - + //example: radio_rx 54657374696E6720313233 _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); send_success = true; return TX_WITH_RX; @@ -555,9 +539,6 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode) } else if(receivedData.startsWith("radio_rx")) { //example: radio_rx 54657374696E6720313233 - SerialUSB.print("receivedData:"); - SerialUSB.println(receivedData); - _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); send_success = true; return TX_WITH_RX; @@ -669,8 +650,6 @@ String rn2xx3::base16encode(String input) } String rn2xx3::getRx() { - SerialUSB.print("Get rx: "); - SerialUSB.println(_rxMessenge); return _rxMessenge; } @@ -720,7 +699,6 @@ uint8_t * rn2xx3::base16decodeBytes(String input) input.toCharArray(charsIn, input.length()+1); unsigned i = 0; - SerialUSB.print("out: "); for(i = 0; i Date: Mon, 1 Jan 2018 22:49:55 +0100 Subject: [PATCH 08/10] sys reset --- src/rn2xx3.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 795895f..092589e 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -327,6 +327,7 @@ bool rn2xx3::initABP(String devAddr, String AppSKey, String NwkSKey) } bool rn2xx3::initP2P() { + sendRawCommand(F("sys reset")); _radio2radio = true; String receivedData; @@ -392,7 +393,7 @@ TX_RETURN_TYPE rn2xx3::listenP2P() { } else if(receivedData.startsWith("radio_rx")) { //example: radio_rx 54657374696E6720313233 _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); - + return TX_WITH_RX; } @@ -650,7 +651,8 @@ String rn2xx3::base16encode(String input) } String rn2xx3::getRx() { - return _rxMessenge; + + return _rxMessenge; } int rn2xx3::getSNR() From c4ef4b9a23e3db22320caae4a24a2434fcfb9ba9 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Wed, 3 Jan 2018 13:26:25 +0100 Subject: [PATCH 09/10] change frequency --- src/rn2xx3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 092589e..75ec2c9 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -347,7 +347,7 @@ bool rn2xx3::initP2P() { sendRawCommand(F("radio set freq 869100000")); break; case RN2483: - sendRawCommand(F("radio set freq 869100000")); + sendRawCommand(F("radio set freq 868000000")); break; default: From fb179167422a0aebdfe299a9c0ae129937f6ddc6 Mon Sep 17 00:00:00 2001 From: Dennis Ruigrok Date: Sat, 6 Jan 2018 22:53:53 +0100 Subject: [PATCH 10/10] timout to not block other activities --- examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino | 2 +- src/rn2xx3.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino index 5e9ebca..09e6888 100644 --- a/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino +++ b/examples/SodaqExplorer-p2p/SodaqExplorer-p2p.ino @@ -101,7 +101,7 @@ void loop() led_off(); - for(int i = 0; i < 3; i++) + for(int i = 0; i < 15000/500; i++) // 15 second listen then send ^ switch(myLora.listenP2P()) { case TX_WITH_RX: { diff --git a/src/rn2xx3.cpp b/src/rn2xx3.cpp index 75ec2c9..e48a29d 100644 --- a/src/rn2xx3.cpp +++ b/src/rn2xx3.cpp @@ -375,6 +375,8 @@ bool rn2xx3::initP2P() { sendRawCommand(F("radio set bw 125")); + sendRawCommand(F("radio set wdt 500")); + return true; } @@ -393,7 +395,7 @@ TX_RETURN_TYPE rn2xx3::listenP2P() { } else if(receivedData.startsWith("radio_rx")) { //example: radio_rx 54657374696E6720313233 _rxMessenge = receivedData.substring(receivedData.indexOf(' ', 9)+1); - + initP2P(); // to remove last messenge because it keeps repeating return TX_WITH_RX; }