I am learning to use my UNO Q. Worked up a sketch and a python script with help of AI and some docs digging.
Arduino sketch:
<CODE>
#include <Arduino_RouterBridge.h>
bool result = false;
void setup() {
Bridge.begin();
Monitor.begin();
delay(5000);
Bridge.call("append_to_file", "/home/arduino/data.txt", "Hello from MCU").result(result);
if (result) {
Monitor.println("Append successful");
} else {
Monitor.println("Append failed");
}
}
void loop() {
delay(3000);
Bridge.call("append_to_file", "/home/arduino/data.txt", "Hello from MCU").result(result);
if (result) {
Monitor.println("Append successful");
} else {
Monitor.println("Append failed");
}
}
#main.py
from arduino.app_utils import App, Bridge
def append_to_file(filename: str, text: str):
"""Append text to a file on the Linux side."""
print(f"append to file routine")
try:
with open(filename, "a", encoding="utf-8") as f:
f.write(text + "\n")
print("Outside 'with': file closed?", f.closed) # Should be True
print(f"text written to {filename} {text}")
return True
except Exception as e:
print(f"Error appending to file: {e}")
return False
Bridge.provide("append_to_file", append_to_file)
def loop():
pass # No periodic work needed
App.run(user_loop=loop)
<CODE/>
All of the above appears to run after I click on RUN in AppLab and wait for the message that app is running. In the monitor I see the python messages indicating text has been written. I see the sketch messages that the append to file was successful or not.
As a note: I put in the python message regarding file closed just to be certain that it was closed (as docs say it should be) and buffers written out.
My problem is: I don't see the resulting data.txt file.
I checked arduino user permissions and they were okay. I wrote a short python script to write out a data.txt file and it worked, dummy text was written to the file. I also showed all the files with ls -a and data.txt isn't there.
I am at a loss as to why this app doesn't write out a file. I thought Bridge would allow a script to invoke a python function (append_to_file) to handle file update on the Linux side.
I have searched for clues to the problem, but to no avail. Any suggestions? Am I barking up the wrong tree?
Thanks