Python API via USB

SInce bluetooth/Android are a bit of a struggle to get & remain connected, I though that I would give the Python API a try - but I have very little experience with Python so the comment ```# By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0 doesn’t help as I do not know how to change the path. Could anyone provide the alternative code to replace:

*import meshtastic*
*interface = meshtastic.StreamInterface()*
*interface.sendText("hello mesh")*
TTGO T-beam is connected OK at ttyUSB0 (CP2104 USB to UART).
Thanks in anticipation . . .
1 Like

I think what you’re looking for is something like this:

import meshtastic
interface = meshtastic.StreamInterface("/this/is/the/path/i/defined")
interface.sendText("hello mesh")

The documentation can be found on github.io here: class StreamInterface

The Constructor code for that Class can be found here: StreamInterface init

1 Like

Thanks for the feedback, so now I’ve got past that difficulty.

But now __init__py is throwing up multiple syntax errors, of which this is just one of the reported code lines with a syntax error:
logging.error(“Subclass must provide toradio: {toRadio}”)

I have deleted all the logging.error lines, only to be greeted with an Exception error
"Multiple ports detected, you must specify a device, such as {ports[0].device}

I’m tempted to give up for now & come back in a few months time.

It looks like you might be trying to use this library with Python 2. It requires python3 (or later). pip will only install this on python 3 based systems, so I’m not sure why python 2 found it in its PYTHON_PATH.

(alternatively, can you post a longer log of the error messages - including the run of the python command and your code and I can help take a look)

I am also getting this same error, attempting to use MeshWatch
https://meshtastic.discourse.group/t/meshwatch-testing-and-monitoring-tool/3923

~/meshtalk$ python3 meshwatch.py -s "test"
Traceback (most recent call last):
  File "meshwatch.py", line 47, in <module>
    import meshtastic
  File "/usr/local/lib/python3.5/dist-packages/meshtastic/__init__.py", line 168
    logging.error(f"Subclass must provide toradio: {toRadio}")
                                                            ^
SyntaxError: invalid syntax

Thank you in advance

It looks like you’re using Python 3.5. f-strings in Python were introduced in 3.6. You need a newer version of Python to run Meshtastic.

Thank you greatly for your response. After installing python3.6, (got source, compiled locally) I am still unable to get it to work, just trying the basic “meshtastic -h” to test before using it in a script such as Meshtalk. It seems f-strings are still an issue in my environment. Note the hardware, maybe I should just be using this on an RPi?

linaro@tinkerboard:~/meshtalk$ which python3.6
/usr/local/bin/python3.6
linaro@tinkerboard:~/meshtalk$ alias python='/usr/local/bin/python3.6'
linaro@tinkerboard:~/meshtalk$ python -V
Python 3.6.9
linaro@tinkerboard:~/meshtalk$ meshtastic -h                          
Traceback (most recent call last):
  File "/usr/local/bin/meshtastic", line 7, in <module>
    from meshtastic.__main__ import main
  File "/usr/local/lib/python3.5/dist-packages/meshtastic/__init__.py", line 118
    logging.error(f'An exception of type {exc_type} with value {exc_value} has occurred')

Asus Tinker Board, ARMv7 Processor rev 1 (v7l)
Linux version 4.4.132+ (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.9) ) #8

You’ve aliased python to python3.6 in your shell, but meshtastic’s script (and probably meshtalk) isn’t using your shell at all. You’ll likely need to edit the meshtastic script (vi $(which meshtastic)), as mine starts with this:

#!/usr/bin/python3

You’ll need to change it to

#!/usr/local/bin/python3.6

For meshtalk / meshwatch.py, you should be able to invoke it directly with the python3.6 interpreter – /usr/local/bin/python3.6 ./meshtalk/meshwatch.py