Advertisement
Sweetening

BLE_WiFi_Reboot_Controller.py

Jan 30th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. To interact with this script using nRF Connect (for Android or iOS) or LightBlue (for iOS):
  2.  
  3. nRF Connect
  4. Open nRF Connect on your smartphone.
  5. Scan for Bluetooth devices and connect to your CircuitPython device (it should be advertising as a BLE UART service).
  6. Once connected, go to the "UART" service and find the UART characteristic.
  7. Write the string "reboot" to this characteristic and send it. This should trigger the reboot request.
  8.  
  9. LightBlue
  10. Open LightBlue on your iPhone.
  11. Scan for Bluetooth devices and connect to your CircuitPython device.
  12. Explore the services and look for the UART service.
  13. Write the string "reboot" to the appropriate characteristic to trigger the reboot request.
  14.  
  15. _______________________________________________________________________________________________________________________________________
  16. import time
  17. import os
  18. import board
  19. import busio
  20. import displayio
  21. import digitalio
  22. from digitalio import DigitalInOut, Pull
  23. import socketpool
  24. import ssl
  25. import adafruit_requests
  26. import adafruit_displayio_sh1106
  27. from adafruit_debouncer import Debouncer
  28. import adafruit_ble
  29. from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
  30. from adafruit_ble.services.nordic import UARTService
  31.  
  32. # WiFi credentials
  33. ssid = os.getenv("CIRCUITPY_WIFI_SSID")
  34. passwd = os.getenv("CIRCUITPY_WIFI_PASSWORD")
  35.  
  36. # Connect to WiFi
  37. wifi.radio.connect(ssid=ssid, password=passwd)
  38. print("Connected with IP:", wifi.radio.ipv4_address)
  39.  
  40. # Set up display
  41. displayio.release_displays()
  42. WIDTH, HEIGHT = 130, 64 # Display size
  43. i2c = busio.I2C(board.SCL, board.SDA)
  44. display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
  45. display = adafruit_displayio_sh1106.SH1106(display_bus, width=WIDTH, height=HEIGHT)
  46.  
  47. # Initialize down button
  48. down_button_pin = DigitalInOut(board.IO18) # Use the correct pin for your down button
  49. down_button_pin.pull = Pull.UP
  50. down_button = Debouncer(down_button_pin)
  51.  
  52. # Set up BLE
  53. ble = adafruit_ble.BLERadio()
  54. uart_service = UARTService()
  55. advertisement = ProvideServicesAdvertisement(uart_service)
  56.  
  57. def send_reboot_request():
  58. pool = socketpool.SocketPool(wifi.radio)
  59. requests = adafruit_requests.Session(pool, ssl.create_default_context())
  60.  
  61. # Device info and API endpoint
  62. device_ip = os.getenv("TARGET_IP")
  63. device_password = os.getenv("TARGET_PASSWORD")
  64. url = f"http://{device_ip}/remoteReset"
  65. data = {'password': device_password}
  66.  
  67. # Send POST request
  68. response = requests.post(url, data=data)
  69. return response
  70.  
  71. while True:
  72. # BLE connection handling
  73. if not ble.connected:
  74. print("Waiting for a BLE connection...")
  75. ble.start_advertising(advertisement)
  76. else:
  77. print("Connected via BLE")
  78. ble.stop_advertising()
  79.  
  80. # Check for incoming BLE data
  81. if uart_service.in_waiting:
  82. command = uart_service.read(uart_service.in_waiting)
  83. command = command.decode("utf-8").strip()
  84.  
  85. # Trigger reboot request when receiving a specific command (e.g., "reboot")
  86. if command == "reboot":
  87. print("Received reboot command via BLE")
  88. response = send_reboot_request()
  89. print(f"Response: {response.status_code}\n{response.text}")
  90. print("DONE!")
  91. time.sleep(10)
  92.  
  93. # Button check
  94. down_button.update()
  95. if down_button.fell:
  96. print("Sending reboot request...")
  97. response = send_reboot_request()
  98. print(f"Response: {response.status_code}\n{response.text}")
  99. print("DONE!")
  100. time.sleep(10)
  101.  
  102. time.sleep(0.1) # Small delay to reduce CPU usage
  103. _______________________________________________________________________________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement