Reading data from specific IP and port of a PC

Hey guys,

Just started learning Arduino Network's side of things and have a question please.
I've been building a simpit around Orbiter 2024 flight simulator and would like to extract and send simulation data to Arduino via Ethernet to show it on various displays (such as 7segments, TFTs, etc.) and send commands back from Arduino to Orbiter as well (e.g. button presses, etc.)
Orbiter spits out all that data to 127.0.0.1:38888 address via Orb:Connect plugin (it's pretty much mini web server, you can access it using and browser on your local network by going to e.g. 192.168.x.xx:38888).

My setup is as follows: Arduino Mega with W5100 shield -> ethernet switch -> Win10 PC. I've run several ethernet sketches to verify it works fine & dandy.

Now I'm curious, can I make Arduino to connect to that specific address (e.g. 192.168.x.xx:38888) and take all that necessary data to display it and send back commands as well?

Appreciate any help.
TIA.

Kind regards,
Phil.

Check out the Webclient sketch under Examples, Ethernet. Then it's a matter of parsing the data.

1 Like

Short answer is yes. If you need further help start with the pinned post re 'How to get the most from the forum'

1 Like

you could run a TCP or UDP server on the PC and a client on the Mega connects to the server to transfer data
what programming language are you using on the PC? e.g. C#, Java, Python, etc

1 Like

I thought about an UDP server as well.
I watched the following video, it is pretty much almost what I need but in reverse - Arduino is sending data and a PC is processing it in Python:

In his python script (can been seen at around 52:00 mark) he is specified that particular IP address and port of his Arduino to receive data from pressure/temp sensor:

address = ('10.1.15.243', 5000)

Is there a similar function for Arduno?

Regarding programming languages, I used to program in C/C++, but now I've started learning Python as well.

here is a UDP chat program for a Mega with a ethernet shield

// UDP chat program using Arduino Ethernet Shield

#include <SPI.h>  
#include <Ethernet.h>
#include <EthernetUdp.h>  
#define UDP_TX_PACKET_MAX_SIZE 100 //increase UDP size

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  // MAC address update as required
IPAddress localIP(192, 168, 1, 176);                // local static localIP address
IPAddress remoteIP(192, 168, 1, 68);                // local static localIP address
unsigned int port = 999;                            // port to receive/transmit

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet

EthernetUDP Udp;  // An EthernetUDP instance to let us send and receive packets over UDP

void setup() {
  Serial.begin(115200);
  Serial.println("\n\nUDP chat program");
  // select static or dynamic IP initialization
  //Ethernet.begin(mac, localIP);    // static IP initialize Ethernet shield
  if(Ethernet.begin(mac))            // dyname IP initialize Ethernet shield using DHCP
        Serial.println("Configured Ethernet using DHCP OK");
   else Serial.println("Failed to configure Ethernet using DHCP");

  Serial.print("My localIP address: ");    // print local localIP address:
  Serial.println(Ethernet.localIP());
  // Initializes the ethernet UDP library and network settings.
  if (Udp.begin(port)) Serial.println("UDP begin() OK");
  else Serial.println("UDP begin() failed!");
  Serial.print("UDP_TX_PACKET_MAX_SIZE ");
  Serial.println(UDP_TX_PACKET_MAX_SIZE);
}

void loop() {
  // if datagram received read it
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.print(packetSize);         // datagram packet size
    Serial.print(" From ");
    remoteIP = Udp.remoteIP();  // from localIP
    Serial.print(remoteIP);
    Serial.print(", port ");            // from port
    Serial.println(Udp.remotePort());
    memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
    // read the packet into packetBufffer and print contents
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.print("Contents: ");
    Serial.println(packetBuffer);
  }
  if (Serial.available()) {   // if text entered read it and send it
    char text[100] = { 0 };
    Serial.readBytesUntil('\n', text, 100);
    Serial.print("Transmitting datagram ");
    Serial.println(text);
    Udp.beginPacket(remoteIP, port);   // send to remote localIP and port 999
    Udp.write(text);
    Udp.endPacket();
  }
  delay(10);
}

java programs for PC in file UDPchat.java

// UDPchat.java - simple peer-to-peer chat program using UDP
//           - given remote IP address can send strings using UDP datagrams 
//           - will also wait for datagrams and display contents
// remote IP and port are specified via command line - default IP is 127.0.0.1 (i.e. localhost) and port is 1000  
//
// e.g. to send datagrams to IP 146.227.59.130 port 1006
// java chat 146.227.59.130 1006

import java.io.*;
import java.util.*;
import java.net.*;

public class UDPchat extends Thread
{
   private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   int port=999;//10001;// 10000;                             // port to send/receive datagrams on
   String remoteIPaddress= "192.168.1.177";//"169.254.144.20";//("192.168.1.8");//127.0.0.1");      // IP to send datagrams

