edws

Kobo Wifi Controller

Dec 9th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | Source Code | 0 0
  1. import network
  2. import urequests
  3. import time
  4. import socket
  5. import machine
  6. from machine import Pin
  7. from picozero import pico_led
  8.  
  9. # You will need to first install kobo-wifi-remote on your kobo before this will work, https://github.com/sublipri/kobo-wifi-remote
  10.  
  11. class Koboturner:
  12.     # Initialize the device and network
  13.     def __init__(self, ssid, password):
  14.         self.ssid = ssid
  15.         self.password = password
  16.         self.url = "http://192.168.4"
  17.         self.full_url = ""
  18.         self.urlfound = False
  19.         self.rbutton = Pin(16, Pin.IN) #, Pin.PULL_UP) if using buttons, add pull_up resistor
  20.         self.lbutton = Pin(15, Pin.IN) #, Pin.PULL_UP) and change l/rbutton values to 0 in turn()
  21.         self.ap_mode()  # Initialize AP mode
  22.  
  23.     # Flashes the LED 'i' times.
  24.     def led_flash(self, i):
  25.         for x in range(i):
  26.             print("flash")
  27.             pico_led.on()
  28.             time.sleep(0.1)
  29.             pico_led.off()
  30.             time.sleep(0.1)
  31.  
  32.     # Activates AP mode and sets up the device.
  33.     def ap_mode(self):
  34.         ap = network.WLAN(network.AP_IF)
  35.         ap.config(essid=self.ssid, password=self.password)
  36.         ap.active(True)
  37.  
  38.         while not ap.active():
  39.             pass
  40.         print('AP Mode Is Active, You can Now Connect')
  41.         print('IP Address To Connect to:: ' + ap.ifconfig()[0])
  42.  
  43.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  44.         s.bind(('', 80))
  45.         s.listen(5)
  46.         self.led_flash(2)
  47.  
  48.     def reqloop(self, lower, upper, lr):
  49.         for i in range(lower, upper):
  50.             self.full_url = f"{self.url}.{i}"
  51.             print(self.full_url)
  52.             try:
  53.                 response = urequests.get(f"{self.full_url}/{lr}", timeout=0.1)  # Make HTTP GET request
  54.                 print("URL Response:", response.status_code)
  55.                 response.close()
  56.                 if response.status_code == 200:
  57.                     self.urlfound = True
  58.                     return True
  59.             except Exception as e:
  60.                 print(f"Failed to connect to {self.url}: {e}")
  61.         self.led_flash(1)
  62.  
  63.     # Loops through possible IPs and sends a request to find the right one.
  64.     def getreqloop(self, lr):
  65.         print(f"{lr} button pressed")
  66.         self.led_flash(1)
  67.  
  68.         if self.urlfound:
  69.             response = urequests.get(f"{self.full_url}/{lr}")  # Make HTTP GET request
  70.             print("URL Response:", response.status_code)
  71.             response.close()        
  72.         else:
  73.             if self.reqloop(16, 18, lr): # Devices seem to generally connect as 192.168.4.16 or .17, and the pico is .1
  74.                 return
  75.             else:
  76.                 time.sleep(2)
  77.                 self.reqloop(2, 30, lr) # Expand search if initial loop fails, theoretically could go to 255
  78.             self.led_flash(2)
  79.         time.sleep(0.2)
  80.  
  81.     # Handles the right and left button presses.
  82.     def turn(self):
  83.         while True:
  84.             try:
  85.                 # Handle the right button press
  86.                 if self.rbutton.value() == 1:
  87.                     self.getreqloop("right")
  88.  
  89.                 # Handle the left button press
  90.                 if self.lbutton.value() == 1:
  91.                     self.getreqloop("left")
  92.             except Exception as e:
  93.                 # Catch all exceptions and print the traceback
  94.                 print("Error in turn() function:", str(e))
  95.  
  96. # Main execution
  97. koboturner = Koboturner('koboturner', '12345678')
  98. koboturner.turn()
Add Comment
Please, Sign In to add comment