Skip to content

Commit

Permalink
Auto detect Client interface to use on some boards
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vveljko authored Jun 25, 2019
1 parent e11690b commit 7cad1a2
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 120 deletions.
28 changes: 28 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
platforms:

mkr1000:
board: arduino:samd:mkr1000
package: arduino:samd
gcc:
features:
defines:
- F_CPU=48000000L
- __SAMD21G18A__
- ARDUINO_ARCH_SAMD
- ARDUINO_SAMD_MKR1000
warnings:
flags:

compile:
libraries: ~
platforms:
- uno
- due
# - zero
- leonardo
# - m4
- esp32
# - esp8266
- mega2560
- mkr1000

unittest:
platforms:
# - uno
Expand Down
34 changes: 34 additions & 0 deletions PubNubDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,42 @@


#if !defined(PubNub_BASE_CLIENT)
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#elif defined(ARDUINO_SAMD_ZERO) || \
defined(ARDUINO_SAMD_MKR1000) || \
defined(ARDUINO_SAMD_MKRFox1200)
#include <WiFi101.h>
#define PubNub_BASE_CLIENT WiFiClient
#elif defined(ARDUINO_STM32_FEATHER)
#include <adafruit_feather.h>
#define PubNub_BASE_CLIENT AdafruitTCP
#elif defined(ARDUINO_METRO_M4_AIRLIFT_LITE) || \
defined(ARDUINO_SAMD_MKRWIFI1010) || \
defined(ARDUINO_AVR_UNO_WIFI_REV2)
#include <WiFiNINA.h>
#define PubNub_BASE_CLIENT WiFiSSLClient
#elif defined(ARDUINO_SAMD_MKRGSM1400)
#include <MKRGSM.h>
#define PubNub_BASE_CLIENT GSMClient
#elif defined(ARDUINO_SAMD_MKRWAN1300)
#include <MKRWAN.h>
#define PubNub_BASE_CLIENT LoRaModem
#elif defined(ARDUINO_AVR_YUN)
#include <Process.h>
#define PubNub_BASE_CLIENT Process
#elif defined(ARDUINO_SAMD_MKRNB1500)
#include <MKRNB.h>
#define PubNub_BASE_CLIENT NBModem
#else
#include <Ethernet.h>
#define PubNub_BASE_CLIENT EthernetClient
#endif
#endif /* !defined(PubNub_BASE_CLIENT) */

#ifdef PUBNUB_DEBUG
#define DBGprint(x...) Serial.print(x)
Expand Down
44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,33 @@ issue warnings like:

Invalid version found: x.y.z

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


## Supported Hardware

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

Since version 3.3, several boards are automatically detected and you
don't need to do anything special to use PubNub library on them. For
others, you'll have to `#define` the `Pubnub_BASE_CLIENT` to the class
that you use for networking on your board/shield that has the `Client`
compatible interface _before_ you `#include <PubNub.h>`.

The Arduino ecosystem features a multitude of platforms that
have significant differences regarding their hardware capabilities.
Keeping up with all of them is next to impossible.

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

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

For hadware that doesn't use WiFi101 library, but provides a
`WiFiClient` class, like ESP8266, you would:

#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>

Of course, please keep in mind that you need to initialize your WiFi
hardware, connect to a WiFi network and possibly do some maintenance,
which is hardware specific. But, Pubnub SDK has nothing to do with
that, it expects a working network. We provide examples for some HW.

### ESP8266
### ESP8266 and ESP32

ESP8266 and ESP32 are recognized since version 3.3 so can just:

#include <PubNub.h>

It will include `ESP8266WiFi.h` or `WiFi.h` (for ESP32) automatically.

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

#define PUBNUB_DEFINE_STRSPN_AND_STRNCASECMP
#include <PubNub.h>
Expand Down
1 change: 1 addition & 0 deletions examples/AdafruitFeatherM0WINC1500/.arduino-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ compile:
# - esp32
# - esp8266
- mega2560
- mkr1000

# Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager)
libraries:
Expand Down
98 changes: 50 additions & 48 deletions examples/AdafruitFeatherM0WINC1500/AdafruitFeatherM0WINC1500.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <SPI.h>

#include <WiFi101.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>

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

void setup()
{
/* This is the only line of code that is Feather M0 WINC1500
// put your setup code here, to run once
#if defined(ARDUINO_SAMD_ZERO)
/* This is the only line of code that is Feather M0 WINC1500
specific, the rest is the same as for the WiFi101 */
WiFi.setPins(8, 7, 4, 2);
WiFi.setPins(8, 7, 4, 2);
#endif

// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Serial set up");
Serial.begin(115200);
Serial.println("Serial set up");

// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);

// if you're not connected, stop here:
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while (true)
;
}
else {
Serial.print("WiFi connecting to SSID: ");
Serial.println(ssid);
// if you're not connected, stop here:
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while (true)
;
}
else {
Serial.print("WiFi connecting to SSID: ");
Serial.println(ssid);

PubNub.begin(pubkey, subkey);
Serial.println("PubNub set up");
}
PubNub.begin(pubkey, subkey);
Serial.println("PubNub set up");
}
}


void loop()
{
/* Publish */
{
char msg[] =
"\"Hello world from Arduino for Adafruit Feather M0 WINC1500\"";
WiFiClient* client = PubNub.publish(channel, msg);
if (0 == client) {
Serial.println("publishing error");
delay(1000);
return;
}
/* Don't care about the outcome */
client->stop();
/* Publish */
{
char msg[] =
"\"Hello world from Arduino for Adafruit Feather M0 WINC1500\"";
auto client = PubNub.publish(channel, msg);
if (!client) {
Serial.println("publishing error");
delay(1000);
return;
}
/* Don't care about the outcome */
client->stop();
}
/* Subscribe */
{
auto sclient = PubNub.subscribe(channel);
if (!sclient) {
Serial.println("subscribe error");
delay(1000);
return;
}
/* Subscribe */
{
PubSubClient* sclient = PubNub.subscribe(channel);
if (0 == sclient) {
Serial.println("subscribe error");
delay(1000);
return;
}
/** Just print out what we get */
while (sclient->wait_for_data()) {
Serial.write(sclient->read());
}
sclient->stop();
/** Just print out what we get */
while (sclient->wait_for_data()) {
Serial.write(sclient->read());
}
/* Wait a little before we go again */
delay(1000);
sclient->stop();
}
Serial.println();
/* Wait a little before we go again */
delay(1000);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
compile:
# Choosing to run compilation tests on different Arduino platforms
platforms:
- uno
- due
# - uno
# - due
# - zero
- leonardo
# - leonardo
# - m4
- esp32
# - esp8266
- mega2560
# - mega2560

# Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager)
libraries:
Expand Down
Loading

0 comments on commit 7cad1a2

Please sign in to comment.