@@ -12,7 +12,7 @@ This library is an alternative to the [pubsubclient](https://github.com/knollear
12
12
13
13
* Or even better use the newly available Library Manager in the Arduino IDE.*
14
14
15
- ## Examples
15
+ ## Compatibility
16
16
17
17
The following examples show how you can use the library with various Arduino compatible hardware:
18
18
@@ -30,6 +30,63 @@ Other shields and boards should work if they also provide a [Client](https://www
30
30
31
31
- On the ESP8266 it has been reported that an additional ` delay(10); ` after ` client.loop(); ` fixes many stability issues with WiFi connections.
32
32
33
+ ## Example
34
+
35
+ The following example uses an Arduino Yun and the MQTTClient to connect to shiftr.io:
36
+
37
+ ``` c++
38
+ #include < Bridge.h>
39
+ #include < YunClient.h>
40
+ #include < MQTTClient.h>
41
+
42
+ YunClient net;
43
+ MQTTClient client;
44
+
45
+ unsigned long lastMillis = 0 ;
46
+
47
+ void setup () {
48
+ Bridge.begin();
49
+ Serial.begin(9600);
50
+ client.begin("broker.shiftr.io", net);
51
+
52
+ connect ();
53
+ }
54
+
55
+ void connect () {
56
+ Serial.print("connecting...");
57
+ while (!client.connect("arduino", "try", "try")) {
58
+ Serial.print(".");
59
+ }
60
+
61
+ Serial.println("\nconnected!");
62
+
63
+ client.subscribe("/example");
64
+ // client.unsubscribe("/example");
65
+ }
66
+
67
+ void loop () {
68
+ client.loop();
69
+
70
+ if(!client.connected()) {
71
+ connect();
72
+ }
73
+
74
+ // publish a message roughly every second.
75
+ if(millis() - lastMillis > 1000) {
76
+ lastMillis = millis();
77
+ client.publish("/hello", "world");
78
+ }
79
+ }
80
+
81
+ void messageReceived (String topic, String payload, char * bytes, unsigned int length) {
82
+ Serial.print("incoming: ");
83
+ Serial.print(topic);
84
+ Serial.print(" - ");
85
+ Serial.print(payload);
86
+ Serial.println();
87
+ }
88
+ ```
89
+
33
90
## API
34
91
35
92
Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport:
0 commit comments