Function is not running

Hello
please help, splitter function is not working in blynk sample with esp32.

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

//Define Serial1 Name
#define SIM7000 Serial1

//Define GSM Model
#define TINY_GSM_MODEM_SIM7000

// See all AT commands, if wanted
//#define DUMP_AT_COMMANDS

#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SIM7000, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SIM7000);
#endif

#define UART_BAUD 9600
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4

#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12


void myDelay(int del) {
  unsigned long myPrevMillis = millis();
  unsigned long myCurrentMillis = myPrevMillis;
  while (myCurrentMillis - myPrevMillis <= del) {
    myCurrentMillis = millis();
  }
}

//Variables
int gnss_run_status = 0;
int fix_status = 0;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minutes = 0;
float secondWithSS = 0;
float lat = 0;
float lon = 0;
float msl_alt = 0;
float speed_over_ground = 0;
float course_over_ground = 0;
bool reserved1 = 0;
int fix_mode = 0;
int hdop = 0;
int pdop = 0;
int vdop = 0;
bool reserved2 = 0;
int gnss_satellites_used = 0;
int gps_satellites_used = 0;
int glonass_satellites_used = 0;
bool reserved3 = 0;
int c_n0_max = 0;
float hpa = 0;
float vpa = 0;

void enableGPS(void) {
  // Set SIM7000G GPIO4 LOW ,turn on GPS power
  // CMD:AT+SGPIO=0,4,1,1
  // Only in version 20200415 is there a function to control GPS power
  modem.sendAT("+SGPIO=0,4,1,1");
  if (modem.waitResponse(10000L) != 1) {
    DBG(" SGPIO=0,4,1,1 false ");
  }
  modem.enableGPS();
}

void disableGPS(void) {
  // Set SIM7000G GPIO4 LOW ,turn off GPS power
  // CMD:AT+SGPIO=0,4,1,0
  // Only in version 20200415 is there a function to control GPS power
  modem.sendAT("+SGPIO=0,4,1,0");
  if (modem.waitResponse(10000L) != 1) {
    DBG(" SGPIO=0,4,1,0 false ");
  }
  modem.disableGPS();
}

void modemPowerOn() {
  pinMode(PWR_PIN, OUTPUT);
  digitalWrite(PWR_PIN, LOW);
  delay(1000);  //Datasheet Ton mintues = 1S
  digitalWrite(PWR_PIN, HIGH);
}

void modemPowerOff() {
  pinMode(PWR_PIN, OUTPUT);
  digitalWrite(PWR_PIN, LOW);
  delay(1500);  //Datasheet Ton mintues = 1.2S
  digitalWrite(PWR_PIN, HIGH);
}

void modemRestart() {
  modemPowerOff();
  delay(1000);
  modemPowerOn();
}
String msg1;
BlynkTimer timer;

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V0, V1, V2);
}

void setup() {
  // Set console baud rate
  Serial.begin(115200);

  delay(10);

  //modemPowerOn();

  SIM7000.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

  // Serial.println("/**********************************************************/");
  // Serial.println("To initialize the network test, please make sure your GPS");
  // Serial.println("antenna has been connected to the GPS port on the board.");
  // Serial.println("/**********************************************************/\n\n");

  myDelay(10000);

  if (!modem.testAT()) {
    Serial.println("Failed to restart modem, attempting to continue without restarting");
    modemRestart();
    return;
  }

  // Serial.println("Start positioning . Make sure to locate outdoors.");
  // Serial.println("The blue indicator light flashes to indicate positioning.");

  enableGPS();


  BlynkEdgent.begin();
}

