2021-04-28 14:38:14 -07:00
---
2022-11-04 10:06:45 -07:00
id: library
2022-10-31 10:27:04 -07:00
title: Using the Meshtastic Python Library
sidebar_label: Python Library
2021-04-28 14:38:14 -07:00
---
2021-12-22 16:06:09 -08:00
An example using Python 3 code to send a message to the mesh, get and set a radio configuration preference:
2022-03-08 23:51:46 -08:00
2021-04-28 14:38:14 -07:00
```python
import meshtastic
2021-12-22 15:58:04 -08:00
import meshtastic.serial_interface
2021-12-22 16:06:09 -08:00
# By default will try to find a meshtastic device,
# otherwise provide a device path like /dev/ttyUSB0
2021-12-22 15:58:04 -08:00
interface = meshtastic.serial_interface.SerialInterface()
2021-12-22 16:06:09 -08:00
# or something like this
# interface = meshtastic.serial_interface.SerialInterface(devPath='/dev/cu.usbmodem53230050571')
2021-12-22 15:58:04 -08:00
# or sendData to send binary data, see documentations for other options.
interface.sendText("hello mesh")
2021-12-22 16:06:09 -08:00
ourNode = interface.getNode('^local')
2023-08-06 23:15:43 -07:00
print(f'Our node preferences:{ourNode.localConfig}')
2021-12-22 16:06:09 -08:00
# update a value
print('Changing a preference...')
2023-08-06 22:40:31 -07:00
ourNode.localConfig.position.gps_update_interval = 60
2021-12-22 16:06:09 -08:00
2023-08-06 23:15:43 -07:00
print(f'Our node preferences now:{ourNode.localConfig}')
2023-08-06 22:40:31 -07:00
ourNode.writeConfig("position")
2021-12-22 16:06:09 -08:00
2021-04-28 14:38:14 -07:00
interface.close()
```
2021-11-22 19:24:18 -08:00
Another example using Python 3 code to send a message to the mesh when WiFi is enabled:
2022-03-08 23:51:46 -08:00
2021-11-22 19:24:18 -08:00
```python
import time
import meshtastic
2021-12-21 11:25:57 -08:00
import meshtastic.tcp_interface
2021-11-22 19:24:18 -08:00
from pubsub import pub
def onReceive(packet, interface): # called when a packet arrives
print(f"Received: {packet}")
def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio
# defaults to broadcast, specify a destination ID if you wish
interface.sendText("hello mesh")
pub.subscribe(onReceive, "meshtastic.receive")
pub.subscribe(onConnection, "meshtastic.connection.established")
2021-12-21 11:25:57 -08:00
interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.68.74')
2021-11-22 19:24:18 -08:00
while True:
time.sleep(1000)
interface.close()
```
2021-12-27 11:39:42 -08:00
Note: Be sure to change the IP address in the code above to a valid IP address for your setup.
2021-11-22 19:24:18 -08:00
2023-08-06 23:17:48 -07:00
For the rough notes/implementation plan see [TODO](https://github.com/meshtastic/python/blob/master/TODO.md). See the API for full details of how to use the library.
2021-04-28 14:38:14 -07:00
## A note to developers of this lib
2022-03-08 23:51:46 -08:00
We use the visual-studio-code default python formatting conventions (autopep8). So if you use that IDE you should be able to use "Format Document" and not generate unrelated diffs. If you use some other editor, please don't change formatting on lines you haven't changed.
2021-04-28 14:38:14 -07:00
If you need to build a new release you'll need:
2022-03-08 23:51:46 -08:00
2022-04-01 06:34:49 -07:00
```shell title="Command"
2021-04-28 14:38:14 -07:00
apt install pandoc
sudo pip3 install markdown pandoc webencodings pyparsing twine autopep8
2021-11-22 19:24:18 -08:00
```