summaryrefslogtreecommitdiffstats
path: root/tracker-stm32
diff options
context:
space:
mode:
Diffstat (limited to 'tracker-stm32')
-rw-r--r--tracker-stm32/README.md33
-rw-r--r--tracker-stm32/include/README39
-rw-r--r--tracker-stm32/lib/README46
-rw-r--r--tracker-stm32/platformio.ini18
-rw-r--r--tracker-stm32/src/main.ino207
5 files changed, 343 insertions, 0 deletions
diff --git a/tracker-stm32/README.md b/tracker-stm32/README.md
new file mode 100644
index 0000000..a2bda13
--- /dev/null
+++ b/tracker-stm32/README.md
@@ -0,0 +1,33 @@
+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 A.
+
+See src/main.ino for definitions.
+
+Pinout
+------
+
+- u-blox NEO module connected to PD5 and PD6
+- RFM96 connected to SPI A:
+ - MISO (Arduino 12), MOSI (Arduino 11), SCK (Arduino 13)
+ - NSS pin: PD14 (Arduino 10)
+ - DIO0 pin: PF3 (Arduino 8)
+ - RESET pin: PF15 (Arduino 9)
+
+- SD Card connected to SPI B:
+ - MISO (Arduino 12), MOSI (Arduino 11), SCK (Arduino 13)
+ - CS (Arduino 17)
+
+Build
+-----
+
+This project requires: https://platform.io
+
+Compile:
+
+ pio run
+
+Program NUCLEO board:
+
+ pio run -t upload
diff --git a/tracker-stm32/include/README b/tracker-stm32/include/README
new file mode 100644
index 0000000..194dcd4
--- /dev/null
+++ b/tracker-stm32/include/README
@@ -0,0 +1,39 @@
+
+This directory is intended for project header files.
+
+A header file is a file containing C declarations and macro definitions
+to be shared between several project source files. You request the use of a
+header file in your project source file (C, C++, etc) located in `src` folder
+by including it, with the C preprocessing directive `#include'.
+
+```src/main.c
+
+#include "header.h"
+
+int main (void)
+{
+ ...
+}
+```
+
+Including a header file produces the same results as copying the header file
+into each source file that needs it. Such copying would be time-consuming
+and error-prone. With a header file, the related declarations appear
+in only one place. If they need to be changed, they can be changed in one
+place, and programs that include the header file will automatically use the
+new version when next recompiled. The header file eliminates the labor of
+finding and changing all the copies as well as the risk that a failure to
+find one copy will result in inconsistencies within a program.
+
+In C, the usual convention is to give header files names that end with `.h'.
+It is most portable to use only letters, digits, dashes, and underscores in
+header file names, and at most one dot.
+
+Read more about using header files in official GCC documentation:
+
+* Include Syntax
+* Include Operation
+* Once-Only Headers
+* Computed Includes
+
+https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
diff --git a/tracker-stm32/lib/README b/tracker-stm32/lib/README
new file mode 100644
index 0000000..6debab1
--- /dev/null
+++ b/tracker-stm32/lib/README
@@ -0,0 +1,46 @@
+
+This directory is intended for project specific (private) libraries.
+PlatformIO will compile them to static libraries and link into executable file.
+
+The source code of each library should be placed in a an own separate directory
+("lib/your_library_name/[here are source files]").
+
+For example, see a structure of the following two libraries `Foo` and `Bar`:
+
+|--lib
+| |
+| |--Bar
+| | |--docs
+| | |--examples
+| | |--src
+| | |- Bar.c
+| | |- Bar.h
+| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
+| |
+| |--Foo
+| | |- Foo.c
+| | |- Foo.h
+| |
+| |- README --> THIS FILE
+|
+|- platformio.ini
+|--src
+ |- main.c
+
+and a contents of `src/main.c`:
+```
+#include <Foo.h>
+#include <Bar.h>
+
+int main (void)
+{
+ ...
+}
+
+```
+
+PlatformIO Library Dependency Finder will find automatically dependent
+libraries scanning project source files.
+
+More information about PlatformIO Library Dependency Finder
+- https://docs.platformio.org/page/librarymanager/ldf.html
diff --git a/tracker-stm32/platformio.ini b/tracker-stm32/platformio.ini
new file mode 100644
index 0000000..a622f22
--- /dev/null
+++ b/tracker-stm32/platformio.ini
@@ -0,0 +1,18 @@
+; PlatformIO Project Configuration File
+;
+; Build options: build flags, source filter
+; Upload options: custom upload port, speed and extra flags
+; Library options: dependencies, extra library storages
+; Advanced options: extra scripting
+;
+; Please visit documentation for the other options and examples
+; https://docs.platformio.org/page/projectconf.html
+
+[env:nucleo_h743zi]
+platform = ststm32
+board = nucleo_h743zi
+framework = arduino
+lib_deps =
+ jgromes/RadioLib@^6.0.0
+ sparkfun/SparkFun u-blox GNSS Arduino Library@^2.2.22
+ arduino-libraries/SD@^1.2.4
diff --git a/tracker-stm32/src/main.ino b/tracker-stm32/src/main.ino
new file mode 100644
index 0000000..da82e01
--- /dev/null
+++ b/tracker-stm32/src/main.ino
@@ -0,0 +1,207 @@
+#include <Arduino.h>
+#include <RadioLib.h>
+#include <Wire.h>
+#include <SparkFun_u-blox_GNSS_Arduino_Library.h>
+#include <SD.h>
+
+File myFile;
+
+constexpr int SD_CS = 17;
+
+// SX1278 has the following connections:
+// 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;
+}
+static char* get_mh(double lat, double lon, int size) {
+ static char locator[11];
+ double LON_F[]={20,2.0,0.083333,0.008333,0.0003472083333333333};
+ double LAT_F[]={10,1.0,0.0416665,0.004166,0.0001735833333333333};
+ int i;
+ lon += 180;
+ lat += 90;
+
+ if (size <= 0 || size > 10) size = 6;
+ size/=2; size*=2;
+
+ for (i = 0; i < size/2; i++){
+ if (i % 2 == 1) {
+ locator[i*2] = (char) (lon/LON_F[i] + '0');
+ locator[i*2+1] = (char) (lat/LAT_F[i] + '0');
+ } else {
+ locator[i*2] = letterize((int) (lon/LON_F[i]));
+ locator[i*2+1] = letterize((int) (lat/LAT_F[i]));
+ }
+ lon = fmod(lon, LON_F[i]);
+ lat = fmod(lat, LAT_F[i]);
+ }
+ locator[i*2]=0;
+ return locator;
+}
+
+void setup() {
+ Serial.begin(9600);
+ pinMode(LED_BUILTIN, OUTPUT);
+
+ pinMode(SD_CS, OUTPUT);
+
+ if (!SD.begin(SD_CS)) {
+ Serial.println("SD init failed!");
+ return;
+ }
+
+
+ Wire.begin();
+
+ SerialGNSS.begin(9600);
+ if (!gnss.begin(SerialGNSS)) {
+ while (true) {
+ Serial.println(F("Check GNSS"));
+ delay(4000);
+ }
+ }
+
+ //gnss.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
+
+ Serial.print(F("Version: "));
+ byte versionHigh = gnss.getProtocolVersionHigh();
+ Serial.print(versionHigh);
+ Serial.print(".");
+ byte versionLow = gnss.getProtocolVersionLow();
+ Serial.println(versionLow);
+
+ Serial.print(F("[RFM] Init "));
+ int state = radio.begin(433.900);
+ if (state == RADIOLIB_ERR_NONE) {
+ Serial.println(F("success!"));
+ } else {
+ Serial.print(F("failed, code "));
+ Serial.println(state);
+ 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);
+}
+
+void loop()
+{
+ const auto now = millis();
+ if (now - lastGnssPoll > 1000) {
+ lastGnssPoll = now;
+
+ long latitude = gnss.getLatitude();
+ Serial.print(F("Lat: "));
+ Serial.print(latitude);
+
+ long longitude = gnss.getLongitude();
+ Serial.print(F(" Long: "));
+ Serial.print(longitude);
+
+ char *locator = get_mh((double)latitude/1e7, (double)longitude/1e7, 10);
+ Serial.print(F(" Loc: "));
+ Serial.print(locator);
+
+ long altitude = gnss.getAltitude();
+ Serial.print(F(" Alt: "));
+ Serial.print(altitude);
+ Serial.print(F(" (mm)"));
+
+ long speed = gnss.getGroundSpeed();
+ Serial.print(F(" Speed: "));
+ 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)"));
+ */
+
+ 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();
+
+ if (now - lastPositionReport > TX_INTERVAL) {
+ lastPositionReport = now;
+
+ digitalWrite(LED_BUILTIN, HIGH);
+
+ ssize_t len = snprintf(positionReport, POSITION_REPORT_LEN, "HB9EGM %s", locator);
+
+ Serial.print(F("TX: "));
+ Serial.print(positionReport);
+
+ int state = radio.transmit(positionReport, len);
+
+ if (state == RADIOLIB_ERR_NONE) {
+ Serial.print(F(" RFM OK Datarate: "));
+ Serial.print(radio.getDataRate());
+ Serial.println(F(" bps"));
+
+ }
+ else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
+ Serial.println(F(" too long!"));
+
+ }
+ else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
+ Serial.println(F(" timeout!"));
+
+ }
+ else {
+ Serial.print(F(" failed, code "));
+ Serial.println(state);
+ }
+
+ digitalWrite(LED_BUILTIN, LOW);
+ }
+ }
+}