This is a tutorial on how to write small modules which run on the device. Modules are bits of regular 'Arduino' code that can send and receive packets to other Nodes/Apps/PCs using the Meshtastic mesh.
All modules should be sub-classes of MeshModule. By inheriting from this class and creating an instance of your new module - your module will be automatically registered to receive packets.
Messages are sent to particular port numbers (similar to UDP networking). Your new module should eventually pick its own port number (see below). For development use, you can simply use
PRIVATE_APP (which is the default).
Packets can be sent and received as either:
- Raw binary structures
You will typically want to inherit from either SinglePortModule (if you are just sending/receiving raw bytes) or ProtobufModule (if you are sending/receiving protobufs). You'll implement your own handleReceived/handleReceivedProtobuf - probably based on the example code.
The relevant bits of the class hierarchy are as follows:
First Level: MeshModule
- src/mesh/MeshModule.h - you probably don't want to use this base-class directly.
Second Level: SinglePortModule
- src/mesh/SinglePortModule.h - for modules that send/receive from a single port number (the normal case).
Third Level: ProtobufModule
- src/mesh/ProtobufModule.h - for modules that send/receive a single particular Protobuf type. Inherit from this if you are using protocol buffers in your module.
If your module needs to perform any operations at startup you can override and implement the
setup() method to run your code.
If you need to send a packet you can call
service.sendToMesh with code like this (from the examples):
MeshPacket *p = allocReply();
p->to = dest;
A number of key services are implemented using the Module API, These modules are as follows:
TextMessageModule - Receives text messages and displays them on the LCD screen/stores them in the local DB.
NodeInfoModule - Receives/sends User information to other nodes so that usernames are available in the databases.
RemoteHardwareModule - A module that provides easy remote access to device hardware (for things like turning GPIOs on or off). Intended to be a more extensive example and provide a useful feature of its own. See remote-hardware for details.
ReplyModule - A simple module that just replies to any packet it receives (provides a 'ping' service).
The easiest way to get started is:
- Build the firmware codebase.
- Copy the ReplyModule as a template into
cp src/modules/ReplyModule.* src/modules/YourModule.*
- Change the port number from
- Edit the
setupModules()function located at
modules/Moduless.cppto add a call to create an instance of your module (see comment at head of that function).
- Rebuild with your new module and install on the device.
- Use the Meshtastic Python CLI tool to send a packet to your board, for example:
meshtastic --dest 1234 --sendpingwhere 1234 is another mesh node to send the ping to.
It is very common that you would like your module to be invoked periodically.
We use a crude/basic cooperative threading system to allow this on any of our supported platforms. Simply inherit from OSThread and implement
runOnce(). See the OSThread documentation for more details.
If you would like to proactively send messages (rather than just responding to them), just call
service.sendToMesh(). For an example of this, see NodeInfoModule::sendOurNodeInfo(...).
Picking a port number
How to add custom protocol buffers
If you would like to use protocol buffers to define the structures you send over the mesh (recommended), here's how to do that.
- Create a new
.protofile in the protos directory.
./bin/regen-protos.shto regenerate the C code for accessing the protocol buffers. If you don't have the required nanopb tool, follow the instructions printed by the script to get it.
- Done! You can now use your new protobuf just like any of the existing protobufs in Meshtastic.