-
Notifications
You must be signed in to change notification settings - Fork 30
/
advanced_consume.ino
121 lines (89 loc) · 4.77 KB
/
advanced_consume.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Client mqtt("broker.hivemq.com");
// The example will also work with a broker -- just replace the line above with:
// PicoMQTT::Server mqtt;
void setup() {
// Setup serial
Serial.begin(115200);
// Connect to WiFi
Serial.printf("Connecting to WiFi %s\n", WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) { delay(1000); }
Serial.println("WiFi connected.");
mqtt.subscribe("picomqtt/string_payload", [](const char * payload) {
// Use this type of handler to work with messages which should be strings. PicoMQTT will always add an extra
// zero terminator at the end of the payload, so it's safe to treat it as a string.
// Note however, that in the general case, payload can be binary (so it can contain any data, including zero
// bytes).
Serial.printf("Received message in topic 'picomqtt/string_payload': %s\n", payload);
});
mqtt.subscribe("picomqtt/binary_payload", [](const void * payload, size_t payload_size) {
// Here the payload is in a void * buffer and payload_size tells us its size.
Serial.printf("Received message in topic 'picomqtt/binary_payload', size: %u\n", payload_size);
});
mqtt.subscribe("picomqtt/string_payload/+/wildcard", [](const char * topic, const char * payload) {
// Use this type of callback to capture both the topic and the payload as string
// If the topic contains wildcards, their values can be extracted easily too
String wildcard_value = mqtt.get_topic_element(topic, 2);
Serial.printf("Received message in topic '%s' (wildcard = '%s'): %s\n", topic, wildcard_value.c_str(), payload);
});
mqtt.subscribe("picomqtt/binary_payload/+/wildcard", [](const char * topic, const void * payload, size_t payload_size) {
// Here the payload is in a void * buffer and payload_size tells us its size.
// If the topic contains wildcards, their values can be extracted easily too
String wildcard_value = mqtt.get_topic_element(topic, 2);
Serial.printf("Received message in topic '%s' (wildcard = '%s'), size: %u\n",
topic, wildcard_value.c_str(), payload_size);
});
mqtt.subscribe("picomqtt/big_message", [](const char * topic, PicoMQTT::IncomingPacket & packet) {
// This is the most advanced handler type. The packet parameter is a subclass of Stream and can be read from
// in small chunks using Stream's typical API. This is useful for handling messages too big to fit in RAM.
// Retrieve payload size
size_t payload_size = packet.get_remaining_size();
size_t digit_count = 0;
while (payload_size--) {
int val = packet.read();
if ((val >= '0') && (val <= '9')) {
// the byte is a digit!
++digit_count;
}
}
// Note that it's OK to not read the entire content of the message. Reading beyond the end of the packet
// is an error.
Serial.printf("Received message in topic '%s', it contained %u digits\n", topic, digit_count);
});
// Arduino Strings can be used instead of const char *. Note that this can be inefficient, especially with big
// payloads.
mqtt.subscribe("picomqtt/arduino_string/payload", [](const String & payload) {
Serial.printf("Received message in topic 'picomqtt/arduino_string/payload': %s\n", payload.c_str());
});
mqtt.subscribe("picomqtt/arduino_string/+/topic_and_payload", [](const String & topic, const String & payload) {
// If the topic contains wildcards, their values can be extracted easily too
String wildcard_value = mqtt.get_topic_element(topic, 2);
Serial.printf("Received message in topic '%s' (wildcard = '%s'): %s\n", topic.c_str(), wildcard_value.c_str(), payload.c_str());
});
// Different types of strings can be mixed too
mqtt.subscribe("picomqtt/arduino_string/mix/1", [](const String & topic, const char * payload) {
Serial.printf("Received message in topic '%s': %s\n", topic.c_str(), payload);
});
mqtt.subscribe("picomqtt/arduino_string/mix/2", [](const char * topic, const String & payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload.c_str());
});
// const and reference can be skipped too
mqtt.subscribe("picomqtt/arduino_string/mix/3", [](char * topic, String payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload.c_str());
});
mqtt.begin();
}
void loop() {
mqtt.loop();
}