Skip to content

Commit d42be48

Browse files
committed
Merge pull request 256dpi#17 from 256dpi/shields
Ethernet and WiFi shield examples
2 parents 3fa36a1 + 67c9f25 commit d42be48

File tree

4 files changed

+209
-15
lines changed

4 files changed

+209
-15
lines changed

README.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,23 @@ 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-
## Caveats
16-
17-
- The maximum size for packets being published and received is set by default to 128 bytes. To change that value, you need to download the library manually and change the value in the following file: https://github.com/256dpi/arduino-mqtt/blob/master/src/MQTTClient.h#L5.
18-
19-
- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections.
20-
21-
## Compatibility
15+
## Examples
2216

23-
This library has been officially tested on the **Arduino Yùn**. Other boards and shields should work, but may need custom initialization of the Client class.
17+
The following examples show how you can use the library with various Arduino compatible hardware:
2418

25-
Here is a list of platforms that are supported:
19+
- [Arduino Yun (MQTTClient)](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_MQTTClient/ArduinoYun_MQTTClient.ino)
20+
- [Arduino Yun (YunMQTTClient)](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_YunMQTTClient/ArduinoYun_YunMQTTClient.ino)
21+
- [Arduino Ethernet Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino)
22+
- [Arduino WiFi Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino)
23+
- [Adafruit HUZZAH ESP8266](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino)
2624

27-
- [Arduino Yùn](https://www.arduino.cc/en/Main/ArduinoBoardYun)
28-
- [ESP8266](https://github.com/esp8266/Arduino)
25+
Other shields and boards should work if they also provide a [Client](https://www.arduino.cc/en/Reference/ClientConstructor) based network implementation.
2926

30-
## Examples
27+
## Caveats
3128

32-
The examples show how you can use the library with your hardware:
29+
- The maximum size for packets being published and received is set by default to 128 bytes. To change that value, you need to download the library manually and change the value in the following file: https://github.com/256dpi/arduino-mqtt/blob/master/src/MQTTClient.h#L5.
3330

34-
- [ArduinoYun_MQTTClient](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_MQTTClient/ArduinoYun_MQTTClient.ino)
35-
- [ArduinoYun_YunMQTTClient](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_YunMQTTClient/ArduinoYun_YunMQTTClient.ino)
31+
- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections.
3632

3733
## API
3834

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This example uses an Adafruit Huzzah ESP8266
2+
// to connect to shiftr.io.
3+
//
4+
// You can check on your device after a successful
5+
// connection here: https://shiftr.io/try.
6+
//
7+
// by Joël Gähwiler
8+
// https://github.com/256dpi/arduino-mqtt
9+
10+
#include <ESP8266WiFi.h>
11+
#include <MQTTClient.h>
12+
13+
const char *ssid = "ssid";
14+
const char *pass = "pass";
15+
16+
WiFiClient net;
17+
MQTTClient client;
18+
19+
unsigned long lastMillis = 0;
20+
21+
void connect(); // <- predefine connect() for setup()
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
WiFi.begin(ssid, pass);
26+
client.begin("broker.shiftr.io", net);
27+
28+
connect();
29+
}
30+
31+
void connect() {
32+
Serial.print("checking wifi...");
33+
while (WiFi.status() != WL_CONNECTED) {
34+
Serial.print(".");
35+
delay(500);
36+
}
37+
38+
Serial.print("\nconnecting...");
39+
while (!client.connect("arduino", "try", "try")) {
40+
Serial.print(".");
41+
}
42+
43+
Serial.println("\nconnected!");
44+
45+
client.subscribe("/example");
46+
// client.unsubscribe("/example");
47+
}
48+
49+
void loop() {
50+
client.loop();
51+
delay(10); // <- fixes some issues with WiFi stability
52+
53+
if(!client.connected()) {
54+
connect();
55+
}
56+
57+
// publish a message roughly every second.
58+
if(millis() - lastMillis > 1000) {
59+
lastMillis = millis();
60+
client.publish("/hello", "world");
61+
}
62+
}
63+
64+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
65+
Serial.print("incoming: ");
66+
Serial.print(topic);
67+
Serial.print(" - ");
68+
Serial.print(payload);
69+
Serial.println();
70+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This example uses an Arduino Uno together with
2+
// an Ethernet Shield to connect to shiftr.io.
3+
//
4+
// You can check on your device after a successful
5+
// connection here: https://shiftr.io/try.
6+
//
7+
// by Joël Gähwiler
8+
// https://github.com/256dpi/arduino-mqtt
9+
10+
#include <Ethernet.h>
11+
#include <MQTTClient.h>
12+
13+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
14+
byte ip[] = { 192, 168, 1, 177 };
15+
16+
EthernetClient net;
17+
MQTTClient client;
18+
19+
unsigned long lastMillis = 0;
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
Ethernet.begin(mac, ip);
24+
client.begin("broker.shiftr.io", net);
25+
26+
connect();
27+
}
28+
29+
void connect() {
30+
Serial.print("connecting...");
31+
while (!client.connect("arduino", "try", "try")) {
32+
Serial.print(".");
33+
}
34+
35+
Serial.println("\nconnected!");
36+
37+
client.subscribe("/example");
38+
// client.unsubscribe("/example");
39+
}
40+
41+
void loop() {
42+
client.loop();
43+
44+
if(!client.connected()) {
45+
connect();
46+
}
47+
48+
// publish a message roughly every second.
49+
if(millis() - lastMillis > 1000) {
50+
lastMillis = millis();
51+
client.publish("/hello", "world");
52+
}
53+
}
54+
55+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
56+
Serial.print("incoming: ");
57+
Serial.print(topic);
58+
Serial.print(" - ");
59+
Serial.print(payload);
60+
Serial.println();
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This example uses an Arduino Uno together with
2+
// a WiFi Shield to connect to shiftr.io.
3+
//
4+
// You can check on your device after a successful
5+
// connection here: https://shiftr.io/try.
6+
//
7+
// by Joël Gähwiler
8+
// https://github.com/256dpi/arduino-mqtt
9+
10+
#include <WiFi.h>
11+
#include <MQTTClient.h>
12+
13+
char *ssid = "ssid";
14+
char *pass = "pass";
15+
16+
WiFiClient net;
17+
MQTTClient client;
18+
19+
unsigned long lastMillis = 0;
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
WiFi.begin(ssid, pass);
24+
client.begin("broker.shiftr.io", net);
25+
26+
connect();
27+
}
28+
29+
void connect() {
30+
Serial.print("checking wifi...");
31+
while (WiFi.status() != WL_CONNECTED) {
32+
Serial.print(".");
33+
delay(500);
34+
}
35+
36+
Serial.print("\nconnecting...");
37+
while (!client.connect("arduino", "try", "try")) {
38+
Serial.print(".");
39+
}
40+
41+
Serial.println("\nconnected!");
42+
43+
client.subscribe("/example");
44+
// client.unsubscribe("/example");
45+
}
46+
47+
void loop() {
48+
client.loop();
49+
50+
if(!client.connected()) {
51+
connect();
52+
}
53+
54+
// publish a message roughly every second.
55+
if(millis() - lastMillis > 1000) {
56+
lastMillis = millis();
57+
client.publish("/hello", "world");
58+
}
59+
}
60+
61+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
62+
Serial.print("incoming: ");
63+
Serial.print(topic);
64+
Serial.print(" - ");
65+
Serial.print(payload);
66+
Serial.println();
67+
}

0 commit comments

Comments
 (0)