Skip to content

Commit 72ef9bc

Browse files
committed
updated examples to not rely on arduinos automatic forward declaration generation
1 parent 8eaffb4 commit 72ef9bc

File tree

13 files changed

+217
-221
lines changed

13 files changed

+217
-221
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,6 @@ MQTTClient client;
4848

4949
unsigned long lastMillis = 0;
5050

51-
void setup() {
52-
Serial.begin(115200);
53-
WiFi.begin(ssid, pass);
54-
55-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
56-
// You need to set the IP address directly.
57-
client.begin("broker.shiftr.io", net);
58-
client.onMessage(messageReceived);
59-
60-
connect();
61-
}
62-
6351
void connect() {
6452
Serial.print("checking wifi...");
6553
while (WiFi.status() != WL_CONNECTED) {
@@ -79,6 +67,22 @@ void connect() {
7967
// client.unsubscribe("/hello");
8068
}
8169

70+
void messageReceived(String &topic, String &payload) {
71+
Serial.println("incoming: " + topic + " - " + payload);
72+
}
73+
74+
void setup() {
75+
Serial.begin(115200);
76+
WiFi.begin(ssid, pass);
77+
78+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
79+
// You need to set the IP address directly.
80+
client.begin("broker.shiftr.io", net);
81+
client.onMessage(messageReceived);
82+
83+
connect();
84+
}
85+
8286
void loop() {
8387
client.loop();
8488

@@ -92,10 +96,6 @@ void loop() {
9296
client.publish("/hello", "world");
9397
}
9498
}
95-
96-
void messageReceived(String &topic, String &payload) {
97-
Serial.println("incoming: " + topic + " - " + payload);
98-
}
9999
```
100100
101101
## API

examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ MQTTClient client;
1818

1919
unsigned long lastMillis = 0;
2020

21-
void connect(); // <- predefine connect() for setup()
22-
23-
void setup() {
24-
Serial.begin(115200);
25-
WiFi.begin(ssid, pass);
26-
27-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
28-
// You need to set the IP address directly.
29-
client.begin("broker.shiftr.io", net);
30-
client.onMessage(messageReceived);
31-
32-
connect();
33-
}
34-
3521
void connect() {
3622
Serial.print("checking wifi...");
3723
while (WiFi.status() != WL_CONNECTED) {
@@ -51,6 +37,22 @@ void connect() {
5137
// client.unsubscribe("/hello");
5238
}
5339

40+
void messageReceived(String &topic, String &payload) {
41+
Serial.println("incoming: " + topic + " - " + payload);
42+
}
43+
44+
void setup() {
45+
Serial.begin(115200);
46+
WiFi.begin(ssid, pass);
47+
48+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
49+
// You need to set the IP address directly.
50+
client.begin("broker.shiftr.io", net);
51+
client.onMessage(messageReceived);
52+
53+
connect();
54+
}
55+
5456
void loop() {
5557
client.loop();
5658
delay(10); // <- fixes some issues with WiFi stability
@@ -65,7 +67,3 @@ void loop() {
6567
client.publish("/hello", "world");
6668
}
6769
}
68-
69-
void messageReceived(String &topic, String &payload) {
70-
Serial.println("incoming: " + topic + " - " + payload);
71-
}

examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@ MQTTClient client;
1818

1919
unsigned long lastMillis = 0;
2020

21-
void connect(); // <- predefine connect() for setup()
22-
23-
void setup() {
24-
Serial.begin(115200);
25-
WiFi.begin(ssid, pass);
26-
27-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
28-
// You need to set the IP address directly.
29-
//
30-
// MQTT brokers usually use port 8883 for secure connections.
31-
client.begin("broker.shiftr.io", 8883, net);
32-
client.onMessage(messageReceived);
33-
34-
connect();
35-
}
36-
3721
void connect() {
3822
Serial.print("checking wifi...");
3923
while (WiFi.status() != WL_CONNECTED) {
@@ -53,6 +37,24 @@ void connect() {
5337
// client.unsubscribe("/hello");
5438
}
5539

40+
void messageReceived(String &topic, String &payload) {
41+
Serial.println("incoming: " + topic + " - " + payload);
42+
}
43+
44+
void setup() {
45+
Serial.begin(115200);
46+
WiFi.begin(ssid, pass);
47+
48+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
49+
// You need to set the IP address directly.
50+
//
51+
// MQTT brokers usually use port 8883 for secure connections.
52+
client.begin("broker.shiftr.io", 8883, net);
53+
client.onMessage(messageReceived);
54+
55+
connect();
56+
}
57+
5658
void loop() {
5759
client.loop();
5860
delay(10); // <- fixes some issues with WiFi stability
@@ -67,7 +69,3 @@ void loop() {
6769
client.publish("/hello", "world");
6870
}
6971
}
70-
71-
void messageReceived(String &topic, String &payload) {
72-
Serial.println("incoming: " + topic + " - " + payload);
73-
}

examples/ArduinoEthernetShield/ArduinoEthernetShield.ino

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ MQTTClient client;
1818

1919
unsigned long lastMillis = 0;
2020

21-
void setup() {
22-
Serial.begin(115200);
23-
Ethernet.begin(mac, ip);
24-
25-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
26-
// You need to set the IP address directly.
27-
client.begin("broker.shiftr.io", net);
28-
client.onMessage(messageReceived);
29-
30-
connect();
31-
}
32-
3321
void connect() {
3422
Serial.print("connecting...");
3523
while (!client.connect("arduino", "try", "try")) {
@@ -43,6 +31,22 @@ void connect() {
4331
// client.unsubscribe("/hello");
4432
}
4533

34+
void messageReceived(String &topic, String &payload) {
35+
Serial.println("incoming: " + topic + " - " + payload);
36+
}
37+
38+
void setup() {
39+
Serial.begin(115200);
40+
Ethernet.begin(mac, ip);
41+
42+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
43+
// You need to set the IP address directly.
44+
client.begin("broker.shiftr.io", net);
45+
client.onMessage(messageReceived);
46+
47+
connect();
48+
}
49+
4650
void loop() {
4751
client.loop();
4852

@@ -56,7 +60,3 @@ void loop() {
5660
client.publish("/hello", "world");
5761
}
5862
}
59-
60-
void messageReceived(String &topic, String &payload) {
61-
Serial.println("incoming: " + topic + " - " + payload);
62-
}

examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ MQTTClient client;
2424

2525
unsigned long lastMillis = 0;
2626

27-
void setup() {
28-
Serial.begin(115200);
29-
30-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
31-
// You need to set the IP address directly.
32-
client.begin("broker.shiftr.io", net);
33-
client.onMessage(messageReceived);
34-
35-
connect();
36-
}
37-
3827
void connect() {
3928
// connection state
4029
bool connected = false;
@@ -52,7 +41,7 @@ void connect() {
5241
delay(1000);
5342
}
5443
}
55-
44+
5645
Serial.print("\nconnecting...");
5746
while (!client.connect("arduino", "try", "try")) {
5847
Serial.print(".");
@@ -65,6 +54,21 @@ void connect() {
6554
// client.unsubscribe("/hello");
6655
}
6756

57+
void messageReceived(String &topic, String &payload) {
58+
Serial.println("incoming: " + topic + " - " + payload);
59+
}
60+
61+
void setup() {
62+
Serial.begin(115200);
63+
64+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
65+
// You need to set the IP address directly.
66+
client.begin("broker.shiftr.io", net);
67+
client.onMessage(messageReceived);
68+
69+
connect();
70+
}
71+
6872
void loop() {
6973
client.loop();
7074

@@ -78,7 +82,3 @@ void loop() {
7882
client.publish("/hello", "world");
7983
}
8084
}
81-
82-
void messageReceived(String &topic, String &payload) {
83-
Serial.println("incoming: " + topic + " - " + payload);
84-
}

examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@ MQTTClient client;
2424

2525
unsigned long lastMillis = 0;
2626

27-
void setup() {
28-
Serial.begin(115200);
29-
30-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
31-
// You need to set the IP address directly.
32-
//
33-
// MQTT brokers usually use port 8883 for secure connections.
34-
client.begin("broker.shiftr.io", 8883, net);
35-
client.onMessage(messageReceived);
36-
37-
connect();
38-
}
39-
4027
void connect() {
4128
// connection state
4229
bool connected = false;
@@ -54,7 +41,7 @@ void connect() {
5441
delay(1000);
5542
}
5643
}
57-
44+
5845
Serial.print("\nconnecting...");
5946
while (!client.connect("arduino", "try", "try")) {
6047
Serial.print(".");
@@ -67,6 +54,23 @@ void connect() {
6754
// client.unsubscribe("/hello");
6855
}
6956

57+
void messageReceived(String &topic, String &payload) {
58+
Serial.println("incoming: " + topic + " - " + payload);
59+
}
60+
61+
void setup() {
62+
Serial.begin(115200);
63+
64+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
65+
// You need to set the IP address directly.
66+
//
67+
// MQTT brokers usually use port 8883 for secure connections.
68+
client.begin("broker.shiftr.io", 8883, net);
69+
client.onMessage(messageReceived);
70+
71+
connect();
72+
}
73+
7074
void loop() {
7175
client.loop();
7276

@@ -80,7 +84,3 @@ void loop() {
8084
client.publish("/hello", "world");
8185
}
8286
}
83-
84-
void messageReceived(String &topic, String &payload) {
85-
Serial.println("incoming: " + topic + " - " + payload);
86-
}

examples/ArduinoWiFi101/ArduinoWiFi101.ino

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ MQTTClient client;
2020

2121
unsigned long lastMillis = 0;
2222

23-
void setup() {
24-
Serial.begin(115200);
25-
WiFi.begin(ssid, pass);
26-
27-
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
28-
// You need to set the IP address directly.
29-
client.begin("broker.shiftr.io", net);
30-
client.onMessage(messageReceived);
31-
32-
connect();
33-
}
34-
3523
void connect() {
3624
Serial.print("checking wifi...");
3725
while (WiFi.status() != WL_CONNECTED) {
@@ -51,6 +39,22 @@ void connect() {
5139
// client.unsubscribe("/hello");
5240
}
5341

42+
void messageReceived(String &topic, String &payload) {
43+
Serial.println("incoming: " + topic + " - " + payload);
44+
}
45+
46+
void setup() {
47+
Serial.begin(115200);
48+
WiFi.begin(ssid, pass);
49+
50+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
51+
// You need to set the IP address directly.
52+
client.begin("broker.shiftr.io", net);
53+
client.onMessage(messageReceived);
54+
55+
connect();
56+
}
57+
5458
void loop() {
5559
client.loop();
5660

@@ -64,7 +68,3 @@ void loop() {
6468
client.publish("/hello", "world");
6569
}
6670
}
67-
68-
void messageReceived(String &topic, String &payload) {
69-
Serial.println("incoming: " + topic + " - " + payload);
70-
}

0 commit comments

Comments
 (0)