Writing to Linux text file from sketch Issue

Hi @ptillisch, thank you for yours active support of this forum, I like yours answers very much. Containerisation of apps is good thing but sometimes it catch me. :) Yesterday I found out the sudo command is missing. Again obvious, but I miss it. So end with small daemon on host callable from container over local network which do the things. Sorry for little bit off topic.

I'm glad you were able to find a way to progress with your project!

If you would prefer a solution that remains entirely inside the Arduino App framework, feel free to make a dedicated forum topic on the subject. There is a good chance this could be accomplished using the recently added "custom Brick" feature.

1. CLI commands interact directly with the Linux operating system.

2. Using CLI commands, I could not view or access the files associated with my project under App Lab. From this, I infer that there is some form of security or isolation layer in place.

3. In the application from post #36, the MCU instructs the MPU to create a file named data.txt in the /home/arduino directory. The file is created successfully, the message (Hello from MCU ) is written to it, the file is read back, and the message is then sent back to the MCU for display on the Serial Monitor. This entire sequence works as expected.

4. From the shell terminal, I issued the ls -a command to list the contents of the /home/arduino directory and expected to see data.txt ; however, the file does not appear in the listing.

5. Similarly, the command cat data.txt cannot access the file, even though my shell prompt indicates that I am currently in the home directory.

Does not appear in my case. This is my python script:

import os
import time
from arduino.app_utils import *

def writeToFile(msg, FILE_PATH):   
    print(FILE_PATH)
    try:
        with open(FILE_PATH, "w") as f:
            f.write(msg)
            #print("Data writing ok");
        return True
    except Exception as e:
        print(f"Error writing to eMMC: {e}")
        return False

def readFromFile(FILE_PATH):
    print("Reading data from eMMC")
    if not os.path.exists(FILE_PATH):
        #print("Error")
        return "Error: File does not exist yet."
    
    try:
        with open(FILE_PATH, "r") as f:
            #print("No error")
            fContent = f.read()
            print(fContent)
            return fContent #f.read()
    except Exception as e:
        return f"Error reading file from eMMC: {e}"

def main():
    # Register the functions so the C++ sketch can call them
    Bridge.provide("write_to_file", writeToFile)
    Bridge.provide("read_from_file", readFromFile)
    
    # Keep the script running to listen for RPC commands
    while True:
        time.sleep(1)

if __name__ == "__main__":
    main()

Note: I remember, for some other app (adcMcuMpu), I once saw my created adc.txt file under App Lab files.

... deleted.

The script isn't sufficient information. What is the path of the file your App created?

The file path ("/home/arduino/data.txt") came from MCU to MPU. I am giving below both sketch and script.

Sketch:

#include <Arduino_RouterBridge.h>

String fileContent = "";
bool status = false;
unsigned long prMillis = millis();

void setup() 
{
  Bridge.begin();
  Monitor.begin(1);
} 
   
void loop() 
{
  Bridge.call("write_to_file", "Hello from MCU", "/home/arduino/data.txt");
  
  do
  {
    RpcCall rpc = Bridge.call("read_from_file", "/home/arduino/data.txt");
    status = rpc.result(fileContent);
  }
  while(status != true && millis() - prMillis < 5000);
  if(status == true )
  {
    status = false;
    Monitor.println(fileContent);
  } 
  else 
  {
    Monitor.println("Error: Failed to read file from eMMC.");
  }
  delay(3000);
}

Script:

import os
import time
from arduino.app_utils import *

def writeToFile(msg, FILE_PATH):   
    print(FILE_PATH)
    try:
        with open(FILE_PATH, "w") as f:
            f.write(msg)
            #print("Data writing ok");
        return True
    except Exception as e:
        print(f"Error writing to eMMC: {e}")
        return False

def readFromFile(FILE_PATH):
    print("Reading data from eMMC")
    if not os.path.exists(FILE_PATH):
        #print("Error")
        return "Error: File does not exist yet."
    
    try:
        with open(FILE_PATH, "r") as f:
            #print("No error")
            fContent = f.read()
            print(fContent)
            return fContent #f.read()
    except Exception as e:
        return f"Error reading file from eMMC: {e}"

def main():
    # Register the functions so the C++ sketch can call them
    Bridge.provide("write_to_file", writeToFile)
    Bridge.provide("read_from_file", readFromFile)
    
    # Keep the script running to listen for RPC commands
    while True:
        time.sleep(1)

if __name__ == "__main__":
    main()