raspi - Connection to Raspberry Pi board - MATLAB (original) (raw)
Connection to Raspberry Pi board
Description
This object represents a connection from the MATLAB® software to the Raspberry Pi® board. To interact with the Raspberry Pi board, use this object with the functions listed in Object Functions.
Creation
Syntax
Description
[mypi](#mw%5F533cac1c-b5e3-4644-9fb7-434ecdac81c2) = raspi
creates a connection, mypi
, from the MATLAB software to the Raspberry Pi board. Use this syntax to connect or reconnect to the same board.
You do not need to supply the user IP address, user name, and password to create a connection. The raspi
object reuses these settings from the most recent successful connection to a Raspberry Pi board. These settings must be provided for the first connection created during the setup process.
In MATLAB Online™, the raspi
object reuses the settings from the most recent successful connection. If connecting for the first time, this syntax is used to create a connection to the first Raspberry Pi board listed by raspilist with status "Ready to connect"
.
After connecting to the board, you can use mypi
to interact with the Raspberry Pi board and peripheral devices.
To close the connection, use clear
to removemypi
and any other connections that usemypi
.
[mypi](#mw%5F533cac1c-b5e3-4644-9fb7-434ecdac81c2) = raspi([ipaddress](#mw%5F0a10a9f1-d988-4472-874e-893138201903),[username](#mw%5F947a10db-3e9d-4033-9879-d403004af655),[password](#mw%5Fd5327ed0-9ded-4b03-a16b-a49f7f42ece1))
overrides the IP address, user name, and password from the previous connection. Use this syntax to connect to a board whose settings are different from the previous successful connection. After changing the password on a board, use this syntax. Or, after connecting from the MATLAB software to a second Raspberry Pi board, use this syntax. You can use this syntax withoutusername
and address
if a successful connection has been previously created with this syntax. This syntax is not supported in MATLAB Online.
Note
The firmware has a default user name, 'pi'
, and password, 'raspberry'
. The pi
user name has sudo root powers at the command line. It is a good security practice to change the default password to a strong password.
[mypi](#mw%5F533cac1c-b5e3-4644-9fb7-434ecdac81c2) = raspi([hostname](#mw%5F3f4b34e4-ba98-4603-81e0-7add5334c6ba),[username](#mw%5F947a10db-3e9d-4033-9879-d403004af655),[password](#mw%5Fd5327ed0-9ded-4b03-a16b-a49f7f42ece1))
uses the host name instead of the IP address to make a connection to a Raspberry Pi. Use this syntax to connect a board whose settings are different from the previous successful connection or to connect to a second board. You can use this syntax without username
andaddress
if a successful connection has been previously created with this syntax. This syntax is not supported inMATLAB Online.
[mypi](#mw%5F533cac1c-b5e3-4644-9fb7-434ecdac81c2) = raspi(`serialnumber`)
creates a connection to a Raspberry Pi board in MATLAB Online using its SerialNumber. Identify the serial number using raspilist.
Note
This syntax is supported only in MATLAB Online.
[mypi](#mw%5F533cac1c-b5e3-4644-9fb7-434ecdac81c2) = raspi(___,`"Timeout"`,time)
creates a connection to a Raspberry Pi board in MATLAB Online, and waits for up to time
seconds for the connection to complete. The value of time
must be greater than the default connection timeout period of 12.5 seconds. You can use this syntax to specify a larger timeout value if you are unable to connect to your board.
Use this syntax with any of the input argument combinations in previous syntaxes.
Note
This syntax is supported only in MATLAB Online.
Input Arguments
IP address of the board, specified as a string.
Example: '169.254.0.2'
Data Types: char
Linux user name, specified as a string.
Example: 'pi'
Data Types: char
Linux user password, specified as a string.
Note
The firmware has a default user name, 'pi'
, and password, 'raspberry'
. This user has sudo root powers at the command line. It is a good security practice to change this default password to a strong password.
Example: 'raspberry'
Data Types: char
Host name of the board, specified as a string.
Example: 'raspberrypi-hysdu8X38o'
Data Types: char
Name in MATLAB Online, specified as a character array or string.
Example: 'myPi'
Example: "myPi"
Data Types: char
| string
Output Arguments
A connection from the MATLAB software to the Raspberry Pi board, returned as a raspi object. You can use this connection to interact with the Raspberry Pi hardware and attached devices (e.g., Camera Board, I2C, serial, and SPI).
You can use mypi
with the methods listed in Object Functions.
Properties
This property is read-only.
The IP address or host name of the Ethernet port on the Raspberry Pi hardware, returned as a string.
Example: 'raspberrypi-computername'
Data Types: char
Serial number of board in MATLAB Online, specified as a character array or string.
Example: '00000000a9f2c18c'
Example: "00000000a9f2c18c"
Data Types: char
| string
This property is read-only.
The IP port number used for TCP/IP connections, returned as a double.
Example: 18735
Data Types: double
This property is read-only.
The name of the Raspberry Pi hardware, model, and revision, returned as a string.
Example: 'Raspberry Pi Model B Rev 2'
Data Types: char
This property is read-only.
A list of LEDs that are available on the board, returned as a cell array of strings.
Example: {'led0'}
Data Types: cell
This property is read-only.
The numbers of the available GPIO pins on the Raspberry Pi hardware, returned as a vector of doubles.
Example: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
Data Types: array
This property is read-only.
The names of the available SPI channels on the Raspberry Pi hardware, returned as a cell array of strings.
Example: {'CE0' 'CE1'}
Data Types: cell
This property is read-only.
The names of the available I2C buses on the Raspberry Pi hardware, returned as a cell array of strings.
Example: {'i2c-0' 'i2c-1'}
Data Types: cell
This property is read-only.
The speed of the I2C buses on the Raspberry Pi hardware, returned as a double.
Example: 100000
Data Types: double
List of the available CAN interfaces on the Raspberry Pi hardware, returned as a cell array of strings.
Example: {"can0", "can1"}
Data Types: cell
Object Functions
configurePin | Configure GPIO pin as digital input, digital output, or PWM output |
---|---|
readDigitalPin | Read logical value from GPIO input pin |
writeDigitalPin | Write logical value to GPIO output pin |
showPins | Show diagram of GPIO pins |
writeLED | Turn LED on or off |
showLEDs | Show location, name, and color of user-controllable LEDs |
scanI2CBus | Scan I2C bus device addresses |
system | Run command in Linux shell on Raspberry Pi hardware |
openShell | Open terminal on host computer to use Linux shell on Raspberry Pi hardware |
getFile | Transfer file from Raspberry Pi hardware to host computer or MATLAB Drive |
putFile | Transfer file from host computer to target hardware |
deleteFile | Delete file on target hardware |
i2cdev | Connection to device on Raspberry Pi hardware |
spidev | Connection to SPI device on Raspberry Pi hardware |
serialdev | Connection to serial device on Raspberry Pi hardware |
cameraboard | Connection to camera board on Raspberry Pi hardware |
canChannel | Connection to CAN channel through specified device |
enableCAN | Enable CAN interface |
disableCAN | Disable CAN interface |
webcam | Create connection to Raspberry Pi web camera |
Examples
You can connect from the MATLAB software to a Raspberry Pi board. Use this connection to interact with the Raspberry Pi hardware.
Create a connection, mypi
, from the MATLAB software to a Raspberry Pi board.
mypi =
Raspi with Properties:
DeviceAddress: 'raspberrypi-hysdu8X38o'
Port: 18735
BoardName: 'Raspberry Pi Model B Rev 2'
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
AvailableSPIChannels: {}
AvailableI2CBuses: {'i2c-0' 'i2c-1'}
AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
I2CBusSpeed: 100000
AvailableCANInterfaces: {"can0"}
Use mypi
to connect from the MATLAB software to a serial device.
myserialdevice = serialdev(mypi,'/dev/ttyAMA0')
To close the connection, use clear
to remove mypi
and any other connections that use mypi
.
clear myserialdevice clear mypi
You can connect from the MATLAB software to Raspberry Pi board that has a different IP address, user name, and password from the previous successful connection. This syntax is not supported in MATLAB Online.
Follow this example to reconnect to a board after you change its settings, or to connect to multiple boards concurrently.
mysecondpi = raspi('169.254.0.4','rocky','bullwinkle')
mysecondpi =
Raspi with Properties:
DeviceAddress: '169.254.0.4'
Port: 18735
BoardName: 'Raspberry Pi Model B Rev 2'
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
AvailableSPIChannels: {}
AvailableI2CBuses: {'i2c-0' 'i2c-1'}
AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
I2CBusSpeed: 100000
AvailableCANInterfaces: {"can0"}
You can use a host name instead of an IP address to connect from the MATLAB software to a Raspberry Pi board. This syntax is not supported in MATLAB Online.
Support Package Installer generates the Raspberry Pi host name during the setup process.
mysecondpi = raspi('raspberrypi-hysdu8X38o','rocky','bullwinkle')
mysecondpi =
Raspi with Properties:
DeviceAddress: 'raspberrypi-hysdu8X38o'
Port: 18735
BoardName: 'Raspberry Pi Model B Rev 2'
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
AvailableSPIChannels: {}
AvailableI2CBuses: {'i2c-0' 'i2c-1'}
AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
I2CBusSpeed: 100000
AvailableCANInterfaces: {"can0"}
The DeviceAddress
reflects whether a host name or IP address created the connection.
To configure your Raspberry Pi board, see Connect to Raspberry Pi Hardware Board in MATLAB Online. If connecting to a Raspberry Pi for the first time in MATLAB Online, you can create a connection to the first board listed byraspilist with status "Ready to connect"
.
List the Raspberry Pi boards available for MATLAB Online connection.
list =
2x4 table
Name SerialNumber PackageVersion Status
______ __________________ ______________ __________________
"homePi" "00000000a9f2c18c" "18.1.0" "Ready to connect"
"workPi" "00000000f23438fd" "18.1.0" "Ready to connect"
Connect to the first board with status "Ready to connect"
.
r =
raspi with properties:
DeviceAddress: 'homePi'
SerialNumber: '00000000a9f2c18c'
BoardName: Raspberry Pi 3 Model B
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27] AvailableSPIChannels: {'CE0','CE1'} AvailableI2CBuses: {'i2c-1'} AvailableWebcams: {'UVC Camera (046d:0809)'} AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'} I2CBusSpeed: 0 AvailableCANInterfaces: {"can0"}
For later raspi
connections, this syntax reuses the settings from the most recent successful connection, instead of connecting to the first board with status "Ready to connect"
.
You can connect to your board using the name you created during the setup described in Connect to Raspberry Pi Hardware Board in MATLAB Online.
List the Raspberry Pi boards available for MATLAB Online connection.
ans =
1x4 table
Name SerialNumber PackageVersion Status
______ __________________ ______________ __________________
"myPi" "00000000a9f2c18c" "18.1.0" "Ready to connect"
Connect to your board using the name listed.
r =
raspi with properties:
DeviceAddress: 'myPi'
SerialNumber: '00000000a9f2c18c'
BoardName: Raspberry Pi 3 Model B
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27] AvailableSPIChannels: {'CE0','CE1'} AvailableI2CBuses: {'i2c-1'} AvailableWebcams: {'UVC Camera (046d:0809)'} I2CBusSpeed: 0 AvailableCANInterfaces: {"can0"}
You can connect to your board using the serial number inMATLAB Online.
List the available Raspberry Pi boards in MATLAB Online.
ans =
1x4 table
Name SerialNumber PackageVersion Status
______ __________________ ______________ __________________
"myPi" "00000000a9f2c18c" "18.1.0" "Ready to connect"
Connect to your board using the serial number from theraspilist
output.
myPi = raspi('00000000a9f2c18c')
r =
raspi with properties:
DeviceAddress: 'myPi'
SerialNumber: '00000000a9f2c18c'
BoardName: Raspberry Pi 3 Model B
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27] AvailableSPIChannels: {'CE0','CE1'} AvailableI2CBuses: {'i2c-1'} AvailableWebcams: {'UVC Camera (046d:0809)'} I2CBusSpeed: 0 AvailableCANInterfaces: {"can0"}
Extended Capabilities
- You can also deploy
raspi
on the Raspberry Pi hardware in MATLAB Online.