Comments (7)
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.
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.
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.
Have just released v2.3 that introduces a publish(topic,payload,retained)
function.
from pubsubclient.
Ha ha! Cheers, I'll grab it shortly.
from pubsubclient.
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.
If you do stick with what you've got there, you'll want to use
strlen(myIP)
rather than50
- 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)
- Reconnects every 4 hours HOT 2
- Sometimes payload are mixed up with topic. HOT 1
- Callback is not called when i am pubishing audio data in bytes.
- Intermittent delay of minutes between ESP publish and receipt by mosquitto in a pi HOT 1
- Anomolous performing when using along with I2S
- Issue while setting keep alive zero. HOT 2
- Bug in PubSubClient::connected HOT 1
- Retain Flag not working HOT 1
- receiving arbitrarily large messages HOT 1
- Use hostname for the MQTT broker IP HOT 2
- Library when subscribing to third topic won't connect to ethernet
- Connecting and running smoothly on mobile hotspot while rc=-2 on fiber broadband wifi router
- Future of this module HOT 7
- Reconnect Problem. HOT 11
- Different error on Ubuntu arduino ide from windows arduino ide.
- #error "This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor." HOT 1
- Client.connected() does not return false if MQTT Broker Pc disconnects from Internet or local network. HOT 1
- Can't Connect to mqtt broker after including SPI.h
- When callback is registered to topic that contains underscore, the underscore is replaced with space HOT 1
- pubsub library not thread safe.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pubsubclient.