void loop() {

  String gps_raw = modem.getGPSraw();
  Serial.println(gps_raw);

  if (gps_raw != "") {
    gnss_run_status = splitter(gps_raw, ',', 0).toInt();
    fix_status = splitter(gps_raw, ',', 1).toInt();
    String date = splitter(gps_raw, ',', 2);  //yyyyMMddhhmm ss.sss
    year = date.substring(0, 4).toInt();
    month = date.substring(4, 6).toInt();
    day = date.substring(6, 8).toInt();
    hour = date.substring(8, 10).toInt();
    minutes = date.substring(10, 12).toInt();
    secondWithSS = date.substring(12, 18).toFloat();
    lat = splitter(gps_raw, ',', 3).toFloat();                 //±dd.dddddd
    lon = splitter(gps_raw, ',', 4).toFloat();                 //±ddd.dddddd
    msl_alt = splitter(gps_raw, ',', 5).toFloat();             //meters
    speed_over_ground = splitter(gps_raw, ',', 6).toFloat();   //Km/hour
    course_over_ground = splitter(gps_raw, ',', 7).toFloat();  //degrees
    fix_mode = splitter(gps_raw, ',', 8).toInt();
    reserved1 = splitter(gps_raw, ',', 9).toInt();
    hdop = splitter(gps_raw, ',', 10).toInt();
    pdop = splitter(gps_raw, ',', 11).toInt();
    vdop = splitter(gps_raw, ',', 12).toInt();
    reserved2 = splitter(gps_raw, ',', 13).toInt();
    gnss_satellites_used = splitter(gps_raw, ',', 14).toInt();
    gps_satellites_used = splitter(gps_raw, ',', 15).toInt();
    glonass_satellites_used = splitter(gps_raw, ',', 16).toInt();
    reserved3 = splitter(gps_raw, ',', 17).toInt();
    c_n0_max = splitter(gps_raw, ',', 18).toInt();  //dBHz
    hpa = splitter(gps_raw, ',', 19).toFloat();     //meters
    vpa = splitter(gps_raw, ',', 20).toFloat();     //meters

    Serial.println("----------------------------------");

    // Serial.print("GNSS Run Status:"); Serial.println(gnss_run_status);
    // Serial.print("Fix Status:"); Serial.println(gnss_run_status);
    Serial.print("Date: ");
    Serial.print(year);
    Serial.print(".");
    Serial.print(month);
    Serial.print(".");
    Serial.println(day);
    //msg1 = (day, "." ,year, "." ,month);
    Blynk.virtualWrite(V0, "msg1");
    Serial.print("Time: ");
    Serial.print(hour);
    Serial.print(":");
    Serial.print(minutes);
    Serial.print(":");
    Serial.println(secondWithSS, 6);

    Serial.print("Latitude:");
    Serial.println(lat, 6);
    Serial.print("Longitude:");
    Serial.println(lon, 6);
    Blynk.virtualWrite(V1, String(lat, 6));
    Blynk.virtualWrite(V2, String(lon, 6));
    // Serial.print("MSL Altitude:"); Serial.println(msl_alt, 6);
    // Serial.print("Speed Over Ground:"); Serial.println(speed_over_ground, 6);
    // Serial.print("Course Over Ground:"); Serial.println(course_over_ground, 6);
    // Serial.print("Fix Mode:"); Serial.println(fix_mode);
    // Serial.print("Reserved1:"); Serial.println(reserved1);
    // Serial.print("HDOP:"); Serial.println(hdop);
    // Serial.print("PDOP:"); Serial.println(pdop);
    // Serial.print("VDOP:"); Serial.println(vdop);
    // //Serial.print("Reserved2:"); Serial.println(reserved2);
    Serial.print("GNSS Satellites in View:");
    Serial.println(gnss_satellites_used);
    Serial.print("GPS Satellites Used:");
    Serial.println(gps_satellites_used);
    Serial.print("GLONASS Satellites used:");
    Serial.println(glonass_satellites_used);
    // Serial.print("Reserved3:"); Serial.println(reserved3);
    // //Serial.print("C/N0 max:"); Serial.println(c_n0_max);
    // Serial.print("HPA:"); Serial.println(hpa, 6);
    // Serial.print("VPA:"); Serial.println(vpa, 6);
    myDelay(5000);
  }

  BlynkEdgent.run();
  timer.run();
}

String splitter(String data, char separator, int index) {
    int stringData = 0;
    String dataPart = "";

    for (int i = 0; i < data.length(); i++) {

      if (data[i] == separator) {

        stringData++;
      } else if (stringData == index) {

        dataPart.concat(data[i]);
      } else if (stringData > index) {

        return dataPart;
        break;
      }


      return dataPart;
    }
  }

17:09:36.252 -> 1,1,20230127120936.000,41.301882,69.277445,459.100,0.00,166.4,1,,1.3,1.5,0.8,,20,5,2,,44,,

17:09:36.252 -> ----------------------------------

17:09:36.252 -> Date: 0.0.0

17:09:36.252 -> Time: 0:0:0.000000

17:09:36.252 -> Latitude:0.000000

17:09:36.252 -> Longitude:0.000000

17:09:36.385 -> GNSS Satellites in View:0

17:09:36.385 -> GPS Satellites Used:0

17:09:36.385 -> GLONASS Satellites used:0

First of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

1 Like

I’d suggest that (once you’ve cleaned-up your void loop) you add some serial print statements into your splitter function to show you what sort of values it is using internally and how the program flow is proceeding within that function.
It would probably help if you simplified your sketch by commenting-out most of the calls to the splitter function.

