LoRa_APRS_Tracker/src/LoRa_APRS_Tracker.cpp

350 lines
8.5 KiB
C++
Raw Normal View History

2020-04-03 21:21:35 +00:00
#include <Arduino.h>
#include <LoRa.h>
#include <APRS-Decoder.h>
#include <TinyGPS++.h>
#include <TimeLib.h>
#include <WiFi.h>
2020-04-03 21:21:35 +00:00
#include "display.h"
2020-11-03 19:11:10 +00:00
#include "pins.h"
#include "power_management.h"
2021-03-03 20:58:31 +00:00
#include "configuration.h"
Configuration Config;
2020-04-03 21:21:35 +00:00
#include "power_management.h"
PowerManagement powerManagement;
2020-04-03 21:21:35 +00:00
2021-03-03 20:58:31 +00:00
#include "logger.h"
2020-05-17 14:16:53 +00:00
HardwareSerial ss(1);
TinyGPSPlus gps;
void load_config();
void setup_lora();
2020-05-17 14:16:53 +00:00
void setup_gps();
2021-03-03 20:58:31 +00:00
2020-04-03 21:21:35 +00:00
String create_lat_aprs(RawDegrees lat);
String create_long_aprs(RawDegrees lng);
2020-11-09 22:09:58 +00:00
String createDateString(time_t t);
String createTimeString(time_t t);
2021-03-05 14:01:07 +00:00
String getSmartBeaconState();
String padding(unsigned int number, unsigned int width);
2020-05-17 14:16:53 +00:00
2020-05-29 19:13:33 +00:00
// cppcheck-suppress unusedFunction
2020-04-03 21:21:35 +00:00
void setup()
{
Serial.begin(115200);
#ifdef TTGO_T_Beam_V1_0
Wire.begin(SDA, SCL);
if (!powerManagement.begin(Wire))
{
logPrintlnI("AXP192 init done!");
}
else
{
logPrintlnE("AXP192 init failed!");
}
powerManagement.activateLoRa();
powerManagement.activateOLED();
powerManagement.activateGPS();
powerManagement.activateMeasurement();
#endif
2020-04-03 21:21:35 +00:00
delay(500);
logPrintlnI("LoRa APRS Tracker by OE5BPA (Peter Buchegger)");
setup_display();
2021-03-05 14:01:07 +00:00
show_display("OE5BPA", "LoRa APRS Tracker", "by Peter Buchegger", 2000);
load_config();
2021-03-05 14:01:07 +00:00
2020-05-17 14:16:53 +00:00
setup_gps();
2020-04-03 21:21:35 +00:00
setup_lora();
// make sure wifi and bt is off as we don't need it:
WiFi.mode(WIFI_OFF);
btStop();
logPrintlnI("Smart Beacon is " + getSmartBeaconState());
show_display("INFO", "Smart Beacon is " + getSmartBeaconState(), 1000);
logPrintlnI("setup done...");
2021-03-05 14:01:07 +00:00
delay(500);
2020-04-03 21:21:35 +00:00
}
2020-05-29 19:13:33 +00:00
// cppcheck-suppress unusedFunction
2020-04-03 21:21:35 +00:00
void loop()
{
2021-03-07 22:08:49 +00:00
if(Config.debug)
2020-04-03 21:21:35 +00:00
{
2021-03-07 22:08:49 +00:00
while(Serial.available() > 0)
{
2021-03-07 22:08:49 +00:00
char c = Serial.read();
//Serial.print(c);
gps.encode(c);
}
2020-04-03 21:21:35 +00:00
}
else
2020-11-23 20:10:47 +00:00
{
2021-03-07 22:08:49 +00:00
while(ss.available() > 0)
{
2021-03-07 22:08:49 +00:00
char c = ss.read();
//Serial.print(c);
gps.encode(c);
}
2020-11-23 20:10:47 +00:00
}
2020-04-03 21:21:35 +00:00
bool gps_time_update = gps.time.isUpdated();
2020-11-23 20:10:47 +00:00
bool gps_loc_update = gps.location.isUpdated();
2020-11-09 22:09:58 +00:00
static time_t nextBeaconTimeStamp = -1;
static bool send_update = true;
2020-07-29 13:04:14 +00:00
2020-11-01 22:19:32 +00:00
if(gps.time.isValid())
2020-07-29 13:04:14 +00:00
{
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(), gps.date.day(), gps.date.month(), gps.date.year());
2020-11-01 22:19:32 +00:00
if(nextBeaconTimeStamp <= now())
2020-11-01 22:19:32 +00:00
{
send_update = true;
}
2020-11-23 20:10:47 +00:00
}
static double lastTxLat = 0.0;
static double lastTxLng = 0.0;
static double lastTxdistance = 0.0;
static unsigned long txInterval = 60000L; // Initial 60 secs internal
2020-11-23 20:10:47 +00:00
static double currentHeading = 0;
static double previousHeading = 0;
static int lastTxTime = millis();
if(Config.smart_beacon.active)
{
if(gps_loc_update)
2020-11-23 20:10:47 +00:00
{
lastTxdistance = TinyGPSPlus::distanceBetween(gps.location.lat(), gps.location.lng(), lastTxLat, lastTxLng);
// Get headings and heading delta
currentHeading = gps.course.deg();
double headingDelta = abs(previousHeading - currentHeading);
int lastTx = millis() - lastTxTime;
if(lastTx > Config.smart_beacon.min_bcn * 1000)
2020-11-23 20:10:47 +00:00
{
// Check for heading more than 25 degrees
if(headingDelta > Config.smart_beacon.turn_min && lastTxdistance > Config.smart_beacon.min_tx_dist)
{
send_update = true;
}
2020-11-23 20:10:47 +00:00
}
if(lastTx >= txInterval)
2020-11-23 20:10:47 +00:00
{
// Trigger Tx Tracker when Tx interval is reach
// Will not Tx if stationary bcos speed < 5 and lastTxDistance < 20
if (lastTxdistance > 20)
{
send_update = true;
}
2020-11-23 20:10:47 +00:00
}
}
2020-07-29 13:04:14 +00:00
}
if(send_update && gps_loc_update)
2020-07-29 13:04:14 +00:00
{
send_update = false;
nextBeaconTimeStamp = now() + (Config.beacon.timeout * SECS_PER_MIN);
2020-07-29 13:04:14 +00:00
APRSMessage msg;
2021-03-03 20:58:31 +00:00
msg.setSource(Config.callsign);
2021-03-19 20:06:07 +00:00
msg.setDestination("APLT00");
2020-07-29 13:04:14 +00:00
String lat = create_lat_aprs(gps.location.rawLat());
String lng = create_long_aprs(gps.location.rawLng());
String alt = padding((int)gps.altitude.feet(), 6);
String course = padding((int)gps.course.deg(), 3);
String speed = padding((int)gps.speed.knots(), 3);
2021-03-05 14:01:07 +00:00
#ifdef TTGO_T_Beam_V1_0
String batteryVoltage(powerManagement.getBatteryVoltage(), 2);
String batteryChargeCurrent(powerManagement.getBatteryChargeDischargeCurrent(), 0);
msg.getAPRSBody()->setData(String("=") + lat + Config.beacon.overlay + lng + Config.beacon.symbol + course + "/" + speed + "/A=" + alt + Config.beacon.message + " - Bat.: " + batteryVoltage + "V - Cur.: " + batteryChargeCurrent + "mA");
#else
msg.getAPRSBody()->setData(String("=") + lat + Config.beacon.overlay + lng + Config.beacon.symbol + course + "/" + speed + "/A=" + alt + Config.beacon.message);
#endif
2020-07-29 13:04:14 +00:00
String data = msg.encode();
logPrintlnD(data);
2020-07-29 13:04:14 +00:00
show_display("<< TX >>", data);
LoRa.beginPacket();
// Header:
LoRa.write('<');
LoRa.write(0xFF);
LoRa.write(0x01);
// APRS Data:
LoRa.write((const uint8_t *)data.c_str(), data.length());
LoRa.endPacket();
2020-11-23 20:10:47 +00:00
2021-03-05 14:01:07 +00:00
if (Config.smart_beacon.active)
{
2021-03-05 14:01:07 +00:00
lastTxLat = gps.location.lat();
lastTxLng = gps.location.lng();
previousHeading = currentHeading;
lastTxdistance = 0.0;
2021-03-05 14:01:07 +00:00
lastTxTime = millis();
}
2020-07-29 13:04:14 +00:00
}
if(gps_time_update)
{
#ifdef TTGO_T_Beam_V1_0
String batteryVoltage(powerManagement.getBatteryVoltage(), 2);
String batteryChargeCurrent(powerManagement.getBatteryChargeDischargeCurrent(), 0);
#endif
2021-03-03 20:58:31 +00:00
show_display(Config.callsign,
2020-11-09 22:09:58 +00:00
createDateString(now()) + " " + createTimeString(now()),
String("Sats: ") + gps.satellites.value() + " HDOP: " + gps.hdop.hdop(),
String("Nxt Bcn: ") + createTimeString(nextBeaconTimeStamp)
#ifdef TTGO_T_Beam_V1_0
2021-03-07 22:08:49 +00:00
, String("Bat: ") + batteryVoltage + "V, " + batteryChargeCurrent + "mA"
#endif
2021-03-07 22:08:49 +00:00
, String("Smart Beacon: " + getSmartBeaconState())
2020-04-03 21:21:35 +00:00
);
2020-11-23 20:10:47 +00:00
if(Config.smart_beacon.active)
{
2021-03-05 14:01:07 +00:00
// Change the Tx internal based on the current speed
if(gps.speed.kmph() < 5)
2021-03-05 14:01:07 +00:00
{
txInterval = 300000; // Change Tx internal to 5 mins
}
else if(gps.speed.kmph() < Config.smart_beacon.slow_speed)
2021-03-05 14:01:07 +00:00
{
txInterval = Config.smart_beacon.slow_rate * 1000;
}
else if(gps.speed.kmph() > Config.smart_beacon.fast_speed)
2021-03-05 14:01:07 +00:00
{
txInterval = Config.smart_beacon.fast_rate * 1000;
}
2021-03-05 14:01:07 +00:00
else
{
// Interval inbetween low and high speed
txInterval = (Config.smart_beacon.fast_speed / gps.speed.kmph()) * Config.smart_beacon.fast_rate * 1000;
}
2021-03-05 14:01:07 +00:00
}
2020-04-03 21:21:35 +00:00
}
2021-03-05 14:01:07 +00:00
if ((Config.debug == false) && (millis() > 5000 && gps.charsProcessed() < 10))
2020-04-03 21:21:35 +00:00
{
logPrintlnE("No GPS frames detected! Try to reset the GPS Chip with this firmware: https://github.com/lora-aprs/TTGO-T-Beam_GPS-reset");
2020-04-03 21:21:35 +00:00
}
}
2021-03-05 14:01:07 +00:00
void load_config()
{
ConfigurationManagement confmg("/tracker.json");
Config = confmg.readConfiguration();
if(Config.callsign == "NOCALL-10")
{
logPrintlnE("You have to change your settings in 'data/is-cfg.json' and upload it via \"Upload File System image\"!");
show_display("ERROR", "You have to change your settings in 'data/is-cfg.json' and upload it via \"Upload File System image\"!");
while(true)
{}
}
2020-04-03 21:21:35 +00:00
}
void setup_lora()
{
logPrintlnI("Set SPI pins!");
2020-05-11 14:11:07 +00:00
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
logPrintlnI("Set LoRa pins!");
2020-05-11 14:11:07 +00:00
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
2020-04-03 21:21:35 +00:00
2021-03-03 20:58:31 +00:00
long freq = Config.lora.frequencyTx;
logPrintI("frequency: ");
logPrintlnI(String(freq));
2020-04-03 21:21:35 +00:00
if (!LoRa.begin(freq)) {
logPrintlnE("Starting LoRa failed!");
2020-04-03 21:21:35 +00:00
show_display("ERROR", "Starting LoRa failed!");
while(true)
{}
2020-04-03 21:21:35 +00:00
}
2021-03-03 20:58:31 +00:00
LoRa.setSpreadingFactor(Config.lora.spreadingFactor);
LoRa.setSignalBandwidth(Config.lora.signalBandwidth);
LoRa.setCodingRate4(Config.lora.codingRate4);
2020-04-03 21:21:35 +00:00
LoRa.enableCrc();
2020-04-08 11:03:11 +00:00
LoRa.setTxPower(Config.lora.power);
logPrintlnI("LoRa init done!");
2020-04-03 21:21:35 +00:00
show_display("INFO", "LoRa init done!", 2000);
}
2020-05-17 14:16:53 +00:00
void setup_gps()
{
ss.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
}
2020-04-03 21:21:35 +00:00
String create_lat_aprs(RawDegrees lat)
{
char str[20];
char n_s = 'N';
if(lat.negative)
{
n_s = 'S';
}
sprintf(str, "%02d%05.2f%c", lat.deg, lat.billionths / 1000000000.0 * 60.0, n_s);
String lat_str(str);
return lat_str;
}
String create_long_aprs(RawDegrees lng)
{
char str[20];
char e_w = 'E';
if(lng.negative)
{
e_w = 'W';
}
sprintf(str, "%03d%05.2f%c", lng.deg, lng.billionths / 1000000000.0 * 60.0, e_w);
String lng_str(str);
return lng_str;
}
2020-11-09 22:09:58 +00:00
String createDateString(time_t t)
{
return String(padding(day(t), 2) + "." + padding(month(t), 2) + "." + padding(year(t), 4));
2020-11-09 22:09:58 +00:00
}
String createTimeString(time_t t)
{
2020-11-09 22:09:58 +00:00
if(t == -1)
{
return String("00:00:00");
}
return String(padding(hour(t), 2) + "." + padding(minute(t), 2) + "." + padding(second(t), 2));
}
2021-03-05 14:01:07 +00:00
String getSmartBeaconState()
{
if (Config.smart_beacon.active)
2021-03-05 14:01:07 +00:00
{
return "On";
2021-03-05 14:01:07 +00:00
}
return "Off";
2021-03-05 14:01:07 +00:00
}
String padding(unsigned int number, unsigned int width)
{
String result;
String num(number);
if(num.length() > width)
{
width = num.length();
}
for(unsigned int i = 0; i < width - num.length(); i++)
{
result.concat('0');
}
result.concat(num);
return result;
}