Hello There, I am trying it hard to extract the GPS lat/long data from the lora receivered incoming string to publish it to MQTT server, but no idea what arduino code required to working out for this?
// This sketch displays information every time a new sentence is correctly encoded.
if (Serial2.available()) {
String incomming = Serial2.readString();
Serial.println(incomming);
double latitude;
double longitude;
Serial.println("********** Publish MQTT data");
char mqtt_payload[50] = "";
snprintf (mqtt_payload, 50, "m1=%lf;%lf", latitude, longitude);
Serial.print("Publish message: ");
Serial.println(mqtt_payload);
client.publish(pubTopic, mqtt_payload);
Serial.println("> MQTT data published");
Serial.println("********** End ");
Serial.println("*****************************************************");
delay(writeInterval);// delay
} else {
Serial.println(F("INVALID"));
}
}
For any Arduino code adding/updating help will be greatly appreciated to make it work. Thanking you.
have a look at how-to-get-the-best-out-of-this-forum
in particular what microcontroller are you using
what Lora board are you using
are you using LoRaWAN or Lora point-to-point communication
upload a schematic of your wiring
just confirm you are receiving the GPS location OK as follows
and need to parse it to extract the lat/log
if so upload a sample of the received string as text (you can put dummy data in lat/long)
assuming the format of the received data is consistent try using the string tokeniser, e.g.
// strtok example - parse tokens
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char str[] = "+RCV=2,20,41.000583,21.134611|,-34,11";
char* pch; // pointer to tokens
Serial.print("Splitting string ");
Serial.print(str);
Serial.println(" into tokens:");
pch = strtok(str, ",="); // get first token
int count = 0;
String latitude, longitude;
while (pch != NULL) {
Serial.print("string found ");
Serial.println(pch); // print it
if (count == 3) {
Serial.println("lat found");
latitude = pch;
}
if (count == 4) {
Serial.println("long found");
longitude = pch;
}
count++;
pch = strtok(NULL, ",|"); // get next token
}
Serial.print("latitude ");
Serial.println(latitude);
Serial.print("longitude ");
Serial.println(longitude);
}
void loop() {}
the serial montor prints
Splitting string +RCV=2,20,41.000583,21.134611|,-34,11 into tokens:
string found +RCV
string found 2
string found 20
string found 41.000583
lat found
string found 21.134611
long found
string found -34
string found 11
latitude 41.000583
longitude 21.134611
after tokenising the Strins latitude and longitude contains the values with you can then transmit over MQTT - no need to convert to doubles
if you received packets are terminated by newline you could try
Hello @horace ! I have tried to uplaod your first one provided code, but sorry to say that nothing happned, even got an error run time error i.e. latitude & longitude is not declared in this scope.
Hello @horace ! i have uploaded and run your updated code, but sorry to say that its not working properly receiving corrupt data and mqqt server is sending error msg. Thanks.
Hello @horace ! Thanks a lot. fixed the issue. Now getting data smoothly at serial monitor and mqtt server. I really appreciated your support to fix the issue.
Hello @horace! Next could you please assist me a bit more further that what LoRaWAN gateway to use with many of these Reyax based remote GPS lora transmitter nodes moving at different locations to monitor the different objects' locations on a cloud based plateform? Would be any LoRaWAN gateways are compitable with this and/or need to be compitable with this Reyax based lora module required for LoRaWAN gateway? Thanking you.
as you have indicated the problem of the current thread is solved and if you are moving to a new topic perhaps you should start a new thread
which LoRa modules are you planning to use?
the rylr998 documentation does not mention LoRaWAN just long range LoRa communication
Due to its complexity LoRaWAN requires a much more powerful internal processor than simple LoRa peer-to-peer hence you find some LoRa devices do not support LoRaWAN
if the device does support LoRaWAN any Gateway of the correct frequency etc should work with it
if setting the LoRaWAN device up in a city you may well find there are Gateways in range and it just works.
If there are none you need to set up your own gateway -
if you have access to local WiFi there are LoRaWAN gateways which connect to the TTN network, e.g. I have The Things Gateway and a The Things Indoor Gateway which I use when traveling
there are LoRaWAN gateways with GSM Modems - you would require power which may be mains or solar etc