2020-04-03 21:21:35 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <LoRa.h>
|
|
|
|
#include <APRS-Decoder.h>
|
|
|
|
#include <TinyGPS++.h>
|
2020-11-08 13:14:56 +00:00
|
|
|
#include <TimeLib.h>
|
|
|
|
#include <WiFi.h>
|
2020-04-03 21:21:35 +00:00
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
#include "display.h"
|
2020-11-03 19:11:10 +00:00
|
|
|
#include "pins.h"
|
2020-11-08 13:14:56 +00:00
|
|
|
#include "power_management.h"
|
2020-04-03 21:21:35 +00:00
|
|
|
|
2020-11-08 13:14:56 +00:00
|
|
|
#include "power_management.h"
|
|
|
|
PowerManagement powerManagement;
|
2020-04-03 21:21:35 +00:00
|
|
|
|
2020-05-17 14:16:53 +00:00
|
|
|
HardwareSerial ss(1);
|
|
|
|
TinyGPSPlus gps;
|
2020-11-08 13:14:56 +00:00
|
|
|
|
|
|
|
void setup_lora();
|
2020-05-17 14:16:53 +00:00
|
|
|
void setup_gps();
|
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);
|
2020-05-17 14:16:53 +00:00
|
|
|
|
2020-11-01 22:19:32 +00:00
|
|
|
static bool send_update = true;
|
|
|
|
|
2020-11-23 20:10:47 +00:00
|
|
|
// Initial lat/lng pos, change to your base station coordnates
|
|
|
|
float lastTxLat = 0;
|
|
|
|
float lastTxLng = 0;
|
|
|
|
float lastTxdistance;
|
|
|
|
unsigned long txInterval = 60000L; // Initial 60 secs internal
|
|
|
|
int previousHeading, currentHeading, headingDelta, headingTresh = 0;
|
|
|
|
int lastTxTime = millis();
|
|
|
|
|
2020-05-29 19:13:33 +00:00
|
|
|
// cppcheck-suppress unusedFunction
|
2020-04-03 21:21:35 +00:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
2020-11-08 13:14:56 +00:00
|
|
|
|
|
|
|
#ifdef TTGO_T_Beam_V1_0
|
|
|
|
Wire.begin(SDA, SCL);
|
|
|
|
if (!powerManagement.begin(Wire))
|
|
|
|
{
|
|
|
|
Serial.println("[INFO] AXP192 init done!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.println("[ERROR] AXP192 init failed!");
|
|
|
|
}
|
|
|
|
powerManagement.activateLoRa();
|
|
|
|
powerManagement.activateOLED();
|
|
|
|
powerManagement.activateGPS();
|
2020-11-09 22:09:41 +00:00
|
|
|
powerManagement.activateMeasurement();
|
2020-11-08 13:14:56 +00:00
|
|
|
#endif
|
2020-04-03 21:21:35 +00:00
|
|
|
|
|
|
|
delay(500);
|
2020-04-05 18:19:31 +00:00
|
|
|
Serial.println("[INFO] LoRa APRS Tracker by OE5BPA (Peter Buchegger)");
|
2020-11-08 13:14:56 +00:00
|
|
|
setup_display();
|
2020-04-05 18:19:31 +00:00
|
|
|
show_display("OE5BPA", "LoRa APRS Tracker", "by Peter Buchegger", 2000);
|
2020-04-03 21:21:35 +00:00
|
|
|
|
2020-11-23 20:10:47 +00:00
|
|
|
#ifdef SB_ACTIVE
|
|
|
|
Serial.println("[INFO] Smart Beaconing Active");
|
|
|
|
show_display("DJ1AN", "Smart Beaconing","Active", 1000);
|
|
|
|
#endif
|
|
|
|
|
2020-05-17 14:16:53 +00:00
|
|
|
setup_gps();
|
2020-04-03 21:21:35 +00:00
|
|
|
setup_lora();
|
2020-11-08 13:14:56 +00:00
|
|
|
|
|
|
|
// make sure wifi and bt is off as we don't need it:
|
|
|
|
WiFi.mode(WIFI_OFF);
|
|
|
|
btStop();
|
|
|
|
|
2020-04-03 21:21:35 +00:00
|
|
|
delay(500);
|
2020-11-08 13:14:56 +00:00
|
|
|
Serial.println("[INFO] setup done...");
|
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()
|
|
|
|
{
|
2020-11-23 20:10:47 +00:00
|
|
|
#if !defined(DEBUGMODE)
|
2020-04-03 21:21:35 +00:00
|
|
|
while (ss.available() > 0)
|
|
|
|
{
|
|
|
|
char c = ss.read();
|
2020-04-05 18:19:31 +00:00
|
|
|
//Serial.print(c);
|
2020-04-03 21:21:35 +00:00
|
|
|
gps.encode(c);
|
|
|
|
}
|
2020-11-23 20:10:47 +00:00
|
|
|
#else
|
|
|
|
while (Serial.available() > 0)
|
|
|
|
{
|
|
|
|
char c = Serial.read();
|
|
|
|
//Serial.print(c);
|
|
|
|
gps.encode(c);
|
|
|
|
}
|
|
|
|
#endif
|
2020-04-03 21:21:35 +00:00
|
|
|
|
2020-11-08 13:14:56 +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;
|
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
|
|
|
{
|
2020-11-01 22:19:32 +00:00
|
|
|
setTime(gps.time.hour(),gps.time.minute(),gps.time.second(),gps.date.day(),gps.date.month(),gps.date.year());
|
|
|
|
|
2020-11-09 22:09:58 +00:00
|
|
|
if (nextBeaconTimeStamp <= now() || nextBeaconTimeStamp == -1)
|
2020-11-01 22:19:32 +00:00
|
|
|
{
|
|
|
|
send_update = true;
|
|
|
|
}
|
2020-11-23 20:10:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SB_ACTIVE
|
|
|
|
send_update = false;
|
|
|
|
if ( gps_loc_update )
|
|
|
|
{
|
|
|
|
lastTxdistance = TinyGPSPlus::distanceBetween(
|
|
|
|
gps.location.lat(),
|
|
|
|
gps.location.lng(),
|
|
|
|
lastTxLat,
|
|
|
|
lastTxLng
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get headings and heading delta
|
|
|
|
currentHeading = (int) gps.course.deg();
|
|
|
|
if ( currentHeading >= 180 )
|
|
|
|
{
|
|
|
|
currentHeading = currentHeading-180;
|
|
|
|
}
|
|
|
|
headingDelta = (int) ( previousHeading - currentHeading ) % 360;
|
|
|
|
int turn_slope = 100;
|
|
|
|
headingTresh = (float) SB_TURN_MIN + turn_slope / gps.speed.kmph();
|
|
|
|
|
|
|
|
int lastTx = millis() - lastTxTime;
|
|
|
|
|
|
|
|
if ( lastTx > SB_MIN_BCN * 1000)
|
|
|
|
{
|
|
|
|
//send_update = true;
|
|
|
|
// Check for heading more than 25 degrees
|
|
|
|
if ( (headingDelta < -SB_TURN_MIN || headingDelta > SB_TURN_MIN) && lastTxdistance > SB_MIN_TX_DIST )
|
|
|
|
{
|
|
|
|
send_update = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lastTx >= txInterval )
|
|
|
|
{
|
|
|
|
// 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-07-29 13:04:14 +00:00
|
|
|
}
|
2020-11-23 20:10:47 +00:00
|
|
|
#endif
|
2020-07-29 13:04:14 +00:00
|
|
|
|
2020-11-23 20:10:47 +00:00
|
|
|
if(send_update && gps.location.isValid() && gps_loc_update)
|
2020-07-29 13:04:14 +00:00
|
|
|
{
|
2020-11-09 22:09:41 +00:00
|
|
|
powerManagement.deactivateMeasurement();
|
2020-11-09 22:09:58 +00:00
|
|
|
nextBeaconTimeStamp = now() + (BEACON_TIMEOUT * SECS_PER_MIN);
|
2020-07-29 13:04:14 +00:00
|
|
|
send_update = false;
|
|
|
|
|
|
|
|
APRSMessage msg;
|
|
|
|
msg.setSource(CALL);
|
|
|
|
msg.setDestination("APLT0");
|
|
|
|
String lat = create_lat_aprs(gps.location.rawLat());
|
|
|
|
String lng = create_long_aprs(gps.location.rawLng());
|
2020-11-05 23:13:47 +00:00
|
|
|
msg.getAPRSBody()->setData(String("=") + lat + SYMBOL_OVERLAY + lng + SYMBOL_CODE + BEACON_MESSAGE);
|
2020-07-29 13:04:14 +00:00
|
|
|
String data = msg.encode();
|
|
|
|
Serial.println(data);
|
|
|
|
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-09 22:09:41 +00:00
|
|
|
powerManagement.activateMeasurement();
|
2020-11-23 20:10:47 +00:00
|
|
|
|
|
|
|
#ifdef SB_ACTIVE
|
|
|
|
lastTxLat = gps.location.lat();
|
|
|
|
lastTxLng = gps.location.lng();
|
|
|
|
previousHeading = currentHeading;
|
|
|
|
lastTxdistance = 0;
|
|
|
|
lastTxTime = millis();
|
|
|
|
#endif
|
|
|
|
|
2020-07-29 13:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(gps_time_update)
|
|
|
|
{
|
2020-11-08 13:14:56 +00:00
|
|
|
#ifdef TTGO_T_Beam_V1_0
|
|
|
|
String batteryVoltage(powerManagement.getBatteryVoltage(), 2);
|
|
|
|
String batteryChargeCurrent(powerManagement.getBatteryChargeDischargeCurrent(), 0);
|
|
|
|
#endif
|
2020-11-05 22:51:29 +00:00
|
|
|
|
2020-05-17 14:16:53 +00:00
|
|
|
show_display(CALL,
|
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)
|
2020-11-08 13:14:56 +00:00
|
|
|
#ifdef TTGO_T_Beam_V1_0
|
2020-11-09 22:09:58 +00:00
|
|
|
, String("Bat: ") + batteryVoltage + "V " + batteryChargeCurrent + "mA"
|
2020-11-08 13:14:56 +00:00
|
|
|
#endif
|
2020-04-03 21:21:35 +00:00
|
|
|
);
|
2020-11-23 20:10:47 +00:00
|
|
|
|
|
|
|
#ifdef SB_ACTIVE
|
|
|
|
// Change the Tx internal based on the current speed
|
|
|
|
if ( gps.speed.kmph() < 5 )
|
|
|
|
{
|
|
|
|
txInterval = 300000; // Change Tx internal to 5 mins
|
|
|
|
}
|
|
|
|
else if ( gps.speed.kmph() < SB_SLOW_SPEED )
|
|
|
|
{
|
|
|
|
txInterval = SB_SLOW_RATE * 1000; // Change Tx interval
|
|
|
|
}
|
|
|
|
else if ( gps.speed.kmph() > SB_FAST_SPEED)
|
|
|
|
{
|
|
|
|
txInterval = SB_FAST_RATE * 1000; // Change Tx interval
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Interval inbetween low and high speed
|
|
|
|
txInterval = (SB_FAST_SPEED / gps.speed.kmph()) * SB_FAST_RATE * 1000;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-03 21:21:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 20:10:47 +00:00
|
|
|
#if !defined(DEBUGMODE)
|
2020-04-03 21:21:35 +00:00
|
|
|
if(millis() > 5000 && gps.charsProcessed() < 10)
|
|
|
|
{
|
|
|
|
Serial.println("No GPS detected!");
|
|
|
|
}
|
2020-11-23 20:10:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-04-03 21:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_lora()
|
|
|
|
{
|
|
|
|
Serial.println("[INFO] Set SPI pins!");
|
2020-05-11 14:11:07 +00:00
|
|
|
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
|
|
|
|
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
|
2020-04-03 21:21:35 +00:00
|
|
|
Serial.println("[INFO] Set LoRa pins!");
|
|
|
|
|
2021-02-17 15:58:42 +00:00
|
|
|
long freq = FREQ_SET;
|
2020-04-03 21:21:35 +00:00
|
|
|
Serial.print("[INFO] frequency: ");
|
|
|
|
Serial.println(freq);
|
|
|
|
if (!LoRa.begin(freq)) {
|
|
|
|
Serial.println("[ERROR] Starting LoRa failed!");
|
|
|
|
show_display("ERROR", "Starting LoRa failed!");
|
|
|
|
while (1);
|
|
|
|
}
|
2021-02-17 15:58:42 +00:00
|
|
|
LoRa.setSpreadingFactor(SF_SET);
|
|
|
|
LoRa.setSignalBandwidth(BW_SET);
|
|
|
|
LoRa.setCodingRate4(CR_SET);
|
2020-04-03 21:21:35 +00:00
|
|
|
LoRa.enableCrc();
|
2020-04-08 11:03:11 +00:00
|
|
|
|
|
|
|
LoRa.setTxPower(20);
|
2020-04-03 21:21:35 +00:00
|
|
|
Serial.println("[INFO] LoRa init done!");
|
|
|
|
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-08 13:14:56 +00:00
|
|
|
|
2020-11-09 22:09:58 +00:00
|
|
|
String createDateString(time_t t)
|
|
|
|
{
|
|
|
|
char line[30];
|
|
|
|
sprintf(line, "%02d.%02d.%04d", day(t), month(t), year(t));
|
|
|
|
return String(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
String createTimeString(time_t t)
|
2020-11-08 13:14:56 +00:00
|
|
|
{
|
2020-11-09 22:09:58 +00:00
|
|
|
if(t == -1)
|
|
|
|
{
|
|
|
|
return String("00:00:00");
|
|
|
|
}
|
2020-11-08 13:14:56 +00:00
|
|
|
char line[30];
|
2020-11-09 22:09:58 +00:00
|
|
|
sprintf(line, "%02d:%02d:%02d", hour(t), minute(t), second(t));
|
2020-11-08 13:14:56 +00:00
|
|
|
return String(line);
|
|
|
|
}
|