Dragino and GPS shield


(Charley Guillaume) #1

Hello,

I’ve set up a dragino with a gps shield.
The whole process was done using this tutorial.
I’ve used the following script:

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial ss(3, 4); // Arduino RX, TX to conenct to GPS module.

static void smartdelay(unsigned long ms);

unsigned int count = 1;        //For times count


String datastring1="";        
String datastring2="";        
String datastring3="";
uint8_t datasend[20];     //Used to store GPS data for uploading

char gps_lon[20]={"\0"};  //Storage GPS info
char gps_lat[20]={"\0"}; //Storage latitude
char gps_alt[20]={"\0"}; //Storage altitude
float flat, flon,falt;

static uint8_t mydata[] = "Hello, world!";      //For test using.

/* LoRaWAN NwkSKey, network session key
   This is the default Semtech key, which is used by the prototype TTN
   network initially.
   ttn*/
static const PROGMEM u1_t NWKSKEY[16] =  { mynwkskey };

/* LoRaWAN AppSKey, application session key
   This is the default Semtech key, which is used by the prototype TTN
   network initially.
   ttn*/
static const u1_t PROGMEM APPSKEY[16] = {myappskey;

/*
 LoRaWAN end-device address (DevAddr)
 See http://thethingsnetwork.org/wiki/AddressSpace
 ttn*/
static const u4_t DEVADDR = mydevaddr;


/* These callbacks are only used in over-the-air activation, so they are
  left empty here (we cannot leave them out completely unless
   DISABLE_JOIN is set in config.h, otherwise the linker will complain).*/
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }


static osjob_t initjob,sendjob,blinkjob;

/* Schedule TX every this many seconds (might become longer due to duty
 cycle limitations).*/
const unsigned TX_INTERVAL = 20;

// Pin mapping
const lmic_pinmap lmic_pins = {
    .nss = 10,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 9,
    .dio = {2, 6, 7},
};

void do_send(osjob_t* j){
    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println("OP_TXRXPEND, not sending");
    } else {
        GPSRead();
        GPSWrite();
  
        // Prepare upstream data transmission at the next possible time.
          LMIC_setTxData2(1,datasend,sizeof(datasend)-1,0);
        //LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
        Serial.println("Packet queued");
        Serial.print("LMIC.freq:");
        Serial.println(LMIC.freq);
        Serial.println("");
        Serial.println("");
        Serial.println("Receive data:");
        
    } 
    // Next TX is scheduled after TX_COMPLETE event.
}

void onEvent (ev_t ev) {
    Serial.print(os_getTime());
    Serial.print(": ");
    Serial.println(ev);
    switch(ev) {
        case EV_SCAN_TIMEOUT:
            Serial.println("EV_SCAN_TIMEOUT");
            break;
        case EV_BEACON_FOUND:
            Serial.println("EV_BEACON_FOUND");
            break;
        case EV_BEACON_MISSED:
            Serial.println("EV_BEACON_MISSED");
            break;
        case EV_BEACON_TRACKED:
            Serial.println("EV_BEACON_TRACKED");
            break;
        case EV_JOINING:
            Serial.println("EV_JOINING");
            break;
        case EV_JOINED:
            Serial.println("EV_JOINED");
            break;
        case EV_RFU1:
            Serial.println("EV_RFU1");
            break;
        case EV_JOIN_FAILED:
            Serial.println("EV_JOIN_FAILED");
            break;
        case EV_REJOIN_FAILED:
            Serial.println("EV_REJOIN_FAILED");
            break;
        case EV_TXCOMPLETE:
            Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)");
            if(LMIC.dataLen) {
                // data received in rx slot after tx
                Serial.print("Data Received: ");
                Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
                Serial.println();
            }
            // Schedule next transmission
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            break;
        case EV_LOST_TSYNC:
            Serial.println("EV_LOST_TSYNC");
            break;
        case EV_RESET:
            Serial.println("EV_RESET");
            break;
        case EV_RXCOMPLETE:
            // data received in ping slot
            Serial.println("EV_RXCOMPLETE");
            break;
        case EV_LINK_DEAD:
            Serial.println("EV_LINK_DEAD");
            break;
        case EV_LINK_ALIVE:
            Serial.println("EV_LINK_ALIVE");
            break;
         default:
            Serial.println("Unknown event");
            break;
    }
}

