Lora Raspberry Dragino GPS tracker

Hi everyone.
I have a problem sending GPS coordinates. In TTN it gets wrong coordinates. Can you look what’s wrong?
I used this guide:

My Code:

#include <stdio.h>
#include <time.h>
#include <wiringPi.h>
#include <lmic.h>
#include <hal.h>
#include <local_hal.h>







// LoRaWAN Application identifier (AppEUI)
// Not used in this example
static const u1_t APPEUI[8]  = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xF6, 0x02 };

// LoRaWAN DevEUI, unique device ID (LSBF)
// Not used in this example
static const u1_t DEVEUI[8]  = { 0xAA, 0x05, 0x07, 0x4C, 0x5A, 0x2B, 0x12, 0x9A };

// LoRaWAN NwkSKey, network session key 
// Use this key for The Things Network
static const u1_t DEVKEY[16] = { 0x36, 0x6A, 0x52, 0x5F, 0x7F, 0xB9, 0xE6, 0x89, 0x2E, 0x94, 0x98, 0x96, 0xDB, 0xB7, 0xB2, 0x97 };

// LoRaWAN AppSKey, application session key
// Use this key to get your data decrypted by The Things Network
static const u1_t ARTKEY[16] = { 0x2E, 0xDF, 0x89, 0x02, 0x05, 0x97, 0xE9, 0xE1, 0x69, 0x5B, 0xAD, 0x14, 0xF5, 0xD6, 0xC1, 0xE7 };

// LoRaWAN end-device address (DevAddr)
// See http://thethingsnetwork.org/wiki/AddressSpace
static const u4_t DEVADDR = 0x26011116 ; // <-- Change this address for every node!

//////////////////////////////////////////////////
// APPLICATION CALLBACKS
//////////////////////////////////////////////////

// provide application router ID (8 bytes, LSBF)
void os_getArtEui (u1_t* buf) {
    memcpy(buf, APPEUI, 8);
}

// provide device ID (8 bytes, LSBF)
void os_getDevEui (u1_t* buf) {
    memcpy(buf, DEVEUI, 8);
}

// provide device key (16 bytes)
void os_getDevKey (u1_t* buf) {
    memcpy(buf, DEVKEY, 16);
}

u4_t cntr=0;

static osjob_t sendjob;
bool next = false;
unsigned long entry;


uint txBuffer[9];
uint LatitudeBinary, LongitudeBinary;
uint altitudeGps;
uint hdopGps;
//stdin toLog;
uint coords[9];



float FIX_LAT = 52.605217;
float FIX_LONG = 1.111111;


// Pin mapping
lmic_pinmap pins = {
  .nss = 6,
  .rxtx = UNUSED_PIN, // Not connected on RFM92/RFM95
  .rst = 0,  // Needed on RFM92/RFM95
  .dio = {7,4,5}
};

void get_coords()
{
  LatitudeBinary = ((FIX_LAT + 90) / 180.0) * 16777215;
  LongitudeBinary = ((FIX_LONG + 180) / 360.0) * 16777215;

  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 = 350;
  txBuffer[6] = ( altitudeGps >> 8 ) & 0xFF;
  txBuffer[7] = altitudeGps & 0xFF;

  hdopGps = 40 / 10;
  txBuffer[8] = hdopGps & 0xFF;

  printf("%f", LatitudeBinary);
  printf("%f", LongitudeBinary);

}


void onEvent (ev_t ev) {
    //debug_event(ev);

    switch(ev) {
      // scheduled data sent (optionally data received)
      // note: this includes the receive window!
      case EV_TXCOMPLETE:
          // use this event to keep track of actual transmissions
          fprintf(stdout, "Event EV_TXCOMPLETE, time: %d\n", millis() / 1000);
          if(LMIC.dataLen) { // data received in rx slot after tx
              //debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
              fprintf(stdout, "Data Received!\n");
          }
          break;
       default:
          break;
    }
}

static void do_send(osjob_t* j){
      time_t t=time(NULL);
      fprintf(stdout, "[%x] (%ld) %s\n", hal_ticks(), t, ctime(&t));
      // Show TX channel (channel numbers are local to LMIC)
      // Check if there is not a current TX/RX job running
    if (LMIC.opmode & (1 << 7)) {
      fprintf(stdout, "OP_TXRXPEND, not sending");
    } else {
      get_coords();
      
      
      LMIC_setTxData2(1, (u1_t*) txBuffer, sizeof(txBuffer), 0);    }
    // Schedule a timed job to run at the given timestamp (absolute system time)
    os_setTimedCallback(j, os_getTime()+sec2osticks(20), do_send);
         
}


