Udp to arduino fifo

Hello Processing Community !!!

First, I’m sorry for my bad english-level but I come from France; if I do some mistake, please, excuse me !

I’m working on a projet : This is a Raspberry with a Processing Program wich can receive some UDP packet by internet and sending-back the information to the Arduino by USB.

At this time, the program can receive the UDP datagram very well and in live. The program can, also, send some information via USB to the arduino.

My problem is : I receive a lot of datagram per seconde by UDP and I need to send-back only one data per 100 milis. I don’t know the fonction which can take only the last datagram UDP before sending him back to the Arduino.

If somone can held me,

Thank a lot !!!

Here the program :

import hypermedia.net.*;
import apsync.*;
import processing.serial.*;

UDP udp;
AP_Sync streamer;

StringBuilder toArduino1 = new StringBuilder();
public int arduinoMotor1;


void setup() {
  udp = new UDP( this, 6000 );
  udp.listen( true );
 // streamer = new AP_Sync(this,"COM20", 9600);

}


void draw() {
}


void receive(byte [] message, String ip, int port) {

   String toArduino1 = new String( message );

  streamer.send(toArduino1.toString());
  println("B SEND : "+toArduino1 +"   GET : "+  arduinoMotor1);


  delay(100); /* <-- When I put the delay, the datagram go into a variable and folow the logic of the First In First Out */
  loop();
}
  • In your receive() method, remove the streamer.send() line. Instead, just store the values you received in a global variable:

      public String[] lastReceivedMessage;
    
      //...
    
       void receive(byte[] message, String ip, int port){
           lastReceivedMessage = message;
      }
    
  • Then, use a timing logic in draw() and only send a message every 100 ms. Easiest way is to use frameRate(10) in setup(). Then draw() gets executed every 100 ms automatically.

    void draw()
    {
       String toArduino1 = new String( message );
    
       streamer.send(toArduino1.toString());
       println("B SEND : “+toArduino1 +” GET : "+ arduinoMotor1);
    }
    

This will only send the last received message but discards the older messages. If that’s not what you want you’ll have to implement a FIFO buffer of your own. An ArrayList could potentially work here.

Also, this forum seriously lacks a code button!

Not adding much to the topic but just wanted to let you know that Discourse supports markdown so you can get syntax-highlighted code by wrapping code with ``` (without the slashes)

```java
<Code in here>
\\```

For example, your code would be:

void draw()
{
   String toArduino1 = new String( message );

   streamer.send(toArduino1.toString());
   println("B SEND : “+toArduino1 +” GET : "+ arduinoMotor1);
}

This is a nice markdown cheatsheet for your reference.

Hey @WakeMeAtThree, @colouredmirrorball, we have a “Site Feedback” category for discussions about this kind of thing.

1 Like

Thank a lot @colouredmirrorball for your answer. I tryed to do what you said but … it’s not working ! Look at the new program :

import hypermedia.net.*;
import apsync.*;
import processing.serial.*;

UDP udp;
AP_Sync streamer;

StringBuilder toArduino1 = new StringBuilder();
public int arduinoMotor1;
public String[] lastReceivedMessage;

void setup() {
  udp = new UDP( this, 6000 );
  udp.listen( true );
 // streamer = new AP_Sync(this,"COM20", 9600);

}


void draw() {
  String toArduino1 = new String( message );

   streamer.send(toArduino1.toString());
   println("B SEND : "+toArduino1 +" GET : "+ arduinoMotor1);
 delay(100);
}


void receive(byte [] message, String ip, int port) {
  
  lastReceivedMessage = message;

  println("B SEND : "+toArduino1 +"   GET : "+  arduinoMotor1);


  //delay(100); /* <-- When I put the delay, the datagram go into a variable and folow the logic of the First In First Out */
  loop();
}

The variable “message” doesn’t exist.
Processing say : "Type mismatch, “byte[]” does not match with “java.lang.String[]”…

What can I do ? Must I convert “byte[]” into a “String” ?

Sorry, I made a mistake. The line public String[] lastReceivedMessage; should of course be public byte[] lastReceivedMessage;. Also, I don’t think the loop(); line is necessary.

@colouredmirrorball no problem, I correst the program but it doesn’t want to work :frowning:
It is righten : NullPointerException on this line :

String toArduino1 = new String( message );

But, no problem, I found an other way tu run this program : I put a delay on the program who send the UDP !!! But really thank a lot for your help :slight_smile: