This example shows you how to run remote Linux command on your Raspberry Pi® hardware.
Raspberry Pi hardware runs a Linux® distribution as the operating system. Using utilities shipped in the Simulink Support Package for Raspberry Pi Hardware, you can remotely execute Linux commands on the Raspberry Pi hardware directly from the MATLAB® command line. For example, you can run and stop a Simulink® model, list the contents of a directory, look up the CPU load of a process running on the Raspberry Pi hardware, etc. You can also launch an interactive PuTTY SSH session directly from within MATLAB.
We recommend completing Getting Started with Raspberry Pi® Hardware example.
Simulink Support Package for Raspberry Pi Hardware uses an SSH connection over TCP/IP to remotely execute Linux commands while building and running Simulink models on the Raspberry Pi hardware. You can use the infrastructure developed for this purpose to communicate with the Raspberry Pi hardware.
1. Create a raspberrypi object by executing the following on the MATLAB command line:
h = raspberrypi
The raspberrypi function returns a connection object, h, for Raspberry Pi hardware that has been set up using the targetupdater function. The hostname, user name, and password used to construct the raspberrypi object are the default MATLAB session values for these parameters. Simulink Support Package for Raspberry Pi Hardware saves one set of communication parameters, i.e. hostname, user name and password, for the Raspberry Pi hardware as default MATLAB session values. Note, the default MATLAB session values for the communication parameters are first determined during the firmware update process. The communication parameters may subsequently be changed using the Tools > Run on Target Hardware > Options UI in a Simulink model and are sticky, meaning that once you change the communication parameter values they are saved as default MATLAB session values, and are used for all Simulink models.
You may explicitly specify the hostname or IP address, user name, password when you create the raspberrypi object:
h = raspberrypi('<hostname or IP address>', '<user name>', '<password>');
The command above shows how to specify hostname, user name, and password. You may want to use this form if you have multiple Raspberry Pi hardware in your network that you need to distinguish individually.
2. Test connection to your Raspberry Pi hardware by executing the following command on the MATLAB command line:
message = h.connect
If successful, the connect method returns 'Connection successful' in the output argument message. The connect method executes the "echo Connection successful" Linux command over SSH. In case of a connection failure, a diagnostics error message is reported on the MATLAB command line. If the connection has failed, the most likely cause is incorrect IP address. To diagnose connection problems refer to the Troubleshooting Connection Problems section below.
You can use the execute method of the raspberrypi object to execute various Linux commands on the Raspberry Pi hardware from MATLAB.
1. Execute the following on the MATLAB command line:
[status, message] = execute(h,['ls -al ~'])
If successful, the command above returns a status of 0 and a message representing the output returned by the executed command. The returned message is a directory listing of the home directory.
2. Execute the following on the MATLAB command line:
execute(h,['ls -al ~'], true)
This command executes the same Linux command as in Step 1 of this task but directly outputs the result on the MATLAB command line rather than returning it in a variable.
The execute method of the raspberrypi object runs a Linux® command on the Raspberry Pi hardware using SSH protocol. Alternatively, you can open a PuTTY SSH terminal by executing the following command:
The PuTTY SSH terminal enables you to run interactive Linux commands on Raspberry Pi hardware. You may, for example, execute the same Linux commands introduced in the previous section to get a listing of the home directory.
Simulink Support Package for Raspberry Pi Hardware generates a Linux executable for each Simulink model you run on the Raspberry Pi hardware. The generated executable has the same name as the Simulink model and is stored in a directory under the Build directory you selected. To run/stop a Simulink model, you can use the run and stop methods of the raspberrypi communication object.
1. To run a Simulink model you previously run on the Raspberry Pi hardware, execute the following command on the MATLAB command line:
where the string '<model name>' is the name of the Simulink model you want to run on the Raspberry Pi hardware. The run method launches the Linux process corresponding to the Simulink model you specified.
2. To stop a Simulink model running on the Raspberry Pi hardware, execute the following command on the MATLAB command line:
This command kills the Linux process with the name '<model name>' on the Raspberry Pi hardware. Alternatively, you may execute the following commands to accomplish the same:
[~, pid] = execute(h,'pidof <model name>'); [st, msg] = execute(h,['echo ', h.Password, ' |sudo -S kill -9 ', pid])
1. How do I verify my Ethernet connection?
Verify that the FDX, LNK and 10M LEDs on your Raspberry Pi hardware is on and that the green LNK LED is blinking. If not, then the other end of the Ethernet connection may not be live or the Ethernet cable might be bad.
2. What are the different Ethernet connection options for the Raspberry Pi hardware?
Your Raspberry Pi hardware must be connected to:
a) a LAN/WAN network with an Ethernet cable b) the host computer directly with an Ethernet cable
If your Raspberry Pi hardware has a direct connection to the host computer, you must set a static IP address for your Raspberry Pi hardware. You must also ensure that the Ethernet card in your host computer talking to your Raspberry Pi hardware has been
assigned a static IP address.
3. How do I find the IP address of the Raspberry Pi hardware?
You can use your Raspberry Pi hardware in one of the following configurations:
i). Desktop with a monitor and keyboard attached ii). Headless (i.e. no monitor or keyboard)
Below you will find instructions on how to find the IP address of the board according to the configuration you are using:
i) Desktop: If you are operating your Raspberry Pi hardware with a keyboard and monitor attached, you can find the IP address of the board by logging in to your Raspberry Pi hardware and typing the following command on a Linux shell:
This command lists the network information for all Ethernet interfaces on your board. Example output looks like the following:
eth0 Link encap:Ethernet HWaddr b8:27:eb:63:40:b8 inet addr:172.28.145.190 Bcast:172.28.145.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:27224 errors:0 dropped:0 overruns:0 frame:0 TX packets:733 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2801074 (2.6 MiB) TX bytes:107019 (104.5 KiB) .....
The wired Ethernet connection on your Raspberry Pi hardware corresponds to the network interface 'eth0'. The IP address of the wired connection 'eth0' is displayed in the 'inet addr' field.
ii) Headless: If you are using your Raspberry Pi hardware in headless configuration, there are a number of ways to find the IP address:
a) If you configured your board to automatically get an IP address, you can simply use the hostname you assigned to communicate with your board. Assuming that you assigned 'raspberrypi-john' as the hostname during firmware update, you can connect to your board using the following commands:
h = raspberrypi('raspberrypi-john') h.connect
You may also try pinging your board by executing the following on the MATLAB command line:
b) Your Raspberry Pi will speak its IP address when it boots. In order to hear the IP address, you must connect a pair of headphones / speakers to the analog audio output connector.
c) You can configure your board to automatically send an e-mail whenever the IP address changes. The Raspbian Wheezy image distributed by MathWorks is configured to include the 'ssmtp' package that helps you send an e-mail from the Raspberry Pi hardware. The ssmtp package is a simple mail transfer agent (MTA) that requires an authenticated e-mail server to send e-mail. For simplicity, we configured ssmtp to use Gmail. The configuration file for the ssmtp is located in '/etc/ssmtp/ssmtp.conf' file on the Raspberry Pi hardware. The contents of this file is given below:
# # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=postmaster
# The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587
# Where will the mail seem to come from? rewriteDomain=gmail.com
# The full hostname hostname=gmail.com
# Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES
Modify the 'AuthUser' and 'AuthPass' entries in this file to reflect you Gmail account user name and the password to enable automatic e-mail feature. For example if your Gmail account user name is 'michael_doe' and your password is 'michael101', modify the relevant entries in the '/etc/ssmtp/ssmtp.conf' file as shown in the following:
You may also change the mail server if you wish to use a mail server other than Gmail. Modify the appropriate entries in the '/etc/ssmtp/ssmtp.conf' to match your e-mail server settings. The auto-email feature uses a shell script to send an e-mail whenever IP address changes. As a last step, you need to make this script executable. Execute the following command on a Linux shell on your Raspberry Pi hardware
sudo chmod ugo+x /etc/network/if-up.d/mailip
To test the auto-email feature, execute the following on a Linux shell on your Raspberry Pi hardware:
d) You can configure your Raspberry Pi hardware to use Dynamic DNS (DDNS). Dynamic DNS is a method of updating the Domain Name System to point to a changing IP address on the Internet. Dynamic DNS requires a DDNS server. You can, for example, use dnsdynamic.org. The instructions for setting up a dynamic DNS client on Raspberry Pi hardware can be found elsewhere on the Internet.
4. How do I test the SSH connection to Raspberry Pi hardware?
To test the SSH connection to your Raspberry Pi hardware, create a raspberrypi communication object and execute the following command on the MATLAB command line:
message = connect(h)
If successful, the connect method returns 'Connection successful' in the output argument message. In case of a connection failure, a diagnostics error message is reported on the MATLAB command line. If the connection has failed, the most likely cause is the wrong hostname or IP address. Follow 3 above to find the IP address. If you found the IP address and still cannot connect to the Raspberry Pi hardware, refer to 5.
5. I know the IP address of the Raspberry Pi hardware but SSH connection still fails.
There are a couple of likely scenarios. If your Raspberry Pi hardware is connected directly to the host computer using an Ethernet cable, you must make sure that the Ethernet card on your host computer to which the Raspberry Pi hardware is connected has a static IP address. For instructions on how to assign a static IP address to your Ethernet card under Windows refer to the following
guide. You must also ensure that the Raspberry Pi hardware and your computer is on the same subnet. For example, if you assigned a static IP address of 192.168.1.1 to your Ethernet card on the host computer, and the subnet mask is set to 255.255.255.0, you must assign an IP address in the range 192.168.1.2 to 192.168.1.254 to the Raspberry Pi hardware and a subnet mask that matches that of your host computer (usually 255.255.255.0).
If you are on a LAN/WAN network and cannot establish an SSH connection to the Raspberry Pi hardware, check that you do not have a firewall or anti-virus program that blocks TCP/IP port 22 used by the PuTTY SSH client. If you do, add this port to the list of TCP/IP ports that are not blocked or disable your firewall or anti-virus program. If SSH connection still fails, check that the SSH daemon is running on the Raspberry Pi hardware. To do this, execute the following on your Raspberry Pi hardware:
ps aux | grep /usr/sbin/sshd
This command returns the status of the SSH server. You should see an output similar to the following if the SSH server is running:
root 1915 0.0 0.5 6200 1008 ? Ss 10:57 0:00 /usr/sbin/sshd ...
If SSH server is not running for some reason, try restarting the server by executing the following command on your Raspberry Pi hardware:
sudo /etc/init.d/ssh restart
If SSH connection is still failing, restart network:
sudo /etc/init.d/networking restart