Go Down

Topic: MKR 1300 to TTN sending data problem (Read 1 time) previous topic - next topic

JeremyWelburn

Hi all

I'm having trouble with my MKR1300 to send data to the TTN network, at the moment I have been able to connect it to my own gateway (Raspi + IC880A-SPI) and receive payloads like "hi" and "hello", which I can see in the TTN console.

Now I want to send GPS (using Adafruit Ultimate GPS) coordinates to the console. I receive the GPS data onto the Serial, so that is working, but when I try to send it to the TTN console, it's sometimes successful the first time after a fresh upload, but then keeps producing an error.

I don't understand why it would work once and not the rest of the time, and I've searched Google but there aren't many resources on the MKR1300 yet. Screenshots attached to show the data sent first time and received on TTN.

Here is my code:

PS. total newbie to arduino and programming so apologies if I haven't posted my topic correctly or forgotten something.

Code: [Select]

#include <MKRWAN.h>
#include "arduino_secrets.h"
#include <Adafruit_GPS.h> //Load the GPS Library. Make sure you have installed the library form the adafruit site above
Adafruit_GPS GPS(&Serial1); //Create GPS object

String NMEA1;  //We will use this variable to hold our first NMEA sentence
String NMEA2;  //We will use this variable to hold our second NMEA sentence
char c;       //Used to read the characters spewing from the GPS module

// Select your region (AS923, AU915, EU868, KR920, IN865, US915, US915_HYBRID)
_lora_band region = EU868;

LoRaModem modem(Serial1);

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);
  GPS.begin(9600);       //Turn GPS on at baud rate of 9600
  GPS.sendCommand("$PGCMD,33,0*6D"); // Turn Off GPS Antenna Update
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Tell GPS we want only $GPRMC and $GPGGA NMEA sentences
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  delay(1000);  //Pause
  while (!Serial);
  if (!modem.begin(region)) {
    Serial.println("Failed to start module");
    while (1) {}
  };
  Serial.print("Your device EUI is: ");
  Serial.println(modem.deviceEUI());

  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
  Serial.println("Successfully joined the network!");

  Serial.println("Enabling ADR and setting low spreading factor");
  modem.setADR(true);
  modem.dataRate(5);
}

void loop() {
 /*
  modem.beginPacket();
  modem.print("hello");
 
  int err = modem.endPacket(false);

  if (err > 0) {
    Serial.println("Big success!");
  } else {
    Serial.println("Error");
  }
  delay(1000 * 60);
*/
 
  readGPS();  //This is a function we define below which reads two NMEA sentences from GPS
}

void readGPS(){  //This function will read and remember two NMEA sentences from GPS
  clearGPS();    //Serial port probably has old or corrupt data, so begin by clearing it all out
  while(!GPS.newNMEAreceived()) { //Keep reading characters in this loop until a good NMEA sentence is received
  c=GPS.read(); //read a character from the GPS
  }
GPS.parse(GPS.lastNMEA());  //Once you get a good NMEA, parse it
NMEA1=GPS.lastNMEA();      //Once parsed, save NMEA sentence into NMEA1
while(!GPS.newNMEAreceived()) {  //Go out and get the second NMEA sentence, should be different type than the first one read above.
  c=GPS.read();
  }
GPS.parse(GPS.lastNMEA());
NMEA2=GPS.lastNMEA();
  Serial.println(NMEA1);
  Serial.println(NMEA2);
 
  Serial.println("");
  modem.beginPacket();
  modem.print(NMEA1);
 
  int err = modem.endPacket(false);

  if (err > 0) {
    Serial.println("GPS data sent");
  } else {
    Serial.println("GPS Error");
  }
}

void clearGPS() {  //Since between GPS reads, we still have data streaming in, we need to clear the old data by reading a few sentences, and discarding these
while(!GPS.newNMEAreceived()) {
  c=GPS.read();
  }
GPS.parse(GPS.lastNMEA());
while(!GPS.newNMEAreceived()) {
  c=GPS.read();
  }
GPS.parse(GPS.lastNMEA());

}

Go Up