Skip to main content

Sending data from Arduino - part 2

Horacio de Oro
Author
Horacio de Oro
Available to help you find and implement the right solution for your business. Expert on Google Cloud and AWS Architecture, Security, Kubernetes, Django, Python, PostgreSql.

Schedule a free consultation at 👉 calendly.com 👈

Part 2: let’s learn about the performance of sending single data point per UDP packet.

Basically, we’ll have “data points”, or “measurements” to send from the Arduinos to the Raspberry Pi. The idea is to try some simple methods to send those measurements or data points.

Experiment with single data point per UDP packet
#

Method 1: send a string
#

Arduino pseudo code: create and UDP packet that contains a string: data and a timestamp.

timestamp = micros();

Udp.beginPacket(ip, 4545);
Udp.write(data);
Udp.write(",");
Udp.write(timestamp);
Udp.write("\n");
Udp.endPacket();

Method 2: write two unsigned ints
#

Arduino code: do 2 writes to create the UDP packet.

timestamp = micros();

Udp.beginPacket(ip, 4545);
Udp.write((byte *) &data, sizeof(unsigned int));
Udp.write((byte *) &timestamp, sizeof(unsigned int));
Udp.endPacket();

Method 3: write the whole data point
#

Arduino code: store data in a struct, and write the whole struct in a single operation.

In this case, the struct is bigger than “Method 2”, since here I include 3 unsigned ints, so, I expect this method to be, maybe, slower than “Method 2”.

struct message {
    unsigned int msg_type;
    unsigned int value;
    unsigned int timestamp;
};

And to send the data point:

data_point.msg_type = 0;
data_point.value = counter;
data_point.timestamp = micros();

Udp.beginPacket(ip, 4545);
Udp.write((byte *) &data_point, sizeof(message) * len);
Udp.endPacket();

Results
#

MethodPackets per second
Method 1: send a string509
Method 2: write two unsigned ints640
Method 3: write the whole data point726

Clearly “Method 3” is the best regarding performance.

Source code
#

Real code used to run these tests can be found at udp.cpp.