Skip to content

Commit c3aa680

Browse files
committed
update
1 parent d42be48 commit c3aa680

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This library is an alternative to the [pubsubclient](https://github.com/knollear
1212

1313
*Or even better use the newly available Library Manager in the Arduino IDE.*
1414

15-
## Examples
15+
## Compatibility
1616

1717
The following examples show how you can use the library with various Arduino compatible hardware:
1818

@@ -30,6 +30,63 @@ Other shields and boards should work if they also provide a [Client](https://www
3030

3131
- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections.
3232

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+
3390
## API
3491
3592
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

Comments
 (0)