Skip to main content

Remote control of servo motor by Ubuntu Laptop

Raspberry Pi 3 is a Linux based board.It has bluetooth and wifi built in it.We can use these modules to communicate with other Linux based computers.
  In this tutorial I am using socket programming to communicate between raspberry pi and Ubuntu based computer.
  Sockets uses Internet connection and it also uses TCP/IP protocol .It is a peer to peer connection.
BLOCK DIAGRAM


In above diagram Raspberry pi act as server and Ubuntu laptop act as client.Data is sent to from Laptop to raspberry pi 3 via websocket. On receiving data from Laptop rpi sends PWM signal to it's GPIO pin and servo starts moving according to signal on its signal pin.

Connections
Connect  VCC pin of servo to 5V pin of raspberry pi 3.Connect GND pin of  servo to GND pin of raspberry pi 3.Connect signal pin of servo to GPIO  19 pin of raspberry pi 3.
PROGRAM

Now make one python file in Ubuntu named as client.py and other file on raspberry pi named as rpiserver.py.

Server side program(on raspberry pi):-

import socket                 # Import socket module
import time
import RPi.GPIO as GPIO       # Import GPIO library
     
GPIO.setwarnings(False)          # do not show any warnings
GPIO.setmode (GPIO.BCM)            # programming the GPIO by BCM pin numbers
GPIO.setup(19,GPIO.OUT)             # initialize GPIO19 as an output
p = GPIO.PWM(19,50)              # GPIO19 as PWM output, with 50Hz frequency
p.start(1)

port = 12346
msg=0

angle=0

def motor_move_0():
       p.ChangeDutyCycle(2)
       time.sleep(1)
def motor_move_1():
      p.ChangeDutyCycle(3)
      time.sleep(1)
def motor_move_2():
      p.ChangeDutyCycle(4)
      time.sleep(1)
def motor_move_3():
       p.ChangeDutyCycle(5)
       time.sleep(1)
def motor_move_4():
        p.ChangeDutyCycle(6)
        time.sleep(1)
def motor_move_5():
         p.ChangeDutyCycle(7)
         time.sleep(1)
def motor_move_6():
         p.ChangeDutyCycle(8)
         time.sleep(1)
def motor_move_7():
           p.ChangeDutyCycle(9)
           time.sleep(1)
def motor_move_8():
            p.ChangeDutyCycle(10)
            time.sleep(1)
def motor_move_9():
             p.ChangeDutyCycle(11)
             time.sleep(1)

   # Create a socket object
                 # Now wait for client connection.
while True:

    s = socket.socket()
    host = "10.0.0.4"        # Get local machine name

    print port# Port
    s.bind((host, port))          # Bind to the port
    s.listen(5)
    c, addr = s.accept()       # Establish connection with client.
    print 'Got connection from', addr
    msg = c.recv(1024)
    time.sleep(1)
    global msg
    if msg=='0':
        motor_move_0()
    if msg=='1':
        motor_move_1()
    if msg=='2':
        motor_move_2()
    if msg=='3':
        motor_move_3()
    if msg=='4':
        motor_move_4()
    if msg=='5':
        motor_move_5()
    if msg=='6':
        motor_move_6()
    if msg=='7':
        motor_move_7()
    if msg=='8':
        motor_move_8()
    if msg=='9':
        motor_move_9()
    print msg

    c.close()
    port=port+1
    time.sleep(4)
CLIENT PROGRAM(on ubuntu):
#!/usr/bin/python
import socket
import time             # Import socket module
port = 12346 # port
while 1:
    s = socket.socket()         # Create a socket object
        host = "10.0.0.4" # Get local machine name ip address

    print port
    s.connect((host,port))
    angle=raw_input("Enter SERVO ANGLE:")
    s.send(angle)

#time.sleep(1)
    s.close()
    port=port+1
    time.sleep(8)




It is needed to run rpiserver.py file on raspberry pi and then client.py file on ubuntu laptop.It is needed to  put the ip local address of server in rpiserver.py and client.py files.
To view  video go to this link:



Comments