Pete.

but splitter function works well without blynk.


//Define Serial1 Name
#define SIM7000 Serial1

//Define GSM Model
#define TINY_GSM_MODEM_SIM7000

// See all AT commands, if wanted
//#define DUMP_AT_COMMANDS


#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SIM7000, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SIM7000);
#endif


#define UART_BAUD           9600
#define PIN_DTR             25
#define PIN_TX              27
#define PIN_RX              26
#define PWR_PIN             4

#define SD_MISO             2
#define SD_MOSI             15
#define SD_SCLK             14
#define SD_CS               13
#define LED_PIN             12


//Variables
int gnss_run_status = 0;
int fix_status = 0;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minutes = 0;
float secondWithSS = 0;
float lat = 0;
float lon = 0;
float msl_alt = 0;
float speed_over_ground = 0;
float course_over_ground = 0;
bool reserved1 = 0;
int fix_mode = 0;
int   hdop = 0;
int   pdop = 0;
int vdop = 0;
bool reserved2       = 0;
int gnss_satellites_used       = 0;
int gps_satellites_used       = 0;
int glonass_satellites_used       = 0;
bool reserved3       = 0;
int c_n0_max       = 0;
float hpa       = 0;
float vpa       = 0;

void enableGPS(void)
{
  // Set SIM7000G GPIO4 LOW ,turn on GPS power
  // CMD:AT+SGPIO=0,4,1,1
  // Only in version 20200415 is there a function to control GPS power
  modem.sendAT("+SGPIO=0,4,1,1");
  if (modem.waitResponse(10000L) != 1) {
    DBG(" SGPIO=0,4,1,1 false ");
  }
  modem.enableGPS();


}

void disableGPS(void)
{
  // Set SIM7000G GPIO4 LOW ,turn off GPS power
  // CMD:AT+SGPIO=0,4,1,0
  // Only in version 20200415 is there a function to control GPS power
  modem.sendAT("+SGPIO=0,4,1,0");
  if (modem.waitResponse(10000L) != 1) {
    DBG(" SGPIO=0,4,1,0 false ");
  }
  modem.disableGPS();
}

void modemPowerOn()
{
  pinMode(PWR_PIN, OUTPUT);
  digitalWrite(PWR_PIN, LOW);
  delay(1000);    //Datasheet Ton mintues = 1S
  digitalWrite(PWR_PIN, HIGH);
}

void modemPowerOff()
{
  pinMode(PWR_PIN, OUTPUT);
  digitalWrite(PWR_PIN, LOW);
  delay(1500);    //Datasheet Ton mintues = 1.2S
  digitalWrite(PWR_PIN, HIGH);
}


void modemRestart()
{
  modemPowerOff();
  delay(1000);
  modemPowerOn();
}

void setup()
{
  // Set console baud rate
  Serial.begin(115200);

  delay(10);

  //modemPowerOn();

  SIM7000.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

  Serial.println("/**********************************************************/");
  Serial.println("To initialize the network test, please make sure your GPS");
  Serial.println("antenna has been connected to the GPS port on the board.");
  Serial.println("/**********************************************************/\n\n");

  delay(10000);

  if (!modem.testAT()) {
    Serial.println("Failed to restart modem, attempting to continue without restarting");
    modemRestart();
    return;
  }

  Serial.println("Start positioning . Make sure to locate outdoors.");
  Serial.println("The blue indicator light flashes to indicate positioning.");

  enableGPS();

}

