Communicate with an Arduino Board Using udpport
This example shows how to use udpport
to communicate with an Arduino® board.This example uses an Arduino Uno and an Arduino Ethernet Shield.
Setup
Plug in an Arduino Uno and Ethernet Shield to your computer. The Ethernet Shield must be configured with the correct IP and port settings. To do this, identify a network adapter on your machine that was not previously used for an internet connection. Configure this network adapter with an IP address. Make sure the Ethernet Shield linked with the Arduino is connected to the ethernet port corresponding to the network adapter that you configured. The Ethernet Shield should be configured with an IP address that is in the same subnet as the IP address configured for the network adapter.
Upload the following sketch to your Arduino using the Arduino IDE.
#include <SPI.h> #include <Ethernet.h> #include <EthernetUdp.h> // Specify maximum UDP packet size #define MAX_PACKET_SIZE 512 // Specify MAC and IP address for Ethernet shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // IP adress configured for arduino. // Must belong in same subnet (192.168.1.1) as network adapter ethernet shield is connected to. IPAddress ip(192, 168, 1, 177); // Specify UDP port to listen on unsigned int localPort = 9999; // Create data array for storing the received UDP packet data payload char packetData[MAX_PACKET_SIZE]; EthernetUDP Udp; void setup() { Ethernet.begin(mac,ip); Udp.begin(localPort); } void loop() { // Process received packet int packetSize = Udp.parsePacket(); if (packetSize > MAX_PACKET_SIZE) { packetSize = MAX_PACKET_SIZE; } // If a packet was received, send a response packet if (packetSize) { // Read the received UDP packet data Udp.read(packetData,MAX_PACKET_SIZE); // Transmit an UDP packet back to the remote IP address and port specified in the received packet header // The transmitted UDP packet data byte values are the received values + 1 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); for (int i = 0; i < packetSize; i++) { Udp.write(packetData[i]+1); } Udp.endPacket(); } delay(1); }
This sketch listens for UDP data, adds one value to each element in the data, and sends the data back to the sender.
MATLAB Script
Clear any existing UDP connections.
clear udpSender;
Set the IP address and port of the Arduino Ethernet shield. This will vary depending on your device.
arduinoIPAddress = "192.168.1.177";
arduinoPort = 9999;
Create a UDP object in MATLAB®.
udpSender = udpport
udpSender = UDPPort with properties: IPAddressVersion: "IPV4" LocalHost: "0.0.0.0" LocalPort: 56541 Tag: "" NumBytesAvailable: 0 Show all properties, functions
Define the data you would like to send and write it to the Arduino board.
counterValue = 64; dataToSend = uint8(counterValue);
Write the data to the Arduino board.
write(udpSender,dataToSend,arduinoIPAddress,arduinoPort);
Wait for a response, read the received data, and display it.
pause(1);
receivedData = read(udpSender,udpSender.NumBytesAvailable,"uint8")
receivedData = 65
This approach uses MATLAB to directly communicate with the Arduino board over UDP. Adjust the IP address, port, and data as needed for your specific setup.