Skip to content

Commit 7cad1a2

Browse files
authored
Auto detect Client interface to use on some boards
Only boards that have a default, intrinsic way of handling networking (like WiFi boards, such as MKR1000) are recognized. Also, only boards we could test or that have well-known implementation and good documentation are recognized.
1 parent e11690b commit 7cad1a2

File tree

10 files changed

+183
-120
lines changed

10 files changed

+183
-120
lines changed

.arduino-ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
platforms:
2+
3+
mkr1000:
4+
board: arduino:samd:mkr1000
5+
package: arduino:samd
6+
gcc:
7+
features:
8+
defines:
9+
- F_CPU=48000000L
10+
- __SAMD21G18A__
11+
- ARDUINO_ARCH_SAMD
12+
- ARDUINO_SAMD_MKR1000
13+
warnings:
14+
flags:
15+
16+
compile:
17+
libraries: ~
18+
platforms:
19+
- uno
20+
- due
21+
# - zero
22+
- leonardo
23+
# - m4
24+
- esp32
25+
# - esp8266
26+
- mega2560
27+
- mkr1000
28+
129
unittest:
230
platforms:
331
# - uno

PubNubDefs.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,42 @@
99

1010

1111
#if !defined(PubNub_BASE_CLIENT)
12+
#if defined(ARDUINO_ARCH_ESP8266)
13+
#include <ESP8266WiFi.h>
14+
#define PubNub_BASE_CLIENT WiFiClient
15+
#elif defined(ARDUINO_ARCH_ESP32)
16+
#include <WiFi.h>
17+
#define PubNub_BASE_CLIENT WiFiClient
18+
#elif defined(ARDUINO_SAMD_ZERO) || \
19+
defined(ARDUINO_SAMD_MKR1000) || \
20+
defined(ARDUINO_SAMD_MKRFox1200)
21+
#include <WiFi101.h>
22+
#define PubNub_BASE_CLIENT WiFiClient
23+
#elif defined(ARDUINO_STM32_FEATHER)
24+
#include <adafruit_feather.h>
25+
#define PubNub_BASE_CLIENT AdafruitTCP
26+
#elif defined(ARDUINO_METRO_M4_AIRLIFT_LITE) || \
27+
defined(ARDUINO_SAMD_MKRWIFI1010) || \
28+
defined(ARDUINO_AVR_UNO_WIFI_REV2)
29+
#include <WiFiNINA.h>
30+
#define PubNub_BASE_CLIENT WiFiSSLClient
31+
#elif defined(ARDUINO_SAMD_MKRGSM1400)
32+
#include <MKRGSM.h>
33+
#define PubNub_BASE_CLIENT GSMClient
34+
#elif defined(ARDUINO_SAMD_MKRWAN1300)
35+
#include <MKRWAN.h>
36+
#define PubNub_BASE_CLIENT LoRaModem
37+
#elif defined(ARDUINO_AVR_YUN)
38+
#include <Process.h>
39+
#define PubNub_BASE_CLIENT Process
40+
#elif defined(ARDUINO_SAMD_MKRNB1500)
41+
#include <MKRNB.h>
42+
#define PubNub_BASE_CLIENT NBModem
43+
#else
44+
#include <Ethernet.h>
1245
#define PubNub_BASE_CLIENT EthernetClient
1346
#endif
47+
#endif /* !defined(PubNub_BASE_CLIENT) */
1448

1549
#ifdef PUBNUB_DEBUG
1650
#define DBGprint(x...) Serial.print(x)

README.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -255,27 +255,33 @@ issue warnings like:
255255

256256
Invalid version found: x.y.z
257257

258-
Where `x.y.z` would be the version ID ofthe manually installed library.
259-
This is just a warning, the build and upload process is not impacted in
260-
any way by this.
258+
Where `x.y.z` would be the version ID of the manually installed
259+
library. This is just a warning, the build and upload process is not
260+
impacted by this.
261261

262262

263263
## Supported Hardware
264264

