Networking Examples

This section provides examples related to communication to PiMotion™ devices.

Scan for PiMotion™ devices on local network

A simple way to scan the local network for all PiMotion™ devices is to use pilib.scan()

[source code]

1
2
3
4
5
6
import pilib

devices = pilib.Pidev.pidev_scan('all', 2000, 10)
for device in devices:
   print (device)

The output should look similar to:

('pimotionA3D49CAC.local', '192.168.1.200', '0.53R', 'pimotion', 'app', 0L, 39743L)
('pimotionA3D48EC5.local', '192.168.1.201', '0.53R', 'pimotion', 'app', 0L, 39743L)

Identifying a PiMotion™ device

This Python code will identify a PiMotion™ device by rapid blinking of the red LED.

[source code]

1
2
3
4
5
6
7
8
import pilib

pm = pilib.Pidev('192.168.1.200')        # make a connection to the PiMotion

# tell the PiMotion device at 192.168.1.200 to rapidly
# blink the red LED for 5 seconds
pm.pidev_set_id_duration(5000)

Modifying the port number of a PiMotion™ device

PiMotion™ devices can be setup to operate on alternative port numbers. The default port number is 39743. The port number can be specified when making a connection to the device.

[source code]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pilib
import time

def set_port_restart(pm, port = 39743):
    """
    Set the new port number in the configuration flash memory, commit it
    to flash and restart the device.
    """
    
    pm.config_set_ip_port(port)  # set the new port number
    pm.config_commit()           # save the configuration (port) change
    
    # restart the device
    try:
        pm.config_reset()
    except:
        print ("Restarting the PiMotion...")
    
    time.sleep(15)               # wait for the PiMotion to restart
    
print ("Connecting to PiMotion %s on port %d" % ('192.168.1.200', 39743))
 
# make a connection to the PiMotion on the default port   
pm = pilib.Pidev('192.168.1.200')

print ("Setting the port to 39733...")

# set the port to 39733 and restart
set_port_restart(pm, 39733)

print ("Connecting to PiMotion %s on port %d" % ('192.168.1.200', 39733))

# make a connection to the PiMotion on the new port
pm = pilib.Pidev('192.168.1.200', port = 39733)

# read the port number from the configuration
new_port = pm.config_get_ip_port() 

print ("The port number is now %d!" % (new_port))
print ("Setting the port back to the default 39743...")

# reset the port back to the default 39743
set_port_restart(pm, 39743)

Modifying the password of a PiMotion™ device

PiMotion™ devices should be configured to use a password other than the default. The example below provides a method to modify the password.

[source code]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pilib
import time

def set_password_restart(pm, password = 'password'):
    """
    Set the new password and restart the device
    """
    
    pm.pidev_set_password(password)  # set the new password
    
    # restart the device
    try:
        pm.config_reset()
    except:
        print ("Restarting the PiMotion...")
    
    time.sleep(10)               # wait for the PiMotion to restart
    
print ("Connecting to PiMotion %s with default password" % ('192.168.1.200'))
 
# make a connection to the PiMotion on the default port   
pm = pilib.Pidev('192.168.1.200')

print ("Setting the password to temp_pass...")

# set the new password and restart
set_password_restart(pm, 'temp_pass')

print ("Connecting to PiMotion %s with new password..." % ('192.168.1.200'))

# make a connection to the PiMotion on the new port
pm = pilib.Pidev('192.168.1.200', password = 'temp_pass')

# read the temperature from the device
temperature = pm.pidev_get_temperature()

print ("The temperature of the amplifier is %.2f C" % (temperature))

print ("Setting the password back to the default...")

# reset the port back to the default 39743
set_password_restart(pm, 'password')