0

I am in search for some examples as to how can one use the AsyncUDPMessage in an Application for sending out UDP packets to a host with specific IP address and Port number from an ESP-WROOM-32 (Olimex ESP32-PoE) module?

The Class for AsyncUDPMessage in arduino-esp32 library inherits from Print.

I want to send sensor measurements to InfluxDB in the Line Protocol (InfluxDB specific).

Example

 imu,location=front,nodeid=node1 status=0.00,liac_x=-1.18,liac_y=0.02,liac_z=9.63,eul_x=0.00,eul_y=-6.81,eul_z=0.25 1538507409930

The above mentioned example is stored in the form of a String and the methods to send information via UDP viz. send() sendTo() all mention usage of AsyncUDPMessage as the first parameter.

goal

Send the above measurement to the particular Device's IP and port

udp.sendTo(Message, IPAddress(192,168,3,11), 8089);

I am currently using broadcastTo(const char * data, port) menthod to send information the InfluxDB instance.

Code

void loop(void) {
 DateTime now_time = rtc.now();
 // create a measurement
 influxMeasurement row("imu");
 row.addTag("location", "front");
 row.addTag("nodeid", "node1");
// get information from BNO055
 imu::Vector<3> li_ac = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
 imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
// Serial.print(li_ac.x());
// Serial.print(li_ac.y());
 row.addField("status", 0);
 row.addField("liac_x", li_ac.x());
 row.addField("liac_y", li_ac.y());
 row.addField("liac_z", li_ac.z());
 row.addField("eul_x", euler.x());
 row.addField("eul_y", euler.y());
 row.addField("eul_z", euler.z());
 // add timestamp
 row.addTimeStamp_ms(String(now_time.unixtime()) + String(millis()%1000));
 // print the Line Protocol output
 Serial.println(row.postString());
 // UDP Sending 
 char buf[2048]; // make buffer for `broadcastTo()`
 row.postString().toCharArray(buf, sizeof(buf)); // string to char array
 // Serial.println(buf);
 udp.broadcastTo(buf ,8089);
 
 delay(100);
}

method

I create a Line protocol String, make a buffer, convert the string to char [] and send it out.

Question

  1. how do I use AsyncUDPMessage so I can pass the string to it directly?

  2. I am not sure but will char buf[2048] within a loop is a good idea as every time a buffer allocation might make the ESP32 run out of memory? And if AsyncUDPMessage can avoid this concern?

asked Oct 2, 2018 at 17:19
3
  • 1
    String has .c_str() Commented Oct 2, 2018 at 17:23
  • the library has AsyncUDPClient example Commented Oct 2, 2018 at 17:25
  • Yes but there is not mention of AsyncUDPMessage. there is only AsyncUDPPacket Commented Oct 2, 2018 at 17:26

1 Answer 1

1

Use udp.print(row.postString().c_str())

AsyncUDPMessage in AsyncUDP library is only a buffer. It is used only in AsyncUDP send function:

size_t AsyncUDPPacket::send(AsyncUDPMessage &message)
{
 return write(message.data(), message.length());
}
answered Oct 2, 2018 at 17:43
3
  • So just connect once and then print the information will send the udp to the destination porT? Commented Oct 2, 2018 at 18:01
  • udp doesn't connect. only the library takes the server and port parameters in a connect like function. the function can only resolve hostname to IP. but the implementation is hidden in not open source espressif SDK Commented Oct 2, 2018 at 18:25
  • if you don't need nonblocking UDP sending, then you don't need the AsyncUDP library Commented Oct 2, 2018 at 18:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.