Greetings, I am working on a project and have broken it down into byte sized chunks (pun intended). The idea is to make a GPS tracker that communicates over LoRa which will be set up at a base camp. This project is used to ensure campers have a safe environment and the counselors can see if anyone needs aid.
Stuff Used:
Arduino UNO
Xbee Shield
Multitech mDot
Adafruit Ultimate GPS V3
Part 1
I started and solved how to connect the end device to the LoRa server and how to send strings via LoRaWAN protocol. The following is the working code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
// AT Commands for MultiTech
Serial.println("Joining Lora Network");
mySerial.begin(115200);
//Test
mySerial.println("AT");
//OTAA network ID
mySerial.println("AT+NI=1,Ky-Newton");
//OTAA network key
mySerial.println("AT+NK=1,Ky-Newton");
//frequency sub band 3
mySerial.println("AT+FSB=3");
//join delay 5 sec
mySerial.println("AT+JD=5");
mySerial.println("AT+NJM=1");
mySerial.println("AT+TXDR=1");
//store in memory
mySerial.println("AT&W");
//reset
mySerial.println("ATZ");
delay(5000);
//Join network
mySerial.println("AT+JOIN");
delay(1000);
}
void loop() {
//Join network
//mySerial.println("AT+JOIN");
delay(1000);
//send message
mySerial.println("AT+SEND=Hello World");
// Reading the output in terminal
if (mySerial.available()) {
Serial.write(mySerial.read());
}
}
Part 2
Here I solved how to use the GPS and then string the latitude and longitude and ensured I could send the string variable, again working code is below:
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 5
// Connect the GPS RX (receive) pin to Digital 4
SoftwareSerial mySerial(5, 4);
Adafruit_GPS GPS(&mySerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
void setup()
{
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
// setup delay
delay(5000);
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
delay(1000);
// Ask for firmware version
mySerial.println(PMTK_Q_RELEASE);
}
uint32_t timer = millis();
void loop() // run over and over again
{
int siz = 0;
char myLat[9]; //hold lattitude
char myLon[9]; //hold lognitude
char myString[100]; //Empty string with the limit of 53 bytes
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if ((c) && (GPSECHO))
Serial.write(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trytng to print out data
//Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
if (GPS.fix) {
dtostrf(GPS.latitude, 7, 2, myLat);
dtostrf(GPS.longitude, 7, 2, myLon);
strcat(myString, "AT+SEND=");
strcat(myString, "D01,");
strcat(myString, myLat);
strcat(myString, ",");
strcat(myString, myLon);
//concatanate the rest here as above.
Serial.println(myString);
strcpy(myString, "");
}
}
}
Now for part 3 I am trying to combine the codes so that I can send the string to the gateway and I do still get affirmed connection to the gateway which is handled first in the setup but I am seeing zero attempts to actually send the string. It should also be mentioned that the GPS is attaining a fix which means its setup has gone through as well. here is the combined code:
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 5
// Connect the GPS RX (receive) pin to Digital 4
SoftwareSerial mySerial2(2, 3); // RX, TX
SoftwareSerial mySerial1(5, 4);
Adafruit_GPS GPS(&mySerial1);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
void setup()
{
//-------------------------------------LoRaWAN-Setup-----------------------------------------------------------
// AT Commands for MultiTech
Serial.println("Joining Lora Network");
mySerial2.begin(115200);
//Test
mySerial2.println("AT");
//OTAA network ID
mySerial2.println("AT+NI=1,Ky-Newton");
//OTAA network key
mySerial2.println("AT+NK=1,Ky-Newton");
//frequency sub band 3
mySerial2.println("AT+FSB=3");
//join delay 5 sec
mySerial2.println("AT+JD=5");
mySerial2.println("AT+NJM=1");
mySerial2.println("AT+TXDR=1");
//store in memory
mySerial2.println("AT&W");
//reset
mySerial2.println("ATZ");
delay(5000);
//Join network
mySerial2.println("AT+JOIN");
delay(1000);
mySerial2.end();
//--------------------------------------GPS-Setup--------------------------------------------------------------
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
mySerial1.begin(115200);
// setup delay
delay(5000);
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
delay(1000);
// Ask for firmware version
mySerial1.println(PMTK_Q_RELEASE);
// End Communication
mySerial1.end();
}
uint32_t timer = millis();
void loop() // run over and over again
{
//-------------------------------------------GPS----------------------------------------------------------
mySerial1.begin(115200);
//delay
delay(5000);
int siz = 0;
char myLat[9]; //hold lattitude
char myLon[9]; //hold lognitude
char myString[100]; //Empty string
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if ((c) && (GPSECHO))
mySerial1.write(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trytng to print out data
//Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 5000) {
timer = millis(); // reset the timer
if (GPS.fix) {
dtostrf(GPS.latitude, 7, 2, myLat);
dtostrf(GPS.longitude, 7, 2, myLon);
strcat(myString, "AT+SEND=");
strcat(myString, "D01,");
strcat(myString, myLat);
strcat(myString, ",");
strcat(myString, myLon);
Serial.println(myString);
mySerial1.end();
//concatanate the rest here as above.
//--------------------------------------------------LoRaWAN---------------------------------------------------------
mySerial2.begin(115200);
//delay
delay(5000);
//send message
mySerial2.println(myString);
//clear string
strcpy(myString, "");
// Reading the output in terminal
if (mySerial2.available()) {
Serial.write(mySerial2.read());
}
mySerial2.end();
}
}
}
Has anyone attempted something like this before and if so do you see where I may have went wrong trying to combine the two parts? Any help is appreciated.
Sincerely,
Josh