Simulink

Using WiFi Shield with Arduino® Uno Hardware

This example shows how to use Simulink Support Package for Arduino Uno hardware to receive and send TCP/IP or UDP messages using an Arduino Uno with a WiFi Shield.

Prerequisites

We recommend completing Getting Started with Arduino Uno HardwareGetting Started with Arduino Uno Hardware.

Required Hardware

To run this example you will need the following hardware:

  • Arduino Uno board

  • WiFi Shield

  • USB cable

Introduction

Simulink Support Package for Arduino Uno hardware enables you to create and run Simulink® models on Arduino Uno board. The target includes a library of Simulink blocks for configuring and accessing Arduino sensors, actuators, and communication interfaces.

In this example, you will learn how to create Simulink models receiving TCP/IP or UDP messages from a remote host and sending TCP/IP or UDP messages to a remote host, identified with a unique IP address and port number.

The host model uses the blocks from the Instrument Control Toolbox to send and receive TCP/IP or UDP messages to the target hardware.

Connect WiFi Shield to Arduino Uno Board

In this task, you will connect your WiFi Shield to the Arduino Uno board

1. Connect the WiFi Shield on the Arduino Uno board.

2. Connect a USB cable from the PC to the Arduino Uno hardware.

For more details on connecting the Arduino Uno to WiFi Shield, refer to Connecting Arduino WiFi Shield to ArduinoConnecting Arduino WiFi Shield to Arduino in the documentation.

Configure Network Properties for Host Machine and WiFi Shield

Refer to Configure network settings for Arduino WiFi ShieldConfigure network settings for Arduino WiFi Shield section of the documentation to configure the IP Address for the host machine and the WiFi Shield.

Task 1 - Sending TCP/IP Data with Arduino Uno and WiFi Shield

This example shows how to use Simulink Support Package for Arduino Uno hardware to send TCP/IP messages using an Arduino Uno board with a WiFi Shield.

Models involved in the demo

1. Target model running on the Arduino Uno: Arduino Uno TCP/IP SendArduino Uno TCP/IP Send.

2. Simulink host model: TCP/IP Host ReceiveTCP/IP Host Receive.

Run the Model configured for the target on the hardware

1. Open the Arduino Uno TCP/IP SendArduino Uno TCP/IP Send model.

2. In your Simulink model, click the Deploy to Hardware button on the toolbar. Wait for the WiFi Shield Link LED to be turned on, indicating successful connection to the WiFi network. Check the network properties, if the WiFi Shield Error LED turns on.

Run the host model to receive data from the target model

1. Open the TCP/IP Host ReceiveTCP/IP Host Receive model. Double-click on the TCP/IP Receive block. Make sure that the port number is matching the WiFi Shield settings specified earlier to ensure that the data is received from the correct target hardware.

2. In the Simulink host model, click the Run button. The scope should display the data being sent from the target hardware.

Task 2 - Receiving TCP/IP Data with Arduino Uno and WiFi Shield

This task shows how to use Simulink Support Package for Arduino Uno hardware to receive TCP/IP messages using an Arduino Uno board with a WiFi Shield.

Models involved in the demo

1. Target model running on the Arduino Uno: Arduino Uno TCP/IP ReceiveArduino Uno TCP/IP Receive.

2. Simulink host model: TCP/IP Host SendTCP/IP Host Send.

Run the model configured for the target on the hardware

1. Open the Arduino Uno TCP/IP ReceiveArduino Uno TCP/IP Receive model.

2. In your Simulink model, click the Deploy to Hardware button on the toolbar. Wait for the WiFi Shield Link LED to be turned on, indicating successful connection to the WiFi network. Check the network properties, if the WiFi Shield Error LED turns on.

Run the host model to send data to the target model

1. Open the TCP/IP Host SendTCP/IP Host Send model. Double-click on the TCP/IP Send block. Make sure that the IP address and port number are matching the WiFi Shield settings earlier specified to ensure that the data is sent to the correct target hardware.

2. The target model running on Arduino Uno will receive data from TCP/IP and send it back to the serial port for monitoring and debugging. You can use a serial monitor or hyperterminal on the host machine to watch the serial data coming back from the target.

3. In the Simulink host model, click the Run button to start running the host model.

4. At this point, TCP/IP data is sent from the host to the target and serial data is sent back from the target to the host. You should be able to see the serial data on the host using a serial monitor or hyperterminal.

Task 3 - Sending UDP Data with Arduino Uno and WiFi Shield

This example shows how to use Simulink Support Package for Arduino Uno hardware to send UDP messages using an Arduino Uno board with a WiFi Shield.

Models involved in the demo

1. Target model running on the Arduino Uno: Arduino Uno UDP SendArduino Uno UDP Send.

2. Simulink host model: UDP Host receiveUDP Host receive.

Run the Model configured for the target on the hardware

1. Open the Arduino Uno UDP SendArduino Uno UDP Send model.

2. In your Simulink model, click the Deploy to Hardware button on the toolbar. Wait for the WiFi Shield Link LED to be turned on, indicating successful connection to the WiFi network. Check the network properties, if the WiFi Shield Error LED turns on.

Run the host model to receive data from the target model

1. Open the UDP Host receiveUDP Host receive model. Double-click on the UDP Receive block. Make sure that the port number is matching the WiFi Shield settings specified earlier to ensure that the data is received from the correct target hardware.

2. In the Simulink host model, click the Run button. The scope should display the data being sent from the target hardware.

Task 4 - Receiving UDP Data with Arduino Uno and WiFi Shield

This task shows how to use Simulink Support Package for Arduino Uno hardware to receive UDP messages using an Arduino Uno board with a WiFi Shield.

Models involved in the demo

1. Target model running on the Arduino Uno: Arduino Uno UDP ReceiveArduino Uno UDP Receive.

2. Simulink host model: UDP Host SendUDP Host Send.

Run the model configured for the target on the hardware

1. Open the Arduino Uno UDP ReceiveArduino Uno UDP Receive model.

2. In your Simulink model, click the Deploy to Hardware button on the toolbar. Wait for the WiFi Shield Link LED to be turned on, indicating successful connection to the WiFi network. Check the network properties, if the WiFi Shield Error LED turns on.

Run the host model to send data to the target model

1. Open the UDP Host SendUDP Host Send model. Double-click on the UDP Send block. Make sure that the IP address and port number are matching the WiFi Shield settings specified earlier to ensure that the data is sent to the correct target hardware.

2. The target model running on Arduino Uno will receive data from UDP and send it back to the serial port for monitoring and debugging. You can use a serial monitor or hyperterminal on the host machine to watch the serial data coming back from the target.

3. In the Simulink host model, click the Run button to start running the host model.

4. At this point, UDP data is sent from the host to the target and serial data is sent back from the target to the host. You should be able to see the serial data on the host using a serial monitor or hyperterminal.

Summary

This example showed you how to use Simulink Support Package for Arduino Uno hardware to send TCP/IP or UDP messages to a remote host and to receive TCP/IP or UDP messages from a remote host using an Arduino Uno board with a WiFi Shield.