Upload files to "/"
This commit is contained in:
70
sequence-echo.py
Normal file
70
sequence-echo.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import asyncio
|
||||
import time
|
||||
from meshcore import MeshCore, EventType
|
||||
|
||||
# --- CONFIGURATION ---
|
||||
SERIAL_PORT = "/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0" # Change to your port (e.g., "COM3" on Windows)
|
||||
CHANNEL_NAME = "#dotmesh" # Target channel
|
||||
INTERVAL = 60 # Seconds between messages
|
||||
# ---------------------
|
||||
|
||||
async def send_periodic_sequence():
|
||||
# Connect via USB serial
|
||||
meshcore = await MeshCore.create_serial(SERIAL_PORT)
|
||||
print(f"Connected to MeshCore on {SERIAL_PORT}")
|
||||
|
||||
# 1. Programmatically look up the channel index
|
||||
CHANNEL_INDEX = await get_channel_index_by_name(meshcore, CHANNEL_NAME)
|
||||
|
||||
# 2. Safety check: Did we actually find it?
|
||||
if CHANNEL_INDEX is None:
|
||||
print(f"Critical Error: Channel '{CHANNEL_NAME}' is not configured on this radio.")
|
||||
return # Exit the script if we can't find the channel
|
||||
|
||||
print(f"Success: Found '{CHANNEL_NAME}' at index {CHANNEL_INDEX}")
|
||||
|
||||
sequence_number = 0
|
||||
|
||||
while True:
|
||||
try:
|
||||
message = f"Seq: {sequence_number}"
|
||||
|
||||
# 3. Use the dynamically found index to send the message!
|
||||
await meshcore.commands.send_chan_msg(CHANNEL_INDEX, message)
|
||||
print(f"Sent: '{message}' to {CHANNEL_NAME} (Index {CHANNEL_INDEX})")
|
||||
|
||||
sequence_number += 1
|
||||
await asyncio.sleep(INTERVAL)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
await asyncio.sleep(10)
|
||||
|
||||
|
||||
async def get_channel_index_by_name(meshcore, target_name):
|
||||
"""
|
||||
Queries the device to find the index of a specific channel by name.
|
||||
"""
|
||||
print(f"Looking up index for channel '{target_name}'...")
|
||||
|
||||
# Loop through the standard channel slots (0 through 7)
|
||||
for idx in range(8):
|
||||
# Fetch the configuration for this channel index
|
||||
result = await meshcore.commands.get_channel(idx)
|
||||
|
||||
# Check if the command succeeded and has payload data
|
||||
if result.type != EventType.ERROR and result.payload:
|
||||
# Print the raw dictionary to see the real keys!
|
||||
#print(f"Slot {idx} raw data: {result.payload}")
|
||||
|
||||
# We will update this logic once you see the correct key in your terminal
|
||||
channel_name = result.payload.get('channel_name')
|
||||
|
||||
if channel_name == target_name:
|
||||
return idx
|
||||
|
||||
return None # Return None if the channel wasn't found on the device
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(send_periodic_sequence())
|
||||
Reference in New Issue
Block a user