Communication between Portenta H7 + Vision Shield sketches

I need help sending data from one sketch to another in the same device. I've tried Serial and I/O on an SD card and it's still not working.

I'm currently using @jerteach's repositories (ArduinoCore-stm32l0 and portenta-pro-community-solutions) to send messages through raw LoRa from a Portenta H7 to a Raspberry Pi. But I need to send the results of a TinyML model from a file (flashed using the Mbed Portenta H7 board library) to a sketch that's based on the Vision Shield branch of the first repository that I mentioned (Let's call it Murata module).

First file:

char rx_buf[64];
char tx_buf[64];

int rx = 0;
int tx = 0;

bool mySent = false;

void setup() {
   SerialLoRa.begin(9600); // for LoRa must be 9600
   Serial.begin(115200);
     
    pinMode(LEDR, OUTPUT); 
    pinMode(LEDG, OUTPUT);   
    pinMode(LEDB, OUTPUT);  
    
    digitalWrite(LEDR, HIGH); 
    digitalWrite(LEDG, HIGH); 
    digitalWrite(LEDB, HIGH); 
}

void loop() {

  delay(10);

  // Back and forth to the Murata module
  while (Serial.available()) {      // If anything comes in Serial (USB),
    tx_buf[tx++] = Serial.read();   // read it and send it out Serial1 (pins 0 & 1)
  }

  if (tx > 0 ) {
    SerialLoRa.write(tx_buf, tx);
    tx = 0;
  }

  while (SerialLoRa.available()) {      // If anything comes in Serial (USB),
    rx_buf[rx++] = SerialLoRa.read();   // read it and send it out Serial1 (pins 0 & 1)
    
  }
  if (rx > 0) {
    Serial.write(rx_buf, rx);
    rx = 0;
  }
}


Murata module:

#include "LoRaRadio.h"
#include "TimerMillis.h"

// Global Variables
#include <SPI.h>
#include <SD.h>

const char* filename = "/fs/crowdcountresult.txt";
const char* filelockname = "/fs/lock.txt";

TimerMillis myReceivePrintTimer, mySendTimer, mySendPrintTimer;

long lastSendTime = 0;
int interval = 10000;

bool mySendThreadFree = true;
bool myReceiveThreadFree = true;

String mySendString;

int myIncomingInt ;
int myRssi     ;
int mySnr      ;
const int messageSize = 16;

char mySendArray[messageSize];


void mySendTimerOn(void){
    while (Serial.available() > 0) {
      mySendString.concat( (char)Serial.read());  // could be BYTE
    }
    
    while (SD.exists(filelockname)){
      Serial.println("Esperando remocion de Lockfile.");
    }
    File file = SD.open(filename, FILE_READ); // aparentemente no puede ser read binary
    if(file) {
      // leer valores de archivo (id sala, ocupacion)
      mySendString = file.read();
      file.close();
    } else {
      Serial.print("sd.open failed");
    }
    
    if (mySendString.length() > 0){
      if (mySendString.length() > messageSize){
        Serial.println("TOO BIG!"); 
      }

      Serial.print("Mensaje recibido: ");
      Serial.println(mySendArray);
      
      strncpy(mySendArray, "", messageSize);  // erase the array of chars
      mySendString.toCharArray(mySendArray, mySendString.length());
      
      /*Code to send LoRa message*/

      mySendPrintTimer.start(mySendPrintIt, 5);  // just once 5 milliseconds later
    
    }
}

void mySendPrintIt(void){
    Serial.println("Sending with length:"+ String(mySendString.length()));
    Serial.println(mySendString.length()); 
    Serial.println(mySendString);
    Serial.println();
    mySendString = ""; 
}

void setup( void ){
    Serial.begin(9600);
    while (!Serial) { }   // non-blocking for the murata module on the Portenta

    Serial.print("Initializing SD card... ");
    if (SD.begin()){
      Serial.println("SD initialization done.");
    } else {
      Serial.println("SD initialization failed.");
      while(1);
    }

  /* LoRa settings */

}


void loop( void ){
  if (millis() - lastSendTime > interval) {
    mySendTimerOn();
    lastSendTime = millis();
    interval = 10000;
  }
}


In the original example, the first file is writing to SerialLora what's written as input in the Serial Monitor, but even when I send a constant char array, the Monitor must be open, and I need the Portenta to work by itself with a battery and no computer.

I've also tried writing the result in a file on an SD card and reading it from the other sketch, but I haven't been able to initialize the card from the murata module. I've tried using the SD package from Arduino so far, because the default Portenta example (using SDMMCBlockDevice and FATFileSystem) is not compatible with the ArduinoCore-stm32l0 library, but I don't know the pin that corresponds to the SD card adapter (I've tried some of the most common values, such as 7, 10, etc...).

Any suggestion on how to use serial communication without the need of the Serial Monitor or how to initialize the SD card would be really appreciated.

Another alternative could be to copy all the board dependencies of the Murata module and import it on the main file as a custom library, that way I wouldn't need to send messages, just import the methods to use LoRa.

It's getting complex, given that the methods to use the LoRa radio depend on a pretty different hardware code (stm32-based code adapted to work on the Portenta Vision Shield), so I'm guessing another way would be the other way around: To copy the code I'm using on the main sketch into the murata module.

The only problem is that it uses the camera, which is programmed based on the Mbed Portenta H7 library. @jerteach, do you think it would be feasible to adapt the camera library to the stm32-arduino core?

Thanks, and sorry if I'm not being clear

In the end, to make it clearer to anyone who's reading:

  • The first file is being flashed and executed on the Portenta H7 M7 core (STM32H7), using the Mbed Portenta OS board library, while
  • The second one (murata module) is being executed on the Portenta Vision Shield core (STM32L0), using ArduinoCore-stm32l0 custom board library.

What was going on was that the first file was defining two Serial objects (baud rate: 115200 to print text on the Serial Monitor, and baud rate: 9600 to communicate with the second file), and was stuck on the while (!Serial) { } loop (which doesn't appear on the first post for reasons), which made it necessary to open the Serial Monitor to execute the communication.

So, in summary, all I had to do (which I hope I'd realized sooner) was using the Serial methods with the same baud rate, and not letting any other form of communication get in the way, in order for them to send and receive messages.