MegunoLink supports UDP connections with your Arduino through WiFi or wired ethernet connections. The ESP8266 and ESP32 boards support WiFi connections while the EtherTen and Ethernet Shield support wired LAN connections.
To create a UDP connection:
- Open a connection manager visualizer
- Choose UDP from the Add Connection drop-down menu
MegunoLink will create a configuration panel for the new UDP connection:
The title box (top-left corner) lets you give the connection a name. This is useful to keep track when you have multiple connections. The name you enter here will be displayed on the Connect button shown in visualizers.
The Receive port is the UDP port that MegunoLink will listen on. Values less than 1,023 are reserved by the system while values between 1024 and 49,152 may be used by other applications. Generally any number between 49,152 and 65,535 should be fine.
The Destination IP address and port is the address that MegunoLink will send commands or messages to. The IP address 255.255.255.255
is a broadcast address. Messages will be sent to all devices listening on the destination port.
Click the Connect button to open the connection and begin receiving data. Only one program can use the port at any time so MegunoLink will show an error if another program already has the port open.
Sending UDP Data from an ESP8266 Arduino
This example sketch demonstrates plotting data and sending messages through a UDP connection receiving on port 52,791. It uses the ESP8266 Arduino toolchain.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include "ESP8266WiFi.h" #include "WiFiUdp.h" #include "MegunoLink.h" #include "ArduinoTimer.h" const int SourcePort = 52790; const int DestinationPort = 52791; const IPAddress DestinationIp(255, 255, 255, 255); WiFiUDP UdpConnection; ArduinoTimer SendTimer; // Setup WiFi credentials (SSID and password). #define USE_WIFI_CONFIG_FILE // Comment out this line to use Option 2 below #ifdef USE_WIFI_CONFIG_FILE // Include SSID and password from a library file. See: // https://www.megunolink.com/articles/wireless/how-do-i-connect-to-a-wireless-network-with-the-esp32/ #include "WiFiConfig.h" #else // Option 2 const char *SSID = "Your SSID"; const char *WiFiPassword = "Your Password"; #endif void setup() { Serial.begin(9600); Serial.println("UDP Plotting Example"); // Setup the WiFi connection, waiting until it is ready. WiFi.begin(SSID, WiFiPassword); auto Result = WiFi.waitForConnectResult(); if (Result == WL_CONNECTED) { Serial.print(F("Connected. My IP address: ")); Serial.println(WiFi.localIP()); UdpConnection.begin(SourcePort); } } void loop() { // Periodically send data when the connection is available. if (WiFi.isConnected() && SendTimer.TimePassed_Milliseconds(400)) { // Send a message UdpConnection.beginPacket(DestinationIp, DestinationPort); UdpConnection.println("A wireless hello"); UdpConnection.endPacket(); // Send data for plotting UdpConnection.beginPacket(DestinationIp, DestinationPort); TimePlot Plot("ADC", UdpConnection); Plot.SendData("A0", analogRead(A0)); UdpConnection.endPacket(); } } |
Processing Commands sent over a UDP Connection
This example sketch demonstrates receiving commands sent over a UDP connection. Commands are sent to port 52790 and processed using the command dispatcher. The sketch handles Hello
and GetADCValue
commands replying with a greeting and a measurement from the adc converter, respectively. The ADC value is sent to a MegunoLink table. You can open the Arduino code and MegunoLink project file from MegunoLink’s Library Examples menu.
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 44 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 70 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 113 |
/* ****************************************************************************** * Demonstrate processing commands sent over a UDP connection * See "Handle UDP Commands.mlx" file in example's folder for a MegunoLink * project to send commands and show the results. * Download a free trial of MegunoLink from www.MegunoLink.com * Check out: https://www.megunolink.com/documentation/connecting/udp-connections/ * for more information on UDP connections. */ #include "DNSServer.h" #include "ESP8266WebServer.h" #include "ESP8266WiFi.h" #include "WiFiUdp.h" #include "MegunoLink.h" #include "CommandDispatcher.h" const int SourcePort = 52790; const int DestinationPort = 52791; const IPAddress DestinationIp(255, 255, 255, 255); WiFiUDP UdpConnection; // ------------------------------------------------------------------------------- // Connection settings for the WiFi network to use: // Setup WiFi credentials (SSID and password). #define USE_WIFI_CONFIG_FILE // Comment out this line to use Option 2 below #ifdef USE_WIFI_CONFIG_FILE // Include SSID and password from a library file. See: // https://www.megunolink.com/articles/wireless/how-do-i-connect-to-a-wireless-network-with-the-esp32/ #include "WiFiConfig.h" #else // Option 2 const char *SSID = "Your SSID"; const char *WiFiPassword = "Your Password"; #endif // ------------------------------------------------------------------------------- // Handling commands const int MaxCommands = 10; CommandDispatcher Commands; void Cmd_GetADCValue(CommandParameter &p) { uint16_t ADCValue = analogRead(0); UdpConnection.beginPacket(DestinationIp, DestinationPort); Table MLPTable(UdpConnection); MLPTable.SendData("A0", ADCValue); UdpConnection.endPacket(); } void Cmd_Hello(CommandParameter &p) { UdpConnection.beginPacket(DestinationIp, DestinationPort); UdpConnection.println(F("Hello World!")); UdpConnection.endPacket(); } // Replaces the first '\r' or '\n' character with a null terminating the string. void TruncateAtNewLine(char *Buffer) { for(char *Current = Buffer; *Current; ++Current) { if (*Current == '\r' || *Current == '\n') { *Current = '\0'; break; } } } // ------------------------------------------------------------------------------- // Arduino entry points void setup() { delay(2000); Serial.begin(9600); Serial.println("WiFi Receive Test"); // Setup the WiFi connection, waiting until it is ready. WiFi.begin(SSID, WiFiPassword); auto Result = WiFi.waitForConnectResult(); if (Result == WL_CONNECTED) { Serial.print(F("Connected. My IP address: ")); Serial.println(WiFi.localIP()); UdpConnection.begin(SourcePort); } Commands.AddCommand(F("Hello"), Cmd_Hello); Commands.AddCommand(F("GetADCValue"), Cmd_GetADCValue); } void loop() { int PacketSize = UdpConnection.parsePacket(); if (PacketSize != 0) { char ReceiveBuffer[100]; int Length = UdpConnection.read(ReceiveBuffer, sizeof(ReceiveBuffer)); if (Length >= 0 && Length < sizeof(ReceiveBuffer)) { ReceiveBuffer[Length] = 0; } else { Serial.println(F("Message too big!")); Length = 0; } Serial.print(F("Received packet of ")); Serial.print(PacketSize); Serial.print(F(" bytes, from ")); Serial.print(UdpConnection.remoteIP()); if (Length > 0) { Serial.print(F(" content: ")); Serial.println(ReceiveBuffer); } if (Length >0 && ReceiveBuffer[0] == '!') { TruncateAtNewLine(ReceiveBuffer); Commands.DispatchCommand(ReceiveBuffer + 1, UdpConnection); } } } |