Skip to content

Instantly share code, notes, and snippets.

@ronnywang
Last active September 24, 2024 11:07
Show Gist options
  • Save ronnywang/4a466f963bc2419ec1e454bd9ee64039 to your computer and use it in GitHub Desktop.
Save ronnywang/4a466f963bc2419ec1e454bd9ee64039 to your computer and use it in GitHub Desktop.
import time
import os
import json
import meshtastic
import meshtastic.serial_interface
from rich import print as rprint
from pubsub import pub
def onReceive(packet, interface):
indent_packet = json.dumps(packet, indent=2, default=lambda s: " ".join(str(s).split()))
if packet['decoded']['portnum'] != 'POSITION_APP' and packet['decoded']['portnum'] != 'TELEMETRY_APP':
print(indent_packet)
# write one line json to log.jsonl without json encode
packet = json.dumps(packet, default=lambda s: " ".join(str(s).split()))
with open('log.jsonl', 'a') as f:
f.write(packet + "\n")
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
print("Connected to radio")
pub.subscribe(onReceive, "meshtastic.receive")
pub.subscribe(onConnection, "meshtastic.connection.established")
interface = meshtastic.serial_interface.SerialInterface()
node = interface.nodes
packet = json.dumps(node, indent=2, default=lambda s: " ".join(str(s).split()))
# write packet to config.json
with open('config.json', 'w') as f:
f.write(packet)
# empty log.jsonl
open('log.jsonl', 'w').close()
# get the filesize of message.jsonl
cursor = os.path.getsize('message.jsonl')
while True:
time.sleep(1)
if cursor < os.path.getsize('message.jsonl'):
# read line from cursor = cursor
with open('message.jsonl') as f:
f.seek(cursor)
for line in f:
interface.sendText(line, channelIndex=2)
print("New message added to message.jsonl, sending message to radio" + line)
cursor = os.path.getsize('message.jsonl')
interface.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment