Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add int, long & float publication capability #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions PubSubClient/CHANGES.txt
@@ -1,3 +1,6 @@
1.9.1
* Add functions to publish int, float, long. Handles type conversion. Added for flexibility

1.9
* Do not split MQTT packets over multiple calls to _client->write()
* API change: All constructors now require an instance of Client
Expand Down
26 changes: 25 additions & 1 deletion PubSubClient/PubSubClient.cpp
Expand Up @@ -124,7 +124,7 @@ uint8_t PubSubClient::readByte() {
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
uint16_t len = 0;
buffer[len++] = readByte();
uint8_t multiplier = 1;
uint32_t multiplier = 1;
uint16_t length = 0;
uint8_t digit = 0;
do {
Expand Down Expand Up @@ -203,6 +203,25 @@ boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plengt
return publish(topic, payload, plength, false);
}

boolean PubSubClient::publish(char* topic, float payload, boolean retained, unsigned int minlength, unsigned int precision) {
if (minlength < 4 && payload < 0) minlength = 4; // 4 = sign + 1 integral digit + fractional point + 1 franctional digit
char mqtt_messagebuff[longNbDigits((long) payload) + 1 + precision + 1];

dtostrf(payload, minlength, precision, mqtt_messagebuff);
return publish(topic, (uint8_t*)mqtt_messagebuff, strlen(mqtt_messagebuff), retained);
}

boolean PubSubClient::publish(char* topic, int payload, boolean retained) {
return publish(topic, (long)payload, retained);
}

boolean PubSubClient::publish(char* topic, long payload, boolean retained) {
char mqtt_messagebuff[longNbDigits(payload) + 1]; //number of digits including sign + null terminaison

itoa(payload, mqtt_messagebuff, 10);
return publish(topic, (uint8_t*)mqtt_messagebuff, strlen(mqtt_messagebuff), retained);
}

boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength, boolean retained) {
if (connected()) {
// Leave room in the buffer for header and variable length field
Expand Down Expand Up @@ -357,3 +376,8 @@ boolean PubSubClient::connected() {
return rc;
}

unsigned int PubSubClient::longNbDigits (long number) {
if (number < 0 ) return 1 + longNbDigits (abs(number));
if (number < 10) return 1;
return 1 + longNbDigits (number / 10);
}
4 changes: 4 additions & 0 deletions PubSubClient/PubSubClient.h
Expand Up @@ -53,6 +53,7 @@ class PubSubClient {
uint8_t *ip;
char* domain;
uint16_t port;
unsigned int longNbDigits (long);
public:
PubSubClient();
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
Expand All @@ -65,6 +66,9 @@ class PubSubClient {
boolean publish(char *, char *);
boolean publish(char *, uint8_t *, unsigned int);
boolean publish(char *, uint8_t *, unsigned int, boolean);
boolean publish(char *, float, boolean, unsigned int, unsigned int);
boolean publish(char *, int, boolean);
boolean publish(char *, long, boolean);
boolean publish_P(char *, uint8_t PROGMEM *, unsigned int, boolean);
boolean subscribe(char *);
boolean unsubscribe(char *);
Expand Down