import socket
import time

# Replace with the IP address of the WeMos D1 Mini
WEMOS_IP = "192.168.233.158"  # Update this with the actual IP
PORT = 80
angle = 180
step = -1

def sc(channel, angle):
    """Sends a command to control the servo at specified channel and angle."""
    # Validate channel and angle
    if not (0 <= channel <= 15 and 0 <= angle <= 180):
        print("Invalid channel or angle. Channel must be 0-15 and angle must be 0-180.")
        return
    
    # Format the command in "C#:Angle" format
    command = f"C{channel}:{angle}\n"
    
    # Create a socket connection
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
        try:
            client_socket.connect((WEMOS_IP, PORT))
            client_socket.sendall(command.encode())  # Send the command to the server

            # Receive and print the response from the server
            response = client_socket.recv(1024).decode()
            print("Response from server:", response)
        except Exception as e:
            print("Connection error:", e)

def wave():
    sc(12, 180)
    sc(14, 60)
    sc(13, 170)
    time.sleep(0.4 )
    sc(0, 10)
    time.sleep(1.8)
    sc(15, 60)
    time.sleep(0.5)
    sc(15, 120)
    time.sleep(0.5)
    sc(15, 90)
    time.sleep(0.5)
    sc(0,90)
    time.sleep(1.2)
    sc(12, 90)
    sc(13, 90)
    
def give():
     
     sc(0, 65)
     sc(12, 135)   
     sc(13, 0)
     time.sleep(5)

     # Sweep channel 14 from 180 to 0 degrees
     for angle in range(180, 0, -10):  # From 180 to 0, inclusive
         sc(14, angle)
         time.sleep(0.05)  # Adjust for speed of the sweep

    # Continue with the rest of the sequence
     time.sleep(2)
     sc(0, 90)
     time.sleep(0.3)
     sc(12, 90)
     sc(13, 0) 

def relax():
    sc(0,90)
    time.sleep(1)
    sc(1,90)
    sc(12,90)
    sc(13,90)
    sc(14,180)
    sc(15,110)
    sc(2,90)
def cover_Face():
    sc(0, 60)
    sc(12,180)
    sc(13,0)
    time.sleep(0.2)
    sc(15,60)
    time.sleep(0.2)
    sc(14,45)
def intro1():
    sc(0,30)
    sc(12, 180)
def elbow():
    
    sc(12, 150)
    time.sleep(0.5)
    sc(12, 180)
def wrist():
    sc(15,80)
    time.sleep(0.5)
    sc(15, 110)
def rotation():
    sc(1, 120)
    time.sleep(0.5)
    sc(1, 90)



while True:

    ges = input(":")
    if "intro1" in ges:
        intro1()
    if "elbow" in ges:
        elbow()
    if "relax" in ges:
        relax()
    if "rotation" in ges:
        rotation()
    if "wrist" in ges:
        wrist()
    if "wave" in ges:
        wave()


