Parsing the String variables values in the Master's message sent to the Slave

How to parse the GPS live, real-time String variable values strlat1 & strlon1 from the Data Frame of the I2C Slave message received:

Bits of the Master sketch
//Write message to the slave
//Wire.println(((strlat1)) + (", ") + ((strlon1)));

Bits of the Slave sketch
//Slave received message from the Master
//snprintf(message, 64, "%lu Packets.");
//Wire.slaveWrite((uint8_t )message, strlen(message));
Serial Monitor did print the received Master's message correctly, as sent!!!!

//Slave// Serial.println(strlat1);
//Slave// Serial.println(strlon1);??????
However, never printed the String's variable assigned value, blank???????


Sketch: Step_3_I2C_com_with_a_ESP32-Wrover_Slave_2.ino below

Start of the sketch

//Libraries I2C Edit on GitHub I2C

//https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/i2c.html#arduino-esp32-i2c-api

#include "Wire.h"
#include "String.h"

#define I2C_DEV_ADDR 0x55//ESP32-Wrover - Hex = 0055 - Dec = 85 - Elecrow ILI9488 3.5 in. TFT display 

float flat1, flon1;
String strlat1, strlon1;

void onRequest(){
  Wire.print(" Packets.");
  Serial.println("onRequest");
}

void onReceive(int len){

  while(Wire.available()){
    Serial.write(Wire.read());
  }
  Serial.println();
}

void setup() {
  Serial.begin(115200);

Serial.println("Step_3_I2C_com_with_a_ESP32-Wrover_Slave_2.ino");
Serial.println("ILI9488 3.5in. ESP32-32 TFT Display");
Serial.println("06_07_2024");

  Serial.setDebugOutput(true);
  Wire.onReceive(onReceive);
  Wire.onRequest(onRequest);
  Wire.begin((uint8_t)I2C_DEV_ADDR);

}

void loop() {

#if CONFIG_IDF_TARGET_ESP32
  char message[64];
//  snprintf(message, 64, "%lu Packets.", i++);
  snprintf(message, 64, "%lu Packets.");
  Wire.slaveWrite((uint8_t *)message, strlen(message));
//  size_t slaveWrite(const unit_t *, size_t);

Serial.println(strlat1);
Serial.println(strlon1);

#endif

Serial.println("************************************");
   Serial.println(strlat1);
   Serial.println(strlon1);
Serial.println("************************************");

}

End of the sketch

Is there a method to attach a screenshot for additional clarity?

[Imgur: The magic of the Internet]
(Imgur: The magic of the Internet)

PrtScr > Ctrl-V

void loop() {

#if CONFIG_IDF_TARGET_ESP32
  char message[64];
  strlat1 = "";
  strlon1 = "";
  //  snprintf(message, 64, "%lu Packets.", i++);
  snprintf(message, 64, "%lu Packets.");
  Wire.slaveWrite((uint8_t *)message, strlen(message));
  //  size_t slaveWrite(const unit_t *, size_t);
  int i;
  for ( i = 0; i < sizeof(message); i++) {
    if (message[i] == ',')break;
  }
  int b;
  for ( b = 0; b < i; b++) strlat1 = strlat1 + message[b];
  i = i + 2;
  for ( b = 0; b < (sizeof(message) - i); b++) strlon1 = strlon1 + message[b + i];

#endif

  Serial.println("************************************");
  Serial.println(strlat1);
  Serial.println(strlon1);
  Serial.println("************************************");

}

Hello Kolaha, I finally got back on track with my post issue. Your suggested sketch changes where incorporated with no problem compiling. However no results parsing the String variables from the folling?

char message[64];
** strlat1 = "";**
** strlon1 = "";**

//Libraries I2C Edit on GitHub I2C

//https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/i2c.html#arduino-esp32-i2c-api

#include <SoftwareSerial.h>

#include "Wire.h"
#include "String.h"

#define I2C_DEV_ADDR 0x55//ESP32-Wrover - Hex = 0055 - Dec = 85 - Elecrow ILI9488 3.5 in. TFT display 

float flat1, flon1;
String strlat1, strlon1;

void onRequest(){
  Wire.print(" Packets.");
  Serial.println("onRequest");
}

void onReceive(int len){

  while(Wire.available()){
    Serial.write(Wire.read());//prints the message to the Serial Monitor!!!!
  }
//  Serial.println();
}

