import time import threading from http.server import BaseHTTPRequestHandler, HTTPServer import RPi.GPIO as GPIO # ================= GPIO SETUP ================= GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) OUT1, OUT2, OUT3, OUT4 = 12, 11, 13, 15 PINS = [OUT1, OUT2, OUT3, OUT4] for p in PINS:     GPIO.setup(p, GPIO.OUT)     GPIO.output(p, GPIO.LOW) # ================= MOTOR SEQUENCE (same as motor_test) ================= SEQ_HALF = [     (1,0,0,0),     (1,0,1,0),     (0,0,1,0),     (0,1,1,0),     (0,1,0,0),     (0,1,0,1),     (0,0,0,1),     (1,0,0,1), ] STEP_DELAY = 0.01 STEPS_90 = 100 motor_lock = threading.Lock() def set_coils(s):     GPIO.output(OUT1, GPIO.HIGH if s[0] else GPIO.LOW)     GPIO.output(OUT2, GPIO.HIGH if s[1] else GPIO.LOW)     GPIO.output(OUT3, GPIO.HIGH if s[2] else GPIO.LOW)     GPIO.output(OUT4, GPIO.HIGH if s[3] else GPIO.LOW) def step_motor_halfsteps(halfsteps, direction=+1):     """     direction=+1 forward:  SEQ_HALF[i % 8]     direction=-1 reverse:  SEQ_HALF[(-i) % 8]     Exactly matches motor_test behavior.     """     for i in range(halfsteps):         idx = (i % 8) if direction > 0 else ((-i) % 8)         set_coils(SEQ_HALF[idx])         time.sleep(STEP_DELAY)     # for p in PINS:     #     GPIO.output(p, GPIO.LOW) # ================= WEB PAGE ================= HTML = f"""      Project 1 – Grabber Control      

Stepper Motor Grabber

  

90° = {STEPS_90} half-steps

        
Ready.
   """ class Handler(BaseHTTPRequestHandler):     def do_GET(self):         if self.path == "/":             self.send_response(200)             self.send_header("Content-Type", "text/html")             self.end_headers()             self.wfile.write(HTML.encode())         else:             self.send_response(404)             self.end_headers()     def do_POST(self):         with motor_lock:             if self.path == "/grab":                 step_motor_halfsteps(STEPS_90, direction=+1)                 msg = "Grab: rotated +90°"             elif self.path == "/release":                 step_motor_halfsteps(STEPS_90, direction=-1)                 msg = "Release: rotated -90°"             else:                 self.send_response(404)                 self.end_headers()                 return         self.send_response(200)         self.send_header("Content-Type", "text/plain")         self.end_headers()         self.wfile.write(msg.encode())     def log_message(self, format, *args):         return def main():     server = HTTPServer(("0.0.0.0", 8000), Handler)     print("Server running:")     print("  Local:   http://127.0.0.1:8000/")     print("  Network: http://10.247.137.239:8000/")     try:         server.serve_forever()     except KeyboardInterrupt:         pass     finally:         server.server_close()         for p in PINS:             GPIO.output(p, GPIO.LOW)         GPIO.cleanup()         print("GPIO cleaned up.") if __name__ == "__main__":     main()