Python API setup issues

Hi Guys,

I have a T-BEAM V1.0 w/NEO-6M running Meshtastic 0.6.8 and the Python API 0.6.7

As I seem to have been caught out by the current bluetooth issue where by it constantly shows a connected but sleeping in the App I thought I’d try out the python API but I get the following (same issue if I run a sudo)

pi@pi-nas:~/meshtastic $  meshtastic --debug
WARNING: No module named 'pexpect'
DEBUG:root:Connecting to /dev/ttyUSB0
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 310, in _reconfigure_port
    fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
BlockingIOError: [Errno 11] Resource temporarily unavailable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/.local/bin/meshtastic", line 10, in <module>
    sys.exit(main())
  File "/home/pi/.local/lib/python3.7/site-packages/meshtastic/__main__.py", line 128, in main
    client = StreamInterface(args.device, debugOut=logfile)
  File "/home/pi/.local/lib/python3.7/site-packages/meshtastic/__init__.py", line 345, in __init__
    devPath, 921600, exclusive=True, timeout=0.5)
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 272, in open
    self._reconfigure_port(force_update=True)
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 312, in _reconfigure_port
    raise SerialException(msg.errno, "Could not exclusively lock port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 11] Could not exclusively lock port /dev/ttyUSB0: [Errno 11] Resource temporarily unavailable
pi@pi-nas:~/meshtastic $ 

Any ideas or suggestions?

Cheesr
Paul

1 Like

hmm two things.

Not your problem but your error message makes me realize we’re forgetting to ask for a library one of our libs need:

for the time being can you run “pip install pexpect”? I’ll update the next release of the lib to include that dependency automatically.

For the real problem it sounds like your user account might not have permissions to access /dev/ttyUSB0. Is it possible some other thread already has that port open? Can you try “sudo meshtastic --debug” and see what happens?

OK so this was user error!!

“pip install pexpect” solved part 1 and part 2 was because I had IDE open that was hogging the port; although it should not have been.

Thanks!!

1 Like

Making some progress… Messages are sometimes sent via the meshtastic executable but basit pythin has some issues:

pi@pi-nas:~/meshtastic $ python3 experiment.py 
ERROR:root:Node not found for fromId
ERROR:root:Error handling FromRadio, possibly corrupted? 40
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/meshtastic/__init__.py", line 404, in __reader
    self._handleFromRadio(self._rxBuf[HEADER_LEN:])
  File "/home/pi/.local/lib/python3.7/site-packages/meshtastic/__init__.py", line 194, in _handleFromRadio
    self._handlePacketFromRadio(fromRadio.packet)
  File "/home/pi/.local/lib/python3.7/site-packages/meshtastic/__init__.py", line 252, in _handlePacketFromRadio
    self._nodesByNum[asDict["from"]]["position"] = p
KeyError: 40
ERROR:root:Node not found for fromId
ERROR:root:Node not found for fromId
ERROR:root:Error handling FromRadio, possibly corrupted? 40

Python code:

import meshtastic
import time

import meshtastic
interface = meshtastic.StreamInterface() # By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
interface.sendText("hello mesh") # or sendData to send binary data, see documentations for other options.

for i in range(20):
        interface.sendText("hello mesh "+str(i)) # or sendData to send binary data, see documentations for other options.
        time.sleep(30)

re: messages sometimes don’t arrive
I think you’ll be happier if you change to:

interface.sendText("hello mesh "+str(i), wantAck=True)

(That requests reliable datagram send. Though perhaps I should change that to be the default?)

re:
ERROR:root:Node not found for fromId
Hmm - I’m trying to think of why this could occur. When the client starts it asks the node for its node db. Is it possible you woke the node only after connecting it to the computer? If that’s the case, I think there is a device improvement we should make:

I appear to be having the same error issue, although the message was sent.

Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.

import meshtastic
interface = meshtastic.StreamInterface()
interface.sendText(“hello mesh”)
ERROR:root:Node not found for fromId
ERROR:root:Error handling FromRadio, possibly corrupted? 2883442996
Traceback (most recent call last):
File “C:\Python\Python37\lib\site-packages\meshtastic_init_.py”, line 407, in __reader
self._handleFromRadio(self.rxBuf[HEADER_LEN:])
File "C:\Python\Python37\lib\site-packages\meshtastic_init
.py", line 197, in _handleFromRadio
self.handlePacketFromRadio(fromRadio.packet)
File "C:\Python\Python37\lib\site-packages\meshtastic_init
.py", line 261, in _handlePacketFromRadio
self._nodesByNum[asDict[“from”]][“user”] = u
KeyError: 2883442996

Just speculating. Based on my findings with the debugger I think this is when the remote node sends a message and it is the first time the api has seen this fromId. I guess a quick reset of the remote node would trigger a ‘here I am’ message to all other nodes and the api would get to know the remoteId and add it to the internal datastructure.

For me I have updated my python to use:

interface.sendText("hello mesh "+str(i), wantAck=True)

But I don’t see the message being recieved by the second device

Thanks. I’m still slowly working on it, but the reset didn’t help.

When I use --sendping or --reply on my heltec running 1.1.32 the screen brightness bar triggers, not sure if its python, firmware, user related i submitteda bug report via Android. Im not aware of anyway to debug and --reply or – sendping. As I type that im thinking --debug —sendping might be a close start. Lol ill try that and report back. But I’m using python 3.9.1 and latest meshtastic it doesn’t do this on my other boards


meshtastic --debug --reply
DEBUG:root:Connecting to COM5
DEBUG:root:Sending: want_config_id: 42

    Trigger powerFSM 9
    DEBUG:root:Received: {'myInfo': {'myNodeNum': 3221004200, 'numChannels': 13, 'region': '1.0-US', 'hwModel': 'heltec', 'firmwareVersion': '1.1.32', 'packetIdBits': 32, 'currentPacketId': 1288118805, 'nodeNumBits': 32, 'messageTimeoutMsec': 300000, 'minAppVersion': 20120}}
    DEBUG:root:Received: {'radio': {'preferences': {'lsSecs': 300, 'region': 'US'}, 'channelSettings': {'modemConfig': 'Bw125Cr48Sf4096', 'psk': 'AQ=='}}}
    DEBUG:root:Received: {'nodeInfo': {'num': 3221004200, 'user': {'id': '!3c71bffc9fa8', 'longName': 'Unknown 9fa8', 'shortName': '?A8', 'macaddr': 'PHG//J+o'}}}
    DEBUG:root:Node without position
    DEBUG:root:Received: {'nodeInfo': {'num': 2443305892, 'user': {'id': '!500291a1e3a4', 'longName': 'TTGO 2.1', 'shortName': 'T2', 'macaddr': 'UAKRoeOk'}, 'snr': 9.5}}
    DEBUG:root:Node without position
    DEBUG:root:Received: {'configCompleteId': 42}
    Connected to radio
    Trigger powerFSM 3
    Trigger powerFSM 5
    DEBUG:root:Received: {'packet': {'from': 2443305892, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': 'dGVzdA=='}}, 'id': 2343353859, 'rxSnr': 12.5, 'hopLimit': 1}}
    DEBUG:root:Publishing topic meshtastic.receive.text
    DEBUG:root:Received: {'from': 2443305892, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': b'test', 'text': 'test'}}, 'id': 2343353859, 'rxSnr': 12.5, 'hopLimit': 1, 'fromId': '!500291a1e3a4', 'toId': '^all'}
    message: test
    Sending reply:  got msg 'test' with rxSnr: 12.5 and hopLimit: 1
    DEBUG:root:Sending: packet {
      to: 4294967295
      decoded {
        data {
          portnum: TEXT_MESSAGE_APP
          payload: "got msg \'test\' with rxSnr: 12.5 and hopLimit: 1"
        }
      }
      id: 3435602454
    }

    Trigger powerFSM 9
    Trigger powerFSM 5
    Trigger powerFSM 3
    Trigger powerFSM 5
    DEBUG:root:Received: {'packet': {'from': 3221004200, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': 'Z290IG1zZyAndGVzdCcgd2l0aCByeFNucjogMTIuNSBhbmQgaG9wTGltaXQ6IDE='}}, 'id': 3435602454}}
    DEBUG:root:Publishing topic meshtastic.receive.text
    DEBUG:root:Received: {'from': 3221004200, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': b"got msg 'test' with rxSnr: 12.5 and hopLimit: 1", 'text': "got msg 'test' with rxSnr: 12.5 and hopLimit: 1"}}, 'id': 3435602454, 'fromId': '!3c71bffc9fa8', 'toId': '^all'}
    'rxSnr'
    Trigger powerFSM 3
    Trigger powerFSM 5
    DEBUG:root:Received: {'packet': {'from': 2443305892, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': 'bWF5YmUgdGhpcyBpcyBlbm91Z2g/'}}, 'id': 2343353860, 'rxSnr': 12.5, 'hopLimit': 1}}
    DEBUG:root:Publishing topic meshtastic.receive.text
    DEBUG:root:Received: {'from': 2443305892, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': b'maybe this is enough?', 'text': 'maybe this is enough?'}}, 'id': 2343353860, 'rxSnr': 12.5, 'hopLimit': 1, 'fromId': '!500291a1e3a4', 'toId': '^all'}
    message: maybe this is enough?
    Sending reply:  got msg 'maybe this is enough?' with rxSnr: 12.5 and hopLimit: 1
    DEBUG:root:Sending: packet {
      to: 4294967295
      decoded {
        data {
          portnum: TEXT_MESSAGE_APP
          payload: "got msg \'maybe this is enough?\' with rxSnr: 12.5 and hopLimit: 1"
        }
      }
      id: 3435602455
    }

    Trigger powerFSM 9
    Trigger powerFSM 5
    Trigger powerFSM 3
    Trigger powerFSM 5
    DEBUG:root:Received: {'packet': {'from': 3221004200, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': 'Z290IG1zZyAnbWF5YmUgdGhpcyBpcyBlbm91Z2g/JyB3aXRoIHJ4U25yOiAxMi41IGFuZCBob3BMaW1pdDogMQ=='}}, 'id': 3435602455}}
    DEBUG:root:Publishing topic meshtastic.receive.text
    DEBUG:root:Received: {'from': 3221004200, 'to': 4294967295, 'decoded': {'data': {'portnum': 'TEXT_MESSAGE_APP', 'payload': b"got msg 'maybe this is enough?' with rxSnr: 12.5 and hopLimit: 1", 'text': "got msg 'maybe this is enough?' with rxSnr: 12.5 and hopLimit: 1"}}, 'id': 3435602455, 'fromId': '!3c71bffc9fa8', 'toId': '^all'}
    'rxSnr'