-1

I am trying to make an application to control DMX Channels. For this I have an ESP8266 which takes a String as an input like "2.255", where the first integer is the DMX Channel and the second integer the value.

For my PC I wrote a method that first builds the string and then sens it to the IP Address of the ESP8266 via a Socket.

for(DMXChannel c : list){
if(lastvalue.get(c.getChannelID() - 1) != c.getValue()){

     try {
            String msg = c.getChannelID() + "." + c.getValue();;
            DatagramSocket clientSocket = new DatagramSocket();

            InetAddress ipaddr = InetAddress.getByName(ip); //IP Address is "192.168.4.1"

            byte[] sendData = new byte[1024];
            sendData = msg.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipaddr, 8888);

            clientSocket.send(sendPacket);
            clientSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

     }
}

This works fine but I have a problem when I am trying to do a fade. The frequency of this piece of code seems to be too slow.

Does anyone have a "faster" solution?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
FreezedHD
  • 1
  • 1

1 Answers1

1

Here are some things you could do to speed up the code:

  1. Do not call getByName for an IP address. This involves DNS, but DNS is not needed for an IP address. Instead call getByAddress. If you do need to call getByName, call it only once and cache the answer.

  2. Do not open, close, reopen, etc. the socket each time. Just keep it open.

Robin Green
  • 32,079
  • 16
  • 104
  • 187