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.