Sending data throught UDP

Hello Guys,

I’m trying to send data (string of caracters exp : ‘Hello\n’ )from a processor to the Photon and then send it throught UDP to a server.

this is my code :


   while(Serial1.available()) {
    
    
  for(i=0;i<=30;i++)   
   
{     
    
   Buffer1=Serial1.read(); // Read whats in the Serial port and store to Buffer1                                                                                                  
    buffer[i] = Buffer1; // put the received character into buffer to send it to server
    
}  

    Udp.begin(48081);                                          
    Udp.sendPacket(buffer,30,remoteIP,port);
    Udp.stop();
    
    }

The code works and the data is sent but i can’t have my string just in one Packet : i have it cutted and sent in 2 packets
example: 1 st Packet : Hell
2 nd Packet : o

can any one please help me?

this a screenshot of the sent packets that i can read on Putty:

Thanks for answering :slight_smile:

while(Serial1.available()) {
  for(i=0;i&lt;=30;i++)   
{     
   Buffer1=Serial1.read(); // Read whats in the Serial port and store to Buffer1                                                                                                
    buffer[i] = Buffer1; // put the received character into buffer to send it to server
}

it looks like you are exiting this loop before your full message is parsed.

your particle device is fast!

you can try to speed up the baud rate of the serial communication and/or add a delay in the parsing loop:

while(Serial1.available()) {
  for(i=0;i&lt;=30;i++)   
{     
  Buffer1=Serial1.read(); // Read whats in the Serial port and store to Buffer1                                                                                                
  buffer[i] = Buffer1; // put the received character into buffer to send it to server
  delay(50); // to test waiting for all of the characters to arrive
}

you probably want an end of message marker (line feed?) to know when to exit that loop.

Hello @BulldogLowell,

Thanks for your answer. In fact i tryed to add a delay in that loop but it doesn’t work…and to exit the loop i changed the code :


   while(Serial1.available()) {

i=0; 
k=1;
    
while (buffer[k] != '\n')   
{  
    
    
   Buffer1=Serial1.read(); // Read whats in the Serial port and store to Buffer1                     
                                                                       
    buffer[i] = Buffer1; // put the received character into buffer to send it to server
    k=i;
    i=i+1;
}  

    Udp.begin(48081);                                          
    Udp.sendPacket(buffer,30,remoteIP,port);
    Udp.stop();
    
    }

After this change the photon goes each time to the Crush-mode (blinks red ) and i can’t receive anything on the server.

@ScruffR

Not exactly what I meant, but you should try to do it in a non-blocking fashion like this:

  if (Serial1.available()) // any time something appears in Serial1 handle it
  {
    char newChar = Serial1.read(); //
    if (newChar != '\n') . // look for the end of line char
    {
      buffer[idx] = newChar;
      idx++;
    }
    else  // you are done reading...
    {
      buffer[idx] = '\0';
      gotMessage = true;
      idx = 0;
    }
  }
  
  if(gotMessage)
  {
    sendMyUdpMessage(buffer);
    gotMessage = false;
  }

with globals defined as:

bool gotMessage = false;
int idx = 0;
char buffer[32] = "";

The same result , it compiles but i can’t see anything on the server. it seems like some problem with the Baud rate or anything else… :sleepy: :sob:

are you sure you are sending end of line character from Serial?

you could also try a terminating character like a ‘;’

char newChar = Serial1.read(); //
    if (newChar != ';') . // look for the end of line char
    {

Hello @BulldogLowell , thanks a lot for your answer. i’m sure i’m sending this : Hello\n…
i really don’t know why the data is coming always false …

The problem with your initial code was that you were only checking if at least one byte was available but then immediately wanted to read 30 bytes.
That’s the reason why you got all these extra characters (0xFF) since Serial1.read() returns (int)-1 when there is no more data to be read.

To debug your current code add some local logging via USB serial and see what you get at what point in your code.

2 Likes

Hello @ScruffR, @BulldogLowell and @peekay123,

Thanks for your answers, i really didn’t understand what you meant… should i do the availability test on each coming caracter ?

I also didn’t understand what you meant here…

I spent hours trying to understand this Local logging but in vain… is it kind of a data-recording ?

When I said local logging I meant either use something like

void loop() {
  ...
  Serial.printlnf("Checkpoint %d: data %s", cpCount, someString);
  ...
}

or use the logging feature

SerialLogHandler myLog;

void loop() {
  ...
  Log.info("Checkpoint %d: data %s", cpCount, someString);
  ...
}

Both will send your data via USB to any serial terminal program on your computer to see what the sender does - not only what the receiver sees.

1 Like