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;
}