From fb5ec5e9db9dd5e7ecc926898d292b306268f298 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Tue, 9 Jul 2024 15:16:58 -0700 Subject: [PATCH] Update mesh_bot.py --- mesh_bot.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/mesh_bot.py b/mesh_bot.py index dfb88bf..c3a6c6e 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -2,7 +2,7 @@ # Meshtastic Autoresponder MESH Bot # K7MHI Kelly Keeton 2024 -import signal # for catching CTL+C +import asyncio # for the event loop import time # for sleep, get some when you can :) from pubsub import pub # pip install pubsub import meshtastic.serial_interface #pip install meshtastic @@ -381,7 +381,8 @@ def send_message(message, ch, nodeid=0): print (f"{log_timestamp()} System: Sending: {message} To: {get_name_from_number(nodeid)}") interface.sendText(text=message, channelIndex=ch, destinationId=nodeid) -def exit_handler(signum, frame): +def exit_handler(): + # Close the interface and save the BBS messages print("\nSystem: Closing Autoresponder") interface.close() print("System: Interface Closed") @@ -391,15 +392,22 @@ def exit_handler(signum, frame): print("System: Exiting") exit (0) -print ("\nMeshtastic Autoresponder MESH Bot CTL+C to exit\n") -pub.subscribe(onReceive, 'meshtastic.receive') -print (f"System: Autoresponder Started for device {get_name_from_number(myNodeNum)}") +def start_rx(): + # Start the receive loop + pub.subscribe(onReceive, 'meshtastic.receive') + print (f"System: Autoresponder Started for device {get_name_from_number(myNodeNum)}") + while True: + time.sleep(0.05) + pass -while True: - # Catch CTL+C to exit - time.sleep(0.05) - signal.signal(signal.SIGINT, exit_handler) - time.sleep(0.05) - pass +# Hello World +print ("\nMeshtastic Autoresponder MESH Bot CTL+C to exit\n") + +loop = asyncio.get_event_loop() +try: + loop.run_forever(start_rx()) +finally: + loop.close() + exit_handler() # EOF