Skip to main content
2 of 2
Commonmark migration

Using AysncUDPMessage for ESP32 to send string data

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?

Shan-Desai
  • 179
  • 3
  • 19