Using the MeshCore Python API
Build custom applications and scripts with the meshtastic-python library for automation, monitoring and integrations
What is the MeshCore Python API?
The meshtastic-python library gives you programmatic access to MeshCore nodes. You can send messages, read telemetry, change configuration, and build custom applications. Perfect for automation, monitoring, and IoT integrations.
The Python API communicates with your node via USB serial or Bluetooth. You can connect to local nodes or remote nodes via MQTT. The library is open source and actively maintained by the community.
In this guide you'll learn how to install the Python API, perform basic operations, and build practical scripts. Whether you're a beginner or experienced developer, the API makes it easy to integrate MeshCore into your projects.
Installing meshtastic-python
Install the library via pip (Python 3.8+ required):
# Install meshtastic library pip install meshtastic # Or with all optional dependencies pip install "meshtastic[all]" # Verify installation meshtastic --version
On Linux you may need extra permissions for serial port access. Add yourself to the dialout group: sudo usermod -a -G dialout $USER (and log in again).
Basic usage
Simple example to connect to a node and send a message:
import meshtastic
import meshtastic.serial_interface
# Connect via USB serial
interface = meshtastic.serial_interface.SerialInterface()
# Send a text message to all nodes
interface.sendText("Hello MeshCore network!")
# Read node info
nodeInfo = interface.getMyNodeInfo()
print(f"Node ID: {nodeInfo['user']['id']}")
print(f"Long name: {nodeInfo['user']['longName']}")
# Close connection
interface.close()
This script connects to your node via USB, sends a message, and retrieves node information. The SerialInterface automatically detects the correct serial port.
Practical applications
Chatbots
Build a bot that automatically responds to messages. For example a weather bot, info bot, or admin bot for network management.
Monitoring dashboards
Collect telemetry from all nodes and visualize in Grafana, InfluxDB, or custom dashboard. Real-time network health monitoring.
Alert systems
Send notifications to email, Telegram, Discord when certain events happen. For example: low battery, node offline, sensor alarm.
Home automation
Integrate MeshCore with Home Assistant, Node-RED, or Domoticz. Trigger automations based on mesh messages.
Gateway scripts
Bridge between MeshCore and other systems: MQTT broker, REST API, database. Centralize data from multiple nodes.
Testing & debugging
Automated tests for network range, latency, packet loss. Debug tools for troubleshooting.
Code examples
1. Receive all messages
Listen to all incoming messages and print them to console:
import meshtastic
import meshtastic.serial_interface
def onReceive(packet, interface):
"""Callback for incoming messages"""
if packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP':
msg = packet['decoded']['text']
sender = packet['fromId']
print(f"[{sender}]: {msg}")
# Connect and subscribe
interface = meshtastic.serial_interface.SerialInterface()
interface.sendText("Bot is online!")
# Loop keeps running
print("Listening for messages... (Ctrl+C to stop)")
while True:
pass # Callbacks are automatically called
2. Send GPS position
Send a custom GPS position to the network (useful for testing):
import meshtastic
import meshtastic.serial_interface
interface = meshtastic.serial_interface.SerialInterface()
# Send GPS position (Amsterdam)
latitude = 52.3676
longitude = 4.9041
altitude = 5
interface.sendPosition(
latitude=latitude,
longitude=longitude,
altitude=altitude
)
print(f"Position sent: {latitude}, {longitude}")
interface.close()
3. Read telemetry from all nodes
Retrieve telemetry data from all known nodes in the network:
import meshtastic
import meshtastic.serial_interface
interface = meshtastic.serial_interface.SerialInterface()
# Get all nodes
nodes = interface.nodes
print("=== Node Telemetry ===")
for nodeId, node in nodes.items():
user = node.get('user', {})
position = node.get('position', {})
telemetry = node.get('deviceMetrics', {})
print(f"\n{user.get('longName', 'Unknown')}")
print(f" ID: {user.get('id', '?')}")
print(f" Battery: {telemetry.get('batteryLevel', '?')}\%")
print(f" Voltage: {telemetry.get('voltage', '?'):.2f}V")
if position:
print(f" Position: {position.get('latitude')}, {position.get('longitude')}")
interface.close()
Benefits of the Python API
Python is easy
Python is one of the most accessible programming languages. Many examples and tutorials available. Ideal for beginners and experts.
Plug & play
Install via pip and start immediately. Automatic port detection, no complex setup. Works on Windows, Mac, Linux, Raspberry Pi.
Good documentation
Extensive docs on python.meshtastic.org. Many code examples and community support on Discord/Telegram.
Active development
Library is actively maintained. New features are added quickly. Open source on GitHub.
Integration possibilities
Easy to integrate with MQTT, REST APIs, databases, Home Assistant, and other Python libraries.
Real-time callbacks
Event-driven architecture with callbacks. React immediately to messages, telemetry updates, or node status changes.
Frequently asked questions
Which Python version do I need?
Python 3.8 or higher is required. Check with python --version or python3 --version. Download Python from python.org if you don't have it yet.
Can I connect via Bluetooth instead of USB?
Yes! Use meshtastic.ble_interface.BLEInterface() instead of SerialInterface. Bluetooth support requires extra dependencies: pip install "meshtastic[ble]". Note: BLE is slower than USB.
How can I send messages to specific nodes?
Use interface.sendText("message", destinationId="!abcd1234"). The destination ID is the node ID (e.g. !a1b2c3d4). Without destinationId it broadcasts to all nodes.
Can I change configuration via the API?
Yes! You can adjust all node settings: interface.localNode.setOwner("New Name"), channel settings, LoRa parameters, etc. See the API documentation for all options.
Does the API work with remote nodes via MQTT?
Yes, you can connect to an MQTT broker: interface = meshtastic.mqtt_interface.MQTTInterface(...). This gives access to the network without a local node. See the MQTT integration page.
Where can I find more code examples?
Check the GitHub repo: github.com/meshtastic/python. The examples/ folder has many practical scripts. Also the official docs on python.meshtastic.org have tutorials.
Start with Python development
Ready to build custom MeshCore applications? Install the Python API and start experimenting.