forked from Imroy/pubsubclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_subscriber.ino
68 lines (56 loc) · 1.42 KB
/
mqtt_subscriber.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
/*
MQTT subscriber example
- connects to an MQTT server
- subscribes to the topic "inTopic"
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char *ssid = "xxxxxxxx"; // cannot be longer than 32 characters!
const char *pass = "yyyyyyyy"; //
// Update these with values suitable for your network.
IPAddress server(172, 16, 0, 2);
#define BUFFER_SIZE 100
void callback(const MQTT::Publish& pub) {
Serial.print(pub.topic());
Serial.print(" => ");
if (pub.has_stream()) {
uint8_t buf[BUFFER_SIZE];
int read;
while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) {
Serial.write(buf, read);
}
pub.payload_stream()->stop();
Serial.println("");
} else
Serial.println(pub.payload_string());
}
WiFiClient wclient;
PubSubClient client(wclient, server);
void setup() {
// Setup console
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("arduinoClient")) {
client.set_callback(callback);
client.subscribe("inTopic");
}
}
if (client.connected())
client.loop();
}
}