void setup() {
  Serial.begin(115200);

Serial.println("Step_3_I2C_com_with_a_ESP32-Wrover_Slave_Kolaha.ino");
Serial.println("ILI9488 3.5in. ESP32-32 TFT Display");
Serial.println("06_10_2024");

  Serial.setDebugOutput(true);
  Wire.onReceive(onReceive);
  Wire.onRequest(onRequest);
  Wire.begin((uint8_t)I2C_DEV_ADDR);

}

void loop() {

#if CONFIG_IDF_TARGET_ESP32
  char message[64];
  strlat1 = "";
  strlon1 = "";
  //  snprintf(message, 64, "%lu Packets.", i++);
  snprintf(message, 64, "%lu Packets.");
  Wire.slaveWrite((uint8_t *)message, strlen(message));
  //  size_t slaveWrite(const unit_t *, size_t);
  int i;
  for ( i = 0; i < sizeof(message); i++) {
    if (message[i] == ',')break;
  }
  int b;
  for ( b = 0; b < i; b++) strlat1 = strlat1 + message[b];
  i = i + 2;
  for ( b = 0; b < (sizeof(message) - i); b++) strlon1 = strlon1 + message[b + i];

  Serial.println("************************************");
  Serial.print("strlat1 #1 ");Serial.println(strlat1);
  Serial.print("strlon1 #1 ");Serial.println(strlon1);
  Serial.println("************************************");

#endif

  Serial.println("************************************");
  Serial.print("strlat1 #2 ");Serial.println(strlat1);
  Serial.print("strlon1 #2 ");Serial.println(strlon1);
  Serial.println("************************************");

}type or paste code here

I used your PrtScr>ctrl-V command to attach a screenshot, not sure if it attached the correct screenshot?? How do I select the actual screenshot to attach, the most recent in memory?

I used igmur to create a link to the screenshot to show the Serial Monitor's results:

i don't see place where sketch get latitude/longitude.
I promoted a way how one string can be splitted in two containing text of float point value.

try:

#include <SoftwareSerial.h>
#include "Wire.h"

#define I2C_DEV_ADDR 0x55//ESP32-Wrover - Hex = 0055 - Dec = 85 - Elecrow ILI9488 3.5 in. TFT display 

float flat1, flon1;

void Request() {
  Wire.print(" Packets.");
  Serial.println("onRequest");
  Serial.println("************************************");
  Serial.print("lat 2 "); Serial.println(flat1, 6);
  Serial.print("lon 2 "); Serial.println(flon1, 6);
  Serial.println("************************************");
}

void Receive(int len) {
  if (Wire.available() > 3) {  // becouse of your empty lines
    flat1 = Wire.parseFloat();
    char b = Wire.read();
    if (b == ',')Wire.read();
    flon1 = Wire.parseFloat();
    Serial.println("************************************");
    Serial.print("lat 1 "); Serial.println(flat1, 6);
    Serial.print("lon 1 "); Serial.println(flon1, 6);
    Serial.println("************************************");
  }
  while (Wire.available() > 0)Wire.read();
}

void setup() {
  Serial.begin(115200);

  Serial.println("Step_3_I2C_com_with_a_ESP32-Wrover_Slave_Kolaha.ino");
  Serial.println("ILI9488 3.5in. ESP32-32 TFT Display");
  Serial.println("06_10_2024");

  Serial.setDebugOutput(true);
  Wire.onReceive(Receive);
  Wire.onRequest(Request);
  Wire.begin((uint8_t)I2C_DEV_ADDR);
}

void loop() {
}

Hello Kolaha, here is the Master's sketch that is receiving the live, real-time GPS data and converting it to flat1 & flon1, the converting it to strlat1 & strlon1 to send the message to the Slave:

    **//Write message to the slave**
    Wire.beginTransmission(I2C_DEV_ADDR);
    Wire.println();
    Wire.println((strlat1) + (", ") + (strlon1));

Master sketch: Step_3_I2C_com_with_ESP32-Wrover_Master_1.ino

//Libraries I2C Edit on GitHub I2C

//https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/i2c.html#arduino-esp32-i2c-api

#include <Arduino.h>
#include <SoftwareSerial.h>

#include <TinyGPS++.h>

#include "Wire.h"
#include "String.h"

static const int RXPin = 35;// blue or white
static const uint32_t GPSBaud = 9600;
SoftwareSerial gpsSerial(35);// blue or white

