How To Read Separate String

Hi everyone, I have problem regarding to read one string in multiple string.
I have two ESP32 and LoRa which one as node/send signal and one as gateway/receive the signal.
For node, its not connected to wifi and just transmit the GPS data through esp32 and lora. While gateway connect to wifi and here I want to send to database. When gateway receive the signal, the serial print as below:

Longitude : 121.123
Latitude : 12.456
Altitude : 45.56

The gateway code as below:


#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h> // connect wifi to esp32
#include <Firebase_ESP_Client.h> // interface esp32 with firebase
#include "addons/TokenHelper.h"
#include "addons/RTDBHelper.h"

#define WIFI_SSID ""
#define WIFI_PASSWORD ""
#define API_KEY ""
#define DATABASE_URL ""

//define the pins used by the transceiver module
#define ss 5
#define rst 14
#define dio0 2

FirebaseData fbdo;  // handle data when there's change on specific database node path
FirebaseAuth auth;  // need for authentication 
FirebaseConfig config; // for configuration

// send data privilage variable is needed for milis function 
// so that we can read and write to Firebase database at specified interval
// in a non-blocking way
unsigned long sendDataPrevMillis = 0;
bool signupOK = false; // flag variable sign up that use later when succesful sign up

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");

  while(WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  config.api_key = API_KEY; // need to specify API key in Firebase configuration
  config.database_url = DATABASE_URL;
  if(Firebase.signUp(&config, &auth, "", "")) {   // call Firebase sign up method and pass in Firebase config and auth
    Serial.println("signUp OK"); // the empty denote for anonymous user sign up and give new anonymous every esp32 connect
    signupOK = true; 
  }else{
    Serial.printf("%s\n", config.signer.signupError.message.c_str()); // give rror message when fail sign up
  }

  config.token_status_callback = tokenStatusCallBack;
  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
  
  while (!Serial);
  Serial.println("LoRa Receiver");

  //setup LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);
  
  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(433E6)) {
    Serial.println(".");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

// read packet
while (LoRa.available()) {
  String LoRaData = LoRa.readString();
  Serial.print(LoRaData); 

     if(Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 5000 || sendDataPrevMillis == 0)) {
      sendDataPrevMillis = millis();
      // -------------------- STORE sensor data to a RTDB
       this become problem
      }
    }

    // // print RSSI of packet
    // Serial.print("' with RSSI ");
    // Serial.println(LoRa.packetRssi());
  }
}

LoRaData in code print the whole gps data as above but I need to specified for each data like latitude, longitude, altitude to follow as below code.

image

Code for sender as below:

#include <TinyGPS++.h> //Libary TinyGPS
// #include <SoftwareSerial.h> //Libarary bawaan
#include <Wire.h>
// #include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <LoRa.h>

//define the pins used by the transceiver module
#define ss 5
#define rst 15
#define dio0 2
#define RXD2 16
#define TXD2 17

HardwareSerial neogps(1);
int GPSBaud = 9600; //Biarin default

int counter = 0;
// Membuat objek TinyGPS++
TinyGPSPlus gps;
// Mmebuat koneksi serial dengan nama "gpsSerial"
// SoftwareSerial gpsSerial(RXD2, TXD2);

void setup()
{
  //Memulai koneksi serial pada baudrate 115200
  Serial.begin(115200);
  //Memulai koneksi serial dengan sensor
  gpsSerial.begin(GPSBaud);
  // //Begin serial communication Neo6mGPS
  neogps.begin(9600, SERIAL_8N1, RXD2, TXD2);

  while (!Serial);
  Serial.println("LoRa Sender");  

  //setup LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);

  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(433E6)) {
    Serial.println("LoRa Error");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

void loop()
{
  boolean newData = false;
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (neogps.available())
    {
      if (gps.encode(neogps.read()))
      {
        newData = true;
      }
    }
  }

  //If newData is true
  if(newData == true)
  {
    newData = false;
    Serial.println(gps.satellites.value());
    print_speed();
  }
  else
  {
    Serial.print("No Data");  
  } 

  //Membuat tampilan data ketika terdapat koneksi
  // while (gpsSerial.available() > 0)
  //   if (gps.encode(gpsSerial.read()))
  //     if (gps.location.isValid())
  //     {
  //       Serial.println("Sending to LoRa");
  //       LoRa.beginPacket();
  //       LoRa.print("Lat: ");
  //       LoRa.print(gps.location.lat(), 6);
  //       LoRa.print("c");
  //       LoRa.print("Long: ");
  //       LoRa.print(gps.location.lng(), 6);
  //       Serial.println("Sent via LoRa");
  //       LoRa.endPacket();
  //       }
  //       else
  //       {
  //         LoRa.beginPacket();
  //         LoRa.println("Location: Not Available");  
  //         LoRa.endPacket();
  //         }

  // Jika dalam 5 detik tidak ada koneksi, maka akan muncul error "No GPS detected"
  // Periksa sambungan dan reset arduino
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("No GPS detected");
    while(true);
  }
}

void print_speed()
{     
  if (gps.location.isValid() == 1)
  {
    Serial.println("Sending to LoRa");
    LoRa.beginPacket();
    LoRa.print("Lat: ");
    LoRa.println(gps.location.lat(),6);

    LoRa.print("Lng: ");
    LoRa.println(gps.location.lng(),6);

    LoRa.print("Speed: ");
    LoRa.println(gps.speed.kmph());
    
    LoRa.print("SAT:");
    LoRa.println(gps.satellites.value());

    LoRa.print("ALT:");
    LoRa.println(gps.altitude.meters(), 0);
    LoRa.endPacket();
    
  }
  else
  {
    LoRa.beginPacket();
    LoRa.print("No Data");
    LoRa.endPacket();
  }  
}

If I didn't separate the whole data will insert in one path database.
I want to look like below.

image

Hello

Search "parse string", this is a very common question and I'm sure you will find what you need

consider using sscanf() or strtrok() for spliting c-strings (not String)

also consider using sprintf() and strcat() to generate your strings

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.