MegunoLink supports TCP Client connections from your Arduino sketch to a TCP Server hosted by MegunoLink. Your Arduino sketch can connect to MegunoLink over WiFi or wired Ethernet. Arduino hardware supporting TCP connections includes:
- ESP8266, a low-cost Arduino with built-in WiFi
- ESP32, a low-cost Arduino with built-in WiFi and Bluetooth
- EtherTen, an Uno compatible Arduino with built-in Ethernet
- Ethernet shield, an Arduino compatible shield that adds an Ethernet connection
Creating a TCP Server
To create a TCP Server in MegunoLink:
- Open a connection manager visualizer
- Choose TCP Server from the Add Connection drop-down menu.
A configuration panel for the new TCP Server connection will be added to the Connection Manager:
Configuring the Connection
The TCP Server connection panel displayed in the Connection Manager is used to configure the connection:
The controls for configuring the connection are:
- Connection name: a free-form name to help you identify the connection. This name will be displayed on the Connection Selector.
- Port: the local port that MegunoLink will open for remote clients to connect to.
- Delete connection: remove the connection from the project.
- Listen/Close: start/stop the TCP Server that listens for connections.
- Connections: Remote clients that have connected to the server. The selected client will receive any commands sent from MegunoLink.
Arduino TCP Client
For your Arduino program to connect MegunoLink it must open a TCP Client connection. You need to know:
- The IP address of the computer running MegunoLink. This article shows how to find your IP address.
- The port that MegunoLink is listening on. This port is set in the TCP Server configuration panel.
Use the WiFiClient
class to create a WiFi client in your Arduino program.
For example, to send data to a plot in MegunoLink using a TCP Client connection on a wireless Arduino such as the ESP32 or ESP8266, you can use the sketch below. Make sure that you:
- Setup a
WiFiConfig
library with the SSID and password for your WiFi connection, or enter them directly into the code. - Set the
Host
variable to the IP address of the computer running MegunoLink and thePort
variable to the port that MegunoLink is listening on.
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 |
/* ****************************************************************** * This sketch sends analog measurements to a MegunoLink TCP Server * Set the Host to the IP address of your computer and Port to the * port you setup on MegunoLink's TCP Server configuration panel. * See https://www.megunolink.com/connecting/tcp-server for more information. */ #include #include "MegunoLink.h" #define USEWIFICONFIGFILE #ifdef USEWIFICONFIGFILE // Include SSID and password from a library file. For more information 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 const char *Host = "192.168.15.117"; const unsigned Port = 11000; // We'll use a single client and remain connected. The Arduino doesn't // inform the server that the client has disconnected which leaves a // lot of dangling connections to manage if we use a new client // for each message. WiFiClient Client; void ConnectToWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(SSID, WiFiPassword); Serial.print("Connecting to "); Serial.println(SSID); uint8_t i = 0; while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(500); if ((++i % 16) == 0) { Serial.println(F(" still trying to connect")); } } Serial.print(F("Connected. My IP address is: ")); Serial.println(WiFi.localIP()); } WiFiClient *GetClient() { if (!Client.connected() && Client.connect(Host, Port)) { return &Client; } return Client.connected() ? &Client : NULL; } void SendMeasurement(unsigned Value) { WiFiClient *pClient = GetClient(); if (pClient != NULL) { TimePlot Plot("", *pClient); Plot.SendData("ADC Value", Value); } else { Serial.println("TCP Connection failed"); } } void setup() { Serial.begin(9600); ConnectToWiFi(); } void loop() { unsigned Measurement = analogRead(0); SendMeasurement(Measurement); delay(1000); } |