void loop()
{
  
  String gps_raw = modem.getGPSraw();
  Serial.println(gps_raw);

  if (gps_raw != "") {
    gnss_run_status = splitter(gps_raw, ',', 0).toInt();
    fix_status = splitter(gps_raw, ',', 1).toInt();
    String date = splitter(gps_raw, ',', 2); //yyyyMMddhhmm ss.sss
    year = date.substring(0, 4).toInt();
    month = date.substring(4, 6).toInt();
    day = date.substring(6, 8).toInt();
    hour = date.substring(8, 10).toInt();
    minutes = date.substring(10, 12).toInt();
    secondWithSS = date.substring(12, 18).toFloat();
    lat = splitter(gps_raw, ',', 3).toFloat();//±dd.dddddd
    lon = splitter(gps_raw, ',', 4).toFloat();//±ddd.dddddd
    msl_alt = splitter(gps_raw, ',', 5).toFloat();//meters
    speed_over_ground = splitter(gps_raw, ',', 6).toFloat();//Km/hour
    course_over_ground = splitter(gps_raw, ',', 7).toFloat();//degrees
    fix_mode = splitter(gps_raw, ',', 8).toInt();
    reserved1 = splitter(gps_raw, ',', 9).toInt();
    hdop = splitter(gps_raw, ',', 10).toInt();
    pdop = splitter(gps_raw, ',', 11).toInt();
    vdop = splitter(gps_raw, ',', 12).toInt();
    reserved2 = splitter(gps_raw, ',', 13).toInt();
    gnss_satellites_used = splitter(gps_raw, ',', 14).toInt();
    gps_satellites_used = splitter(gps_raw, ',', 15).toInt();
    glonass_satellites_used = splitter(gps_raw, ',', 16).toInt();
    reserved3 = splitter(gps_raw, ',', 17).toInt();
    c_n0_max = splitter(gps_raw, ',', 18).toInt();//dBHz
    hpa = splitter(gps_raw, ',', 19).toFloat();//meters
    vpa = splitter(gps_raw, ',', 20).toFloat();//meters

    Serial.println("----------------------------------");

    // Serial.print("GNSS Run Status:"); Serial.println(gnss_run_status);
    // Serial.print("Fix Status:"); Serial.println(gnss_run_status);
    Serial.print("Date: "); 
    Serial.print(year);
    Serial.print("."); 
    Serial.print(month);
    Serial.print("."); 
    Serial.println(day);
    Serial.print("Time: "); 
    Serial.print(hour);
    Serial.print(":"); 
    Serial.print(minutes);
    Serial.print(":"); 
    Serial.println(secondWithSS, 6);

    Serial.print("Latitude:"); Serial.println(lat, 6);
    Serial.print("Longitude:"); Serial.println(lon, 6);
    // Serial.print("MSL Altitude:"); Serial.println(msl_alt, 6);
    // Serial.print("Speed Over Ground:"); Serial.println(speed_over_ground, 6);
    // Serial.print("Course Over Ground:"); Serial.println(course_over_ground, 6);
    // Serial.print("Fix Mode:"); Serial.println(fix_mode);
    // Serial.print("Reserved1:"); Serial.println(reserved1);
    // Serial.print("HDOP:"); Serial.println(hdop);
    // Serial.print("PDOP:"); Serial.println(pdop);
    // Serial.print("VDOP:"); Serial.println(vdop);
    // //Serial.print("Reserved2:"); Serial.println(reserved2);
    Serial.print("GNSS Satellites in View:"); Serial.println(gnss_satellites_used);
    Serial.print("GPS Satellites Used:"); Serial.println(gps_satellites_used);
    Serial.print("GLONASS Satellites used:"); Serial.println(glonass_satellites_used);
    // Serial.print("Reserved3:"); Serial.println(reserved3);
    // //Serial.print("C/N0 max:"); Serial.println(c_n0_max);
    // Serial.print("HPA:"); Serial.println(hpa, 6);
    // Serial.print("VPA:"); Serial.println(vpa, 6);
delay(10000);
  }
}

String splitter(String data, char separator, int index)
{
  int stringData = 0;
  String dataPart = "";

  for (int i = 0; i < data.length(); i++)
  {

    if (data[i] == separator)
    {

      stringData++;
    }
    else if (stringData == index)
    {

      dataPart.concat(data[i]);
    }
    else if (stringData > index)
    {

      return dataPart;
      break;
    }
  }

  return dataPart;
}

Is there any reason why you didn’t share the fact that the code in your latest post works fine, but doesn’t work when you add it in to the Edgent example?

Why have you chosen to use Blynk Edgent as the basis for your Blynk sketch?

Pete.

18:56:26.329 -> 1,1,20230127135626.000,41.301711,69.277304,450.900,,,1,,1.4,1.6,0.9,,20,5,,,36,,
18:56:26.329 -> ----------------------------------
18:56:26.329 -> Date: 2023.1.27
18:56:26.329 -> Time: 13:56:26.000000
18:56:26.329 -> Latitude:41.301712
18:56:26.329 -> Longitude:69.277306
18:56:26.329 -> GNSS Satellites in View:20
18:56:26.329 -> GPS Satellites Used:5
18:56:26.329 -> GLONASS Satellites used:0

because, I have many times used it for my other sketchs

Do you have any other recommendations, Sir?

Static provisioning.

Pete.

I don’t know what it is or how to do that.

Hit the “Docs” link at the top of the page and so a search, or look at any of the Blynk examples that aren’t under the Edgent folder.

Pete.