15.3.1.2. Hello cam¶
The openmv.Camera class is the Python entry
point for everything the package can do. Every other
page in this tutorial calls into it. The smallest
useful program connects, uploads a MicroPython script,
and prints whatever the script writes to stdout:
from openmv import Camera
script = """
import time
while True:
print('hello from the cam')
time.sleep(1)
"""
with Camera('/dev/ttyACM0') as cam:
cam.stop()
cam.exec(script)
while True:
if text := cam.read_stdout():
print(text, end='')
15.3.1.2.1. A few notes¶
stop() before
exec() is not optional. Without
it, any previously running script keeps writing to
stdout, and the new exec fights it for
control of the stdin channel.
exec() uploads the script string
into the cam’s stdin buffer and runs it in the
same MicroPython interpreter the IDE talks to. The
script imports csi, image, ml,
and the rest of the cam surface the earlier chapters
covered – the only thing different is that the
source arrives over USB instead of from main.py
on the camera or the IDE.
15.3.1.2.2. Stopping the script¶
When the host program wants to interrupt the running
script, call stop(). The script
receives the same interrupt the IDE would deliver and
exits at the next opportunity. Leaving the context
manager (the with block) closes the serial port but
does not stop the script – the cam keeps running
whatever was last loaded until something interrupts it.
15.3.1.2.3. Errors and exceptions¶
Anything that goes wrong at the protocol layer raises
an openmv.OMVException or one of its
subclasses (TimeoutException,
ChecksumException,
SequenceException). Wrapping the
with block in try / except OMVException is
the simplest way to surface a disconnected USB cable or
a cam that stopped responding.
The protocol retries internally before raising; by the
time an OMVException reaches the application,
the retry budget is gone and the link is genuinely
broken. The defaults (max_retry=3, timeout=1.0)
work for everyone – tune them at the constructor only
if a specific application needs different behaviour.