Sending TCP or UDP packets by the push on a button

Hi there.

I am a real roockie on this but I have managed to implement arduino in a few simple projects so far by reading tutorials and examples but now I am really stuck.

I have spent the whole day trying to get some examples that I can start from but i do not understand where to begin.

My task is to try to send a udp or preferable a tcp packet from my arduino hardware when I push a triggerbutton. The goal is to Control the play function in a mediaplayer by sending a command. (tcp://192.168.0.15:9091/state=play)

I have an arduino uno
an ethernetshield (W5100processor)
and a button connected to pin nr 8 and + 5v I have also a 10k resistor between pin nr8 and ground.

I have understood that i shall include the Ethernet library and how to set the mac and ipadress for the arduino but then I am totally lost.

Is there anyone who can give me a hint on where to start reading or tell me if this is too complicated for me.

I would like it to be just as easy as:

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

tcp Write(tcp://192.168.0.15:9091/state=play);

}

But sadly I know it is`nt

did you try to adopt the WebClient example of Ethernet library?

Thank you for your answer :slight_smile:

No I tried the “UDPSendReceiveString” example but I didn´t succeed at all

If the “WebClient” is the right way to start from I will give it a try. Thank You!!!

Letterfive5:
No I tried the "UDPSendReceiveString" example but I didn´t succeed at all

what UDP server were you running on the target PC?
what happened? do you open the UDP port in the firewall ?

also try the ping example program

if you have problem run wireshark on the PC it will display the network traffic

What OS are you using on your PC? I have code for the Arduino and Linux that handles UDP. I’ve tested it on Ubuntu and Raspbian.

@horace and @SufferTim, server is some media server, not OP's implementation

Juraj:
@horace and @SufferTim, server is some media server, not OP's implementation

perhaps test with a simple UDP server firsy, e.g. in 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);
}

}

UDP port is 999 so change as required

Juraj:
@horace and @SufferTim, server is some media server, not OP's implementation

And which media server would that be? I communicate with mplayer via UDP.