Im using an Arduino Uno and a Dragino LA66 LoRaWan shield. I'm trying to get GPS data and send it to a router using LoRaWAN that then sends it to TTN. I'm sending the data using the AT+SEND command. I attached my GPS sensor and created 2 software serials, 1 for the AT commands and 1 for the GPS data. However, my AT command Serial is always unavailble for writing. I'm guessing this is because the GPS serial is already using the Serial Port. How can i solve this? Below is the code I am using.
#include <SoftwareSerial.h> // The serial connection to the GPS module
#include <TinyGPS++.h>
#include <Arduino.h>
TinyGPSPlus gps;
SoftwareSerial ss(10, 11);
SoftwareSerial gpsSerial(4, 3);
unsigned long lastSendTime = 0;
void setup()
{
ss.begin(9600);
Serial.begin(9600);
gpsSerial.begin(9600);
}
void loop()
{
while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
if (gps.location.isUpdated())
Serial.println("Latitude: " + String(gps.location.lat(), 6));
Serial.println("Longitude: " + String(gps.location.lng(), 6));
}
}
unsigned long currentTime = millis();
if (currentTime - lastSendTime >= 5000) {
if (ss.available() > 0) sendToTTN();
else Serial.println("Serial for AT commands unavailable");
lastSendTime = currentTime;
}
}
String stringToHex(const String &input)
{
String output = "";
for (int i = 0; i < input.length(); i++)
{
char hex[3];
sprintf(hex, "%02X", input[i]);
output += hex;
}
return output;
}
void sendToTTN()
{
String lat = String(gps.location.lat(), 8);
String alt = String(gps.altitude.meters(), 8);
String lng = String(gps.location.lng(), 8);
String lat_hex = stringToHex(lat);
String lng_hex = stringToHex(lng);
String alt_hex = stringToHex(alt);
String seperator = stringToHex(" | ");
String payload = lat_hex + seperator + lng_hex + seperator + alt_hex;
ss.println("AT+SEND=1,2," + String(payload.length()) + "," + payload);
}