void setup() {
  // LMIC init
  wiringPiSetup();

  os_init();
  // Reset the MAC state. Session and pending data transfers will be discarded.
  LMIC_reset();
  // Set static session parameters. Instead of dynamically establishing a session 
  // by joining the network, precomputed session parameters are be provided.
  LMIC_setSession (0x1, DEVADDR, (u1_t*)DEVKEY, (u1_t*)ARTKEY);
  // Disable data rate adaptation
  LMIC_setAdrMode(0);
  // Disable link check validation
  LMIC_setLinkCheckMode(0);
  // Disable beacon tracking
  LMIC_disableTracking ();
  // Stop listening for downstream data (periodical reception)
  LMIC_stopPingable();
  // Set data rate and transmit power (note: txpow seems to be ignored by the library)
  LMIC_setDrTxpow(DR_SF7,14);
  //
}

void loop() {

do_send(&sendjob);

while(1) {
  os_runloop();
//  os_runloop_once();
  }
}


int main() {
  setup();

  while (1) {
    loop();
  }
  return 0;
}

1

Can you tell us what is ‘wrong’ with the co-ordinates ?

Coordinates are incorrect.

Do you think it might possibly help us to help you if you can reveal to us in what way the co-ordinates are ‘incorrect’ ?

Okay. In ttn lat:52.09 long: -172. Corrret cords in my position are: 52.60 19.03
In /dev/ttys0 cords are correct.
In my opinion code is wrong.

Hi @SzymonJk,

At a very basic level you appear to be trying to encode 9 bytes as payload.
36 bytes of payload are appearing at the TTN core - which is 4 x 9.

I suggest that you add a debug printf statement to dump the content of the txBuffer and then make sure that this is correct.

1 Like

Eventually ! Telling us what is wrong is so helpful to us volunteers.

1 Like

Hello,
in this code the GPS position is fixed.

float FIX_LAT = 52.605217;
float FIX_LONG = 1.111111;

The TTN out shows other cords. Seems that the magic in get_coords (on the sending node) is not reversed by the decoder on the TTN Payload Formats site for this application.
Thank you, Tore

In my code lacks a function that would retrieve data from gps. In the code there are only numeric constants assigned to lat and long? do I understand correctly?

Hello,
there are at least two problems:

  • one is that the GPS cords are fixed data
  • the second is that this cords are not decoded correctly

The first problem is no problem as the node aka tracker will always stay at the same place. You just need to edit the cords.
Thank you, Tore

1 Like

I need a tracker for the stratospheric balloon. I need gps data to locate this balloon. i can’t edit the cords.

Hello,
please try to fix the decoder (so the fixed cords from your code are shown in TTN) before you start to add a GPS to your hardware.

The TTGO ESP32 board that Andreas Spiess is using in his video #271 has no GPS.
Thank you, Tore

Hi @SzymonJk,

I think that your problem is that you have declared your txBuffer as type uint, length 9.
This is 9 x 4 byte unsigned integers = 36 bytes.
You then poke bytes into each location so you are getting:
byte 0 0 0 byte 0 0 0 byte 0 0 0 … etc.
This is clearly visible in your payload.
Fix this and you should be good.

1 Like

Ok. I have repaired and now I have the same coordinate in ttn.
Bez%C2%A0tytu%C5%82u

Unfortunately, I have a problem with GPS. I added the TinyGPS library : https://github.com/mikalhart/TinyGPS

I added code that should add coordinates, but there are compilation errors.
Open picture in a new tab
1

Code:
#include <TinyGPS.h>
TinyGPS gps;

unsigned int count = 1;

float  flat, flon;
long int falt;

void GPSRead()
{
 unsigned long age;
 gps.f_get_position(&flat, &flon, &age);
  falt=gps.f_altitude(); 

 //get altitude 
 
}

In arduino code is " SoftwareSerial ss(3, 4); // Arduino RX, TX to conenct to GPS module". I must use this in Raspberry ?

What I have to do?

EDIT:
do I have to open a port ttyS0 in code?
if ((serial_port = serialOpen ("/dev/ttyS0", 9600)) < 0) /* open serial port */
{
In C : https://www.electronicwings.com/raspberry-pi/gps-module-interfacing-with-raspberry-pi

TinyGPS is an Arduino library, is it compatible with the Raspberry Pi ?

In my opinion it is not compatible with RPi