TinyGPSPlus gps;

#define I2C_DEV_ADDR 0x55//ESP32 TTGO Mini / Hex = 0055 - Dec = 85
#define I2C_SDA 21//white - SDA - data
#define I2C_SCL 22//yellow - SCL - clock
 
int cntr;
float flat1, flon1;
String strlat1, strlon1;

void setup() {
 
  Serial.begin(115200);

  gpsSerial.begin(9600);  

  Serial.println("Step_3_I2C_com_with_ESP32-Wrover_Master_1.ino");
  Serial.println("06/09/2024");

  Serial.setDebugOutput(true);
  Wire.begin();

  Wire.begin (I2C_SDA, I2C_SCL);
  pinMode (I2C_SDA, INPUT_PULLUP); 
  pinMode (I2C_SCL, INPUT_PULLUP); 

  Serial.print("I2C_DEV_ADDR: ");
  Serial.println(((uint8_t)I2C_DEV_ADDR));

  cntr = 1;  

}

void loop() {

    while (gpsSerial.available() > 0)
    { 
 
//    Serial.println("gpsSerial.available");

    gps.encode(gpsSerial.read());

//    Serial.println("gpsSerial.read");

    if (gps.location.isUpdated()) 
     { 

//    Serial.println("gps.location.isUpdated");

      
//++++    TinyGPSPlus Data   ++++++++++
      flat1 = gps.location.lat();
      strlat1 = String(flat1,6);
//      Serial.println(strlat1);

      flon1 = gps.location.lng();
      strlon1 = String(flon1,6);
//      Serial.println(strlon1);

//      Serial.println((strlat1) + (", ") + (strlon1));

      cntr = 0;

//    }//if(gps.encode(gpsSerial.read()))  
//  }//while (gpsSerial.available() > 0)    
//++++   enr  TinyGPSPlus Data   ++++++++++

    if (cntr == 0) {

    //Write message to the slave
    Wire.beginTransmission(I2C_DEV_ADDR);
  
    Wire.println();
  
    Wire.println((strlat1) + (", ") + (strlon1));
  
    Serial.println((strlat1) + (", ") + (strlon1));
  
    uint8_t error = Wire.endTransmission(true);
    Serial.printf("endTransmission: %u\n", error);
    
    //Read 16 bytes from the slave
    uint8_t bytesReceived = Wire.requestFrom(I2C_DEV_ADDR, 16);
    Serial.printf("requestFrom: %u\n", bytesReceived);
    if((bool)bytesReceived){ //If received more than zero bytes
      uint8_t temp[bytesReceived];
      Wire.readBytes(temp, bytesReceived);
  //    log_print_buf(temp, bytesReceived);

      cntr = 1;
    }
    }
    }
 }
}

Screenshot:

Screenshot of Master TX'ing and Slave Rx'ing live GPS data:

I grabed the wrong URL:

Imgur

I trust this helps!

Yet another screenshot for clarity:

Why are you converting the float to text to begin with? Easier to directly send the floats as binary data.

Is there some particular reason you are splitting this between two boards, instead of running the full code on a single ESP32?

The last sreenshot did not show the data updating:

you not used my scetch

Thank you so much for your assistance, I have been baffled with this.iIt works!

My choice to use two micros to do this project is complicated. I have my front end project completed with a pair of TTGO T7 v1.3 micros that are sharing coordinates via LoRa radios and displaying the "distance between" on a I2C 16x2 LCD. Then I decided to convert the display with an ILI9488 3.5 in. TFT Elecrow LCD Display to visualize our positions on off-line maps as we travel the many trails of the Manistee National Forest located in the State of Michigan in the USA. Since the original LCD utilized I2C com and the Elecrow display like wise utilized I2C com, it seemed easier to utilize both micros due to memory considerations. Boy, was I in for an big learning surprise converting from the simple 16x2 LCD to the TFT Display, but I am determined.

The original sketch I found on GitHub worked utilizing Strings, hence I stayed with it.

    Wire.println((strlat1) + (", ") + (strlon1)); // Master's sent message

Can I change the code from a float to a String should the function require Strings to pass the coordinates to display the positions on an off-line map?

    flat1 = Wire.parseFloat(); // Slave's received parsed data     

Inserted the wrong image again, imgur is not that easy, but I have not mastered the other method you suggested !!!

Imgur

The basic setup: