I need to use a specific library that issues error messages in the form of String texts. The library uses the Serial.println()
function to send the messages to the Arduino IDE's serial monitor.
The problem is that I have the Serial lines from the Arduino linked to another device, therefore I can't use the Serial Monitor to see the messages. I have a method in the Main Arduino sketch that sends any String text msg generated to another device using Udp commands (wireless). This device is connected to a serial monitor unit.
My question is: How can I invoke the Udp method inside the main sketch of the Arduino program from within the aforementioned library? This would help me to convert the Serial.print()
text messages from the library directly into Udp messages to be able to visualize them.
The Udp method I'm using is declared as void UdpSend(String msg)
in the Arduino sketch. I'd like to use something similar inside the library to wrap the text msg and send it over to the method on the main program.
Any example or link to get this information will help.
EDIT1:
@Majenko,
- On the target library, added:
void libUdpSend(const char *libMsg);
On the sketch, I added this method (Udp comm handler):
void libUdpSend(const char *libMsg) { Udp.begin(localPort); Udp.beginPacket(Udp.remoteIP(), localPort); Udp.write(libMsg); Udp.endPacket(); delay(40); }
Added this line on the library (right after the declaration on 1.) to test through the remote serial interface:
libUdpSend("String wrapper has been implemented SUCCESSFULLY!");
When trying to compile linking the library, the following exception occurrs:
error: expected constructor, destructor, or type conversion before '(' token
libUdpSend("String wrapper has been implemented SUCCESSFULLY!");
I then switched to String as this is the type I want to wrap, Same steps:
- On the target library, added:
void libUdpSend(String *libMsg);
On the sketch:
void libUdpSend(String *libMsg) { Udp.begin(localPort); Udp.beginPacket(Udp.remoteIP(), localPort); Udp.write(libMsg.c_str()); Udp.endPacket(); delay(40); }
Now the error message says:
request for member 'c_str' in 'libMsg', which is of pointer type 'String*' (maybe you meant to use '->' ?)
I am not an expert on pointers so I'd need some help on how to assign the pointer as argument on the UdpWrite
function using the '->' command proposed by the compliler?
Let me know your thoughts on this.