ttgo-t-beam-gps-ttn-tracker/main/gps.ino

129 lines
3.6 KiB
Arduino
Raw Normal View History

2019-02-10 18:35:12 +00:00
/*
2019-08-23 14:22:03 +00:00
GPS module
2019-02-10 18:35:12 +00:00
2019-08-23 14:22:03 +00:00
Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
2019-02-10 18:35:12 +00:00
2019-08-23 14:22:03 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2019-02-10 18:35:12 +00:00
2019-08-23 14:22:03 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2019-02-10 18:35:12 +00:00
2019-08-23 14:22:03 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-02-10 18:35:12 +00:00
*/
#include <TinyGPS++.h>
2019-08-25 17:26:21 +00:00
uint32_t LatitudeBinary;
2019-08-25 17:24:48 +00:00
uint32_t LongitudeBinary;
2019-02-10 18:35:12 +00:00
uint16_t altitudeGps;
uint8_t hdopGps;
2019-08-25 17:24:48 +00:00
uint8_t sats;
2019-02-10 18:35:12 +00:00
char t[32]; // used to sprintf for Serial output
TinyGPSPlus _gps;
HardwareSerial _serial_gps(GPS_SERIAL_NUM);
void gps_time(char * buffer, uint8_t size) {
snprintf(buffer, size, "%02d:%02d:%02d", _gps.time.hour(), _gps.time.minute(), _gps.time.second());
}
float gps_latitude() {
return _gps.location.lat();
}
float gps_longitude() {
return _gps.location.lng();
}
float gps_altitude() {
return _gps.altitude.meters();
}
float gps_hdop() {
return _gps.hdop.hdop();
}
uint8_t gps_sats() {
return _gps.satellites.value();
}
void gps_setup() {
_serial_gps.begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
}
static void gps_loop() {
while (_serial_gps.available()) {
_gps.encode(_serial_gps.read());
}
}
2019-08-23 14:22:03 +00:00
2019-08-23 22:05:07 +00:00
#if defined(PAYLOAD_USE_FULL)
2019-08-23 14:22:03 +00:00
// More data than PAYLOAD_USE_CAYENNE
2019-08-23 22:05:07 +00:00
void buildPacket(uint8_t txBuffer[10])
2019-08-23 14:22:03 +00:00
{
LatitudeBinary = ((_gps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((_gps.location.lng() + 180) / 360.0) * 16777215;
2019-08-26 22:05:23 +00:00
altitudeGps = _gps.altitude.meters();
hdopGps = _gps.hdop.value() / 10;
sats = _gps.satellites.value();
2019-08-23 14:22:03 +00:00
sprintf(t, "Lat: %f", _gps.location.lat());
Serial.println(t);
sprintf(t, "Lng: %f", _gps.location.lng());
Serial.println(t);
sprintf(t, "Alt: %d", altitudeGps);
2019-08-26 22:05:23 +00:00
Serial.println(t);
sprintf(t, "Hdop: %d", hdopGps);
Serial.println(t);
sprintf(t, "Sats: %d", sats);
Serial.println(t);
2019-08-23 14:22:03 +00:00
txBuffer[0] = ( LatitudeBinary >> 16 ) & 0xFF;
txBuffer[1] = ( LatitudeBinary >> 8 ) & 0xFF;
txBuffer[2] = LatitudeBinary & 0xFF;
txBuffer[3] = ( LongitudeBinary >> 16 ) & 0xFF;
txBuffer[4] = ( LongitudeBinary >> 8 ) & 0xFF;
txBuffer[5] = LongitudeBinary & 0xFF;
txBuffer[6] = ( altitudeGps >> 8 ) & 0xFF;
txBuffer[7] = altitudeGps & 0xFF;
txBuffer[8] = hdopGps & 0xFF;
2019-08-23 22:05:07 +00:00
txBuffer[9] = sats & 0xFF;
2019-08-23 14:22:03 +00:00
}
2019-08-23 22:05:07 +00:00
#elif defined(PAYLOAD_USE_CAYENNE)
// CAYENNE DF
void buildPacket(uint8_t txBuffer[11])
{
sprintf(t, "Lat: %f", _gps.location.lat());
Serial.println(t);
sprintf(t, "Lng: %f", _gps.location.lng());
Serial.println(t);
sprintf(t, "Alt: %f", _gps.altitude.meters());
Serial.println(t);
2019-07-14 02:03:28 +00:00
int32_t lat = _gps.location.lat() * 10000;
int32_t lon = _gps.location.lng() * 10000;
int32_t alt = _gps.altitude.meters() * 100;
2019-07-14 02:03:28 +00:00
txBuffer[2] = lat >> 16;
txBuffer[3] = lat >> 8;
txBuffer[4] = lat;
txBuffer[5] = lon >> 16;
txBuffer[6] = lon >> 8;
txBuffer[7] = lon;
txBuffer[8] = alt >> 16;
txBuffer[9] = alt >> 8;
txBuffer[10] = alt;
2019-08-23 14:22:03 +00:00
}
2019-08-23 22:05:07 +00:00
#endif