Read Temperature Sensor Data from an Arduino Using udpport
This example shows how to use udpport
to communicate with an Arduino® board to read temperature sensor data. This example uses an Arduino Uno, Ethernet Shield, and a TMP36 temperature sensor.
Setup
Attach a TMP36 temperature sensor to the Arduino. Connect the left pin of TMP36 to a 3.3v pin of the Arduino. The middle pin of the TMP36 should be attached to the A1 pin of the Arduino. The right pin of TMP36 should be connected to any ground pin of the Arduino.
Attach an Ethernet Shield to an Arduino Uno.
Plug in the Arduino Uno 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> #include <avr/dtostrf.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 address configured for Arduino. // Must belong in the same subnet (192.168.1.1) as the network adapter the 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; // Pin where the TMP36 is connected const int tempPin = A1; 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, read temperature and send a response packet if (packetSize) { // Read the received UDP packet data int packetSize = Udp.read(packetData, MAX_PACKET_SIZE); // Null-terminate the packet data to make it a valid C string packetData[packetSize] = '\0'; // Compare the received packet data with 'GET_TEMP' if (strncmp(packetData, "GET_TEMP", 8) == 0) { // Read temperature from TMP36 int tempReading = analogRead(tempPin); float voltage = tempReading * (3300 / 1024.0); float temperatureC = (voltage - 500) / 10.0; // Convert temperature to string char tempStr[16]; dtostrf(temperatureC, 6, 2, tempStr); // Transmit the temperature as a UDP packet Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); Udp.write(tempStr); Udp.endPacket(); } } delay(1); }
Establish a Connection to the Arduino
In MATLAB®, clear any existing UDP connections.
clear udpSender;
Set the IP address and port of the Ethernet Shield. This will vary depending on your device.
arduinoIPAddress = "192.168.1.177";
arduinoPort = 9999;
Create a UDP object.
udpSender = udpport
udpSender = UDPPort with properties: IPAddressVersion: "IPV4" LocalHost: "0.0.0.0" LocalPort: 54048 Tag: "" NumBytesAvailable: 0 Show all properties, functions
Receive Data from the Arduino
Define the request and send it to the Arduino Ethernet Shield.
getTempCommand = "GET_TEMP";
writeline(udpSender,getTempCommand,arduinoIPAddress,arduinoPort);
Wait for a response. Read the data sent from the Arduino as a character vector, then convert it to a double.
pause(1);
receivedData = read(udpSender,udpSender.NumBytesAvailable,"char");
temperatureC = str2double(receivedData)
temperatureC = 25.0900
This approach allows MATLAB to request temperature data from the Ethernet Shield, which reads the temperature from the TMP36 sensor and sends it back over UDP. Adjust the IP address, port, and other settings as needed for your specific setup.