Fixed Cayenne LPP gps pos implementation

This commit is contained in:
Scobber 2019-07-12 22:43:39 +10:00
parent 30a4166ed6
commit d9cc01a8af
9 changed files with 215 additions and 250 deletions

View File

@ -1,4 +1,6 @@
{
"board": "arduino:avr:yun",
"sketch": "main.ino"
"board": "esp32:esp32:ttgo-lora32-v1",
"sketch": "main.ino",
"configuration": "FlashFreq=80,UploadSpeed=921600,DebugLevel=none",
"port": "COM8"
}

View File

@ -3,12 +3,14 @@
{
"name": "Win32",
"includePath": [
"C:\\Users\\Scott\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\**",
"C:\\Users\\Scott\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.2\\**",
"C:\\Users\\Scott\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\**",
"C:\\Users\\Scott\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.5.2\\**",
"C:\\Program Files (x86)\\Arduino\\tools\\**",
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**"
],
"forcedInclude": [
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
],
"forcedInclude": [],
"intelliSenseMode": "msvc-x64",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",

View File

@ -43,7 +43,7 @@ void ttn_register(void (*callback)(uint8_t message));
#define DEBUG_PORT Serial // Serial debug port
#define SERIAL_BAUD 115200 // Serial debug baud rate
#define SLEEP_BETWEEN_MESSAGES 0 // Do sleep between messages
#define SEND_INTERVAL 30000 // Sleep for these many millis
#define SEND_INTERVAL 10000 // Sleep for these many millis
#define MESSAGE_TO_SLEEP_DELAY 5000 // Time after message before going to sleep
#define LOGO_DELAY 5000 // Time to show logo on first boot
#define LORAWAN_PORT 10 // Port the messages will be sent to

View File

@ -13,12 +13,12 @@ Credentials file
#ifdef USE_ABP
// LoRaWAN NwkSKey, network session key
static const u1_t PROGMEM NWKSKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u1_t PROGMEM NWKSKEY[16] = { 0xD1, 0x8A, 0x1F, 0x4D, 0x74, 0x69, 0x46, 0xE9, 0x20, 0x8F, 0x05, 0xE8, 0xFC, 0xB4, 0x5F, 0xC5 };
// LoRaWAN AppSKey, application session key
static const u1_t PROGMEM APPSKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u1_t PROGMEM APPSKEY[16] = { 0x42, 0x42, 0xD0, 0xA2, 0xAB, 0xA8, 0x23, 0x93, 0x19, 0x80, 0x9F, 0x90, 0xB3, 0xB4, 0x82, 0x34 };
// LoRaWAN end-device address (DevAddr)
// This has to be unique for every node
static const u4_t DEVADDR = 0x00000000;
static const u4_t DEVADDR = 0x26041F5B;
#endif
@ -28,15 +28,15 @@ Credentials file
// first. When copying an EUI from ttnctl output, this means to reverse
// the bytes. For TTN issued EUIs the last bytes should be 0x00, 0x00,
// 0x00.
static const u1_t PROGMEM APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u1_t PROGMEM APPEUI[8] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xE9, 0x44 };
// This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u1_t PROGMEM DEVEUI[8] = { 0xCB, 0xBE, 0xEB, 0x0A, 0xDD, 0xEA, 0xDB, 0xEE };
// This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In
// practice, a key taken from ttnctl can be copied as-is.
// The key shown here is the semtech default key.
static const u1_t PROGMEM APPKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u1_t PROGMEM APPKEY[16] = { 0x0B, 0xF2, 0xAC, 0xB0, 0x6A, 0xFF, 0x27, 0x1E, 0x89, 0x65, 0x7C, 0xBD, 0x99, 0x9F, 0x7F, 0xE9 };
#endif

View File

