1
0

test_reed_switch.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. """
  3. Simple test script to verify reed switch functionality on GPIO 18.
  4. Run this script on your Raspberry Pi to test the reed switch.
  5. Usage:
  6. python test_reed_switch.py
  7. """
  8. import time
  9. import sys
  10. try:
  11. from modules.connection.reed_switch import ReedSwitchMonitor
  12. except ImportError:
  13. print("Error: Could not import ReedSwitchMonitor")
  14. print("Make sure you're running this from the dune-weaver directory")
  15. sys.exit(1)
  16. def main():
  17. print("=" * 60)
  18. print("Reed Switch Test - GPIO 18")
  19. print("=" * 60)
  20. print()
  21. # Initialize the reed switch monitor
  22. print("Initializing reed switch monitor on GPIO 18...")
  23. reed_switch = ReedSwitchMonitor(gpio_pin=18)
  24. # Check if we're on a Raspberry Pi
  25. if not reed_switch.is_raspberry_pi:
  26. print("❌ ERROR: Not running on a Raspberry Pi!")
  27. print("This test must be run on a Raspberry Pi with GPIO support.")
  28. return
  29. print("✓ Running on Raspberry Pi")
  30. print("✓ GPIO initialized successfully")
  31. print()
  32. print("=" * 60)
  33. print("MONITORING REED SWITCH")
  34. print("=" * 60)
  35. print()
  36. print("Instructions:")
  37. print(" • The reed switch should be connected:")
  38. print(" - One terminal → GPIO 18")
  39. print(" - Other terminal → Ground (any GND pin)")
  40. print()
  41. print(" • Bring a magnet close to the reed switch to trigger it")
  42. print(" • You should see 'TRIGGERED!' when the switch closes")
  43. print(" • Press Ctrl+C to exit")
  44. print()
  45. print("-" * 60)
  46. try:
  47. last_state = None
  48. trigger_count = 0
  49. while True:
  50. # Check if reed switch is triggered
  51. is_triggered = reed_switch.is_triggered()
  52. # Only print when state changes (to avoid spam)
  53. if is_triggered != last_state:
  54. if is_triggered:
  55. trigger_count += 1
  56. print(f"🔴 TRIGGERED! (count: {trigger_count})")
  57. else:
  58. print("⚪ Not triggered")
  59. last_state = is_triggered
  60. # Small delay to avoid overwhelming the GPIO
  61. time.sleep(0.05)
  62. except KeyboardInterrupt:
  63. print()
  64. print("-" * 60)
  65. print(f"✓ Test completed. Reed switch was triggered {trigger_count} times.")
  66. print()
  67. finally:
  68. # Clean up GPIO
  69. reed_switch.cleanup()
  70. print("✓ GPIO cleaned up")
  71. print()
  72. if __name__ == "__main__":
  73. main()