   // constructor, parameter is command line parameters
   public UDPchat(String args[]) throws Exception
    {
    // get remote IP address and port from command line parameters
    if (args.length > 0)    remoteIPaddress =  (args[0]);           // get IPaddress
    if (args.length > 1)    port = Integer.parseInt(args[1]);        // get port number
    System.out.println("chat program: IP address " + InetAddress.getLocalHost().toString() + " port " + port );
    
    start();        // start thread to receive and display datagrams

    // loop waiting for keyboard input, send datagram to remote IP                
    while(true)
      try
        {
        String s = in.readLine();                       // read a String
        System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + s);
        byte[] data = s.getBytes();                                     // convert to byte array
        DatagramSocket theSocket = new DatagramSocket();                // create datagram socket and the datagram
        DatagramPacket   theOutput = new DatagramPacket(data, s.length(), InetAddress.getByName(remoteIPaddress), port);
        theSocket.send(theOutput);                                      // and send the datagram
       }
      catch (Exception e) {System.out.println("Eroor sending datagram " + e);}
    }

   // thread run method, receives datagram and display contents as a string
   public void run()                
        {
          try
              {
              // open DatagramSocket to receive 
              DatagramSocket ds = new DatagramSocket(port);
              // loop forever reading datagrams from the DatagramSocket
              while (true)
                 {
                 byte[] buffer = new byte[65507];                       // array to put datagrams in
                 DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // DatagramPacket to hold the datagram
                 ds.receive(dp);                                     // wait for next datagram
                 String s = new String(dp.getData(),0,dp.getLength());        // get contenets as a String
                 System.out.println("UDP datagram length " + s.length()+ "  from IP " + dp.getAddress() + " received: " + s );
s = "Acknowledge\n";
     //            byte[] data = s.getBytes();                                     // convert to byte array
     //   DatagramSocket theSocket = new DatagramSocket();                // create datagram socket and the datagram
     //   DatagramPacket   theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(remoteIPaddress), port);
    //    theSocket.send(theOutput);                                      // and send the datagram

              }
              }
          catch (SocketException se) {System.err.println("chat error " + se); }
          catch (IOException se) {System.err.println("chat error " + se);}
          System.exit(1);                                                       // exit on error
        }


public static void main(String args[]) throws Exception
{
   UDPchat c=new UDPchat(args);
}

}

test run serial output from Mega

UDP chat program
Configured Ethernet using DHCP OK
My localIP address: 192.168.1.177
UDP begin() OK
UDP_TX_PACKET_MAX_SIZE 100
Received packet of size 5 From 192.168.1.65, port 53704
Contents: hello
Transmitting datagram hello from mega

Received packet of size 21 From 192.168.1.65, port 51635
Contents: hello from java on PC
Transmitting datagram test 2 from mega

Received packet of size 14 From 192.168.1.65, port 53386
Contents: test 2 from PC

output on PC

F:\Ardunio\Networking\Ethernet\UDP\UDP_chat>java UDPchat
chat program: IP address BB-DELL2/172.26.208.1 port 999
hello
Sending to 192.168.1.177 socket 999 data: hello
UDP datagram length 16  from IP /192.168.1.177 received: hello from mega
hello from java on PC
Sending to 192.168.1.177 socket 999 data: hello from java on PC
UDP datagram length 17  from IP /192.168.1.177 received: test 2 from mega
test 2 from PC
Sending to 192.168.1.177 socket 999 data: test 2 from PC

I think I have a Python UDPchat program some where - will try to find it

1 Like

wow, thank you horace, much appreciated.
I'll give it a try tonight. :+1:

BTW, java is even better, as that Orbiter i/o script is written in it as well

Once again, thank you for your code example.
I've tried to run that Java code and had a following error:

The method start() is undefined for the type UDPchat
And I have start() underlined with a red squiggly line

Arduino code ran just fine

can you copy the error output and post it (as text not a screen image)

I can compile and run the Java UDPchat.java program OK, e.g. open CMD console and

F:\Ardunio\Networking\Ethernet\UDP\UDP_chat>javac UDPchat.java

F:\Ardunio\Networking\Ethernet\UDP\UDP_chat>java UDPchat
chat program: IP address BB-DELL2/192.168.1.65 port 999
hello from pc
Sending to 192.168.1.177 socket 999 data: hello from pc
test2 from PC
Sending to 192.168.1.177 socket 999 data: test2 from PC
UDP datagram length 16  from IP /192.168.1.177 received: hello from mrga
UDP datagram length 16  from IP /192.168.1.177 received: test2 from mega

you probably don't have the javac compiler installed - see Oracle Java Downloads

still looking for Python UDP code

1 Like

It works like a charm via CMD! :slight_smile:
It didn't work using Eclipse
Thank you, horace :+1:

I avoid using Eclipse tend to use CMD javac or NetBeans
also worth looking is Dr Java - it allows compiling and running code with having to create a project
could be a firewall problem???

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.