@ -1,68 +0,0 @@
/*
Packet Processor
*/
#pragma once
#ifdef USE_CAYENNE
// CAYENNE DF
static uint8_t txBuffer[11] = {0x03, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void buildPacket(uint8_t txBuffer[11])
{
LatitudeBinary = ((_gps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((_gps.location.lng() + 180) / 360.0) * 16777215;
int32_t Height = gps.altitude.meters() * 100;
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);
txBuffer[2] = ( LatitudeBinary >> 16 ) & 0xFF;
txBuffer[3] = ( LatitudeBinary >> 8 ) & 0xFF;
txBuffer[4] = LatitudeBinary & 0xFF;
txBuffer[5] = ( LongitudeBinary >> 16 ) & 0xFF;
txBuffer[6] = ( LongitudeBinary >> 8 ) & 0xFF;
txBuffer[7] = LongitudeBinary & 0xFF;
txBuffer[8] = Height >> 16;
txBuffer[9] = Height >> 8;
txBuffer[10] = Height;
}
#else
uint8_t txBuffer[9];
void buildPacket(uint8_t txBuffer[9])
{
LatitudeBinary = ((_gps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((_gps.location.lng() + 180) / 360.0) * 16777215;
sprintf(t, "Lat: %f", _gps.location.lat());
Serial.println(t);
sprintf(t, "Lng: %f", _gps.location.lng());
Serial.println(t);
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;
altitudeGps = _gps.altitude.meters();
txBuffer[6] = ( altitudeGps >> 8 ) & 0xFF;
txBuffer[7] = altitudeGps & 0xFF;
hdopGps = _gps.hdop.value()/10;
txBuffer[8] = hdopGps & 0xFF;
}
#endif

View File

@ -62,5 +62,61 @@ static void gps_loop() {
_gps.encode(_serial_gps.read());
}
}
#ifdef USE_CAYENNE
// CAYENNE DF
void buildPacket(uint8_t txBuffer[11])
{
LatitudeBinary = ((_gps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((_gps.location.lng() + 180) / 360.0) * 16777215;
int32_t Height = _gps.altitude.meters() * 100;
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);
txBuffer[2] = ( LatitudeBinary >> 16 ) & 0xFF;
txBuffer[3] = ( LatitudeBinary >> 8 ) & 0xFF;
txBuffer[4] = LatitudeBinary & 0xFF;
txBuffer[5] = ( LongitudeBinary >> 16 ) & 0xFF;
txBuffer[6] = ( LongitudeBinary >> 8 ) & 0xFF;
txBuffer[7] = LongitudeBinary & 0xFF;
txBuffer[8] = Height >> 16;
txBuffer[9] = Height >> 8;
txBuffer[10] = Height;
}
#else
uint8_t txBuffer[9];
void buildPacket(uint8_t txBuffer[9])
{
LatitudeBinary = ((_gps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((_gps.location.lng() + 180) / 360.0) * 16777215;
sprintf(t, "Lat: %f", _gps.location.lat());
Serial.println(t);
sprintf(t, "Lng: %f", _gps.location.lng());
Serial.println(t);
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;
altitudeGps = _gps.altitude.meters();
txBuffer[6] = ( altitudeGps >> 8 ) & 0xFF;
txBuffer[7] = altitudeGps & 0xFF;
hdopGps = _gps.hdop.value()/10;
txBuffer[8] = hdopGps & 0xFF;
}
#endif

View File

@ -6,7 +6,7 @@
// inside the project_config folder.
// Make sure only one of the following is defined (CFG_us915 or CFG_eu868)
#define CFG_us915 1
#define CFG_aus915 1
//#define CFG_eu868 1
#define CFG_sx1276_radio 1

View File

@ -1,29 +1,28 @@
/*
Main module
Main module
# Modified by Kyle T. Gabriel to fix issue with incorrect GPS data for TTNMapper
# Modified by Kyle T. Gabriel to fix issue with incorrect GPS data for TTNMapper
Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
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.
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.
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.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "configuration.h"
#include <rom/rtc.h>
#include "dataformats.h"
#include "rom/rtc.h"
// Message counter, stored in RTC memory, survives deep sleep
@ -31,202 +30,167 @@ RTC_DATA_ATTR uint32_t count = 0;
// -----------------------------------------------------------------------------
// Submodules
// -----------------------------------------------------------------------------
#ifdef USE_GPS 1
#include <TinyGPS++.h>
uint32_t LatitudeBinary, LongitudeBinary;
uint16_t altitudeGps;
uint8_t hdopGps;
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());
}
}
#ifdef USE_CAYENNE
// CAYENNE DF
static uint8_t txBuffer[11] = {0x03, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#else
uint8_t txBuffer[9];
#endif
// -----------------------------------------------------------------------------
// Application
// -----------------------------------------------------------------------------
void send() {
char buffer[40];
snprintf(buffer, sizeof(buffer), "Latitude: %10.6f\n", gps_latitude());
screen_print(buffer);
snprintf(buffer, sizeof(buffer), "Longitude: %10.6f\n", gps_longitude());
screen_print(buffer);
snprintf(buffer, sizeof(buffer), "Error: %4.2fm\n", gps_hdop());
screen_print(buffer);
char buffer[40];
snprintf(buffer, sizeof(buffer), "Latitude: %10.6f\n", gps_latitude());
screen_print(buffer);
snprintf(buffer, sizeof(buffer), "Longitude: %10.6f\n", gps_longitude());
screen_print(buffer);
snprintf(buffer, sizeof(buffer), "Error: %4.2fm\n", gps_hdop());
screen_print(buffer);
buildPacket(txBuffer);
buildPacket(txBuffer);
#if LORAWAN_CONFIRMED_EVERY > 0
bool confirmed = (count % LORAWAN_CONFIRMED_EVERY == 0);
#else
bool confirmed = false;
#endif
#if LORAWAN_CONFIRMED_EVERY > 0
bool confirmed = (count % LORAWAN_CONFIRMED_EVERY == 0);
#else
bool confirmed = false;
#endif
ttn_cnt(count);
ttn_send(txBuffer, sizeof(txBuffer), LORAWAN_PORT, confirmed);
ttn_cnt(count);
ttn_send(txBuffer, sizeof(txBuffer), LORAWAN_PORT, confirmed);
count++;
count++;
}
void sleep() {
#if SLEEP_BETWEEN_MESSAGES
#if SLEEP_BETWEEN_MESSAGES
// Show the going to sleep message on the screen
char buffer[20];
snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0));
screen_print(buffer);
// Show the going to sleep message on the screen
char buffer[20];
snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0));
screen_print(buffer);
// Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep
delay(MESSAGE_TO_SLEEP_DELAY);
// Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep
delay(MESSAGE_TO_SLEEP_DELAY);
// Turn off screen
screen_off();
// Turn off screen
screen_off();
// Set the user button to wake the board
sleep_interrupt(BUTTON_PIN, LOW);
// Set the user button to wake the board
sleep_interrupt(BUTTON_PIN, LOW);
// We sleep for the interval between messages minus the current millis
// this way we distribute the messages evenly every SEND_INTERVAL millis
uint32_t sleep_for = (millis() < SEND_INTERVAL) ? SEND_INTERVAL - millis() : SEND_INTERVAL;
sleep_millis(sleep_for);
// We sleep for the interval between messages minus the current millis
// this way we distribute the messages evenly every SEND_INTERVAL millis
uint32_t sleep_for = (millis() < SEND_INTERVAL) ? SEND_INTERVAL - millis() : SEND_INTERVAL;
sleep_millis(sleep_for);
#endif
#endif
}
void callback(uint8_t message) {
if (EV_JOINING == message) screen_print("Joining TTN...\n");
if (EV_JOINED == message) screen_print("TTN joined!\n");
if (EV_JOIN_FAILED == message) screen_print("TTN join failed\n");
if (EV_REJOIN_FAILED == message) screen_print("TTN rejoin failed\n");
if (EV_RESET == message) screen_print("Reset TTN connection\n");
if (EV_LINK_DEAD == message) screen_print("TTN link dead\n");
if (EV_ACK == message) screen_print("ACK received\n");
if (EV_PENDING == message) screen_print("Message discarded\n");
if (EV_QUEUED == message) screen_print("Message queued\n");
if (EV_JOINING == message) screen_print("Joining TTN...\n");
if (EV_JOINED == message) screen_print("TTN joined!\n");
if (EV_JOIN_FAILED == message) screen_print("TTN join failed\n");
if (EV_REJOIN_FAILED == message) screen_print("TTN rejoin failed\n");
if (EV_RESET == message) screen_print("Reset TTN connection\n");
if (EV_LINK_DEAD == message) screen_print("TTN link dead\n");
if (EV_ACK == message) screen_print("ACK received\n");
if (EV_PENDING == message) screen_print("Message discarded\n");
if (EV_QUEUED == message) screen_print("Message queued\n");
if (EV_TXCOMPLETE == message) {
screen_print("Message sent\n");
sleep();
}
if (EV_RESPONSE == message) {
screen_print("[TTN] Response: ");
size_t len = ttn_response_len();
uint8_t data[len];
ttn_response(data, len);
char buffer[6];
for (uint8_t i=0; i<len; i++) {
snprintf(buffer, sizeof(buffer), "%02X", data[i]);
screen_print(buffer);
}
screen_print("\n");
if (EV_TXCOMPLETE == message) {
screen_print("Message sent\n");
sleep();
}
if (EV_RESPONSE == message) {
screen_print("[TTN] Response: ");
size_t len = ttn_response_len();
uint8_t data[len];
ttn_response(data, len);
char buffer[6];
for (uint8_t i = 0; i < len; i++) {
snprintf(buffer, sizeof(buffer), "%02X", data[i]);
screen_print(buffer);
}
screen_print("\n");
}
}
uint32_t get_count() {
return count;
return count;
}
void setup() {
// Debug
#ifdef DEBUG_PORT
DEBUG_PORT.begin(SERIAL_BAUD);
#endif
// Debug
#ifdef DEBUG_PORT
DEBUG_PORT.begin(SERIAL_BAUD);
#endif
// Buttons & LED
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
// Buttons & LED
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
// Hello
DEBUG_MSG(APP_NAME " " APP_VERSION "\n");
// Hello
DEBUG_MSG(APP_NAME " " APP_VERSION "\n");
// Display
screen_setup();
// Display
screen_setup();
// Init GPS
gps_setup();
// Init GPS
gps_setup();
// Show logo on first boot
if (0 == count) {
screen_print(APP_NAME " " APP_VERSION, 0, 0);
screen_show_logo();
screen_update();
delay(LOGO_DELAY);
}
// Show logo on first boot
if (0 == count) {
screen_print(APP_NAME " " APP_VERSION, 0, 0);
screen_show_logo();
screen_update();
delay(LOGO_DELAY);
}
// TTN setup
if (!ttn_setup()) {
screen_print("[ERR] Radio module not found!\n");
delay(MESSAGE_TO_SLEEP_DELAY);
screen_off();
sleep_forever();
}
// TTN setup
if (!ttn_setup()) {
screen_print("[ERR] Radio module not found!\n");
delay(MESSAGE_TO_SLEEP_DELAY);
screen_off();
sleep_forever();
}
ttn_register(callback);
ttn_join();
ttn_sf(LORAWAN_SF);
ttn_adr(LORAWAN_ADR);
ttn_register(callback);
ttn_join();
ttn_sf(LORAWAN_SF);
ttn_adr(LORAWAN_ADR);
}
void loop() {
gps_loop();
ttn_loop();
screen_loop();
gps_loop();
ttn_loop();
screen_loop();
// Send every SEND_INTERVAL millis
static uint32_t last = 0;
static bool first = true;
if (0 == last || millis() - last > SEND_INTERVAL) {
if (0 < gps_hdop() && gps_hdop() < 50 && gps_latitude() != 0 && gps_longitude() != 0) {
last = millis();
first = false;
send();
} else {
if (first) {
screen_print("Waiting GPS lock\n");
first = false;
}
if (millis() > GPS_WAIT_FOR_LOCK) {
sleep();
}
}
// Send every SEND_INTERVAL millis
static uint32_t last = 0;
static bool first = true;
if (0 == last || millis() - last > SEND_INTERVAL) {
if (0 < gps_hdop() && gps_hdop() < 50 && gps_latitude() != 0 && gps_longitude() != 0) {
last = millis();
first = false;
Serial.println("TRANSMITTING");
send();
} else {
if (first) {
screen_print("Waiting GPS lock\n");
first = false;
}
if (millis() > GPS_WAIT_FOR_LOCK) {
sleep();
}
}
}
}

View File

@ -155,6 +155,14 @@ void ttn_join() {
// https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
LMIC_selectSubBand(1);
#elif defined(CFG_au915)
Serial.println("AU_915");
// NA-US channels 0-71 are configured automatically
// but only one group of 8 should (a subband) should be active
// TTN recommends the second sub band, 1 in a zero based count.
// https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
LMIC_selectSubBand(1);
#endif
// If using a mono-channel gateway disable all channels
@ -210,6 +218,7 @@ void ttn_send(uint8_t * data, uint8_t data_size, uint8_t port, bool confirmed){
// Prepare upstream data transmission at the next possible time.
// Parameters are port, data, length, confirmed
LMIC_setTxData2(port, data, data_size, confirmed ? 1 : 0);
_ttn_callback(EV_QUEUED);