pubsubclient
pubsubclient copied to clipboard
Efficient way of publishing IP?
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.
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.
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
Works on a Wemos D1 R1, ie, ESP8288:
mqttClient.publish(out_topic_2, WiFi.localIP().toString().c_str(),
true);