Arduino MEGA: best way to send a message to a specific IP

Hi to all,

I'm using Arduino MEGA with ethernet and SD card shield to send data from sensors to a FTP server.
It works pretty well and I can get all the measurements by reading the text file stored in the FTP.

I would like to send also the same data to a specific IP address in the local network.
Is it possible to do that without having to implement a TCP server or client?
I was looking for a solution similar to a Serial.print(), but over the network.

Thank you

think you need to implement some TCP/IP client/server code
couple of ideas

  1. run a simple telnet client on the Mega transmitting to a terminal emulator on target machine, e.g. see error-when-connecting-led-with-serial-monitor
  2. or for the Mega to transmit UDP datagrams to the target UDP server

You need to have a process running on the computer at that IP address, to receive the message.

Maybe I'm missing something but if you are sending data to an FTP server you are already sending it to a specific IP address: the one the FTP server is at. Do the same for the local IP address.

Mod edit credentials removed.

I use this function to send the data log to the FTP server:

void sendToFTP(void) {

  int c = ftp.connect("x.x.x.x", "username", "password");
  while (!c) {
    Serial.println("I have to connect again to the FTP server!");
    delay(500);
    c = ftp.connect("x.45.x.y", "user", "passwd");
  }

  // Open the SD file
  File dataFile = SD.open(filename_sd);

  // Check if the file was succesfully open
  if (dataFile) {
    Serial.println("Sending trough FTP connection");
    ftp.store(filename_sd.c_str(), dataFile);
    dataFile.close();

  } else {
    Serial.println("Error opening2 datalog.txt");
    while (true)
      ;
  }
  ftp.stop();
}

How should I modify it to send just a text string to a local IP 192.168.1.100 ?
It does not have a user and a password, it is just a IP address.

Yes, this is normal, the problem is on the Arduino side.

What process is running and listening on 192.168.1.100, and on which port?

I don't know the answer to that, but I'd try the same thing without the username and password, just the IP address. If that doesn't work there are people here who understand this better than I do, hopefully one of them will help you.

Note I have removed your credentials from the code you posted, maybe be more careful about posting that kind of thing in public.

Teraterm on Windows.

Are you using telnet protocol? Check the Teraterm docs:
https://ttssh2.osdn.jp/manual/4/en/menu/setup-tcpip.html

And this:

simple telnet example

// telenet example switch NodeMCU ESP8266 LED on/oof

// adapted from https://forum.arduino.cc/t/error-when-connecting-led-with-serial-monitor/1121385

#include <ESP8266WiFi.h>

const int ledPin = LED_BUILTIN;

#define WIFI_SSID "xxxxxxx"
#define WIFI_PASSWORD "zzzzzzzz"

WiFiServer server(23);

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Telnet test - Connecting to AP");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(200);
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("using telnet enter 1 to switch LED ON 0 for OFF");
  server.begin();
}

WiFiClient client;

void loop() {
  static int connected = 0;
  if (!connected) {  // when connected display message
    client = server.available();
    if (client) {
      if (client.connected()) {
        connected = 1;
        Serial.println("connected to telenet server");
      }
    }
  } else
    //  connected - if data available from telnet server read and switch LED
    if (client.available()) {
      char c = client.read();
      if (c == '1') {
        digitalWrite(ledPin, LOW);  // turn the LED on
        Serial.println("LED ON");
      } else if (c == '0') {
        digitalWrite(ledPin, HIGH);  // turn the LED on
        Serial.println("LED OFF");
      }
    }
}

serial monitor output

Telnet test - Connecting to AP.................................
WiFi connected.
IP address: 
192.168.1.250
using telnet enter 1 to switch LED ON 0 for OFF
connected to telenet server
LED OFF
LED OFF
LED ON
LED OFF
LED ON

teraterm acting as a telnet server
image

when connected
image