265265
In general, the most widely available Arduino boards and shields are
266266
supported and tested. Any Arduino board that has networking hardware
267-
that supports an `Client` compatible class should work. In most cases,
267+
that supports a `Client` compatible class should work. In most cases,
268268
they are actually derived from `Client`, but there are some subtle
269269
differences in the base `Client` as implemented in various libraries.
270270

271+
Since version 3.3, several boards are automatically detected and you
272+
don't need to do anything special to use PubNub library on them. For
273+
others, you'll have to `#define` the `Pubnub_BASE_CLIENT` to the class
274+
that you use for networking on your board/shield that has the `Client`
275+
compatible interface _before_ you `#include <PubNub.h>`.
276+
271277
The Arduino ecosystem features a multitude of platforms that
272278
have significant differences regarding their hardware capabilities.
273279
Keeping up with all of them is next to impossible.
274280

275281
If you find some Arduino board/shield that does provide an `Client`
276-
compatbile class and it doesn't work with Pubnub library, let us
277-
know. In general, this means that it is not _really_ compatible. Such
278-
was the case with ESP32 library.
282+
compatbile class and it doesn't work with Pubnub library, let us know
283+
and we'll make it work. In general, this means that it is not _really_
284+
compatible. Such was the case with ESP32 library.
279285

280286
Also, if you have some Arduino board/shield that doesn't provide an
281287
`Client` compatible class and you want to use Pubnub with it, please
@@ -311,25 +317,23 @@ So, for any WiFi101 compatible hardware, you would:
311317
#define PubNub_BASE_CLIENT WiFiClient
312318
#include <PubNub.h>
313319

314-
For hadware that doesn't use WiFi101 library, but provides a
315-
`WiFiClient` class, like ESP8266, you would:
316-
317-
#include <ESP8266WiFi.h>
318-
#define PubNub_BASE_CLIENT WiFiClient
319-
#include <PubNub.h>
320-
321320
Of course, please keep in mind that you need to initialize your WiFi
322321
hardware, connect to a WiFi network and possibly do some maintenance,
323322
which is hardware specific. But, Pubnub SDK has nothing to do with
324323
that, it expects a working network. We provide examples for some HW.
325324

326-
### ESP8266
325+
### ESP8266 and ESP32
326+
327+
ESP8266 and ESP32 are recognized since version 3.3 so can just:
328+
329+
#include <PubNub.h>
330+
331+
It will include `ESP8266WiFi.h` or `WiFi.h` (for ESP32) automatically.
327332

328-
In previous section we already showed how to use ESP8266, but in some
329-
(older) versions of ESP8266 support for Arduino, some of the
330-
(de-facto) standard library functions that we use are missing. To use
331-
our own implementation of them, `#define` a macro constant before you
332-
include `PubNub.h`, like this:
333+
In some (older) versions of ESP8266 support for Arduino, some of the
334+
(de-facto) standard library functions were missing. To use our own
335+
implementation of them, `#define` a macro constant before you include
336+
`PubNub.h`, like this:
333337

334338
#define PUBNUB_DEFINE_STRSPN_AND_STRNCASECMP
335339
#include <PubNub.h>

examples/AdafruitFeatherM0WINC1500/.arduino-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ compile:
99
# - esp32
1010
# - esp8266
1111
- mega2560
12+
- mkr1000
1213

1314
# Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager)
1415
libraries:

examples/AdafruitFeatherM0WINC1500/AdafruitFeatherM0WINC1500.ino

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <SPI.h>
66

77
#include <WiFi101.h>
8-
#define PubNub_BASE_CLIENT WiFiClient
98
#include <PubNub.h>
109

1110
static char ssid[] = "wifi_network_ssid"; // your network SSID (name)
@@ -18,63 +17,66 @@ const static char channel[] = "hello_world";
1817

