diff --git a/.arduino-ci.yml b/.arduino-ci.yml index 453b10f..e998136 100644 --- a/.arduino-ci.yml +++ b/.arduino-ci.yml @@ -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 diff --git a/PubNubDefs.h b/PubNubDefs.h index 98f4e08..ef1fb90 100644 --- a/PubNubDefs.h +++ b/PubNubDefs.h @@ -9,8 +9,42 @@ #if !defined(PubNub_BASE_CLIENT) +#if defined(ARDUINO_ARCH_ESP8266) +#include +#define PubNub_BASE_CLIENT WiFiClient +#elif defined(ARDUINO_ARCH_ESP32) +#include +#define PubNub_BASE_CLIENT WiFiClient +#elif defined(ARDUINO_SAMD_ZERO) || \ + defined(ARDUINO_SAMD_MKR1000) || \ + defined(ARDUINO_SAMD_MKRFox1200) +#include +#define PubNub_BASE_CLIENT WiFiClient +#elif defined(ARDUINO_STM32_FEATHER) +#include +#define PubNub_BASE_CLIENT AdafruitTCP +#elif defined(ARDUINO_METRO_M4_AIRLIFT_LITE) || \ + defined(ARDUINO_SAMD_MKRWIFI1010) || \ + defined(ARDUINO_AVR_UNO_WIFI_REV2) +#include +#define PubNub_BASE_CLIENT WiFiSSLClient +#elif defined(ARDUINO_SAMD_MKRGSM1400) +#include +#define PubNub_BASE_CLIENT GSMClient +#elif defined(ARDUINO_SAMD_MKRWAN1300) +#include +#define PubNub_BASE_CLIENT LoRaModem +#elif defined(ARDUINO_AVR_YUN) +#include +#define PubNub_BASE_CLIENT Process +#elif defined(ARDUINO_SAMD_MKRNB1500) +#include +#define PubNub_BASE_CLIENT NBModem +#else +#include #define PubNub_BASE_CLIENT EthernetClient #endif +#endif /* !defined(PubNub_BASE_CLIENT) */ #ifdef PUBNUB_DEBUG #define DBGprint(x...) Serial.print(x) diff --git a/README.md b/README.md index 41b4d4a..dcdc3b1 100644 --- a/README.md +++ b/README.md @@ -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 `. + 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 @@ -311,25 +317,23 @@ So, for any WiFi101 compatible hardware, you would: #define PubNub_BASE_CLIENT WiFiClient #include -For hadware that doesn't use WiFi101 library, but provides a -`WiFiClient` class, like ESP8266, you would: - - #include - #define PubNub_BASE_CLIENT WiFiClient - #include - 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 + +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 diff --git a/examples/AdafruitFeatherM0WINC1500/.arduino-ci.yml b/examples/AdafruitFeatherM0WINC1500/.arduino-ci.yml index a219a42..b3436e6 100644 --- a/examples/AdafruitFeatherM0WINC1500/.arduino-ci.yml +++ b/examples/AdafruitFeatherM0WINC1500/.arduino-ci.yml @@ -9,6 +9,7 @@ compile: # - esp32 # - esp8266 - mega2560 + - mkr1000 # Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager) libraries: diff --git a/examples/AdafruitFeatherM0WINC1500/AdafruitFeatherM0WINC1500.ino b/examples/AdafruitFeatherM0WINC1500/AdafruitFeatherM0WINC1500.ino index 61f0202..1d8fe25 100644 --- a/examples/AdafruitFeatherM0WINC1500/AdafruitFeatherM0WINC1500.ino +++ b/examples/AdafruitFeatherM0WINC1500/AdafruitFeatherM0WINC1500.ino @@ -5,7 +5,6 @@ #include #include -#define PubNub_BASE_CLIENT WiFiClient #include static char ssid[] = "wifi_network_ssid"; // your network SSID (name) @@ -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); } diff --git a/examples/PubNubjsonWifi/.arduino-ci.yml b/examples/PubNubEsp32_Json/.arduino-ci.yml similarity index 82% rename from examples/PubNubjsonWifi/.arduino-ci.yml rename to examples/PubNubEsp32_Json/.arduino-ci.yml index 62bbce6..83e6963 100644 --- a/examples/PubNubjsonWifi/.arduino-ci.yml +++ b/examples/PubNubEsp32_Json/.arduino-ci.yml @@ -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: diff --git a/examples/PubNubjsonWifi/PubNubjsonWifi.ino b/examples/PubNubEsp32_Json/PubNubEsp32_Json.ino similarity index 77% rename from examples/PubNubjsonWifi/PubNubjsonWifi.ino rename to examples/PubNubEsp32_Json/PubNubEsp32_Json.ino index e16bd45..e5d58c8 100644 --- a/examples/PubNubjsonWifi/PubNubjsonWifi.ino +++ b/examples/PubNubEsp32_Json/PubNubEsp32_Json.ino @@ -1,22 +1,19 @@ /* - PubNub sample JSON-parsing client with WiFi support - This combines two sketches: the PubNubJson example of PubNub library - and the WifiWebClientRepeating example of the WiFi library. - This sample client will properly parse JSON-encoded PubNub subscription - replies using the ArduinoJson library(v6.10). It will send a simple message, then + PubNub sample(Esp32, Esp8266 boards) JSON-parsing client with WiFi support. + This combines two sketches: the ArduinoJson example of PubNub library + and the WifiClient example of the WiFi library. + This sample client will properly parse JSON-encoded PubNub subscription replies + using the ArduinoJson library(v6.10). It will send a simple message, then properly parsing and inspecting a subscription message received back. Circuit: - * Wifi shield attached to pins 10, 11, 12, 13 - * (Optional) Analog sensors attached to analog pin. - * (Optional) LEDs to be dimmed attached to PWM pins 8 and 9. + * (Optional) Analog sensors attached to analog pins. + (Them pins should be picked and initialized properly for any particular use + according to the chosen board specifications and scatch adjusted suitably.) + * (Optional) LEDs to be dimmed attached to PWM pins 8 and 9 (Esp8266 board). - Please refer to the PubNubJson example description for some important - notes, especially regarding memory saving on Arduino Uno/Duemilanove. - You can also save some RAM by not using WiFi password protection. - - Note that due to use of the ArduinoJSON library, this sketch is more memory - sensitive than the others. In order to be able to use it on the boards + Note that due to use of the ArduinoJson library, this sketch is more memory + sensitive. In order to be able to use it on the boards based on ATMega328, some special care is needed. Memory saving tips: (i) In the file hardware/arduino/cores/arduino/HardwareSerial.cpp @@ -30,32 +27,46 @@ */ #include + +#if defined(ARDUINO_ARCH_ESP8266) +#include +#define ANALOG_INPUT_PIN_0 A0 +#define NUM_ANALOG_INPUT_PINS 1 +#elif defined(ARDUINO_ARCH_ESP32) #include -#define PubNub_BASE_CLIENT WiFiClient +#define ANALOG_INPUT_PIN_0 32 +/* Number of analog input pins used, starting with ANALOG_INPUT_PIN_0 */ +#define NUM_ANALOG_INPUT_PINS 3 +#endif #include #include using namespace ArduinoJson; -// Some Ethernet shields have a MAC address printed on a sticker on the shield; -// fill in that address here, or choose your own at random: +// fill in MAC address here, or choose your own at random: const static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -static char ssid[] = "ssid_name"; // your network SSID (name) -static char pass[] = "password"; // your network password -//static int keyIndex = 0; // your network key Index number (needed only for WEP) +static char ssid[] = "your-wifi-network"; // your network SSID (name) +static char pass[] = "your-wifi-password"; // your network password const static char pubkey[] = "demo"; const static char subkey[] = "demo"; const static char channel[] = "hello_world"; + void setup() { Serial.begin(115200); Serial.println("Serial set up"); + /* In this scatch demo all input pins are 'floating' */ + pinMode(ANALOG_INPUT_PIN_0, INPUT); + /* Only one analog input pin for ESP8266 */ +#if !defined(ARDUINO_ARCH_ESP8266) + pinMode(ANALOG_INPUT_PIN_0 + 1, INPUT); + pinMode(ANALOG_INPUT_PIN_0 + 2, INPUT); +#endif -#if defined(ARDUINO_ARCH_ESP32) // attempt to connect using WPA2 encryption: Serial.println("Attempting to connect to WPA network..."); WiFi.begin(ssid, pass); @@ -65,23 +76,6 @@ void setup() delay(500); Serial.println("Connecting to WiFi.."); } -#else - int status; - - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - while(true); // stop - } - // attempt to connect to Wifi network: - do { - Serial.print("WiFi connecting to SSID: "); - Serial.println(ssid); - // Connect to the network. Uncomment whichever line is right for you: - //status = WiFi.begin(ssid); // open network - //status = WiFi.begin(ssid, keyIndex, key); // WEP network - status = WiFi.begin(ssid, pass); // WPA / WPA2 Personal network - } while (status != WL_CONNECTED); -#endif /* defined(ARDUINO_ARCH_ESP32) */ Serial.println("WiFi set up"); Serial.print("WL_CONNECTED="); Serial.println(WL_CONNECTED); @@ -99,8 +93,8 @@ JsonObject createMessage(JsonDocument jd) sender["mac_last_byte"] = mac[5]; JsonArray analog = msg.createNestedArray("analog"); - for (int i = 0; i < 6; i++) { - analog.add(analogRead(i)); + for (int i = 0; i < NUM_ANALOG_INPUT_PINS; i++) { + analog.add(analogRead(ANALOG_INPUT_PIN_0 + i)); } return msg; @@ -160,19 +154,19 @@ void dumpMessage(Stream& s, JsonArray response) } s.print(sender["mac_last_byte"].as(), DEC); - s.print(" A2: "); + s.print(" A0: "); JsonArray analog = obj["analog"]; if (analog.isNull()) { s.println("analog not acquired"); delay(1000); return; } - if (analog.size() < 3) { - s.println("analog[2] not acquired"); + if (analog.size() < 1) { + s.println("analog[0] not acquired"); delay(1000); return; } - s.print(analog[2].as(), DEC); + s.print(analog[0].as(), DEC); s.println(); } diff --git a/examples/PubNubWifi101/.arduino-ci.yml b/examples/PubNubWifi101/.arduino-ci.yml index 3c76c3c..4451540 100644 --- a/examples/PubNubWifi101/.arduino-ci.yml +++ b/examples/PubNubWifi101/.arduino-ci.yml @@ -9,3 +9,4 @@ compile: # - esp32 # - esp8266 - mega2560 + - mkr1000 diff --git a/examples/PubNubWifi101/PubNubWifi101.ino b/examples/PubNubWifi101/PubNubWifi101.ino index 56c7fdd..6d3b6c8 100644 --- a/examples/PubNubWifi101/PubNubWifi101.ino +++ b/examples/PubNubWifi101/PubNubWifi101.ino @@ -12,7 +12,6 @@ #include #include -#define PubNub_BASE_CLIENT WiFiClient #include static char ssid[] = "your-wifi-network"; // your network SSID (name) @@ -27,7 +26,7 @@ const static char channel[] = "hello_world"; // channel to use void setup() { // put your setup code here, to run once: - Serial.begin(9600); + Serial.begin(115200); Serial.println("Serial set up"); // attempt to connect using WPA2 encryption: @@ -51,7 +50,7 @@ void setup() void loop() { - char msg[] = "\"Yo!\""; + char msg[] = "\"arduino-WiFi101-hello-world!\""; auto client = PubNub.publish(channel, msg); if (!client) { Serial.println("publishing error"); diff --git a/library.properties b/library.properties index 8fccc00..cdf15cd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Pubnub -version=3.2.2 +version=3.3.0 author=Vladimir Veljkovic maintainer=Vladimir Veljkovic sentence=Pubnub SDK for Arduino.