Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 4254c71

Browse files
authored
Support binary data (#104)
* Initial branch commit for supporting binary payloads * Adds wrappers for transmitting binary data
1 parent 6441784 commit 4254c71

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

examples/Esp32-lwmqtt/esp32-mqtt.h

+8
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,18 @@ void publishTelemetry(String data) {
8989
mqtt->publishTelemetry(data);
9090
}
9191

92+
void publishTelemetry(const char* data, int length) {
93+
mqtt->publishTelemetry(data, length);
94+
}
95+
9296
void publishTelemetry(String subfolder, String data) {
9397
mqtt->publishTelemetry(subfolder, data);
9498
}
9599

100+
void publishTelemetry(String subfolder, const char* data, int length) {
101+
mqtt->publishTelemetry(subfolder, data, length);
102+
}
103+
96104
void connect() {
97105
connectWifi();
98106
mqtt->mqttConnect();

examples/Esp8266-lwmqtt/esp8266_mqtt.h

+8
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,18 @@ void publishTelemetry(String data) {
116116
mqtt->publishTelemetry(data);
117117
}
118118

119+
void publishTelemetry(const char* data, int length) {
120+
mqtt->publishTelemetry(data, length);
121+
}
122+
119123
void publishTelemetry(String subfolder, String data) {
120124
mqtt->publishTelemetry(subfolder, data);
121125
}
122126

127+
void publishTelemetry(String subfolder, const char* data, int length) {
128+
mqtt->publishTelemetry(subfolder, data, length);
129+
}
130+
123131
void connect() {
124132
mqtt->mqttConnect();
125133
}

examples/MKR1000-lwmqtt/mkr1000-mqtt.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,27 @@ void connectWifi() {
8181
///////////////////////////////
8282
// Orchestrates various methods from preceeding code.
8383
///////////////////////////////
84-
void connect() {
85-
connectWifi();
86-
mqtt->mqttConnect();
87-
}
88-
8984
void publishTelemetry(String data) {
9085
mqtt->publishTelemetry(data);
9186
}
9287

88+
void publishTelemetry(const char* data, int length) {
89+
mqtt->publishTelemetry(data, length);
90+
}
91+
9392
void publishTelemetry(String subfolder, String data) {
9493
mqtt->publishTelemetry(subfolder, data);
9594
}
9695

96+
void publishTelemetry(String subfolder, const char* data, int length) {
97+
mqtt->publishTelemetry(subfolder, data, length);
98+
}
99+
100+
void connect() {
101+
connectWifi();
102+
mqtt->mqttConnect();
103+
}
104+
97105
void setupCloudIoT() {
98106
device = new CloudIoTCoreDevice(
99107
project_id, location, registry_id, device_id,

examples/universal-lwmqtt/universal-mqtt.h

+8
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,18 @@ void publishTelemetry(String data) {
301301
mqtt->publishTelemetry(data);
302302
}
303303

304+
void publishTelemetry(const char* data, int length) {
305+
mqtt->publishTelemetry(data, length);
306+
}
307+
304308
void publishTelemetry(String subfolder, String data) {
305309
mqtt->publishTelemetry(subfolder, data);
306310
}
307311

312+
void publishTelemetry(String subfolder, const char* data, int length) {
313+
mqtt->publishTelemetry(subfolder, data, length);
314+
}
315+
308316
void connect() {
309317
mqtt->mqttConnect();
310318
}

src/CloudIoTCoreMqtt.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "CloudIoTCoreMqtt.h"
1616

1717
// Forward global callback declarations
18-
String getJwt();
18+
String getJwt();
1919
void messageReceived(String &topic, String &payload);
2020

2121

@@ -42,15 +42,27 @@ void CloudIoTCoreMqtt::publishTelemetry(String data) {
4242
this->mqttClient->publish(device->getEventsTopic(), data);
4343
}
4444

45+
void CloudIoTCoreMqtt::publishTelemetry(const char* data, int length) {
46+
this->mqttClient->publish(device->getEventsTopic().c_str(), data, length);
47+
}
48+
4549
void CloudIoTCoreMqtt::publishTelemetry(String subtopic, String data) {
4650
this->mqttClient->publish(device->getEventsTopic() + subtopic, data);
4751
}
4852

53+
void CloudIoTCoreMqtt::publishTelemetry(String subtopic, const char* data, int length) {
54+
this->mqttClient->publish(String(device->getEventsTopic() + subtopic).c_str(), data, length);
55+
}
56+
4957
// Helper that just sends default sensor
5058
void CloudIoTCoreMqtt::publishState(String data) {
5159
this->mqttClient->publish(device->getStateTopic(), data);
5260
}
5361

62+
void CloudIoTCoreMqtt::publishState(const char* data, int length) {
63+
this->mqttClient->publish(device->getStateTopic().c_str(), data, length);
64+
}
65+
5466
void CloudIoTCoreMqtt::onConnect() {
5567
if (logConnect) {
5668
publishState("connected");
@@ -170,7 +182,7 @@ void CloudIoTCoreMqtt::mqttConnect() {
170182
}
171183

172184
// Set QoS to 1 (ack) for configuration messages
173-
this->mqttClient->subscribe(device->getConfigTopic(), 1);
185+
this->mqttClient->subscribe(device->getConfigTopic(), 1);
174186
// QoS 0 (no ack) for commands
175187
this->mqttClient->subscribe(device->getCommandsTopic(), 0);
176188

src/CloudIoTCoreMqtt.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ class CloudIoTCoreMqtt {
3939

4040
void startMQTT();
4141
void publishTelemetry(String data);
42+
void publishTelemetry(const char* data, int length);
4243
void publishTelemetry(String subtopic, String data);
44+
void publishTelemetry(String subtopic, const char* data, int length);
4345
void publishState(String data);
46+
void publishState(const char* data, int length);
4447
void onConnect();
4548
void setLogConnect(boolean enabled);
4649
void logError();

0 commit comments

Comments
 (0)