[go: up one dir, main page]

0% found this document useful (0 votes)
39 views3 pages

TTN Example

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

D:\Mijn Documenten\01. Commint\13. Ontwikkelingsprojecten\43, Seeeduino\SW\Implementaties\GPS\ABP-ttn-gps-0.2b-test\ABP-ttn-gps-0.2b-test.

ino
maandag 21 augustus 2017 8:46
#include <TinyGPS.h>
#include <LoRaWan.h>

TinyGPS gps;

char memBuffer[256];
char packetBuffer[51];

char DevEUI[] = "";


char AppEUI[] = "";
char devAddr[] = "";
char nwkSKey[] = "";
char appSKey[] = "";
char AppKey[] = "";

#define LoraDeviceMode LWABP // LWOTA or LWABP


#define LoraDataRate DR0
// datarate SF/BW bits/s
// DR0 SF12/125KHz 250
// DR1 SF11/125KHz 440
// DR2 SF10/125KHz 980
// DR3 SF9/125KHz 1760
// DR4 SF8/125KHz 3125
// DR5 SF7/125KHz 5470
// DR6 SF7/250KHz 11000
// DR7 FSK:50kbps 50000
#define LoraPower 20
#define LoraPort 1
#define LoraADR 1
#define LoraNET 1 //1: Public net

#define DEBUG 1

void setup(void)
{
if (DEBUG) {
SerialUSB.begin(115200);
// while(!SerialUSB);
}

Serial.begin(9600);
lora.init();

if (DEBUG) {
memset(memBuffer, 0, 256);
lora.getVersion(memBuffer, 256, 1);
SerialUSB.print(memBuffer);
}

if (DEBUG) {
memset(memBuffer, 0, 256);
lora.getId(memBuffer, 256, 1);
SerialUSB.print(memBuffer);
}

// Disable UART timeout


memset(memBuffer, 0, 256);
SerialLoRa.print("AT+UART=TIMEOUT, 0\r\n");
if (DEBUG) {
SerialUSB.print(memBuffer);
}

// Set public network key


lora.setPubNetwKey(LoraNET);

// NwkSKey, AppSKey, AppKey


lora.setKey(nwkSKey, appSKey, AppKey);

lora.setId(devAddr,DevEUI,AppEUI);
if (DEBUG) {
-1-
D:\Mijn Documenten\01. Commint\13. Ontwikkelingsprojecten\43, Seeeduino\SW\Implementaties\GPS\ABP-ttn-gps-0.2b-test\ABP-ttn-gps-0.2b-test.ino
maandag 21 augustus 2017 8:46
SerialUSB.println();
}
lora.setReceiceWindowFirst(1);

//lora.setReceiceWindowSecond(869.525, DR3);

// Set RXwin2

memset(memBuffer, 0, 256);
SerialLoRa.print("AT+RXWIN2=869.525,DR3\r\n");
if (DEBUG) {
SerialUSB.print(memBuffer);
}

lora.setPower(LoraPower);

lora.setDeciveMode(LoraDeviceMode);

lora.setChannel(0, 868.1, DR0, DR7);


//lora.setChannel(1, 868.3);
//lora.setChannel(2, 868.5);
lora.setChannel(1, 0);
lora.setChannel(2, 0);

lora.setDataRate(LoraDataRate, EU868);

lora.setPort(LoraPort);

if (DEBUG) {
SerialUSB.println("Setup completed");
}
}

void loop(void)
{

String packetString = "";

packetString = get_gpsdata();
if (DEBUG) {
SerialUSB.println(packetString);
}
int strLength = packetString.length() + 1;

packetString.toCharArray(packetBuffer, strLength);

if (DEBUG) {
SerialUSB.println("Start transmission");
}

bool result = false;


result = lora.transferPacket(packetBuffer, strLength);
if(result)
{
short length;
short rssi;

memset(memBuffer, 0, 256);
length = lora.receivePacket(memBuffer, 256, &rssi);

if (DEBUG) {
if(length)
{
SerialUSB.print("Length is: ");
SerialUSB.println(length);
-2-
D:\Mijn Documenten\01. Commint\13. Ontwikkelingsprojecten\43, Seeeduino\SW\Implementaties\GPS\ABP-ttn-gps-0.2b-test\ABP-ttn-gps-0.2b-test.ino
maandag 21 augustus 2017 8:46
SerialUSB.print("RSSI is: ");
SerialUSB.println(rssi);
SerialUSB.print("Data is: ");
for(unsigned char i = 0; i < length; i ++)
{
SerialUSB.print("0x");
SerialUSB.print(memBuffer[i], HEX);
SerialUSB.print(" ");
}
SerialUSB.println();
}
}
}

delay(120*1000);
}

String get_gpsdata() {
bool newData = false;
String returnString = "";
float flat, flon;
unsigned long age;

// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial.available())
{
char c = Serial.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}

if (newData)
{
gps.f_get_position(&flat, &flon, &age);
returnString = "LAT=";
returnString += String(flat,6);
returnString += " LON=";
returnString += String(flon,6);
returnString += " SAT=";
returnString += String(gps.satellites());
returnString += " PREC=";
returnString += String(gps.hdop());
returnString += " AGE=";
returnString += String(age);
} else {
returnString = "NO GPS data available!";
}

return returnString;
}

-3-

You might also like