UDP is not working on Argon


#1

i’m trying to send UDP packet through Argon. but for some reason there are not packets received. However, I tried the same code on Photon hardware and the code working fine.

if you can help me with that. below code for your reference.

UDP Udp;
char myIpAddress[24];

uint8_t server[] = { 192, 168, 23, 12}; // ip address of my phone

IPAddress IPfromBytes( server );
unsigned int localPort = 33334;

int led1 = D7; 
int counter=0;
uint32_t timer;

void setup() {
publish("tripStatus") ]
 pinMode(led1, OUTPUT);
     
        Particle.variable("ipAddress", myIpAddress, STRING);
        IPAddress myIp = WiFi.localIP();
        sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
           // start the UDP
        Udp.begin(localPort);
}

void loop() {

digitalWrite(led1, HIGH);
 
  // We'll leave it on for 1 second...
  delay(1000);

  // Then we'll turn it off...
  digitalWrite(led1, LOW);
 
  // Wait 1 second...
  delay(1000);
  
  char  ReplyBuffer[] = "acknowledged"; 
        Udp.beginPacket(IPfromBytes, localPort);
        sprintf(ReplyBuffer, "%d", counter);
        Udp.write(ReplyBuffer);
    
        Udp.endPacket();

counter++;
}



#2

There is a bug in 0.8.0-rc.25 affecting IPv4 IPAddress. We have a fix ready to be released in 0.8.0-rc.26 (https://github.com/particle-iot/firmware/pull/1610/commits/110b4bf6ae5976a3edd312211baee92908a7f766). For now you may manually reverse the order of bytes:

UDP Udp;
char myIpAddress[24];

#if SYSTEM_VERSION == SYSTEM_VERSION_v080RC25
uint8_t server[] = { 12, 23, 168, 192 }; // ip address of my phone
#else
uint8_t server[] = { 192, 168, 23, 12 }; // ip address of my phone
#endif // SYSTEM_VERSION == SYSTEM_VERSION_v080RC25

IPAddress IPfromBytes( server );
unsigned int localPort = 33334;

int led1 = D7; 
int counter=0;
uint32_t timer;

void setup() {
publish("tripStatus") ]
 pinMode(led1, OUTPUT);
     
        Particle.variable("ipAddress", myIpAddress, STRING);
        IPAddress myIp = WiFi.localIP();
        sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
           // start the UDP
        Udp.begin(localPort);
}

void loop() {

digitalWrite(led1, HIGH);
 
  // We'll leave it on for 1 second...
  delay(1000);

  // Then we'll turn it off...
  digitalWrite(led1, LOW);
 
  // Wait 1 second...
  delay(1000);
  
  char  ReplyBuffer[] = "acknowledged"; 
        Udp.beginPacket(IPfromBytes, localPort);
        sprintf(ReplyBuffer, "%d", counter);
        Udp.write(ReplyBuffer);
    
        Udp.endPacket();

counter++;
}

#3

i really appreciate your help. now it’s working. I’m trying to do is to gather variables from Xenon(s) and send it to Argon and then get this values on UDP packet to the laptop. since the BLE central is not published yet. however, Xenon is stop sending the values after some time. this is the code i’m using

Xenon

int count=1;
char publishString[10];

void setup() {

}


void loop() {
sprintf(publishString, "%d ", count);
Mesh.publish("tripStatus", publishString);
delay (100);
}

Argon

UDP Udp;
char myIpAddress[24];

#if SYSTEM_VERSION == SYSTEM_VERSION_v080RC25
uint8_t server[] = { 12, 23, 168, 192 }; // ip address of my phone
#else
uint8_t server[] = { 192, 168, 23, 12 }; // ip address of my phone
#endif // SYSTEM_VERSION == SYSTEM_VERSION_v080RC25

IPAddress IPfromBytes( server );
unsigned int localPort = 33333;

int led1 = D7; 
int counter=0;

void setup() {
 Mesh.subscribe("tripStatus", myHandler); // the name of the publish() call in sensor node [ publish("tripStatus") ]
 
 pinMode(led1, OUTPUT);
        IPAddress myIp = WiFi.localIP();
        sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
     
        Particle.variable("ipAddress", myIpAddress, STRING);
           // start the UDP
        Udp.begin(localPort);
}

void loop() {

 digitalWrite(led1, HIGH);
 
  // We'll leave it on for 1 second...
  delay(100);

  // Then we'll turn it off...
  digitalWrite(led1, LOW);
 
  // Wait 1 second...
  delay(100);
  

}

void myHandler(const char *event, const char *data)
{


  char  ReplyBuffer[] = "acknowledged"; 
        Udp.beginPacket(IPfromBytes, localPort);
     
        Udp.write(data);
        Udp.endPacket();
}

#4

On the Xenon, you are publishing rather quick. Do you mean to publish every 100ms? In my testing, the Argon isn’t the most stable and the latency on the mesh network can easily exceed 100ms. So one packet won’t finish sending before you try to transmit the next. You also aren’t incrementing count within your code. Is that intentional?

On the Argon, you call Udp.begin() but are you actually listening for UDP packets? I ask because you never check if there is data available on Udp. You could just leave that begin call out. You also transmit via UDP within your subscribe handler. It is always recommended to just set a flag in the handler function and then process the UDP packet send in the loop. Again, you also delay only 100ms, not a full second as your comments describe… is that intended? Not an issue on the Argon, just doesn’t match the comments.


#5

I would like to thank you for your help. after i used the flag things start to be more stable. however i couldn’t decrease the delay less than 100ms.

  • I want to publish as fast as i can.

  • the counter variable i meant to stop the increment and as well the delay i started with 1 sec and i start to decrease the time.

  • whenever i comment out the udp.begin() the udp packet is not sent.

Is there a better/faster way to get the values from Xenon(s) through the Argon to the laptop since the Xenon BLE central is not published yet.


#6

@Areezo, the mesh itself communicates at 250Kbps so “as fast as I can” is most likely not doable on Mesh. If you need real-time streaming data, a Photon may be better. Or rethink how you collect and send data. Does it need to be streamed or can it be collected, treated (avg, etc) and then sent on?


#7

Thanks for the information. I was trying to gather the sensors data in realtime through the bluetooth using Xenon to the phone (iOS, android). but since the BLE is not publish yet i’m went this way just for testing.

So would it be possible to send data in realtime using Xenon on BLE when it’s published?


#8

What is “real time” for this sensor? How fast does the collection and publish need to happen? Does BLE even have the bandwidth you desire if it was available on the mesh devices right now?

Perhaps you want to describe what you are trying to do in more detail for better ideas.


#9

@ninjatill What I would like to do is each Xenon is connected to one ultrasonic sensor. each Xenon should calculate the distance and send the distance back to phone in about realtime. Basically it’s not a lot of data but the the transfer speed is more important to be represented on the phone. most of the processing will be done on phone. what I need is to send is like 3 values (node name, distance, …) between the Xenon and phone.