1918
void setup()
2019
{
21-
/* This is the only line of code that is Feather M0 WINC1500
20+
// put your setup code here, to run once
21+
#if defined(ARDUINO_SAMD_ZERO)
22+
/* This is the only line of code that is Feather M0 WINC1500
2223
specific, the rest is the same as for the WiFi101 */
23-
WiFi.setPins(8, 7, 4, 2);
24+
WiFi.setPins(8, 7, 4, 2);
25+
#endif
2426

25-
// put your setup code here, to run once:
26-
Serial.begin(9600);
27-
Serial.println("Serial set up");
27+
Serial.begin(115200);
28+
Serial.println("Serial set up");
2829

29-
// attempt to connect using WPA2 encryption:
30-
Serial.println("Attempting to connect to WPA network...");
31-
status = WiFi.begin(ssid, pass);
30+
// attempt to connect using WPA2 encryption:
31+
Serial.println("Attempting to connect to WPA network...");
32+
status = WiFi.begin(ssid, pass);
3233

33-
// if you're not connected, stop here:
34-
if (status != WL_CONNECTED) {
35-
Serial.println("Couldn't get a wifi connection");
36-
while (true)
37-
;
38-
}
39-
else {
40-
Serial.print("WiFi connecting to SSID: ");
41-
Serial.println(ssid);
34+
// if you're not connected, stop here:
35+
if (status != WL_CONNECTED) {
36+
Serial.println("Couldn't get a wifi connection");
37+
while (true)
38+
;
39+
}
40+
else {
41+
Serial.print("WiFi connecting to SSID: ");
42+
Serial.println(ssid);
4243

43-
PubNub.begin(pubkey, subkey);
44-
Serial.println("PubNub set up");
45-
}
44+
PubNub.begin(pubkey, subkey);
45+
Serial.println("PubNub set up");
46+
}
4647
}
4748

4849

4950
void loop()
5051
{
51-
/* Publish */
52-
{
53-
char msg[] =
54-
"\"Hello world from Arduino for Adafruit Feather M0 WINC1500\"";
55-
WiFiClient* client = PubNub.publish(channel, msg);
56-
if (0 == client) {
57-
Serial.println("publishing error");
58-
delay(1000);
59-
return;
60-
}
61-
/* Don't care about the outcome */
62-
client->stop();
52+
/* Publish */
53+
{
54+
char msg[] =
55+
"\"Hello world from Arduino for Adafruit Feather M0 WINC1500\"";
56+
auto client = PubNub.publish(channel, msg);
57+
if (!client) {
58+
Serial.println("publishing error");
59+
delay(1000);
60+
return;
61+
}
62+
/* Don't care about the outcome */
63+
client->stop();
64+
}
65+
/* Subscribe */
66+
{
67+
auto sclient = PubNub.subscribe(channel);
68+
if (!sclient) {
69+
Serial.println("subscribe error");
70+
delay(1000);
71+
return;
6372
}
64-
/* Subscribe */
65-
{
66-
PubSubClient* sclient = PubNub.subscribe(channel);
67-
if (0 == sclient) {
68-
Serial.println("subscribe error");
69-
delay(1000);
70-
return;
71-
}
72-
/** Just print out what we get */
73-
while (sclient->wait_for_data()) {
74-
Serial.write(sclient->read());
75-
}
76-
sclient->stop();
73+
/** Just print out what we get */
74+
while (sclient->wait_for_data()) {
75+
Serial.write(sclient->read());
7776
}
78-
/* Wait a little before we go again */
79-
delay(1000);
77+
sclient->stop();
78+
}
79+
Serial.println();
80+
/* Wait a little before we go again */
81+
delay(1000);
8082
}

examples/PubNubjsonWifi/.arduino-ci.yml renamed to examples/PubNubEsp32_Json/.arduino-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
compile:
22
# Choosing to run compilation tests on different Arduino platforms
33
platforms:
4-
- uno
5-
- due
4+
# - uno
5+
# - due
66
# - zero
7-
- leonardo
7+
# - leonardo
88
# - m4
99
- esp32
1010
# - esp8266
11-
- mega2560
11+
# - mega2560
1212

1313
# Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager)
1414
libraries:

0 commit comments

Comments
 (0)