From cb1cc5e64c0db865de76de2e34f1bcb5f90a2dfd Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 10 May 2023 17:59:11 +0200 Subject: Transmit locator over LoRa --- README.md | 2 +- src/main.ino | 115 ++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 79 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 71a3e6d..cc43f96 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ NUCLEO-H743ZI APRS-LoRa Tracker =============================== -Connect u-blox NEO-M8N (or other u-blox receiver) to USART-B (RX PD5 and TX PD6) and RFM96 to SPI. +Connect u-blox NEO-M8N (or other u-blox receiver) to USART-B (RX PD5 and TX PD6) and RFM96 to SPI A and See src/main.ino for definitions. diff --git a/src/main.ino b/src/main.ino index fe4ef95..4bcca3e 100644 --- a/src/main.ino +++ b/src/main.ino @@ -4,15 +4,17 @@ #include // SX1278 has the following connections: -// NSS pin: 10 -// DIO0 pin: 2 -// RESET pin: 9 -// DIO1 pin: 3 -RFM96 radio = new Module(10, 2, 9, 3); - +// NSS pin: PD14 (Arduino 10) +// DIO0 pin: PF3 (Arduino 8) +// RESET pin: PF15 (Arduino 9) +RFM96 radio = new Module(10, 8, 9); HardwareSerial SerialGNSS(PD6, PD5); SFE_UBLOX_GNSS gnss; + long lastGnssPoll = 0; +long lastPositionReport = 0; +constexpr size_t POSITION_REPORT_LEN = 32; +char positionReport[POSITION_REPORT_LEN+1]; static char letterize(int x) { return (char) x + 65; @@ -66,9 +68,8 @@ void setup() { byte versionLow = gnss.getProtocolVersionLow(); Serial.println(versionLow); -#if 0 Serial.print(F("[RFM] Init ")); - int state = radio.begin(); + int state = radio.begin(433.900); if (state == RADIOLIB_ERR_NONE) { Serial.println(F("success!")); } else { @@ -77,8 +78,38 @@ void setup() { while (true); } + if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) { + Serial.println(F("Selected bandwidth is invalid for this module!")); + while (true); + } + + if (radio.setSpreadingFactor(12) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) { + Serial.println(F("Selected spreading factor is invalid for this module!")); + while (true); + } + + if (radio.setCodingRate(5) == RADIOLIB_ERR_INVALID_CODING_RATE) { + Serial.println(F("Selected coding rate is invalid for this module!")); + while (true); + } + + // NOTE: value 0x34 is reserved for LoRaWAN networks and should not be used + if (radio.setSyncWord(0x14) != RADIOLIB_ERR_NONE) { + Serial.println(F("Unable to set sync word!")); + while (true); + } + + if (radio.setOutputPower(16) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) { + Serial.println(F("Selected output power is invalid for this module!")); + while (true); + } + + if (radio.setPreambleLength(8) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) { + Serial.println(F("Selected preamble length is invalid for this module!")); + while (true); + } + // radio.setRfSwitchPins(4, 5); -#endif } void loop() @@ -95,7 +126,7 @@ void loop() Serial.print(F(" Long: ")); Serial.print(longitude); - char* locator = get_mh((double)latitude/1e7, (double)longitude/1e7, 6); + char *locator = get_mh((double)latitude/1e7, (double)longitude/1e7, 10); Serial.print(F(" Loc: ")); Serial.print(locator); @@ -109,45 +140,55 @@ void loop() Serial.print(speed); Serial.print(F(" (mm/s)")); - long heading = gnss.getHeading(); - Serial.print(F(" Heading: ")); - Serial.print(heading); - Serial.print(F(" (degrees * 10^-5)")); + /* + long heading = gnss.getHeading(); + Serial.print(F(" Heading: ")); + Serial.print(heading); + Serial.print(F(" (degrees * 10^-5)")); + */ byte SIV = gnss.getSIV(); Serial.print(F(" SIV: ")); Serial.print(SIV); + Serial.print(F(" TX in: ")); + constexpr long TX_INTERVAL = 15000; + Serial.print(TX_INTERVAL - (now - lastPositionReport)); + Serial.println(); - } -} -void loop2() -{ - digitalWrite(LED_BUILTIN, HIGH); - delay(1000); + if (now - lastPositionReport > TX_INTERVAL) { + lastPositionReport = now; - Serial.print(F("[RFM] Transmitting packet ... ")); + digitalWrite(LED_BUILTIN, HIGH); - int state = radio.transmit("Hello World!"); + ssize_t len = snprintf(positionReport, POSITION_REPORT_LEN, "HB9EGM %s", locator); - if (state == RADIOLIB_ERR_NONE) { - Serial.println(F(" success!")); - Serial.print(F("[RFM] Datarate:\t")); - Serial.print(radio.getDataRate()); - Serial.println(F(" bps")); + Serial.print(F("TX: ")); + Serial.print(positionReport); - } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) { - Serial.println(F("too long!")); + int state = radio.transmit(positionReport, len); - } else if (state == RADIOLIB_ERR_TX_TIMEOUT) { - Serial.println(F("timeout!")); + if (state == RADIOLIB_ERR_NONE) { + Serial.print(F(" RFM OK Datarate: ")); + Serial.print(radio.getDataRate()); + Serial.println(F(" bps")); - } else { - Serial.print(F("failed, code ")); - Serial.println(state); - } + } + else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) { + Serial.println(F(" too long!")); - digitalWrite(LED_BUILTIN, LOW); - delay(1000); + } + else if (state == RADIOLIB_ERR_TX_TIMEOUT) { + Serial.println(F(" timeout!")); + + } + else { + Serial.print(F(" failed, code ")); + Serial.println(state); + } + + digitalWrite(LED_BUILTIN, LOW); + } + } } -- cgit v1.2.3