pubsubclient icon indicating copy to clipboard operation
pubsubclient copied to clipboard

Efficient way of publishing IP?

Open consolacion opened this issue 8 years ago • 3 comments

Nick, Thanks for the great library I was wondering if there is a more efficient way (than I curently use) to publish the local ip address Currently I get my local ip address with Ethernet.localIP() which is an array. I then add the 0-3 elements from the array to a string variable "MyIp" MyIp=String (Ethernet.localIP()[0]); MyIp=MyIp+"."; MyIp=MyIp+String (Ethernet.localIP()[1]); etcetera etcetera I then publish that as follows: mqttClient.publish("topic/ip",MyIp.c_str()); That works well, but was wondering if there is a more efficient method. Adding strings is memory hungry I was hoping ofcourse that: mqttClient.publish("topic/ip",Ethernet.localIP()); would work but it doesnt, just publishes gibberish.

consolacion avatar Feb 21 '17 14:02 consolacion

Hi @consolacion, this is how I publish my IP:

mqttClient.publish("topic/ip", WiFi.localIP().toString().c_str());

However, if [efficiency] is the main question, then I don't know whether this method is more efficient than yours or not.

I used the library ESP8266WiFi (https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi) in the example above.

marcelops avatar Mar 14 '17 19:03 marcelops

Marcel, thanks. I will check what takes more memory, but my example came from a UNO, so the ESP8266WiFi lib may not work there. Nevertheless good to know

consolacion avatar Mar 14 '17 20:03 consolacion

Works on a Wemos D1 R1, ie, ESP8288:

mqttClient.publish(out_topic_2, WiFi.localIP().toString().c_str(),
true);

gns5280 avatar Apr 18 '24 21:04 gns5280