MegunoLink Pro supports communication over Ethernet networks using the User Datagram or UDP protocol. UDP is a simple protocol which is supported by the Arduino Ethernet libraries and Arduino compatible boards such as the EtherTen and EtherMega, both from Freetronics. UDP can also be used with shields including the Arduino Ethernet and WiFi shields.
The Arduino libraries makes it very easy to get started with Ethernet communication. This program will read selected analog inputs and send the data to MegunoLink Pro for plotting. Initially only the first channel is included, but you can toggle each ADC channel on or off by sending the channel number from MegunoLink Pro’s monitor window. The program was tested with the Freetronics EtherMega, but it should work with any Ethernet equipped Arduino. The latest version of the program is available on github.com
The first part of the program sets up some configuration we need. Like any internet device, the Arduino ethernet shield will need a MAC address and its own IP address. In theory the MAC address should be globally unique, but many Arduino devices don’t come with built-in MAC addresses. However, as long as the device is on a private network just pick a MAC address that doesn’t match any other device on your network. You’ll also need an IP address; this should be in the same address range as the IP address of your PC running MegunoLink Pro. To find out your PC’s address, open the Command Prompt (you’ll find it in the start menu) and type ipconfig. Look for the IPv4 Address; its probably something like 192.168.15.100. Normally you’d setup the Arduino to request an address from a DHCP server, but to keep things simpler, we can just make one up. The first three numbers should be the same as you’re computer’s address and the last number should be different to any other device on your network and between 1 and 254. If you don’t have many devices on your network, you can probably just pick one at random!
As well as its own IP address, the Arduino needs to know your computer’s IP address so it knows where to send the data for plotting by MegunoLink Pro. This is the destination address in the listing below.
You’ll also need a service port for each end of the connection. The service port lets several programs all listen for traffic on a single computer (on the PC end anyway). Each program uses a different port and this port number needs to be shared between the sender and receiver so the two ends know they are talking to the right program. The port can be the same, or different for each end. In this program I’ve used 8888 for both ends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <SPI.h> #include <Ethernet.h> #include <EthernetUdp.h> // ------------------------------------------------------------------ // Configuration // The mac address is the physical address associated with the // Ethernet port on the your device. It should be globally unique if // the board is connected to a public network, or at least locally // unique if the board is connected to a private network. byte MacAddress[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // The IP address is the internet protocol address for the board. Again // it should be globally unique if connected to a public network or // locally unique if connected to a private network. To communicate // successfully, it should also be on the same sub-net as your PC. // In practice that means the first three numbers should be the same // as your PC's IP address. And the last number should be different to // all devices on your network. Use ipconfig to find out your PC's IP address IPAddress MyIPAddress(192, 168, 15, 177); unsigned int LocalPort = 8888; // The destination address is the IP address of the computer you want to send // messages to. It would normally be on the same sub-net as the Arduino. In practice // this means the first three numbers of MyIPAddress and DestinationAddress should // be the same. IPAddress DestinationAddress(192, 168, 15, 101); unsigned int DestinationPort = 8888; // ------------------------------------------------------------------ // Communications EthernetUDP Udp; // To send & receive packets using UDP char PacketBuffer[UDP_TX_PACKET_MAX_SIZE]; // Space to hold messages we receive. unsigned long LastSend = 0; // Last time we sent a message const long SendPeriod = 1000; // Time between sending messages [milliseconds] // ------------------------------------------------------------------ // Data bool EnableADCSend[] = { true, false, false, false, false, false, false, false }; |
UDP data is handled by the ProcessUDPTraffic function, which is called each time the Arduino program runs through the main loop. This function checks for any UDP messages and reads them. It assumes the message is a single character which indicates which ADC port to toggle: 0 for the first ADC port, 1 for the second, etc. If a valid character is found at the start of the UDP message, it updates the array which keeps track of which ports should be graphed: EnableADCSend.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// ------------------------------------------------------------------ // Functions void ProcessUDPTraffic() { // See if there is any data available and read it. int nPacketSize = Udp.parsePacket(); if (nPacketSize > 0) { int nRead = Udp.read(PacketBuffer, sizeof(PacketBuffer)); if (nRead >= 1) { int nPort = PacketBuffer[0] - '0'; if (nPort >= 0 && nPort < sizeof(EnableADCSend)) { // Toggle sending data from the specified port. EnableADCSend[nPort] = !EnableADCSend[nPort]; Serial.print("Sending adc data for channel "); Serial.print(nPort); Serial.print(" is "); Serial.println(EnableADCSend[nPort] ? "on" : "off"); } } } } |
Reading the analog port and sending commands to MegunoLino Pro is handled by the SendADCData function, which is called each time through the main program loop. To avoid saturating the network, it doesn’t send data every time through the loop however. Instead, it keeps track of the last time a message was sent (the LastSend variable) and waits until the SendPeriod has passed before sending the next message.
At the appropriate time the function works through all of the ports and for any which are enabled, reads the ADC value and sends a message in the plot command format recognized by MegunoLink Pro.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
void SendADCData() { if (millis() - LastSend < SendPeriod) return; LastSend = millis(); for(int iPort = 0; iPort < sizeof(EnableADCSend); ++iPort) { if (EnableADCSend[iPort]) { int nValue = analogRead(iPort); Udp.beginPacket(DestinationAddress, DestinationPort); Udp.print("{TIMEPLOT|data|ADC "); Udp.print(iPort); Udp.print("|T|"); Udp.print(nValue); Udp.println('}'); Udp.endPacket(); } } } // ------------------------------------------------------------------ // Setup void setup() { // Start ethernet and udp Ethernet.begin(MacAddress, MyIPAddress); Udp.begin(LocalPort); // Start RS232 comms Serial.begin(9600); } // ------------------------------------------------------------------ // Loop void loop() { ProcessUDPTraffic(); SendADCData(); } |
To view the data sent by the Arduino program:
- Fire up MegunoLink Pro and create Time Plot, Monitor and Connection Manager visualizers.
- Select UDP from the Add Connection button on the Connection Manager visualizer to create a new UDP source.
- Set the receive port and the destination port to match the values chosen in your Arduino program. Set the destination address to 255.255.255.255, the UDP broadcast address (this saves having to remember the Arduino’s IP address).
- Set the source selector in the Monitor and Time Plot visualizers to the UDP port you just created and press the connect button
All going well, you’ll see data start to appear in the monitor window and on the graph. Type a number (0-7) into the control at the bottom of the Monitor visualizer and hit the send button. This will toggle transmission of data from the corresponding ADC channel adding and removing traces from the graph.
That’s it for our simple example. Grab the files from GitHub; both the MegunoLink Pro project file and the Arduino source code are in there. Download a free trial for MegunoLink Pro, then post a message in the comments below and let us know how you’re using ethernet communication in your project.
Great, thanks for sharing 🙂
Glad you enjoyed it Mark!