Get last message using Meshtastic Python API? (feature request)

In a simple non-developer world, the Meshtastic python utility is great for simple integration to other IOT environments, but it would be even better if there was a command to allow us to retrieve the last known message (or messages).

This would allow a 3rd party app to easily be able to retrieve, respond to and reply to any messages, which is particularly useful where we are building IOT connected base stations.

4 Likes

Hmm. I think this might already be there. If before your app creates a SerialInterface instance, you subscribe for notification on new messages (via pub.subscribe(onReceive, “meshtastic.receive”)) you’ll be informed of any new received messages or any messages that arrived at the device before your app connected (up to a limit based on the amount of RAM on the device).

Does that help?

1 Like

Not quite. I was referring to the Meshtastic python command line utility, for us non-developers.

2 Likes

If you run

meshtastic --seriallog stdout --debug

There is other output as well, but the messages will show up.

2 Likes

I don’t think this is what @Avron is talking about. I guess what they have in mind (and which is what I need, too) is the following.

  • A node is running on battery power, not connected to a computer, and unattended for some time (hours, days).

  • During this time, the node receives messages.

  • After that time, an “administrator” arrives, connects via USB, and wants to dump all messages received during the period of unattended operation.

Python’s onReceive API needs the computer to be physically connected to the node all the time as does seriallog.

2 Likes

The range test plug-in may fit the bill here. Configure it to save the messages and received messages will be saved to a csv.

Can’t exactly get the data over usb, but it’s a start.

1 Like

What if someone made an “inbox” plugin specifically for storing last n messages with a simple command for retrieval?

1 Like

Maybe :slight_smile: I think the plugin system should be able to support that kind of effort today.

This is close, but not quite.

Ideally I’d like to be able to use the meshtastic command line to show me the last received message in a similar way it allows me to sendtext. for example, something like: meshtastic --gettext
to return the last message in the form of a json, showing sender, time sent and message.

That way it is easy to integrate with less open IOT systems where we can use meshtastic to pass data and react to data, just by using this command line tool.

To further clarify, I am a casual developer and still have no idea how to add “plug-ins” and every time someone mentions just using them, I shudder, so am staying well away from tweaking my own versions of meshtastic.

Instead I’m looking at the command line utility as a great tool for allowing connected devices to use meshtastic as a network to pass IOT data and react to inbound messages.

So, for me being able to use the meshtastic command line utility to sendtext works a treat, but I can’t look for and act on inbound messages and their content…

Understood. For a casual developer, I’d encourage you to try python and our python api (and we’ll help when you have questions). I’d bet it is even easier than parsing any text output from the command line tool.

2 Likes

the 10ish line example here includes reading messages from the device:

https://meshtastic.github.io/Meshtastic-python/meshtastic/index.html

1 Like

Still very deep end for me, but I’ll get there.

In the meantime, I noticed there is a DeviceState field called rx_text_message that keeps the last received message. Is there a way we can let the Meshtastic CLI have a function to return that stored value?

3 Likes

If there is any way of voting this up - be a great feature to have. allow me to get messages easily off the devices and into a more useable environment.

1 Like

I would love it as well. I have put my meshtastic device in a box with a small arm processor and have a permanent USB link between the two. The box is sitting on a remote pole to provide neighborhood coverage. Arm processor is POE powered so I can talk to it and run the python cli. What would be really useful is a way to get the messages off the meshtastic device, into my arm processor and then I can process them/ sort them as do as needed.

took a stab at this today from @mc-hamster advice and just exposing the messages as csv would be pretty simple. I may take another stab at cleaning this up here in the next week or so and implementing then fetching the file with python would be trivial. C++ is pretty new for me though so realistically idk how far out a releasable implementation from me would be

2 Likes

This would be really good. Being able to ask for last messages and getting few back would be great and allows the IoT service to regularly poll the meshtastic CLI and be able to work with those messages it knows it has not yet seen.

2 Likes

First post here. By the way, great job everyone. I’m trying to setup a Madison, WI wide network, and it’d be nice if a new node could request all the messages in memory from a neighboring node.

Also, I’m a big fan of the command line interface, so up-voting the get-all-messages from command line.

I still have lots of questions, but I am having good luck finding answers.
Thanks

1 Like

Here’s what I came up with. I haven’t written any code since 25 years ago in a VisualBasic elective in college, so I’m sure it could be better.

import datetime
import meshtastic
from pubsub import pub

interface = meshtastic.SerialInterface()

def onReceive(packet, interface): # called when a packet arrives
    sender = packet['fromId']
    print(f"\nMessage from {interface.nodes[sender]['user']['longName']} ({interface.nodes[sender]['position']['latitude']}, {interface.nodes[sender]['position']['longitude']}) to {packet['toId']} at {datetime.datetime.fromtimestamp(packet['rxTime']).strftime('%Y-%m-%d %H:%M:%S')}:\n{packet['decoded']['text']}\n")

while True:
    pub.subscribe(onReceive, "meshtastic.receive.text")

If you get rid of the while loop and run this code in the python shell, you can also type

interface.sendText("your message")

at any time to send a message as well.

Edit: thanks for the tip @geeksville

2 Likes

cool. If you wrap code with three back quotes at beginning and end formatting will be preserved.

1 Like