void setup() {
     // initialize digital pin  as an output.
   
    Serial.begin(9600);
     ss.begin(9600);  
    while(!Serial);
    Serial.println("LoRa GPS Example---- ");
    Serial.println("Connect to TTN");
    #ifdef VCC_ENABLE
    // For Pinoccio Scout boards
    pinMode(VCC_ENABLE, OUTPUT);
    digitalWrite(VCC_ENABLE, HIGH);
    delay(1000);
    #endif

    // LMIC init
    os_init();
    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();
    /*LMIC_setClockError(MAX_CLOCK_ERROR * 1/100);
     Set static session parameters. Instead of dynamically establishing a session
     by joining the network, precomputed session parameters are be provided.*/
    #ifdef PROGMEM
    /* On AVR, these values are stored in flash and only copied to RAM
       once. Copy them to a temporary buffer here, LMIC_setSession will
       copy them into a buffer of its own again.*/
    uint8_t appskey[sizeof(APPSKEY)];
    uint8_t nwkskey[sizeof(NWKSKEY)];
    memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
    memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
    LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
    #else
    // If not running an AVR with PROGMEM, just use the arrays directly 
    LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
    #endif
    
    // Disable link check validation
    LMIC_setLinkCheckMode(0);

    // TTN uses SF9 for its RX2 window.
    LMIC.dn2Dr = DR_SF9;

   
    
    // Set data rate and transmit power (note: txpow seems to be ignored by the library)
    LMIC_setDrTxpow(DR_SF7,14);

    // Start job
    do_send(&sendjob);
}

void GPSRead()
{
  unsigned long age;
  gps.f_get_position(&flat, &flon, &age);
  falt=gps.f_altitude();  //get altitude    
  g_lat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6;   
  g_lon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;//save six decimal places 										   
  g_alt == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : falt, 2;//save two decimal places
 
  
}

void GPSWrite()
{
  /*Convert GPS data to format*/
  datastring1 +=dtostrf(flat, 0, 6, gps_lat);   
  datastring2 +=dtostrf(flon, 0, 6, gps_lon);
  //datastring3 +=dtostrf(falt, 0, 2, gps_alt);
  
  if(flon!=1000.000000)
  {  
  strcat(gps_lon,",");
  strcat(gps_lon,gps_lat); 
  //strcat(gps_lon,","); 
  //strcat(gps_lon,gps_alt);
    int i = 0;
  for(i = 0; i < 2; i++)
  {
      //datasend.toFloat();
      atof(gps_lon);
     //Serial.println((char*)datasend);
    Serial.println("Testing converted data:");
    Serial.println(gps_lon);
    // atof(gps_alt);
    // Serial.print(gps_alt);
  }
  
  strcpy(datasend,gps_lon); //the format of datasend is longtitude,latitude,altitude
  Serial.print("###########    ");
  Serial.print("NO.");
  Serial.print(count);
  Serial.println("    ###########");
  Serial.println("The longtitude and latitude are:");
  Serial.print("[");
  Serial.print((char*)datasend);
  Serial.print("]");
  Serial.print("");
  /*
  for(int k = 0; k < 20;k++)
  {
  Serial.print("[");
  Serial.print(datasend[k], HEX);
  Serial.print("]");
  }
  Serial.println("");
  Serial.println("");*/
  count++;
  }
  
  int32_t lng = flat * 10000;
  int32_t lat = flon * 10000;

  datasend[0] = lat;
  datasend[1] = lat >> 8;
  datasend[2] = lat >> 16;

  datasend[3] = lng;
  datasend[4] = lng >> 8;
  datasend[5] = lng >> 16;
  smartdelay(1000);
}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
    {
      gps.encode(ss.read());
    }
  } while (millis() - start < ms);
}


void loop() {
    os_runloop_once();
     
   
    
}

On the device side, I’m using thispayload format:

function Decoder(b, port) {
var lat = (b[0] | b[1]<<8 | b[2]<<16 | (b[2] & 0x80 ? 0xFF<<24 : 0)) / 10000;
var lng = (b[3] | b[4]<<8 | b[5]<<16 | (b[5] & 0x80 ? 0xFF<<24 : 0)) / 10000;
return {
location: {
latitude: lat,
longitude: lng
}
};
}

But the data I finally get is incorrect.

On the gateway side, I created one gateway using a RPI & IC880a.

I’ve created both the gateway and the application & device on thethingsnetwok, using the correct Device Address, Network Session Key & App Session Key (msb for the two latest).

Can you guide/help me solving that issue?

Regards,

Charley


#2

ok… maybe some examples with info from your console could help.


(Charley Guillaume) #3

True!

So here is what I got in my gateway:

image

Here is what I get in the device data tab:

image


(Jose Teix) #4

I have the same problem! Anyone can give some help?


(Jose Teix) #5

I figured it out. The GPS is outputting that data because it doesn´t have a fix, yet.
Link the GPS_RX and GPS_TX pins to a USB-TTL and lauch a serial software to see what it outputs.
You will see that in the $GPRMC command you won’t have any data in regards too the time, date and coordenates. That´s why your GPS don´t send the data correctly.
I got mine fixed after 2 hours but in this past weekend i left it off and when i came back couldn’t get a fix yet…
Try to put your GPS in an open space. It need´s at least 3 correct frames from satellites to get a fix!

Hope this helps,
Best Regards,
José