Coder Social home page Coder Social logo

Comments (7)

knolleary avatar knolleary commented on May 28, 2024 1

Hi,

this is what you want:

 client.publish(topic, (uint8_t*)msg, 50, true);

There are so many data types and ways the client gets used, providing every combination of every type and argument wouldn't be manageable. That said, it seems reasonable to want to have a publish(string,string,boolean) function that does what you want without having to worry about the length field - I'll add one next time I'm updating the client.

from pubsubclient.

TotallyInformation avatar TotallyInformation commented on May 28, 2024

Huzzah! Thanks Nick, told you my C was rubbish. I hate it with a vengence but at least it works when you can figure it out unlike NODEMCU/LUA which I was fighting every step of the way and gave up when I got an out of memory error on a simple program.

You've also answered my next question which was why my LWT message wasn't getting the retained flag - true not 1!

from pubsubclient.

TotallyInformation avatar TotallyInformation commented on May 28, 2024

Hmm, always too complex!

      IPAddress myAddr = WiFi.localIP();
      snprintf (myIP, 16, "%s.%s.%s.%s", myAddr[0],myAddr[1],myAddr[2],myAddr[3]);
      client.publish(topic, (uint8_t*)myIP, 50, true);

is what finally worked. It publishes the IP address when the device connects to the broker. The LWT changes the same topic to "Offline".

from pubsubclient.

knolleary avatar knolleary commented on May 28, 2024

Have just released v2.3 that introduces a publish(topic,payload,retained) function.

from pubsubclient.

TotallyInformation avatar TotallyInformation commented on May 28, 2024

Ha ha! Cheers, I'll grab it shortly.

from pubsubclient.

knolleary avatar knolleary commented on May 28, 2024

If you do stick with what you've got there, you'll want to use strlen(myIP) rather than 50 - as it won't be a 50 character string.

from pubsubclient.

Gendiaaa avatar Gendiaaa commented on May 28, 2024

If you do stick with what you've got there, you'll want to use strlen(myIP) rather than 50 - as it won't be a 50 character string.

hello Mr Knolleary

i was not able to publish to topic using a retain flag and Qos as this function shows
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, uint8_t qos, boolean retained);
i follow the same same and got this error
C:\Users\gendi\OneDrive\Documents\Arduino\DosingEsp32working in edit\DosingEsp32working in edit.ino:1415:70: error: no matching function for call to 'PubSubClient::publish(char [60], uint8_t*, unsigned int&, int, bool)'
client.publish(buf_topic,(uint8_t*) msg , arrSize, MQTTpubQos, true);
^
In file included from C:\Users\gendi\OneDrive\Documents\Arduino\DosingEsp32working in edit\DosingEsp32working in edit.ino:10:
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:151:12: note: candidate: 'boolean PubSubClient::publish(const char*, const char*)'
boolean publish(const char* topic, const char* payload);
^~~~~~~
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:151:12: note: candidate expects 2 arguments, 5 provided
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:152:12: note: candidate: 'boolean PubSubClient::publish(const char*, const char*, boolean)'
boolean publish(const char* topic, const char* payload, boolean retained);
^~~~~~~
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:152:12: note: candidate expects 3 arguments, 5 provided
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:153:12: note: candidate: 'boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)'
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^~~~~~~
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:153:12: note: candidate expects 3 arguments, 5 provided
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:154:12: note: candidate: 'boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)'
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^~~~~~~
c:\Users\gendi\OneDrive\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:154:12: note: candidate expects 4 arguments, 5 provided

exit status 1

Compilation error: no matching function for call to 'PubSubClient::publish(char [60], uint8_t*, unsigned int&, int, bool)'

any advice to how to publish Qos2 with retain flag set , i am using V2.8

from pubsubclient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.