diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b5c6f2b..18ffb88 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p Please ensure to specify the following, or your post will be ignored and deleted: * Arduino IDE version (e.g. 1.8.19) or Platform.io version -* `RP2040` Core Version (e.g. Arduino-mbed RP2040 v3.3.0 or arduino-pico core v2.6.0) +* `RP2040` Core Version (e.g. Arduino-mbed RP2040 v3.4.1 or arduino-pico core v2.6.3) * `RP2040` Board type (e.g. NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce @@ -28,10 +28,13 @@ Please ensure to specify the following, or your post will be ignored and deleted ``` Arduino IDE version: 1.8.19 -Arduino-mbed mbed_nano v3.3.0 +Arduino-mbed mbed_nano v3.4.1 NANO_RP2040_CONNECT Module OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.15.0-50-generic #56~20.04.1-Ubuntu SMP Tue Sep 27 15:51:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux + +Context: +I encountered a crash while using this library Steps to reproduce: 1. ... @@ -40,13 +43,34 @@ Steps to reproduce: 4. ... ``` +--- + ### Sending Feature Requests Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful. There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/MQTTPubSubClient_Generic/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them. +--- + ### Sending Pull Requests Pull Requests with changes and fixes are also welcome! +Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux) + +1. Change directory to the library GitHub + +``` +xy@xy-Inspiron-3593:~$ cd Arduino/xy/MQTTPubSubClient_Generic_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/MQTTPubSubClient_Generic_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/MQTTPubSubClient_Generic_GitHub$ bash utils/restyle.sh +``` + + + diff --git a/README.md b/README.md index 397a383..5c1e508 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# MQTTPubSubClient_Generic client for ESP8266, ESP32, etc. +# MQTTPubSubClient_Generic Library (MQTT Client for ESP8266, ESP32, etc.) [![arduino-library-badge](https://www.ardu-badge.com/badge/MQTTPubSubClient_Generic.svg?)](https://www.ardu-badge.com/MQTTPubSubClient_Generic) [![GitHub release](https://img.shields.io/github/release/khoih-prog/MQTTPubSubClient_Generic.svg)](https://github.com/khoih-prog/MQTTPubSubClient_Generic/releases) @@ -6,8 +6,11 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/MQTTPubSubClient_Generic.svg)](http://github.com/khoih-prog/MQTTPubSubClient_Generic/issues) + Donate to my libraries using BuyMeACoffee + + --- @@ -45,6 +48,7 @@ * [10. For RTL8720DN boards using AmebaD core](#10-for-rtl8720dn-boards-using-amebad-core) * [11. For SAMD21 and SAMD51 boards using ArduinoCore-fab-sam core](#11-For-SAMD21-and-SAMD51-boards-using-ArduinoCore-fab-sam-core) * [12. For Seeeduino RP2040 boards](#12-For-Seeeduino-RP2040-boards) + * [13. For Seeeduino nRF52840 boards](#13-For-Seeeduino-nRF52840-boards) * [Libraries' Patches](#libraries-patches) * [1. For application requiring 2K+ HTML page](#1-for-application-requiring-2k-html-page) * [2. For Ethernet library](#2-for-ethernet-library) @@ -96,6 +100,9 @@ * [3. WiFiNINA_MQTToverWebSocketSecure](examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure) * [4. WiFiNINA_MQTTSecure](examples/WiFiNINA/WiFiNINA_MQTTSecure) * [5. WiFiNINA_MQTTSecureAWS](examples/WiFiNINA/WiFiNINA_MQTTSecureAWS) + * [7. For WiFi101](#6-For-WiFi101) + * [1. WiFiMQTT](examples/WiFi101/WiFiMQTT) + * [2. WiFiMQTToverWebSocket](examples/WiFi101/WiFiMQTToverWebSocket) * [Example EtherMQTToverWebSocket](#example-EtherMQTToverWebSocket) * [1. File EtherMQTToverWebSocket.ino](#1-file-EtherMQTToverWebSocketino) * [2. File defines.h](#2-file-definesh) @@ -211,7 +218,7 @@ This [**MQTTPubSubClient_Generic** library](https://github.com/khoih-prog/MQTTPu #### Currently supported WiFi shields/modules 1. WiFiNINA using [`WiFiNINA_Generic library`](https://github.com/khoih-prog/WiFiNINA_Generic) -2. WiFi101 using [`WiFi101 library v0.16.1+`](https://github.com/arduino-libraries/WiFi101) +2. WWiFi101 using [`WiFi101_Generic library`](https://github.com/khoih-prog/WiFi101_Generic) **New** 3. U-Blox W101, W102 using [`WiFiNINA_Generic library`](https://github.com/khoih-prog/WiFiNINA_Generic) 4. ESP8266-AT command using [`WiFiEspAT library`](https://github.com/jandrassy/WiFiEspAT) 5. ESP8266/ESP32-AT command using [`ESP_AT_Lib library`](https://github.com/khoih-prog/ESP_AT_Lib) and [`ESP8266_AT_WebServer`](ESP8266_AT_WebServer) @@ -239,17 +246,17 @@ This [**MQTTPubSubClient_Generic** library](https://github.com/khoih-prog/MQTTPu 2. [`Teensy core v1.57+`](https://www.pjrc.com/teensy/td_download.html) for Teensy (4.1, 4.0, 3.6, 3.5, 3,2, 3.1, 3.0) boards. 3. [`Arduino SAM DUE core v1.6.12+`](https://github.com/arduino/ArduinoCore-sam) for SAM DUE ARM Cortex-M3 boards. **Not yet** 4. [`Arduino SAMD core 1.8.13+`](https://github.com/arduino/ArduinoCore-samd) for SAMD ARM Cortex-M0+ boards. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-samd.svg)](https://github.com/arduino/ArduinoCore-samd/releases/latest) - 5. [`Adafruit SAMD core 1.7.10+`](https://github.com/adafruit/ArduinoCore-samd) for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.). [![GitHub release](https://img.shields.io/github/release/adafruit/ArduinoCore-samd.svg)](https://github.com/adafruit/ArduinoCore-samd/releases/latest) + 5. [`Adafruit SAMD core 1.7.11+`](https://github.com/adafruit/ArduinoCore-samd) for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.). [![GitHub release](https://img.shields.io/github/release/adafruit/ArduinoCore-samd.svg)](https://github.com/adafruit/ArduinoCore-samd/releases/latest) 6. [`Seeeduino SAMD core 1.8.3+`](https://github.com/Seeed-Studio/ArduinoCore-samd) for SAMD21/SAMD51 boards (XIAO M0, Wio Terminal, etc.). [![Latest release](https://img.shields.io/github/release/Seeed-Studio/ArduinoCore-samd.svg)](https://github.com/Seeed-Studio/ArduinoCore-samd/releases/latest/) 7. [`Adafruit nRF52 v1.3.0+`](https://github.com/adafruit/Adafruit_nRF52_Arduino) for nRF52 boards such as Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, **NINA_B302_ublox**, etc. [![GitHub release](https://img.shields.io/github/release/adafruit/Adafruit_nRF52_Arduino.svg)](https://github.com/adafruit/Adafruit_nRF52_Arduino/releases/latest) 8. [`ESP32 Core 2.0.5+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/) 9. [`ESP8266 Core 3.0.2+`](https://github.com/esp8266/Arduino) for ESP8266-based boards. [![Latest release](https://img.shields.io/github/release/esp8266/Arduino.svg)](https://github.com/esp8266/Arduino/releases/latest/). To use ESP8266 core 2.7.1+ for LittleFS. -10. [`ArduinoCore-mbed mbed_rp2040, mbed_nano, mbed_portenta core 3.3.0+`](https://github.com/arduino/ArduinoCore-mbed) for Arduino (Use Arduino Board Manager) **Portenta_H7, RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO**. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-mbed.svg)](https://github.com/arduino/ArduinoCore-mbed/releases/latest) -11. [`Earle Philhower's arduino-pico core v2.6.0+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest) +10. [`ArduinoCore-mbed mbed_rp2040, mbed_nano, mbed_portenta core 3.4.1+`](https://github.com/arduino/ArduinoCore-mbed) for Arduino (Use Arduino Board Manager) **Portenta_H7, RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO**. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-mbed.svg)](https://github.com/arduino/ArduinoCore-mbed/releases/latest) +11. [`Earle Philhower's arduino-pico core v2.6.3+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest) 12. [`Arduino Core for STM32 v2.3.0+`](https://github.com/stm32duino/Arduino_Core_STM32) for STM32F/L/H/G/WB/MP1 boards. [![GitHub release](https://img.shields.io/github/release/stm32duino/Arduino_Core_STM32.svg)](https://github.com/stm32duino/Arduino_Core_STM32/releases/latest). Use STM32 core `v2.2.0` for LAN8720 or compile error. -13. [`WiFiNINA_Generic library v1.8.14-6+`](https://github.com/khoih-prog/WiFiNINA_Generic) if for WiFiNINA. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiNINA_Generic.svg?)](https://www.ardu-badge.com/WiFiNINA_Generic). -14. [`EthernetWebServer library v2.2.3+`](https://github.com/khoih-prog/EthernetWebServer) if necessary to use Ethernet modules/shields. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/EthernetWebServer.svg?)](https://www.ardu-badge.com/EthernetWebServer) +13. [`WiFiNINA_Generic library v1.8.15-1+`](https://github.com/khoih-prog/WiFiNINA_Generic) if for WiFiNINA. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiNINA_Generic.svg?)](https://www.ardu-badge.com/WiFiNINA_Generic). +14. [`EthernetWebServer library v2.3.0+`](https://github.com/khoih-prog/EthernetWebServer) if necessary to use Ethernet modules/shields. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/EthernetWebServer.svg?)](https://www.ardu-badge.com/EthernetWebServer) 15. [`EthernetWebServer_STM32 library v1.5.0+`](https://github.com/khoih-prog/EthernetWebServer_STM32) if necessary to use Ethernet modules/shields. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/EthernetWebServer_STM32.svg?)](https://www.ardu-badge.com/EthernetWebServer_STM32) 16. [`WebServer_WT32_ETH01 library v1.5.1+`](https://github.com/khoih-prog/WebServer_WT32_ETH01) if necessary to use WT32_ETH01 boards. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/WebServer_WT32_ETH01.svg?)](https://www.ardu-badge.com/WebServer_WT32_ETH01) 17. Depending on which Ethernet card you're using: @@ -258,11 +265,11 @@ This [**MQTTPubSubClient_Generic** library](https://github.com/khoih-prog/MQTTPu - [`UIPEthernet library v2.0.12+`](https://github.com/UIPEthernet/UIPEthernet) for ENC28J60. [![GitHub release](https://img.shields.io/github/release/UIPEthernet/UIPEthernet.svg)](https://github.com/UIPEthernet/UIPEthernet/releases/latest) - [`STM32Ethernet library v1.3.0+`](https://github.com/stm32duino/STM32Ethernet) for built-in LAN8742A Ethernet on (Nucleo-144, Discovery). [![GitHub release](https://img.shields.io/github/release/stm32duino/STM32Ethernet.svg)](https://github.com/stm32duino/STM32Ethernet/releases/latest). To be used with [`LwIP library v2.1.2+`](https://github.com/stm32duino/LwIP). [![GitHub release](https://img.shields.io/github/release/stm32duino/LwIP.svg)](https://github.com/stm32duino/LwIP/releases/latest). - [`NativeEthernet Library version stable111+`](https://github.com/vjmuzik/NativeEthernet) for Teensy 4.1 built-in Ethernet. - - [`QNEthernet Library version v0.15.0+`](https://github.com/ssilverman/QNEthernet) Teensy 4.1 built-in Ethernet. **New** + - [`QNEthernet Library version v0.16.0+`](https://github.com/ssilverman/QNEthernet) Teensy 4.1 built-in Ethernet. **New** -18. [`WiFiWebServer library v1.9.5+`](https://github.com/khoih-prog/WiFiWebServer) if necessary to use certain WiFi/WiFiNINA features. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiWebServer.svg?)](https://www.ardu-badge.com/WiFiWebServer) -19. [`Modified WiFi101 Library v0.16.1+`](https://github.com/khoih-prog/WiFi101) to use SAMD MKR1000, etc. boards with WiFi101. -20. [`WebSockets_Generic library v2.16.0+`](https://github.com/khoih-prog/WebSockets_Generic) if necessary to use `MQTT-over-WebSockets` features. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/WebSockets_Generic.svg?)](https://www.ardu-badge.com/WebSockets_Generic) +18. [`WiFiWebServer library v1.10.1+`](https://github.com/khoih-prog/WiFiWebServer) if necessary to use certain WiFi/WiFiNINA features. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiWebServer.svg?)](https://www.ardu-badge.com/WiFiWebServer) +19. [`WiFi101_Generic Library v1.0.0+`](https://github.com/khoih-prog/WiFi101_Generic) to use SAMD MKR1000, etc. boards with WiFi101 to send large data. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFi101_Generic.svg?)](https://www.ardu-badge.com/WiFi101_Generic). +20. [`WebSockets_Generic library v2.16.1+`](https://github.com/khoih-prog/WebSockets_Generic) if necessary to use `MQTT-over-WebSockets` features. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/WebSockets_Generic.svg?)](https://www.ardu-badge.com/WebSockets_Generic) --- --- @@ -278,9 +285,9 @@ You can also use this link [![arduino-library-badge](https://www.ardu-badge.com/ Another way to install is to: 1. Navigate to [**MQTTPubSubClient_Generic**](https://github.com/khoih-prog/MQTTPubSubClient_Generic) page. -2. Download the latest release `MQTTPubSubClient_Generic-master.zip`. -3. Extract the zip file to `MQTTPubSubClient_Generic-master` directory -4. Copy whole `MQTTPubSubClient_Generic-master` folder to Arduino libraries' directory such as `~/Arduino/libraries/`. +2. Download the latest release `MQTTPubSubClient_Generic-main.zip`. +3. Extract the zip file to `MQTTPubSubClient_Generic-main` directory +4. Copy whole `MQTTPubSubClient_Generic-main` folder to Arduino libraries' directory such as `~/Arduino/libraries/`. ### VS Code & PlatformIO: @@ -300,7 +307,6 @@ Another way to install is to: **To be able to compile, run and automatically detect and display BOARD_NAME on nRF52840/nRF52832 boards**, you have to copy the whole [nRF52 Packages_Patches](Packages_Patches/adafruit/hardware/nrf52/1.3.0) directory into Adafruit nRF52 directory (~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0). Supposing the Adafruit nRF52 version is 1.3.0. These files must be copied into the directory: - - `~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/platform.txt` - `~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/boards.txt` - `~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/cores/nRF5/Udp.h` @@ -399,16 +405,16 @@ Whenever the above-mentioned compiler error issue is fixed with the new Arduino #### 5. For Adafruit SAMD boards - ***To be able to compile, run and automatically detect and display BOARD_NAME on Adafruit SAMD (Itsy-Bitsy M4, etc) boards***, you have to copy the whole [Adafruit SAMD Packages_Patches](Packages_Patches/adafruit/hardware/samd/1.7.10) directory into Adafruit samd directory (~/.arduino15/packages/adafruit/hardware/samd/1.7.10). + ***To be able to compile, run and automatically detect and display BOARD_NAME on Adafruit SAMD (Itsy-Bitsy M4, etc) boards***, you have to copy the whole [Adafruit SAMD Packages_Patches](Packages_Patches/adafruit/hardware/samd/1.7.11) directory into Adafruit samd directory (~/.arduino15/packages/adafruit/hardware/samd/1.7.11). -Supposing the Adafruit SAMD core version is 1.7.10. This file must be copied into the directory: +Supposing the Adafruit SAMD core version is 1.7.11. These files must be copied into the directory: -- `~/.arduino15/packages/adafruit/hardware/samd/1.7.10/platform.txt` -- `~/.arduino15/packages/adafruit/hardware/samd/1.7.10/cores/arduino/Print.h` -- `~/.arduino15/packages/adafruit/hardware/samd/1.7.10/cores/arduino/Print.cpp` +- `~/.arduino15/packages/adafruit/hardware/samd/1.7.11/platform.txt` +- `~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.h` +- `~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.cpp` Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz -This file must be copied into the directory: +These files must be copied into the directory: - `~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/platform.txt` - `~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.h` @@ -418,7 +424,7 @@ This file must be copied into the directory: ***To be able to compile, run and automatically detect and display BOARD_NAME on Seeeduino SAMD (XIAO M0, Wio Terminal, etc) boards***, you have to copy the whole [Seeeduino SAMD Packages_Patches](Packages_Patches/Seeeduino/hardware/samd/1.8.3) directory into Seeeduino samd directory (~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3). -Supposing the Seeeduino SAMD core version is 1.8.3. This file must be copied into the directory: +Supposing the Seeeduino SAMD core version is 1.8.3. These files must be copied into the directory: - `~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/platform.txt` - `~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Arduino.h` @@ -426,7 +432,7 @@ Supposing the Seeeduino SAMD core version is 1.8.3. This file must be copied int - `~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.cpp` Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz -This file must be copied into the directory: +These files must be copied into the directory: - `~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/platform.txt` - `~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Arduino.h` @@ -437,24 +443,28 @@ This file must be copied into the directory: #### 7.1 For STM32 boards to use LAN8720 +For `Generic STM32F4 series` boards, such as `STM32F407VE`, using `LAN8720`, please use STM32 core `v2.2.0` as breaking core `v2.3.0` creates the compile error. + +--- + To use LAN8720 on some STM32 boards - **Nucleo-144 (F429ZI, NUCLEO_F746NG, NUCLEO_F746ZG, NUCLEO_F756ZG)** - **Discovery (DISCO_F746NG)** - **STM32F4 boards (BLACK_F407VE, BLACK_F407VG, BLACK_F407ZE, BLACK_F407ZG, BLACK_F407VE_Mini, DIYMORE_F407VGT, FK407M1)** -you have to copy the files [stm32f4xx_hal_conf_default.h](Packages_Patches/STM32/hardware/stm32/2.3.0/system/STM32F4xx) and [stm32f7xx_hal_conf_default.h](Packages_Patches/STM32/hardware/stm32/2.3.0/system/STM32F7xx) into STM32 stm32 directory (~/.arduino15/packages/STM32/hardware/stm32/2.3.0/system) to overwrite the old files. +you have to copy the files [stm32f4xx_hal_conf_default.h](Packages_Patches/STM32/hardware/stm32/2.2.0/system/STM32F4xx) and [stm32f7xx_hal_conf_default.h](Packages_Patches/STM32/hardware/stm32/2.2.0/system/STM32F7xx) into STM32 stm32 directory (~/.arduino15/packages/STM32/hardware/stm32/2.2.0/system) to overwrite the old files. -Supposing the STM32 stm32 core version is 2.3.0. These files must be copied into the directory: +Supposing the STM32 stm32 core version is 2.2.0. These files must be copied into the directory: -- `~/.arduino15/packages/STM32/hardware/stm32/2.3.0/system/STM32F4xx/stm32f4xx_hal_conf_default.h` for STM32F4. -- `~/.arduino15/packages/STM32/hardware/stm32/2.3.0/system/STM32F7xx/stm32f7xx_hal_conf_default.h` for Nucleo-144 STM32F7. +- `~/.arduino15/packages/STM32/hardware/stm32/2.2.0/system/STM32F4xx/stm32f4xx_hal_conf_default.h` for STM32F4. +- `~/.arduino15/packages/STM32/hardware/stm32/2.2.0/system/STM32F7xx/stm32f7xx_hal_conf_default.h` for Nucleo-144 STM32F7. Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz, these files must be copied into the corresponding directory: - `~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/system/STM32F4xx/stm32f4xx_hal_conf_default.h` -- `~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/system/STM32F7xx/stm32f7xx_hal_conf_default.h +- `~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/system/STM32F7xx/stm32f7xx_hal_conf_default.h` #### 7.2 For STM32 boards to use Serial1 @@ -507,12 +517,12 @@ With core after v1.5.0, this step is not necessary anymore thanks to the PR [Add #### 9. For Portenta_H7 boards using Arduino IDE in Linux - **To be able to upload firmware to Portenta_H7 using Arduino IDE in Linux (Ubuntu, etc.)**, you have to copy the file [portenta_post_install.sh](Packages_Patches/arduino/hardware/mbed_portenta/3.3.0/portenta_post_install.sh) into mbed_portenta directory (~/.arduino15/packages/arduino/hardware/mbed_portenta/3.3.0/portenta_post_install.sh). + **To be able to upload firmware to Portenta_H7 using Arduino IDE in Linux (Ubuntu, etc.)**, you have to copy the file [portenta_post_install.sh](Packages_Patches/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh) into mbed_portenta directory (~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh). Then run the following command using `sudo` ``` -$ cd ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.3.0 +$ cd ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1 $ chmod 755 portenta_post_install.sh $ sudo ./portenta_post_install.sh ``` @@ -525,9 +535,9 @@ This will create the file `/etc/udev/rules.d/49-portenta_h7.rules` as follows: SUBSYSTEMS=="usb", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="035b", GROUP="plugdev", MODE="0666" ``` -Supposing the ArduinoCore-mbed core version is 3.3.0. Now only one file must be copied into the directory: +Supposing the ArduinoCore-mbed core version is 3.4.1. Now only one file must be copied into the directory: -- `~/.arduino15/packages/arduino/hardware/mbed_portenta/3.3.0/portenta_post_install.sh` +- `~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh` Whenever a new version is installed, remember to copy this files into the new version directory. For example, new version is x.yy.zz @@ -552,11 +562,11 @@ This file must be copied into the directory: #### 11. For SAMD21 and SAMD51 boards using ArduinoCore-fab-sam core - To avoid compile error relating to SAMD21/SAMD51, you have to copy the file [ArduinoCore-fab-sam core pgmspace.h](Packages_Patches/Fab_SAM_Arduino/hardware/samd/1.7.0/boards.txt) into `ArduinoCore-fab-sam` samd directory (~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/1.7.0/boards.txt). + To avoid compile error relating to SAMD21/SAMD51, you have to copy the file [ArduinoCore-fab-sam core pgmspace.h](Packages_Patches/Fab_SAM_Arduino/hardware/samd/1.9.0/boards.txt) into `ArduinoCore-fab-sam` samd directory (~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/1.9.0/boards.txt). -Supposing the `ArduinoCore-fab-sam` samd core version is 1.7.0. This file must be copied into the directory: +Supposing the `ArduinoCore-fab-sam` samd core version is 1.9.0. This file must be copied into the directory: -- `~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/1.7.0/boards.txt` +- `~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/1.9.0/boards.txt` Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory: @@ -568,18 +578,39 @@ This file must be copied into the directory: ***To be able to compile, run and automatically detect and display BOARD_NAME on Seeeduino RP2040 (XIAO RP2040, Wio RP2040 Mini) boards***, you have to copy the whole [Seeeduino RP2040 Packages_Patches](Packages_Patches/Seeeduino/hardware/rp2040/2.7.2) directory into Seeeduino samd directory (~/.arduino15/packages/Seeeduino/hardware/rp2040/2.7.2). -Supposing the Seeeduino SAMD core version is 2.7.2. This file must be copied into the directory: +Supposing the Seeeduino RP2040 core version is 2.7.2. These files must be copied into the directory: - `~/.arduino15/packages/Seeeduino/hardware/rp2040/2.7.2/boards.txt` - `~/.arduino15/packages/Seeeduino/hardware/rp2040/2.7.2/variants/Seeed_XIAO_RP2040/pins_arduino.h` Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz -This file must be copied into the directory: +These files must be copied into the directory: - `~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/boards.txt` - `~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/variants/Seeed_XIAO_RP2040/pins_arduino.h` +--- + +#### 13. For Seeeduino nRF52840 boards + +**To be able to compile and run on Xiao nRF52840 boards**, you have to copy the whole [nRF52 1.0.0](Packages_Patches/Seeeduino/hardware/nrf52/1.0.0) directory into Seeeduino nRF52 directory (~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0). + +Supposing the Seeeduino nRF52 version is 1.0.0. These files must be copied into the directory: + +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/platform.txt`** +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/cores/nRF5/Print.h`** +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/cores/nRF5/Print.cpp`** +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/cores/nRF5/Udp.h`** + +Whenever a new version is installed, remember to copy these files into the new version directory. For example, new version is x.yy.z +These files must be copied into the directory: + +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/platform.txt`** +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/cores/nRF5/Print.h`** +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/cores/nRF5/Print.cpp`** +- **`~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/cores/nRF5/Udp.h`** + --- --- @@ -985,7 +1016,7 @@ Connect as follows. To program, use **STM32CubeProgrammer** or Arduino IDE with

- +

--- @@ -1045,6 +1076,11 @@ Connect FDTI (USB to Serial) as follows: 4. [WiFiNINA_MQTTSecure](examples/WiFiNINA/WiFiNINA_MQTTSecure) 5. [WiFiNINA_MQTTSecureAWS](examples/WiFiNINA/WiFiNINA_MQTTSecureAWS) +#### 7. For WiFi101 + + 1. [WiFiMQTT](examples/WiFi101/WiFiMQTT) + 2. [WiFiMQTToverWebSocket](examples/WiFi101/WiFiMQTToverWebSocket) + --- --- @@ -1070,9 +1106,9 @@ https://github.com/khoih-prog/MQTTPubSubClient_Generic/blob/168f2ce72da6950d577d This is terminal debug output when running [WiFiMQTToverWebSocket](examples/WiFi/WiFiMQTToverWebSocket) on **ESP8266_NODEMCU_ESP12E** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start WiFiMQTToverWebSocket on ESP8266_NODEMCU_ESP12E with ESP WiFi using WiFi Library -MQTTPubSubClient_Generic v1.2.0 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.145 SSID: HueNet1, Signal strength (RSSI):-39 dBm @@ -1088,9 +1124,9 @@ Subscribed to /mqttPubSub => Hello from ESP8266_NODEMCU_ESP12E This is terminal debug output when running [WiFiMQTToverWebSocketSecure](examples/WiFi/WiFiMQTToverWebSocketSecure) on **ESP32_DEV** connecting to `public.cloud.shiftr.io:443` WebSockets server. -``` +```cpp Start WiFiMQTToverWebSocketSecure on ESP32_DEV -MQTTPubSubClient_Generic v1.2.0 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.93 SSID: HueNet1, Signal strength (RSSI):-35 dBm @@ -1106,10 +1142,10 @@ Subscribed to /mqttPubSub => Hello from WiFiMQTToverWebSocketSecure on ESP32_DEV This is terminal debug output when running [WiFiMQTToverWebSocket](examples/WiFi/WiFiMQTToverWebSocket) on **SAMD_NANO_33_IOT** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start WiFiMQTToverWebSocket on SAMD_NANO_33_IOT with WiFiNINA using WiFiNINA_Generic Library -WiFiWebServer v1.7.0 -MQTTPubSubClient_Generic v1.2.0 +WiFiWebServer v1.10.1 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.150 SSID: HueNet1, Signal strength (RSSI):-15 dBm @@ -1126,10 +1162,10 @@ Subscribed to /mqttPubSub => Hello from SAMD_NANO_33_IOT with WiFiNINA using WiF This is terminal debug output when running [EtherMQTToverWebSocket](examples/Ethernet/EtherMQTToverWebSocket) on **STM32F7 NUCLEO_F767ZI** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start EtherMQTToverWebSocket_STM32 on NUCLEO_F767ZI with W5x00 using default Ethernet_Generic Library -EthernetWebServer_STM32 v1.4.0 -MQTTPubSubClient_Generic v1.2.0 +EthernetWebServer_STM32 v1.5.0 +MQTTPubSubClient_Generic v1.2.1 [EWS] Board : NUCLEO_F767ZI , setCsPin: 10 [EWS] Default SPI pinout: [EWS] MOSI: 11 @@ -1153,10 +1189,10 @@ Subscribed to /mqttPubSub => Hello from NUCLEO_F767ZI with W5x00 using default E This is terminal debug output when running [EtherMQTToverWebSocket_STM32](examples/STM32/EtherMQTToverWebSocket_STM32) on **STM32F7 NUCLEO_F767ZI-ETH01 with LAN8742A** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start EtherMQTToverWebSocket_STM32 on NUCLEO_F767ZI with LAN8742A Ethernet & STM32Ethernet Library -EthernetWebServer_STM32 v1.4.0 -MQTTPubSubClient_Generic v1.2.0 +EthernetWebServer_STM32 v1.5.0 +MQTTPubSubClient_Generic v1.2.1 Connected! IP address: 192.168.2.88 Connecting to WebSockets Server @ test.mosquitto.org Connecting to mqtt broker.... connected! @@ -1171,9 +1207,9 @@ Subscribed to /mqttPubSub => Hello from NUCLEO_F767ZI with LAN8742A Ethernet & S This is terminal debug output when running [MQTToverWebSocket_QNEthernet](examples/QNEthernet/MQTToverWebSocket_QNEthernet) on **TEENSY 4.1 with QNEthernet** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start MQTToverWebSocket_QNEthernet on TEENSY 4.1 -MQTTPubSubClient_Generic v1.2.0 +MQTTPubSubClient_Generic v1.2.1 =========== USE_QN_ETHERNET =========== Initialize Ethernet using static IP => Connecting to WebSockets Server @ test.mosquitto.org Connecting to mqtt broker.... connected! @@ -1188,10 +1224,10 @@ Subscribed to /mqttPubSub => Hello from TEENSY 4.1 using QNEthernet This is terminal debug output when running [EtherMQTToverWebSocket](examples/Ethernet/EtherMQTToverWebSocket) on **MBED RASPBERRY_PI_PICO with W5500 Ethernet** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start EtherMQTToverWebSocket on MBED RASPBERRY_PI_PICO -EthernetWebServer v2.1.2 -MQTTPubSubClient_Generic v1.2.0 +EthernetWebServer v2.3.0 +MQTTPubSubClient_Generic v1.2.1 [EWS] =========== USE_ETHERNET_GENERIC =========== [EWS] Default SPI pinout: [EWS] MOSI: 19 @@ -1227,10 +1263,10 @@ Subscribed to /mqttPubSub => Hello from MBED RASPBERRY_PI_PICO This is terminal debug output when running [EtherMQTToverWebSocket](examples/Ethernet/EtherMQTToverWebSocket) on **RASPBERRY_PI_PICO with W5500 Ethernet** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start EtherMQTToverWebSocket on RASPBERRY_PI_PICO with W5x00 using Ethernet_Generic Library on SPI1 -EthernetWebServer v2.1.2 -MQTTPubSubClient_Generic v1.2.0 +EthernetWebServer v2.3.0 +MQTTPubSubClient_Generic v1.2.1 [EWS] =========== USE_ETHERNET_GENERIC =========== [EWS] Default SPI pinout: [EWS] MOSI: 15 @@ -1263,10 +1299,10 @@ Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO with W5x00 using Ether This is terminal debug output when running [EtherMQTToverWebSocket](examples/Ethernet/EtherMQTToverWebSocket) on **TEENSY 4.0 with W5500 Ethernet** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start EtherMQTToverWebSocket on TEENSY 4.0 with W5x00 using Ethernet_Generic Library on SPI0/SPI -EthernetWebServer v2.1.2 -MQTTPubSubClient_Generic v1.2.0 +EthernetWebServer v2.3.0 +MQTTPubSubClient_Generic v1.2.1 [EWS] =========== USE_ETHERNET_GENERIC =========== [EWS] Default SPI pinout: [EWS] MOSI: 11 @@ -1296,9 +1332,9 @@ Subscribed to /mqttPubSub => Hello from TEENSY 4.0 with W5x00 using Ethernet_Gen This is terminal debug output when running [WiFiMQTT](examples/RP2040W/WiFiMQTT) on **RASPBERRY_PI_PICO_W** connecting to `public.cloud.shiftr.io` MQTT server. -``` +```cpp Start WiFiMQTT on RASPBERRY_PI_PICO_W -MQTTPubSubClient_Generic v1.2.0 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.77 SSID: HueNet1, Signal strength (RSSI):0 dBm @@ -1322,9 +1358,9 @@ MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W This is terminal debug output when running [WiFiMQTTSecure](examples/RP2040W/WiFiMQTTSecure) on **RASPBERRY_PI_PICO_W** connecting to `public.cloud.shiftr.io` MQTT server. -``` +```cpp Start WiFiMQTTSecure on RASPBERRY_PI_PICO_W -MQTTPubSubClient_Generic v1.2.0 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.77 SSID: HueNet1, Signal strength (RSSI):0 dBm @@ -1343,9 +1379,9 @@ Subscribed to /mqttPubSub => Hello from WiFiMQTTSecure on RASPBERRY_PI_PICO_W This is terminal debug output when running [WiFiMQTToverWebSocket](examples/RP2040W/WiFiMQTToverWebSocket) on **RASPBERRY_PI_PICO_W** connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start WiFiMQTToverWebSocket on RASPBERRY_PI_PICO_W -MQTTPubSubClient_Generic v1.2.0 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.77 SSID: HueNet1, Signal strength (RSSI):0 dBm @@ -1362,12 +1398,12 @@ Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO_W This is terminal debug output when running [WiFiNINA_MQTToverWebSocket](examples/WiFiNINA/WiFiNINA_MQTToverWebSocket) on **NANO_RP2040_CONNECT**, with WiFiNINA using `WiFiNINA_Generic` Library and `arduino-pico` core, connecting to `test.mosquitto.org` MQTT server. -``` +```cpp Start WiFiNINA_MQTToverWebSocket on NANO_RP2040_CONNECT with WiFiNINA using WiFiNINA_Generic Library -WiFiNINA_Generic v1.8.14-6 +WiFiNINA_Generic v1.8.15-1 WiFiWebServer v1.9.5 -WebSockets_Generic v2.16.0 -MQTTPubSubClient_Generic v1.2.0 +WebSockets_Generic v2.16.1 +MQTTPubSubClient_Generic v1.2.1 Please upgrade the firmware Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.105 @@ -1385,11 +1421,11 @@ Subscribed to /mqttPubSub => Hello from NANO_RP2040_CONNECT with WiFiNINA using This is terminal debug output when running [WiFiNINA_MQTToverWebSocketSecure](examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure) on **NANO_RP2040_CONNECT**, with WiFiNINA using `WiFiNINA_Generic` Library and `Arduino mbed_nano` core, connecting to `public.cloud.shiftr.io` Secured MQTT server. -``` +```cpp Start WiFiNINA_MQTToverWebSocketSecure on Nano RP2040 Connect -WiFiNINA_Generic v1.8.14-6 -WebSockets_Generic v2.16.0 -MQTTPubSubClient_Generic v1.2.0 +WiFiNINA_Generic v1.8.15-1 +WebSockets_Generic v2.16.1 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.105 SSID: HueNet1, Signal strength (RSSI):-30 dBm @@ -1406,11 +1442,11 @@ Subscribed to /mqttPubSub => Hello from WiFiMQTToverWebSocketSecure on Nano RP20 This is terminal debug output when running [WiFiNINA_MQTToverWebSocketSecure](examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure) on **SAMD_NANO_33_IOT**, with WiFiNINA using `WiFiNINA_Generic` Library, connecting to `public.cloud.shiftr.io` Secured MQTT server. -``` +```cpp Start WiFiNINA_MQTToverWebSocketSecure on Nano RP2040 Connect -WiFiNINA_Generic v1.8.14-6 -WebSockets_Generic v2.16.0 -MQTTPubSubClient_Generic v1.2.0 +WiFiNINA_Generic v1.8.15-1 +WebSockets_Generic v2.16.1 +MQTTPubSubClient_Generic v1.2.1 Connecting to SSID: HueNet1 You're connected to the network, IP = 192.168.2.105 SSID: HueNet1, Signal strength (RSSI):-30 dBm @@ -1484,6 +1520,8 @@ Submit issues to: [MQTTPubSubClient_Generic issues](https://github.com/khoih-pro 18. Use new `waitForLink()` function of QNEthernet 19. Add support to `RP2040W` using `MQTT`, `Secured MQTT` or `MQTT-over-WebSockets` with [arduino-pico core](https://github.com/earlephilhower/arduino-pico) 20. Add support to `Nano_RP2040_Connect`, `Nano_33_IoT`, using `MQTT`, `Secured MQTT`, `MQTT-over-WebSockets` or `Secured MQTT-over-WebSockets` with [`WiFiNINA_Generic` library](https://github.com/khoih-prog/WiFiNINA_Generic) library +21. Add support to `MKRWiFi1010`, etc., using `MQTT`, `MQTT-over-WebSockets` with new [`WiFi101_Generic` library](https://github.com/khoih-prog/WiFi101_Generic) library + --- --- diff --git a/changelog.md b/changelog.md index b13c415..772afdd 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,4 @@ -# MQTTPubSubClient_Generic client for ESP8266, ESP32, etc. +# MQTTPubSubClient_Generic Library (MQTT Client for ESP8266, ESP32, etc.) [![arduino-library-badge](https://www.ardu-badge.com/badge/MQTTPubSubClient_Generic.svg?)](https://www.ardu-badge.com/MQTTPubSubClient_Generic) [![GitHub release](https://img.shields.io/github/release/khoih-prog/MQTTPubSubClient_Generic.svg)](https://github.com/khoih-prog/MQTTPubSubClient_Generic/releases) @@ -6,12 +6,19 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/MQTTPubSubClient_Generic.svg)](http://github.com/khoih-prog/MQTTPubSubClient_Generic/issues) + +Donate to my libraries using BuyMeACoffee + + + + --- --- ## Table of Contents * [Changelog](#changelog) + * [Releases v1.2.1](#releases-v121) * [Releases v1.2.0](#releases-v120) * [Releases v1.1.0](#releases-v110) * [Releases v1.0.1](#releases-v101) @@ -22,6 +29,11 @@ ## Changelog +### Releases v1.2.1 + +1. Add support to `MKRWiFi1010`, etc., using `MQTT`, `MQTT-over-WebSockets` with new [`WiFi101_Generic` library](https://github.com/khoih-prog/WiFi101_Generic) library +2. Add [WiFi101 examples](https://github.com/khoih-prog/MQTTPubSubClient_Generic/tree/main/examples/WiFi101) + ### Releases v1.2.0 1. Add support to `Nano_RP2040_Connect`, `Nano_33_IoT`, using `MQTT`, `Secured MQTT`, `MQTT-over-WebSockets` or `Secured MQTT-over-WebSockets` with [`WiFiNINA_Generic` library](https://github.com/khoih-prog/WiFiNINA_Generic) library diff --git a/examples/Ethernet/EtherMQTT/EtherMQTT.ino b/examples/Ethernet/EtherMQTT/EtherMQTT.ino index b6328a4..8385437 100644 --- a/examples/Ethernet/EtherMQTT/EtherMQTT.ino +++ b/examples/Ethernet/EtherMQTT/EtherMQTT.ino @@ -1,15 +1,15 @@ /**************************************************************************************************************************** EtherMQTT.ino - + MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ @@ -39,11 +39,11 @@ void initEthernet() #elif USE_NATIVE_ETHERNET ET_LOGWARN(F("======== USE_NATIVE_ETHERNET ========")); #elif USE_ETHERNET_GENERIC - ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ===========")); + ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ===========")); #elif USE_ETHERNET_ESP8266 ET_LOGWARN(F("=========== USE_ETHERNET_ESP8266 ===========")); #elif USE_ETHERNET_ENC - ET_LOGWARN(F("=========== USE_ETHERNET_ENC ===========")); + ET_LOGWARN(F("=========== USE_ETHERNET_ENC ===========")); #else ET_LOGWARN(F("=========================")); #endif @@ -51,14 +51,14 @@ void initEthernet() #if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7) #if (USING_SPI2) - #if defined(CUR_PIN_MISO) - ET_LOGWARN(F("Default SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); - ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); - ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); - ET_LOGWARN1(F("SS:"), CUR_PIN_SS); - ET_LOGWARN(F("=========================")); - #endif +#if defined(CUR_PIN_MISO) + ET_LOGWARN(F("Default SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); + ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); + ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); + ET_LOGWARN1(F("SS:"), CUR_PIN_SS); + ET_LOGWARN(F("=========================")); +#endif #else ET_LOGWARN(F("Default SPI pinout:")); ET_LOGWARN1(F("MOSI:"), MOSI); @@ -70,28 +70,28 @@ void initEthernet() #if defined(ESP8266) // For ESP8266, change for other boards if necessary - #ifndef USE_THIS_SS_PIN - #define USE_THIS_SS_PIN D2 // For ESP8266 - #endif +#ifndef USE_THIS_SS_PIN +#define USE_THIS_SS_PIN D2 // For ESP8266 +#endif ET_LOGWARN1(F("ESP8266 setCsPin:"), USE_THIS_SS_PIN); - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) - // For ESP8266 - // Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8 - // EthernetGeneric X X X X X 0 - // Ethernet_ESP8266 0 0 0 0 0 0 - // D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs - // Must use library patch for Ethernet, EthernetLarge libraries - Ethernet.init (USE_THIS_SS_PIN); - - #elif USE_CUSTOM_ETHERNET - - // You have to add initialization for your Custom Ethernet here - // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough - Ethernet.init(USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) + // For ESP8266 + // Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8 + // EthernetGeneric X X X X X 0 + // Ethernet_ESP8266 0 0 0 0 0 0 + // D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs + // Must use library patch for Ethernet, EthernetLarge libraries + Ethernet.init (USE_THIS_SS_PIN); + +#elif USE_CUSTOM_ETHERNET + + // You have to add initialization for your Custom Ethernet here + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough + Ethernet.init(USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #elif defined(ESP32) @@ -103,84 +103,84 @@ void initEthernet() //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet - #ifndef USE_THIS_SS_PIN - #define USE_THIS_SS_PIN 5 //22 // For ESP32 - #endif +#ifndef USE_THIS_SS_PIN +#define USE_THIS_SS_PIN 5 //22 // For ESP32 +#endif ET_LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN); // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) - // Must use library patch for Ethernet, EthernetLarge libraries - // ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge - // ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3 - - //Ethernet.setCsPin (USE_THIS_SS_PIN); - Ethernet.init (USE_THIS_SS_PIN); - - #elif USE_CUSTOM_ETHERNET - - // You have to add initialization for your Custom Ethernet here - // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough - Ethernet.init(USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) + // Must use library patch for Ethernet, EthernetLarge libraries + // ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge + // ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3 + + //Ethernet.setCsPin (USE_THIS_SS_PIN); + Ethernet.init (USE_THIS_SS_PIN); + +#elif USE_CUSTOM_ETHERNET + + // You have to add initialization for your Custom Ethernet here + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough + Ethernet.init(USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #elif ETHERNET_USE_RPIPICO pinMode(USE_THIS_SS_PIN, OUTPUT); digitalWrite(USE_THIS_SS_PIN, HIGH); - + // ETHERNET_USE_RPIPICO, use default SS = 5 or 17 - #ifndef USE_THIS_SS_PIN - #if defined(ARDUINO_ARCH_MBED) - #define USE_THIS_SS_PIN 5 // For Arduino Mbed core - #else - #define USE_THIS_SS_PIN 17 // For E.Philhower core - #endif - #endif +#ifndef USE_THIS_SS_PIN +#if defined(ARDUINO_ARCH_MBED) +#define USE_THIS_SS_PIN 5 // For Arduino Mbed core +#else +#define USE_THIS_SS_PIN 17 // For E.Philhower core +#endif +#endif ET_LOGWARN1(F("RPIPICO setCsPin:"), USE_THIS_SS_PIN); // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) - // Must use library patch for Ethernet, EthernetLarge libraries - // For RPI Pico using Arduino Mbed RP2040 core - // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 - // For RPI Pico using E. Philhower RP2040 core - // SCK: GPIO18, MOSI: GPIO19, MISO: GPIO16, SS/CS: GPIO17 - // Default pin 5/17 to SS/CS - - //Ethernet.setCsPin (USE_THIS_SS_PIN); - Ethernet.init (USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) + // Must use library patch for Ethernet, EthernetLarge libraries + // For RPI Pico using Arduino Mbed RP2040 core + // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 + // For RPI Pico using E. Philhower RP2040 core + // SCK: GPIO18, MOSI: GPIO19, MISO: GPIO16, SS/CS: GPIO17 + // Default pin 5/17 to SS/CS + + //Ethernet.setCsPin (USE_THIS_SS_PIN); + Ethernet.init (USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #else //defined(ESP8266) // unknown board, do nothing, use default SS = 10 - #ifndef USE_THIS_SS_PIN - #define USE_THIS_SS_PIN 10 // For other boards - #endif +#ifndef USE_THIS_SS_PIN +#define USE_THIS_SS_PIN 10 // For other boards +#endif - #if defined(BOARD_NAME) - ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN); - #else - ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN); - #endif +#if defined(BOARD_NAME) + ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN); +#else + ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN); +#endif // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET ) - // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries - - Ethernet.init (USE_THIS_SS_PIN); - - #elif USE_CUSTOM_ETHERNET - - // You have to add initialization for your Custom Ethernet here - // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough - Ethernet.init(USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET ) + // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries + + Ethernet.init (USE_THIS_SS_PIN); + +#elif USE_CUSTOM_ETHERNET + + // You have to add initialization for your Custom Ethernet here + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough + Ethernet.init(USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #endif // defined(ESP8266) @@ -196,79 +196,86 @@ void initEthernet() #if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7) ET_LOGWARN(F("=========================")); - - #if defined( ESP32 ) - // Just info to know how to connect correctly - // To change for other SPI - ET_LOGWARN(F("Currently Used SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), PIN_MOSI); - ET_LOGWARN1(F("MISO:"), PIN_MISO); - ET_LOGWARN1(F("SCK:"), PIN_SCK); - ET_LOGWARN1(F("SS:"), PIN_SS); - #else - #if defined(CUR_PIN_MISO) - ET_LOGWARN(F("Currently Used SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); - ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); - ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); - ET_LOGWARN1(F("SS:"), CUR_PIN_SS); - #else - ET_LOGWARN(F("Currently Used SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), MOSI); - ET_LOGWARN1(F("MISO:"), MISO); - ET_LOGWARN1(F("SCK:"), SCK); - ET_LOGWARN1(F("SS:"), SS); - #endif - #endif - + +#if defined( ESP32 ) + // Just info to know how to connect correctly + // To change for other SPI + ET_LOGWARN(F("Currently Used SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), PIN_MOSI); + ET_LOGWARN1(F("MISO:"), PIN_MISO); + ET_LOGWARN1(F("SCK:"), PIN_SCK); + ET_LOGWARN1(F("SS:"), PIN_SS); +#else +#if defined(CUR_PIN_MISO) + ET_LOGWARN(F("Currently Used SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); + ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); + ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); + ET_LOGWARN1(F("SS:"), CUR_PIN_SS); +#else + ET_LOGWARN(F("Currently Used SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), MOSI); + ET_LOGWARN1(F("MISO:"), MISO); + ET_LOGWARN1(F("SCK:"), SCK); + ET_LOGWARN1(F("SS:"), SS); +#endif +#endif + ET_LOGWARN(F("=========================")); #elif (USE_ETHERNET_PORTENTA_H7) - if (Ethernet.hardwareStatus() == EthernetNoHardware) + + if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("No Ethernet found. Stay here forever"); - - while (true) + + while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } - - if (Ethernet.linkStatus() == LinkOFF) + + if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Not connected Ethernet cable"); } + #endif Serial.print(F("Using mac index = ")); Serial.println(macIndex); Serial.print(F("Connected! IP address: ")); - Serial.println(Ethernet.localIP()); + Serial.println(Ethernet.localIP()); } void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.println(); Serial.println(); + Serial.println(); + Serial.println(); - Serial.print("\nStart EtherMQTT on "); Serial.print(BOARD_NAME); - Serial.print(" with "); Serial.println(SHIELD_TYPE); + Serial.print("\nStart EtherMQTT on "); + Serial.print(BOARD_NAME); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); Serial.println(ETHERNET_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); initEthernet(); - Serial.print("Connecting to host "); Serial.println(MQTT_SERVER); - - while (!client.connect(MQTT_SERVER, MQTT_PORT)) + Serial.print("Connecting to host "); + Serial.println(MQTT_SERVER); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -288,7 +295,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -296,23 +303,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 10000) + + if (millis() > prev_ms + 10000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/Ethernet/EtherMQTT/defines.h b/examples/Ethernet/EtherMQTT/defines.h index ff0005e..032e0f7 100644 --- a/examples/Ethernet/EtherMQTT/defines.h +++ b/examples/Ethernet/EtherMQTT/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -38,7 +38,7 @@ #define ETHERNET_USE_PORTENTA_H7 true #define USE_ETHERNET_PORTENTA_H7 true - + #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -46,19 +46,19 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(ETHERNET_USE_SAMD) - #undef ETHERNET_USE_SAMD - #endif - #define ETHERNET_USE_SAMD true +#if defined(ETHERNET_USE_SAMD) + #undef ETHERNET_USE_SAMD +#endif +#define ETHERNET_USE_SAMD true #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(ETHERNET_USE_NRF528XX) - #undef ETHERNET_USE_NRF528XX - #endif - #define ETHERNET_USE_NRF528XX true +#if defined(ETHERNET_USE_NRF528XX) + #undef ETHERNET_USE_NRF528XX +#endif +#define ETHERNET_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -79,7 +79,7 @@ // For SAMD // Default pin 10 to SS/CS #define USE_THIS_SS_PIN 10 - + #if ( defined(ARDUINO_SAMD_ZERO) && !defined(SEEED_XIAO_M0) ) #define BOARD_TYPE "SAMD Zero" #elif defined(ARDUINO_SAMD_MKR1000) @@ -225,7 +225,7 @@ #elif ( defined(CORE_TEENSY) ) // Default pin 10 to SS/CS #define USE_THIS_SS_PIN 10 - + #if defined(__IMXRT1062__) // For Teensy 4.1/4.0 #if defined(ARDUINO_TEENSY41) @@ -236,7 +236,7 @@ #define BOARD_TYPE "TEENSY 4.0" #else #define BOARD_TYPE "TEENSY 4.x" - #endif + #endif #elif defined(__MK66FX1M0__) #define BOARD_TYPE "Teensy 3.6" #elif defined(__MK64FX512__) @@ -268,23 +268,23 @@ #warning Use ESP32 architecture #define ETHERNET_USE_ESP32 #define BOARD_TYPE ARDUINO_BOARD - + #define W5500_RST_PORT 21 #elif ETHERNET_USE_RPIPICO - + // Default pin 5 (in Mbed) or 17 to SS/CS #if defined(ARDUINO_ARCH_MBED) // For RPI Pico using Arduino Mbed RP2040 core // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 - + #define USE_THIS_SS_PIN 17 #if defined(BOARD_NAME) #undef BOARD_NAME #endif - #if defined(ARDUINO_RASPBERRY_PI_PICO) + #if defined(ARDUINO_RASPBERRY_PI_PICO) #define BOARD_TYPE "MBED RASPBERRY_PI_PICO" #elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) #define BOARD_TYPE "MBED DAFRUIT_FEATHER_RP2040" @@ -293,11 +293,11 @@ #else #define BOARD_TYPE "MBED Unknown RP2040" #endif - + #else - + //#define USING_SPI2 true - + // For RPI Pico using E. Philhower RP2040 core #if (USING_SPI2) // SCK: GPIO14, MOSI: GPIO15, MISO: GPIO12, SS/CS: GPIO13 for SPI1 @@ -308,7 +308,7 @@ #endif #endif - + #define SS_PIN_DEFAULT USE_THIS_SS_PIN // For RPI Pico @@ -321,7 +321,7 @@ // Reduce size for Mega #define SENDCONTENT_P_BUFFER_SZ 512 - + #define BOARD_TYPE "AVR Mega" #endif @@ -341,116 +341,116 @@ // In order to USE_ETHERNET_ESP8266 #if ( !defined(USE_UIP_ETHERNET) || !USE_UIP_ETHERNET ) - // To override the default CS/SS pin. Don't use unless you know exactly which pin to use - // You can define here or customize for each board at same place with BOARD_TYPE - // Check @ defined(SEEED_XIAO_M0) - //#define USE_THIS_SS_PIN 22 //21 //5 //4 //2 //15 - - // Only one if the following to be true - #define USE_ETHERNET_GENERIC true - #define USE_ETHERNET_ESP8266 false - #define USE_ETHERNET_ENC false - #define USE_CUSTOM_ETHERNET false - - //////////////////////////// - - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ESP8266 || USE_ETHERNET_ENC || \ +// To override the default CS/SS pin. Don't use unless you know exactly which pin to use +// You can define here or customize for each board at same place with BOARD_TYPE +// Check @ defined(SEEED_XIAO_M0) +//#define USE_THIS_SS_PIN 22 //21 //5 //4 //2 //15 + +// Only one if the following to be true +#define USE_ETHERNET_GENERIC true +#define USE_ETHERNET_ESP8266 false +#define USE_ETHERNET_ENC false +#define USE_CUSTOM_ETHERNET false + +//////////////////////////// + +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ESP8266 || USE_ETHERNET_ENC || \ USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7 ) - #ifdef USE_CUSTOM_ETHERNET - #undef USE_CUSTOM_ETHERNET - #endif - #define USE_CUSTOM_ETHERNET false - #endif +#ifdef USE_CUSTOM_ETHERNET + #undef USE_CUSTOM_ETHERNET +#endif +#define USE_CUSTOM_ETHERNET false +#endif - #if USE_ETHERNET_PORTENTA_H7 - #include - #include - #warning Using Portenta_Ethernet lib for Portenta_H7. - #define SHIELD_TYPE "Ethernet using Portenta_Ethernet Library" - #elif USE_NATIVE_ETHERNET - #include "NativeEthernet.h" - #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "Custom Ethernet using Teensy 4.1 NativeEthernet Library" - #elif USE_ETHERNET_GENERIC - #if (ESP32) - #include - - // Optional SPI2 - //#define USING_SPI2 true - - #if USING_SPI2 - #define PIN_MISO HSPI_IOMUX_PIN_NUM_MISO - #define PIN_MOSI HSPI_IOMUX_PIN_NUM_MOSI - #define PIN_SCK HSPI_IOMUX_PIN_NUM_CLK - #define PIN_SS HSPI_IOMUX_PIN_NUM_CS - - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI2" - - #else - - #define PIN_MISO MISO - #define PIN_MOSI MOSI - #define PIN_SCK SCK - #define PIN_SS SS - - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI" - - #endif +#if USE_ETHERNET_PORTENTA_H7 + #include + #include + #warning Using Portenta_Ethernet lib for Portenta_H7. + #define SHIELD_TYPE "Ethernet using Portenta_Ethernet Library" +#elif USE_NATIVE_ETHERNET + #include "NativeEthernet.h" + #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error + #define SHIELD_TYPE "Custom Ethernet using Teensy 4.1 NativeEthernet Library" +#elif USE_ETHERNET_GENERIC + #if (ESP32) + #include + + // Optional SPI2 + //#define USING_SPI2 true + + #if USING_SPI2 + #define PIN_MISO HSPI_IOMUX_PIN_NUM_MISO + #define PIN_MOSI HSPI_IOMUX_PIN_NUM_MOSI + #define PIN_SCK HSPI_IOMUX_PIN_NUM_CLK + #define PIN_SS HSPI_IOMUX_PIN_NUM_CS + + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI2" #else - #if USING_SPI2 - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI1" - #else - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI0/SPI" - #endif + + #define PIN_MISO MISO + #define PIN_MOSI MOSI + #define PIN_SCK SCK + #define PIN_SS SS + + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI" + #endif - #define ETHERNET_LARGE_BUFFERS - - #define _ETG_LOGLEVEL_ 1 - - #include "Ethernet_Generic.h" - #warning Using Ethernet_Generic lib - - #elif USE_ETHERNET_ESP8266 - #include "Ethernet_ESP8266.h" - #warning Using Ethernet_ESP8266 lib - #define SHIELD_TYPE "W5x00 using Ethernet_ESP8266 Library" - #elif USE_ETHERNET_ENC - #include "EthernetENC.h" - #warning Using EthernetENC lib - #define SHIELD_TYPE "ENC28J60 using EthernetENC Library" - #elif USE_CUSTOM_ETHERNET - //#include "Ethernet_XYZ.h" - #include "EthernetENC.h" - #warning Using Custom Ethernet library. You must include a library and initialize. - #define SHIELD_TYPE "Custom Ethernet using Ethernet_XYZ Library" #else - #ifdef USE_ETHERNET_GENERIC - #undef USE_ETHERNET_GENERIC + #if USING_SPI2 + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI1" + #else + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI0/SPI" #endif - #define USE_ETHERNET_GENERIC true - #include "Ethernet_Generic.h" - #warning Using default Ethernet_Generic lib - #define SHIELD_TYPE "W5x00 using default Ethernet_Generic Library" #endif - - // Ethernet_Shield_W5200, EtherCard, EtherSia not supported - // Select just 1 of the following #include if uncomment #define USE_CUSTOM_ETHERNET - // Otherwise, standard Ethernet library will be used for W5x00 - - //////////////////////////// - + + #define ETHERNET_LARGE_BUFFERS + + #define _ETG_LOGLEVEL_ 1 + + #include "Ethernet_Generic.h" + #warning Using Ethernet_Generic lib + +#elif USE_ETHERNET_ESP8266 + #include "Ethernet_ESP8266.h" + #warning Using Ethernet_ESP8266 lib + #define SHIELD_TYPE "W5x00 using Ethernet_ESP8266 Library" +#elif USE_ETHERNET_ENC + #include "EthernetENC.h" + #warning Using EthernetENC lib + #define SHIELD_TYPE "ENC28J60 using EthernetENC Library" +#elif USE_CUSTOM_ETHERNET + //#include "Ethernet_XYZ.h" + #include "EthernetENC.h" + #warning Using Custom Ethernet library. You must include a library and initialize. + #define SHIELD_TYPE "Custom Ethernet using Ethernet_XYZ Library" +#else + #ifdef USE_ETHERNET_GENERIC + #undef USE_ETHERNET_GENERIC + #endif + #define USE_ETHERNET_GENERIC true + #include "Ethernet_Generic.h" + #warning Using default Ethernet_Generic lib + #define SHIELD_TYPE "W5x00 using default Ethernet_Generic Library" +#endif + +// Ethernet_Shield_W5200, EtherCard, EtherSia not supported +// Select just 1 of the following #include if uncomment #define USE_CUSTOM_ETHERNET +// Otherwise, standard Ethernet library will be used for W5x00 + +//////////////////////////// + #elif USE_UIP_ETHERNET - #include "UIPEthernet.h" - #warning Using UIPEthernet library - #define SHIELD_TYPE "ENC28J60 using UIPEthernet Library" +#include "UIPEthernet.h" +#warning Using UIPEthernet library +#define SHIELD_TYPE "ENC28J60 using UIPEthernet Library" #endif // #if !USE_UIP_ETHERNET #include #ifndef SHIELD_TYPE - #define SHIELD_TYPE "Unknown Ethernet shield/library" + #define SHIELD_TYPE "Unknown Ethernet shield/library" #endif // Enter a MAC address and IP address for your controller below. diff --git a/examples/Ethernet/EtherMQTToverWebSocket/EtherMQTToverWebSocket.ino b/examples/Ethernet/EtherMQTToverWebSocket/EtherMQTToverWebSocket.ino index 6392133..184ea44 100644 --- a/examples/Ethernet/EtherMQTToverWebSocket/EtherMQTToverWebSocket.ino +++ b/examples/Ethernet/EtherMQTToverWebSocket/EtherMQTToverWebSocket.ino @@ -1,19 +1,19 @@ /**************************************************************************************************************************** EtherMQTToverWebSocket.ino - + MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #include "defines.h" #define MQTTPUBSUBCLIENT_USE_WEBSOCKETS true @@ -43,11 +43,11 @@ void initEthernet() #elif USE_NATIVE_ETHERNET ET_LOGWARN(F("======== USE_NATIVE_ETHERNET ========")); #elif USE_ETHERNET_GENERIC - ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ===========")); + ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ===========")); #elif USE_ETHERNET_ESP8266 ET_LOGWARN(F("=========== USE_ETHERNET_ESP8266 ===========")); #elif USE_ETHERNET_ENC - ET_LOGWARN(F("=========== USE_ETHERNET_ENC ===========")); + ET_LOGWARN(F("=========== USE_ETHERNET_ENC ===========")); #else ET_LOGWARN(F("=========================")); #endif @@ -55,14 +55,14 @@ void initEthernet() #if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7) #if (USING_SPI2) - #if defined(CUR_PIN_MISO) - ET_LOGWARN(F("Default SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); - ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); - ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); - ET_LOGWARN1(F("SS:"), CUR_PIN_SS); - ET_LOGWARN(F("=========================")); - #endif +#if defined(CUR_PIN_MISO) + ET_LOGWARN(F("Default SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); + ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); + ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); + ET_LOGWARN1(F("SS:"), CUR_PIN_SS); + ET_LOGWARN(F("=========================")); +#endif #else ET_LOGWARN(F("Default SPI pinout:")); ET_LOGWARN1(F("MOSI:"), MOSI); @@ -74,28 +74,28 @@ void initEthernet() #if defined(ESP8266) // For ESP8266, change for other boards if necessary - #ifndef USE_THIS_SS_PIN - #define USE_THIS_SS_PIN D2 // For ESP8266 - #endif +#ifndef USE_THIS_SS_PIN +#define USE_THIS_SS_PIN D2 // For ESP8266 +#endif ET_LOGWARN1(F("ESP8266 setCsPin:"), USE_THIS_SS_PIN); - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) - // For ESP8266 - // Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8 - // EthernetGeneric X X X X X 0 - // Ethernet_ESP8266 0 0 0 0 0 0 - // D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs - // Must use library patch for Ethernet, EthernetLarge libraries - Ethernet.init (USE_THIS_SS_PIN); - - #elif USE_CUSTOM_ETHERNET - - // You have to add initialization for your Custom Ethernet here - // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough - Ethernet.init(USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) + // For ESP8266 + // Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8 + // EthernetGeneric X X X X X 0 + // Ethernet_ESP8266 0 0 0 0 0 0 + // D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs + // Must use library patch for Ethernet, EthernetLarge libraries + Ethernet.init (USE_THIS_SS_PIN); + +#elif USE_CUSTOM_ETHERNET + + // You have to add initialization for your Custom Ethernet here + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough + Ethernet.init(USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #elif defined(ESP32) @@ -107,84 +107,84 @@ void initEthernet() //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet - #ifndef USE_THIS_SS_PIN - #define USE_THIS_SS_PIN 5 //22 // For ESP32 - #endif +#ifndef USE_THIS_SS_PIN +#define USE_THIS_SS_PIN 5 //22 // For ESP32 +#endif ET_LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN); // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) - // Must use library patch for Ethernet, EthernetLarge libraries - // ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge - // ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3 - - //Ethernet.setCsPin (USE_THIS_SS_PIN); - Ethernet.init (USE_THIS_SS_PIN); - - #elif USE_CUSTOM_ETHERNET - - // You have to add initialization for your Custom Ethernet here - // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough - Ethernet.init(USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) + // Must use library patch for Ethernet, EthernetLarge libraries + // ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge + // ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3 + + //Ethernet.setCsPin (USE_THIS_SS_PIN); + Ethernet.init (USE_THIS_SS_PIN); + +#elif USE_CUSTOM_ETHERNET + + // You have to add initialization for your Custom Ethernet here + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough + Ethernet.init(USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #elif ETHERNET_USE_RPIPICO pinMode(USE_THIS_SS_PIN, OUTPUT); digitalWrite(USE_THIS_SS_PIN, HIGH); - + // ETHERNET_USE_RPIPICO, use default SS = 5 or 17 - #ifndef USE_THIS_SS_PIN - #if defined(ARDUINO_ARCH_MBED) - #define USE_THIS_SS_PIN 17 // For Arduino Mbed core - #else - #define USE_THIS_SS_PIN 17 // For E.Philhower core - #endif - #endif +#ifndef USE_THIS_SS_PIN +#if defined(ARDUINO_ARCH_MBED) +#define USE_THIS_SS_PIN 17 // For Arduino Mbed core +#else +#define USE_THIS_SS_PIN 17 // For E.Philhower core +#endif +#endif ET_LOGWARN1(F("RPIPICO setCsPin:"), USE_THIS_SS_PIN); // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) - // Must use library patch for Ethernet, EthernetLarge libraries - // For RPI Pico using Arduino Mbed RP2040 core - // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 - // For RPI Pico using E. Philhower RP2040 core - // SCK: GPIO18, MOSI: GPIO19, MISO: GPIO16, SS/CS: GPIO17 - // Default pin 5/17 to SS/CS - - //Ethernet.setCsPin (USE_THIS_SS_PIN); - Ethernet.init (USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) + // Must use library patch for Ethernet, EthernetLarge libraries + // For RPI Pico using Arduino Mbed RP2040 core + // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 + // For RPI Pico using E. Philhower RP2040 core + // SCK: GPIO18, MOSI: GPIO19, MISO: GPIO16, SS/CS: GPIO17 + // Default pin 5/17 to SS/CS + + //Ethernet.setCsPin (USE_THIS_SS_PIN); + Ethernet.init (USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #else //defined(ESP8266) // unknown board, do nothing, use default SS = 10 - #ifndef USE_THIS_SS_PIN - #define USE_THIS_SS_PIN 10 // For other boards - #endif +#ifndef USE_THIS_SS_PIN +#define USE_THIS_SS_PIN 10 // For other boards +#endif - #if defined(BOARD_NAME) - ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN); - #else - ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN); - #endif +#if defined(BOARD_NAME) + ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN); +#else + ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN); +#endif // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET ) - // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries - - Ethernet.init (USE_THIS_SS_PIN); - - #elif USE_CUSTOM_ETHERNET - - // You have to add initialization for your Custom Ethernet here - // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough - Ethernet.init(USE_THIS_SS_PIN); - - #endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET ) + // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries + + Ethernet.init (USE_THIS_SS_PIN); + +#elif USE_CUSTOM_ETHERNET + + // You have to add initialization for your Custom Ethernet here + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough + Ethernet.init(USE_THIS_SS_PIN); + +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #endif // defined(ESP8266) @@ -200,64 +200,69 @@ void initEthernet() #if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7) ET_LOGWARN(F("=========================")); - - #if defined( ESP32 ) - // Just info to know how to connect correctly - // To change for other SPI - ET_LOGWARN(F("Currently Used SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), PIN_MOSI); - ET_LOGWARN1(F("MISO:"), PIN_MISO); - ET_LOGWARN1(F("SCK:"), PIN_SCK); - ET_LOGWARN1(F("SS:"), PIN_SS); - #else - #if defined(CUR_PIN_MISO) - ET_LOGWARN(F("Currently Used SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); - ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); - ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); - ET_LOGWARN1(F("SS:"), CUR_PIN_SS); - #else - ET_LOGWARN(F("Currently Used SPI pinout:")); - ET_LOGWARN1(F("MOSI:"), MOSI); - ET_LOGWARN1(F("MISO:"), MISO); - ET_LOGWARN1(F("SCK:"), SCK); - ET_LOGWARN1(F("SS:"), SS); - #endif - #endif - + +#if defined( ESP32 ) + // Just info to know how to connect correctly + // To change for other SPI + ET_LOGWARN(F("Currently Used SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), PIN_MOSI); + ET_LOGWARN1(F("MISO:"), PIN_MISO); + ET_LOGWARN1(F("SCK:"), PIN_SCK); + ET_LOGWARN1(F("SS:"), PIN_SS); +#else +#if defined(CUR_PIN_MISO) + ET_LOGWARN(F("Currently Used SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); + ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); + ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); + ET_LOGWARN1(F("SS:"), CUR_PIN_SS); +#else + ET_LOGWARN(F("Currently Used SPI pinout:")); + ET_LOGWARN1(F("MOSI:"), MOSI); + ET_LOGWARN1(F("MISO:"), MISO); + ET_LOGWARN1(F("SCK:"), SCK); + ET_LOGWARN1(F("SS:"), SS); +#endif +#endif + ET_LOGWARN(F("=========================")); #elif (USE_ETHERNET_PORTENTA_H7) - if (Ethernet.hardwareStatus() == EthernetNoHardware) + + if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("No Ethernet found. Stay here forever"); - - while (true) + + while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } - - if (Ethernet.linkStatus() == LinkOFF) + + if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Not connected Ethernet cable"); } + #endif Serial.print(F("Using mac index = ")); Serial.println(macIndex); Serial.print(F("Connected! IP address: ")); - Serial.println(Ethernet.localIP()); + Serial.println(Ethernet.localIP()); } void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print("\nStart EtherMQTToverWebSocket on "); Serial.print(BOARD_NAME); - Serial.print(" with "); Serial.println(SHIELD_TYPE); + Serial.print("\nStart EtherMQTToverWebSocket on "); + Serial.print(BOARD_NAME); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); Serial.println(ETHERNET_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); @@ -268,7 +273,7 @@ void setup() Serial.println(WS_SERVER); client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required - + client.setReconnectInterval(2000); // initialize mqtt client @@ -288,7 +293,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -296,23 +301,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/Ethernet/EtherMQTToverWebSocket/defines.h b/examples/Ethernet/EtherMQTToverWebSocket/defines.h index 23070b6..924edf5 100644 --- a/examples/Ethernet/EtherMQTToverWebSocket/defines.h +++ b/examples/Ethernet/EtherMQTToverWebSocket/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -40,7 +40,7 @@ #define ETHERNET_USE_PORTENTA_H7 true #define USE_ETHERNET_PORTENTA_H7 true - + #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -48,19 +48,19 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(ETHERNET_USE_SAMD) - #undef ETHERNET_USE_SAMD - #endif - #define ETHERNET_USE_SAMD true +#if defined(ETHERNET_USE_SAMD) + #undef ETHERNET_USE_SAMD +#endif +#define ETHERNET_USE_SAMD true #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(ETHERNET_USE_NRF528XX) - #undef ETHERNET_USE_NRF528XX - #endif - #define ETHERNET_USE_NRF528XX true +#if defined(ETHERNET_USE_NRF528XX) + #undef ETHERNET_USE_NRF528XX +#endif +#define ETHERNET_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -81,7 +81,7 @@ // For SAMD // Default pin 10 to SS/CS #define USE_THIS_SS_PIN 10 - + #if ( defined(ARDUINO_SAMD_ZERO) && !defined(SEEED_XIAO_M0) ) #define BOARD_TYPE "SAMD Zero" #elif defined(ARDUINO_SAMD_MKR1000) @@ -227,7 +227,7 @@ #elif ( defined(CORE_TEENSY) ) // Default pin 10 to SS/CS #define USE_THIS_SS_PIN 10 - + #if defined(__IMXRT1062__) // For Teensy 4.1/4.0 #if defined(ARDUINO_TEENSY41) @@ -238,7 +238,7 @@ #define BOARD_TYPE "TEENSY 4.0" #else #define BOARD_TYPE "TEENSY 4.x" - #endif + #endif #elif defined(__MK66FX1M0__) #define BOARD_TYPE "Teensy 3.6" #elif defined(__MK64FX512__) @@ -270,23 +270,23 @@ #warning Use ESP32 architecture #define ETHERNET_USE_ESP32 #define BOARD_TYPE ARDUINO_BOARD - + #define W5500_RST_PORT 21 #elif ETHERNET_USE_RPIPICO - + // Default pin 5 (in Mbed) or 17 to SS/CS #if defined(ARDUINO_ARCH_MBED) // For RPI Pico using Arduino Mbed RP2040 core // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 - + #define USE_THIS_SS_PIN 17 #if defined(BOARD_NAME) #undef BOARD_NAME #endif - #if defined(ARDUINO_RASPBERRY_PI_PICO) + #if defined(ARDUINO_RASPBERRY_PI_PICO) #define BOARD_TYPE "MBED RASPBERRY_PI_PICO" #elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) #define BOARD_TYPE "MBED DAFRUIT_FEATHER_RP2040" @@ -295,11 +295,11 @@ #else #define BOARD_TYPE "MBED Unknown RP2040" #endif - + #else #define USING_SPI2 true - + // For RPI Pico using E. Philhower RP2040 core #if (USING_SPI2) // SCK: GPIO14, MOSI: GPIO15, MISO: GPIO12, SS/CS: GPIO13 for SPI1 @@ -310,7 +310,7 @@ #endif #endif - + #define SS_PIN_DEFAULT USE_THIS_SS_PIN // For RPI Pico @@ -323,7 +323,7 @@ // Reduce size for Mega #define SENDCONTENT_P_BUFFER_SZ 512 - + #define BOARD_TYPE "AVR Mega" #endif @@ -343,116 +343,116 @@ // In order to USE_ETHERNET_ESP8266 #if ( !defined(USE_UIP_ETHERNET) || !USE_UIP_ETHERNET ) - // To override the default CS/SS pin. Don't use unless you know exactly which pin to use - // You can define here or customize for each board at same place with BOARD_TYPE - // Check @ defined(SEEED_XIAO_M0) - //#define USE_THIS_SS_PIN 22 //21 //5 //4 //2 //15 - - // Only one if the following to be true - #define USE_ETHERNET_GENERIC true - #define USE_ETHERNET_ESP8266 false - #define USE_ETHERNET_ENC false - #define USE_CUSTOM_ETHERNET false - - //////////////////////////// - - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ESP8266 || USE_ETHERNET_ENC || \ +// To override the default CS/SS pin. Don't use unless you know exactly which pin to use +// You can define here or customize for each board at same place with BOARD_TYPE +// Check @ defined(SEEED_XIAO_M0) +//#define USE_THIS_SS_PIN 22 //21 //5 //4 //2 //15 + +// Only one if the following to be true +#define USE_ETHERNET_GENERIC true +#define USE_ETHERNET_ESP8266 false +#define USE_ETHERNET_ENC false +#define USE_CUSTOM_ETHERNET false + +//////////////////////////// + +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ESP8266 || USE_ETHERNET_ENC || \ USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7 ) - #ifdef USE_CUSTOM_ETHERNET - #undef USE_CUSTOM_ETHERNET - #endif - #define USE_CUSTOM_ETHERNET false - #endif +#ifdef USE_CUSTOM_ETHERNET + #undef USE_CUSTOM_ETHERNET +#endif +#define USE_CUSTOM_ETHERNET false +#endif - #if USE_ETHERNET_PORTENTA_H7 - #include - #include - #warning Using Portenta_Ethernet lib for Portenta_H7. - #define SHIELD_TYPE "Ethernet using Portenta_Ethernet Library" - #elif USE_NATIVE_ETHERNET - #include "NativeEthernet.h" - #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "Custom Ethernet using Teensy 4.1 NativeEthernet Library" - #elif USE_ETHERNET_GENERIC - #if (ESP32) - #include - - // Optional SPI2 - //#define USING_SPI2 true - - #if USING_SPI2 - #define PIN_MISO HSPI_IOMUX_PIN_NUM_MISO - #define PIN_MOSI HSPI_IOMUX_PIN_NUM_MOSI - #define PIN_SCK HSPI_IOMUX_PIN_NUM_CLK - #define PIN_SS HSPI_IOMUX_PIN_NUM_CS - - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI2" - - #else - - #define PIN_MISO MISO - #define PIN_MOSI MOSI - #define PIN_SCK SCK - #define PIN_SS SS - - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI" - - #endif +#if USE_ETHERNET_PORTENTA_H7 + #include + #include + #warning Using Portenta_Ethernet lib for Portenta_H7. + #define SHIELD_TYPE "Ethernet using Portenta_Ethernet Library" +#elif USE_NATIVE_ETHERNET + #include "NativeEthernet.h" + #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error + #define SHIELD_TYPE "Custom Ethernet using Teensy 4.1 NativeEthernet Library" +#elif USE_ETHERNET_GENERIC + #if (ESP32) + #include + + // Optional SPI2 + //#define USING_SPI2 true + + #if USING_SPI2 + #define PIN_MISO HSPI_IOMUX_PIN_NUM_MISO + #define PIN_MOSI HSPI_IOMUX_PIN_NUM_MOSI + #define PIN_SCK HSPI_IOMUX_PIN_NUM_CLK + #define PIN_SS HSPI_IOMUX_PIN_NUM_CS + + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI2" #else - #if USING_SPI2 - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI1" - #else - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI0/SPI" - #endif + + #define PIN_MISO MISO + #define PIN_MOSI MOSI + #define PIN_SCK SCK + #define PIN_SS SS + + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI" + #endif - #define ETHERNET_LARGE_BUFFERS - - #define _ETG_LOGLEVEL_ 1 - - #include "Ethernet_Generic.h" - #warning Using Ethernet_Generic lib - - #elif USE_ETHERNET_ESP8266 - #include "Ethernet_ESP8266.h" - #warning Using Ethernet_ESP8266 lib - #define SHIELD_TYPE "W5x00 using Ethernet_ESP8266 Library" - #elif USE_ETHERNET_ENC - #include "EthernetENC.h" - #warning Using EthernetENC lib - #define SHIELD_TYPE "ENC28J60 using EthernetENC Library" - #elif USE_CUSTOM_ETHERNET - //#include "Ethernet_XYZ.h" - #include "EthernetENC.h" - #warning Using Custom Ethernet library. You must include a library and initialize. - #define SHIELD_TYPE "Custom Ethernet using Ethernet_XYZ Library" #else - #ifdef USE_ETHERNET_GENERIC - #undef USE_ETHERNET_GENERIC + #if USING_SPI2 + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI1" + #else + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library on SPI0/SPI" #endif - #define USE_ETHERNET_GENERIC true - #include "Ethernet_Generic.h" - #warning Using default Ethernet_Generic lib - #define SHIELD_TYPE "W5x00 using default Ethernet_Generic Library" #endif - - // Ethernet_Shield_W5200, EtherCard, EtherSia not supported - // Select just 1 of the following #include if uncomment #define USE_CUSTOM_ETHERNET - // Otherwise, standard Ethernet library will be used for W5x00 - - //////////////////////////// - + + #define ETHERNET_LARGE_BUFFERS + + #define _ETG_LOGLEVEL_ 1 + + #include "Ethernet_Generic.h" + #warning Using Ethernet_Generic lib + +#elif USE_ETHERNET_ESP8266 + #include "Ethernet_ESP8266.h" + #warning Using Ethernet_ESP8266 lib + #define SHIELD_TYPE "W5x00 using Ethernet_ESP8266 Library" +#elif USE_ETHERNET_ENC + #include "EthernetENC.h" + #warning Using EthernetENC lib + #define SHIELD_TYPE "ENC28J60 using EthernetENC Library" +#elif USE_CUSTOM_ETHERNET + //#include "Ethernet_XYZ.h" + #include "EthernetENC.h" + #warning Using Custom Ethernet library. You must include a library and initialize. + #define SHIELD_TYPE "Custom Ethernet using Ethernet_XYZ Library" +#else + #ifdef USE_ETHERNET_GENERIC + #undef USE_ETHERNET_GENERIC + #endif + #define USE_ETHERNET_GENERIC true + #include "Ethernet_Generic.h" + #warning Using default Ethernet_Generic lib + #define SHIELD_TYPE "W5x00 using default Ethernet_Generic Library" +#endif + +// Ethernet_Shield_W5200, EtherCard, EtherSia not supported +// Select just 1 of the following #include if uncomment #define USE_CUSTOM_ETHERNET +// Otherwise, standard Ethernet library will be used for W5x00 + +//////////////////////////// + #elif USE_UIP_ETHERNET - #include "UIPEthernet.h" - #warning Using UIPEthernet library - #define SHIELD_TYPE "ENC28J60 using UIPEthernet Library" +#include "UIPEthernet.h" +#warning Using UIPEthernet library +#define SHIELD_TYPE "ENC28J60 using UIPEthernet Library" #endif // #if !USE_UIP_ETHERNET #include #ifndef SHIELD_TYPE - #define SHIELD_TYPE "Unknown Ethernet shield/library" + #define SHIELD_TYPE "Unknown Ethernet shield/library" #endif // Enter a MAC address and IP address for your controller below. diff --git a/examples/QNEthernet/MQTT_QNEthernet/MQTT_QNEthernet.ino b/examples/QNEthernet/MQTT_QNEthernet/MQTT_QNEthernet.ino index efe4fb1..8a7ba83 100644 --- a/examples/QNEthernet/MQTT_QNEthernet/MQTT_QNEthernet.ino +++ b/examples/QNEthernet/MQTT_QNEthernet/MQTT_QNEthernet.ino @@ -1,18 +1,18 @@ /**************************************************************************************************************************** MQTT_QNEthernet.ino MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ - + #include "defines.h" #include @@ -29,14 +29,17 @@ const char *PubTopic = "/mqttPubSub"; // Top const char *PubMessage = "Hello from " BOARD_NAME " with " SHIELD_TYPE; // Topic Message to publish -void setup() +void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print("\nStart MQTT_QNEthernet on "); Serial.print(BOARD_NAME); - Serial.print(" with "); Serial.println(SHIELD_TYPE); + Serial.print("\nStart MQTT_QNEthernet on "); + Serial.print(BOARD_NAME); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); #if USE_NATIVE_ETHERNET @@ -56,21 +59,23 @@ void setup() Serial.println(F("=========================")); - Serial.print("Using mac index ="); Serial.println(index); - Serial.print("Connected! IP address:"); Serial.println(Ethernet.localIP()); + Serial.print("Using mac index ="); + Serial.println(index); + Serial.print("Connected! IP address:"); + Serial.println(Ethernet.localIP()); #else - #if USING_DHCP - // Start the Ethernet connection, using DHCP - Serial.print("Initialize Ethernet using DHCP => "); - Ethernet.begin(); - #else - // Start the Ethernet connection, using static IP - Serial.print("Initialize Ethernet using static IP => "); - Ethernet.begin(myIP, myNetmask, myGW); - Ethernet.setDNSServerIP(mydnsServer); - #endif +#if USING_DHCP + // Start the Ethernet connection, using DHCP + Serial.print("Initialize Ethernet using DHCP => "); + Ethernet.begin(); +#else + // Start the Ethernet connection, using static IP + Serial.print("Initialize Ethernet using static IP => "); + Ethernet.begin(myIP, myNetmask, myGW); + Ethernet.setDNSServerIP(mydnsServer); +#endif if (!Ethernet.waitForLocalIP(5000)) { @@ -95,14 +100,15 @@ void setup() #endif - Serial.print("Connecting to host "); Serial.println(MQTT_SERVER); - - while (!client.connect(MQTT_SERVER, MQTT_PORT)) + Serial.print("Connecting to host "); + Serial.println(MQTT_SERVER); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -122,7 +128,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -130,23 +136,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/QNEthernet/MQTT_QNEthernet/defines.h b/examples/QNEthernet/MQTT_QNEthernet/defines.h index 481c6c9..5059969 100644 --- a/examples/QNEthernet/MQTT_QNEthernet/defines.h +++ b/examples/QNEthernet/MQTT_QNEthernet/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -16,7 +16,7 @@ #ifndef defines_h #define defines_h -#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) +#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) // For Teensy 4.1 #define BOARD_TYPE "TEENSY 4.1" // Use true for NativeEthernet Library, false if using other Ethernet libraries @@ -43,65 +43,65 @@ #if USE_NATIVE_ETHERNET #define WEBSOCKETS_NETWORK_TYPE NETWORK_NATIVEETHERNET - + #include "NativeEthernet.h" #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error #define SHIELD_TYPE "using NativeEthernet" #elif USE_QN_ETHERNET #define WEBSOCKETS_NETWORK_TYPE NETWORK_QN_ETHERNET - + #include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet using namespace qindesign::network; #warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "using QNEthernet" + #define SHIELD_TYPE "using QNEthernet" #endif #if USE_NATIVE_ETHERNET - // Enter a MAC address and IP address for your controller below. - #define NUMBER_OF_MAC 20 - - byte mac[][NUMBER_OF_MAC] = - { - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, - }; +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; #else - #define USING_DHCP false //true - - #if !USING_DHCP - // Set the static IP address to use if the DHCP fails to assign - IPAddress myIP(192, 168, 2, 222); - IPAddress myNetmask(255, 255, 255, 0); - IPAddress myGW(192, 168, 2, 1); - //IPAddress mydnsServer(192, 168, 2, 1); - IPAddress mydnsServer(8, 8, 8, 8); - #endif +#define USING_DHCP false //true + +#if !USING_DHCP + // Set the static IP address to use if the DHCP fails to assign + IPAddress myIP(192, 168, 2, 222); + IPAddress myNetmask(255, 255, 255, 0); + IPAddress myGW(192, 168, 2, 1); + //IPAddress mydnsServer(192, 168, 2, 1); + IPAddress mydnsServer(8, 8, 8, 8); +#endif #endif #else -#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) +#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) // For Teensy 4.1 #define BOARD_TYPE "TEENSY 4.1" // Use true for NativeEthernet Library, false if using other Ethernet libraries @@ -125,7 +125,7 @@ #define USE_ETHERNET2 false #define USE_ETHERNET3 false #define USE_ETHERNET_LARGE false -#define USE_ETHERNET_ESP8266 false +#define USE_ETHERNET_ESP8266 false #define USE_ETHERNET_ENC false #define USE_CUSTOM_ETHERNET false @@ -137,38 +137,38 @@ #include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet using namespace qindesign::network; #warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "using QNEthernet" + #define SHIELD_TYPE "using QNEthernet" #endif - + #include #if USE_NATIVE_ETHERNET - // Enter a MAC address and IP address for your controller below. - #define NUMBER_OF_MAC 20 - - byte mac[][NUMBER_OF_MAC] = - { - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, - }; +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; #endif diff --git a/examples/QNEthernet/MQTToverWebSocket_QNEthernet/MQTToverWebSocket_QNEthernet.ino b/examples/QNEthernet/MQTToverWebSocket_QNEthernet/MQTToverWebSocket_QNEthernet.ino index 9ae2e6e..1027b5a 100644 --- a/examples/QNEthernet/MQTToverWebSocket_QNEthernet/MQTToverWebSocket_QNEthernet.ino +++ b/examples/QNEthernet/MQTToverWebSocket_QNEthernet/MQTToverWebSocket_QNEthernet.ino @@ -1,14 +1,14 @@ /**************************************************************************************************************************** MQTToverWebSocket_QNEthernet.ino MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -35,14 +35,17 @@ const char *PubTopic = "/mqttPubSub"; // Top const char *PubMessage = "Hello from " BOARD_NAME " with " SHIELD_TYPE; // Topic Message to publish -void setup() +void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print("\nStart MQTToverWebSocket_QNEthernet on "); Serial.print(BOARD_NAME); - Serial.print(" with "); Serial.println(SHIELD_TYPE); + Serial.print("\nStart MQTToverWebSocket_QNEthernet on "); + Serial.print(BOARD_NAME); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); #if USE_NATIVE_ETHERNET @@ -62,21 +65,23 @@ void setup() Serial.println(F("=========================")); - Serial.print("Using mac index ="); Serial.println(index); - Serial.print("Connected! IP address:"); Serial.println(Ethernet.localIP()); + Serial.print("Using mac index ="); + Serial.println(index); + Serial.print("Connected! IP address:"); + Serial.println(Ethernet.localIP()); #else - #if USING_DHCP - // Start the Ethernet connection, using DHCP - Serial.print("Initialize Ethernet using DHCP => "); - Ethernet.begin(); - #else - // Start the Ethernet connection, using static IP - Serial.print("Initialize Ethernet using static IP => "); - Ethernet.begin(myIP, myNetmask, myGW); - Ethernet.setDNSServerIP(mydnsServer); - #endif +#if USING_DHCP + // Start the Ethernet connection, using DHCP + Serial.print("Initialize Ethernet using DHCP => "); + Ethernet.begin(); +#else + // Start the Ethernet connection, using static IP + Serial.print("Initialize Ethernet using static IP => "); + Ethernet.begin(myIP, myNetmask, myGW); + Ethernet.setDNSServerIP(mydnsServer); +#endif if (!Ethernet.waitForLocalIP(5000)) { @@ -106,7 +111,7 @@ void setup() Serial.println(WS_SERVER); client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required - + client.setReconnectInterval(2000); // initialize mqtt client @@ -126,7 +131,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -134,23 +139,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/QNEthernet/MQTToverWebSocket_QNEthernet/defines.h b/examples/QNEthernet/MQTToverWebSocket_QNEthernet/defines.h index c2c3277..4d8acba 100644 --- a/examples/QNEthernet/MQTToverWebSocket_QNEthernet/defines.h +++ b/examples/QNEthernet/MQTToverWebSocket_QNEthernet/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -16,7 +16,7 @@ #ifndef defines_h #define defines_h -#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) +#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) // For Teensy 4.1 #define BOARD_TYPE "TEENSY 4.1" // Use true for NativeEthernet Library, false if using other Ethernet libraries @@ -44,65 +44,65 @@ #if USE_NATIVE_ETHERNET #define WEBSOCKETS_NETWORK_TYPE NETWORK_NATIVEETHERNET - + #include "NativeEthernet.h" #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error #define SHIELD_TYPE "using NativeEthernet" #elif USE_QN_ETHERNET #define WEBSOCKETS_NETWORK_TYPE NETWORK_QN_ETHERNET - + #include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet using namespace qindesign::network; #warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "using QNEthernet" + #define SHIELD_TYPE "using QNEthernet" #endif #if USE_NATIVE_ETHERNET - // Enter a MAC address and IP address for your controller below. - #define NUMBER_OF_MAC 20 - - byte mac[][NUMBER_OF_MAC] = - { - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, - }; +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; #else - #define USING_DHCP false //true - - #if !USING_DHCP - // Set the static IP address to use if the DHCP fails to assign - IPAddress myIP(192, 168, 2, 222); - IPAddress myNetmask(255, 255, 255, 0); - IPAddress myGW(192, 168, 2, 1); - //IPAddress mydnsServer(192, 168, 2, 1); - IPAddress mydnsServer(8, 8, 8, 8); - #endif +#define USING_DHCP false //true + +#if !USING_DHCP + // Set the static IP address to use if the DHCP fails to assign + IPAddress myIP(192, 168, 2, 222); + IPAddress myNetmask(255, 255, 255, 0); + IPAddress myGW(192, 168, 2, 1); + //IPAddress mydnsServer(192, 168, 2, 1); + IPAddress mydnsServer(8, 8, 8, 8); +#endif #endif #else -#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) +#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) // For Teensy 4.1 #define BOARD_TYPE "TEENSY 4.1" // Use true for NativeEthernet Library, false if using other Ethernet libraries @@ -126,7 +126,7 @@ #define USE_ETHERNET2 false #define USE_ETHERNET3 false #define USE_ETHERNET_LARGE false -#define USE_ETHERNET_ESP8266 false +#define USE_ETHERNET_ESP8266 false #define USE_ETHERNET_ENC false #define USE_CUSTOM_ETHERNET false @@ -138,38 +138,38 @@ #include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet using namespace qindesign::network; #warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "using QNEthernet" + #define SHIELD_TYPE "using QNEthernet" #endif - + #include #if USE_NATIVE_ETHERNET - // Enter a MAC address and IP address for your controller below. - #define NUMBER_OF_MAC 20 - - byte mac[][NUMBER_OF_MAC] = - { - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, - }; +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; #endif diff --git a/examples/RP2040W/WiFiMQTT/WiFiMQTT.ino b/examples/RP2040W/WiFiMQTT/WiFiMQTT.ino index eeef257..9b4342d 100644 --- a/examples/RP2040W/WiFiMQTT/WiFiMQTT.ino +++ b/examples/RP2040W/WiFiMQTT/WiFiMQTT.ino @@ -13,7 +13,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) #error This code is intended to run only on the RP2040W boards ! Please check your Tools->Board setting. #endif @@ -62,9 +62,11 @@ void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTT on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiMQTT on ")); + Serial.println(BOARD_NAME); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); /////////////////////////////////// @@ -73,22 +75,23 @@ void setup() if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -97,15 +100,17 @@ void setup() /////////////////////////////////// - Serial.print("Connecting to WebSockets Server @ "); Serial.print(MQTT_SERVER); - Serial.print(", port "); Serial.println(MQTT_PORT); - - while (!client.connect(MQTT_SERVER, MQTT_PORT)) + Serial.print("Connecting to WebSockets Server @ "); + Serial.print(MQTT_SERVER); + Serial.print(", port "); + Serial.println(MQTT_PORT); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -125,7 +130,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -133,23 +138,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/RP2040W/WiFiMQTTSecure/WiFiMQTTSecure.ino b/examples/RP2040W/WiFiMQTTSecure/WiFiMQTTSecure.ino index 47bd078..300fb7c 100644 --- a/examples/RP2040W/WiFiMQTTSecure/WiFiMQTTSecure.ino +++ b/examples/RP2040W/WiFiMQTTSecure/WiFiMQTTSecure.ino @@ -13,7 +13,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) #error This code is intended to run only on the RP2040W boards ! Please check your Tools->Board setting. #endif @@ -59,12 +59,14 @@ void printWifiStatus() Serial.println(F(" dBm")); } -void setup() +void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTTSecure on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiMQTTSecure on ")); + Serial.println(BOARD_NAME); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); /////////////////////////////////// @@ -73,22 +75,23 @@ void setup() if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -97,17 +100,19 @@ void setup() /////////////////////////////////// - Serial.print("Connecting to secured-host:port = "); Serial.print(MQTT_SERVER); - Serial.print(":"); Serial.println(MQTT_PORT); - + Serial.print("Connecting to secured-host:port = "); + Serial.print(MQTT_SERVER); + Serial.print(":"); + Serial.println(MQTT_PORT); + client.setInsecure(); // skip verification - + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -127,7 +132,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -135,23 +140,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/RP2040W/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino b/examples/RP2040W/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino index 7f6cea3..c461a7e 100644 --- a/examples/RP2040W/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino +++ b/examples/RP2040W/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino @@ -86,9 +86,11 @@ void printWifiStatus() void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTTSecureAWS on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiMQTTSecureAWS on ")); + Serial.println(BOARD_NAME); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); /////////////////////////////////// @@ -97,22 +99,23 @@ void setup() if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -122,40 +125,42 @@ void setup() /////////////////////////////////// Serial.print("connecting to host..."); - Serial.print("Connecting to secured-host:port = "); Serial.print(AWS_IOT_ENDPOINT); - Serial.print(":"); Serial.println(AWS_IOT_PORT); - + Serial.print("Connecting to secured-host:port = "); + Serial.print(AWS_IOT_ENDPOINT); + Serial.print(":"); + Serial.println(AWS_IOT_PORT); + // connect to aws endpoint with certificates and keys client.setCACert(AWS_CERT_CA); client.setCertificate(AWS_CERT_CRT); client.setPrivateKey(AWS_CERT_PRIVATE); - - while (!client.connect(AWS_IOT_ENDPOINT, AWS_IOT_PORT)) + + while (!client.connect(AWS_IOT_ENDPOINT, AWS_IOT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client mqttClient.begin(client); Serial.print("connecting to AWS MQTT broker..."); - - while (!mqttClient.connect(DEVICE_NAME)) + + while (!mqttClient.connect(DEVICE_NAME)) { Serial.print("."); delay(1000); } - + Serial.println(" connected!"); // subscribe callback which is called when every packet has come mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -163,20 +168,21 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); } -void loop() +void loop() { mqttClient.update(); static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); diff --git a/examples/RP2040W/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino b/examples/RP2040W/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino index f96f783..a66e497 100644 --- a/examples/RP2040W/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino +++ b/examples/RP2040W/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino @@ -1,19 +1,19 @@ /**************************************************************************************************************************** WiFiMQTToverWebSocket.ino - + MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) #error This code is intended to run only on the RP2040W boards ! Please check your Tools->Board setting. #endif @@ -52,7 +52,7 @@ MQTTPubSubClient mqttClient; // 1883: MQTT, unencrypted, unauthenticated // 8080: MQTT over WebSockets, unencrypted, unauthenticated // 8081: MQTT over WebSockets, encrypted, unauthenticated -#define WS_PORT 8080 +#define WS_PORT 8080 const char *PubTopic = "/mqttPubSub"; // Topic to publish const char *PubMessage = "Hello from " BOARD_NAME; // Topic Message to publish @@ -80,9 +80,11 @@ void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTToverWebSocket on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiMQTToverWebSocket on ")); + Serial.println(BOARD_NAME); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); /////////////////////////////////// @@ -91,22 +93,23 @@ void setup() if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -116,8 +119,10 @@ void setup() /////////////////////////////////// // server address, port and URL - Serial.print("Connecting to WebSockets Server @ "); Serial.print(WS_SERVER); - Serial.print(", port "); Serial.println(WS_PORT); + Serial.print("Connecting to WebSockets Server @ "); + Serial.print(WS_SERVER); + Serial.print(", port "); + Serial.println(WS_PORT); client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required @@ -146,21 +151,22 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/STM32/EtherMQTT_STM32/EtherMQTT_STM32.ino b/examples/STM32/EtherMQTT_STM32/EtherMQTT_STM32.ino index 2c5df6f..8bedccc 100644 --- a/examples/STM32/EtherMQTT_STM32/EtherMQTT_STM32.ino +++ b/examples/STM32/EtherMQTT_STM32/EtherMQTT_STM32.ino @@ -1,14 +1,14 @@ /**************************************************************************************************************************** EtherMQTT_STM32.ino MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -43,15 +43,15 @@ void initEthernet() #if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET) // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) Ethernet.init (USE_THIS_SS_PIN); - #elif USE_CUSTOM_ETHERNET +#elif USE_CUSTOM_ETHERNET // You have to add initialization for your Custom Ethernet here // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough //Ethernet.init(USE_THIS_SS_PIN); - #endif //( ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#endif //( ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #endif // start the ethernet connection and the server: @@ -62,30 +62,34 @@ void initEthernet() Ethernet.begin(mac[index]); Serial.print(F("Connected! IP address: ")); - Serial.println(Ethernet.localIP()); + Serial.println(Ethernet.localIP()); } -void setup() +void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print("\nStart EtherMQTT_STM32 on "); Serial.print(BOARD_NAME); - Serial.print(" with "); Serial.println(SHIELD_TYPE); + Serial.print("\nStart EtherMQTT_STM32 on "); + Serial.print(BOARD_NAME); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); Serial.println(ETHERNET_WEBSERVER_STM32_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); initEthernet(); - Serial.print("Connecting to host "); Serial.println(MQTT_SERVER); - - while (!client.connect(MQTT_SERVER, MQTT_PORT)) + Serial.print("Connecting to host "); + Serial.println(MQTT_SERVER); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -105,7 +109,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -113,23 +117,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/STM32/EtherMQTT_STM32/defines.h b/examples/STM32/EtherMQTT_STM32/defines.h index 011c030..79f815c 100644 --- a/examples/STM32/EtherMQTT_STM32/defines.h +++ b/examples/STM32/EtherMQTT_STM32/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -19,7 +19,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define DEBUG_ETHERNET_WEBSERVER_PORT Serial @@ -27,7 +27,7 @@ // Debug Level from 0 to 4 #define _ETHERNET_WEBSERVER_LOGLEVEL_ 2 -// If USE_BUILTIN_ETHERNET == false and USE_UIP_ETHERNET == false => +// If USE_BUILTIN_ETHERNET == false and USE_UIP_ETHERNET == false => // either use W5x00 with EthernetXYZ library // or ENC28J60 with EthernetENC library #define USE_BUILTIN_ETHERNET true @@ -45,7 +45,7 @@ #if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET) // Only one if the following to be true #define USE_ETHERNET_GENERIC true - #define USE_ETHERNET_ESP8266 false + #define USE_ETHERNET_ESP8266 false #define USE_ETHERNET_ENC false #define USE_CUSTOM_ETHERNET false #endif @@ -69,13 +69,13 @@ #define ETHERNET_LARGE_BUFFERS #define _ETG_LOGLEVEL_ 1 - + #warning Using W5x00 & Ethernet_Generic lib - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library" + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library" #elif USE_ETHERNET_ESP8266 #include "Ethernet_ESP8266.h" - #warning Using W5x00 & Ethernet_ESP8266 lib - #define SHIELD_TYPE "W5x00 & Ethernet_ESP8266 Library" + #warning Using W5x00 & Ethernet_ESP8266 lib + #define SHIELD_TYPE "W5x00 & Ethernet_ESP8266 Library" #elif USE_ETHERNET_ENC #include "EthernetENC.h" #warning Using ENC28J60 & EthernetENC lib @@ -86,7 +86,7 @@ #warning Using Custom Ethernet library. You must include a library and initialize. #define SHIELD_TYPE "Custom Ethernet & Ethernet_XYZ Library" #else - #define USE_ETHERNET_GENERIC true + #define USE_ETHERNET_GENERIC true #include "Ethernet_Generic.h" #warning Using default Ethernet_Generic lib #define SHIELD_TYPE "W5x00 using default Ethernet_Generic Library" diff --git a/examples/STM32/EtherMQTToverWebSocket_STM32/EtherMQTToverWebSocket_STM32.ino b/examples/STM32/EtherMQTToverWebSocket_STM32/EtherMQTToverWebSocket_STM32.ino index 15ab06e..98e9e7a 100644 --- a/examples/STM32/EtherMQTToverWebSocket_STM32/EtherMQTToverWebSocket_STM32.ino +++ b/examples/STM32/EtherMQTToverWebSocket_STM32/EtherMQTToverWebSocket_STM32.ino @@ -1,18 +1,18 @@ /**************************************************************************************************************************** EtherMQTToverWebSocket_STM32.ino MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ - + #include "defines.h" #define MQTTPUBSUBCLIENT_USE_WEBSOCKETS true @@ -49,15 +49,15 @@ void initEthernet() #if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET) // For other boards, to change if necessary - #if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) Ethernet.init (USE_THIS_SS_PIN); - #elif USE_CUSTOM_ETHERNET +#elif USE_CUSTOM_ETHERNET // You have to add initialization for your Custom Ethernet here // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough //Ethernet.init(USE_THIS_SS_PIN); - #endif //( ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) +#endif //( ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) #endif // start the ethernet connection and the server: @@ -68,17 +68,20 @@ void initEthernet() Ethernet.begin(mac[index]); Serial.print(F("Connected! IP address: ")); - Serial.println(Ethernet.localIP()); + Serial.println(Ethernet.localIP()); } -void setup() +void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print("\nStart EtherMQTToverWebSocket_STM32 on "); Serial.print(BOARD_NAME); - Serial.print(" with "); Serial.println(SHIELD_TYPE); + Serial.print("\nStart EtherMQTToverWebSocket_STM32 on "); + Serial.print(BOARD_NAME); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); Serial.println(ETHERNET_WEBSERVER_STM32_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); @@ -89,7 +92,7 @@ void setup() Serial.println(WS_SERVER); client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required - + client.setReconnectInterval(2000); // initialize mqtt client @@ -109,7 +112,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -117,23 +120,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/STM32/EtherMQTToverWebSocket_STM32/defines.h b/examples/STM32/EtherMQTToverWebSocket_STM32/defines.h index 7ab98f0..8503d8d 100644 --- a/examples/STM32/EtherMQTToverWebSocket_STM32/defines.h +++ b/examples/STM32/EtherMQTToverWebSocket_STM32/defines.h @@ -1,13 +1,13 @@ /**************************************************************************************************************************** MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -18,7 +18,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define DEBUG_ETHERNET_WEBSERVER_PORT Serial @@ -26,7 +26,7 @@ // Debug Level from 0 to 4 #define _ETHERNET_WEBSERVER_LOGLEVEL_ 2 -// If USE_BUILTIN_ETHERNET == false and USE_UIP_ETHERNET == false => +// If USE_BUILTIN_ETHERNET == false and USE_UIP_ETHERNET == false => // either use W5x00 with EthernetXYZ library // or ENC28J60 with EthernetENC library #define USE_BUILTIN_ETHERNET true @@ -44,7 +44,7 @@ #if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET) // Only one if the following to be true #define USE_ETHERNET_GENERIC true - #define USE_ETHERNET_ESP8266 false + #define USE_ETHERNET_ESP8266 false #define USE_ETHERNET_ENC false #define USE_CUSTOM_ETHERNET false #endif @@ -68,13 +68,13 @@ #define ETHERNET_LARGE_BUFFERS #define _ETG_LOGLEVEL_ 1 - + #warning Using W5x00 & Ethernet_Generic lib - #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library" + #define SHIELD_TYPE "W5x00 using Ethernet_Generic Library" #elif USE_ETHERNET_ESP8266 #include "Ethernet_ESP8266.h" - #warning Using W5x00 & Ethernet_ESP8266 lib - #define SHIELD_TYPE "W5x00 & Ethernet_ESP8266 Library" + #warning Using W5x00 & Ethernet_ESP8266 lib + #define SHIELD_TYPE "W5x00 & Ethernet_ESP8266 Library" #elif USE_ETHERNET_ENC #include "EthernetENC.h" #warning Using ENC28J60 & EthernetENC lib @@ -85,7 +85,7 @@ #warning Using Custom Ethernet library. You must include a library and initialize. #define SHIELD_TYPE "Custom Ethernet & Ethernet_XYZ Library" #else - #define USE_ETHERNET_GENERIC true + #define USE_ETHERNET_GENERIC true #include "Ethernet_Generic.h" #warning Using default Ethernet_Generic lib #define SHIELD_TYPE "W5x00 using default Ethernet_Generic Library" diff --git a/examples/WiFi/WiFiMQTT/WiFiMQTT.ino b/examples/WiFi/WiFiMQTT/WiFiMQTT.ino index 6c5e579..30ade7e 100644 --- a/examples/WiFi/WiFiMQTT/WiFiMQTT.ino +++ b/examples/WiFi/WiFiMQTT/WiFiMQTT.ino @@ -51,10 +51,13 @@ void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTT on ")); Serial.print(BOARD_NAME); - Serial.print(F(" with ")); Serial.println(SHIELD_TYPE); + Serial.print(F("\nStart WiFiMQTT on ")); + Serial.print(BOARD_NAME); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); Serial.println(WIFI_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); @@ -66,46 +69,49 @@ void setup() WiFi.init(&EspSerial); Serial.println(F("WiFi shield init done")); - + #endif #if !(ESP32 || ESP8266) - + // check for the presence of the shield - #if USE_WIFI_NINA - if (WiFi.status() == WL_NO_MODULE) - #else - if (WiFi.status() == WL_NO_SHIELD) - #endif - { - Serial.println(F("WiFi shield not present")); - // don't continue - while (true); - } - - #if USE_WIFI_NINA - String fv = WiFi.firmwareVersion(); - - if (fv < WIFI_FIRMWARE_LATEST_VERSION) - { - Serial.println(F("Please upgrade the firmware")); - } - #endif - +#if USE_WIFI_NINA + + if (WiFi.status() == WL_NO_MODULE) +#else + if (WiFi.status() == WL_NO_SHIELD) +#endif + { + Serial.println(F("WiFi shield not present")); + + // don't continue + while (true); + } + +#if USE_WIFI_NINA + String fv = WiFi.firmwareVersion(); + + if (fv < WIFI_FIRMWARE_LATEST_VERSION) + { + Serial.println(F("Please upgrade the firmware")); + } + +#endif + #endif Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -113,14 +119,15 @@ void setup() // you're connected now, so print out the data printWifiStatus(); - Serial.print("Connecting to host "); Serial.println(MQTT_SERVER); - - while (!client.connect(MQTT_SERVER, MQTT_PORT)) + Serial.print("Connecting to host "); + Serial.println(MQTT_SERVER); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -140,7 +147,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -148,23 +155,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFi/WiFiMQTT/defines.h b/examples/WiFi/WiFiMQTT/defines.h index 465bab1..fe34ef1 100644 --- a/examples/WiFi/WiFiMQTT/defines.h +++ b/examples/WiFi/WiFiMQTT/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -40,21 +40,21 @@ #define USE_WIFI_NINA false - // To use the default WiFi library here + // To use the default WiFi library here #define USE_WIFI_CUSTOM false - + #elif (ESP32) #define USE_WIFI_NINA false - // To use the default WiFi library here + // To use the default WiFi library here #define USE_WIFI_CUSTOM false #elif (ESP8266) #define USE_WIFI_NINA false - // To use the default WiFi library here + // To use the default WiFi library here #define USE_WIFI_CUSTOM true #elif ( defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) ) @@ -70,17 +70,17 @@ #define USE_WIFI_CUSTOM false #elif defined(ARDUINO_AVR_UNO_WIFI_REV2) - + #define USE_WIFI_NINA false #define USE_WIFI101 true #define USE_WIFI_CUSTOM false - + #else #define USE_WIFI_NINA false #define USE_WIFI101 false - - // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here + + // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here #define USE_WIFI_CUSTOM true #endif @@ -110,21 +110,21 @@ #define SHIELD_TYPE "WiFi101 using WiFi101 Library" #elif (ESP32 || ESP8266) #warning Using ESP WiFi with WiFi Library - #define SHIELD_TYPE "ESP WiFi using WiFi Library" + #define SHIELD_TYPE "ESP WiFi using WiFi Library" #elif USE_WIFI_CUSTOM #warning Using Custom WiFi using Custom WiFi Library #define SHIELD_TYPE "Custom WiFi using Custom WiFi Library" #else - #define SHIELD_TYPE "Unknown WiFi shield/Library" + #define SHIELD_TYPE "Unknown WiFi shield/Library" #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(WIFI_USE_NRF528XX) - #undef WIFI_USE_NRF528XX - #endif - #define WIFI_USE_NRF528XX true +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -132,10 +132,10 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(WIFI_USE_SAMD) - #undef WIFI_USE_SAMD - #endif - #define WIFI_USE_SAMD true +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -149,10 +149,10 @@ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) - #if defined(WIFI_USE_STM32) - #undef WIFI_USE_STM32 - #endif - #define WIFI_USE_STM32 true +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true #endif #ifdef CORE_TEENSY @@ -375,7 +375,7 @@ #define BOARD_NAME BOARD_TYPE #else #define BOARD_NAME "Unknown Board" - #endif + #endif #endif #include diff --git a/examples/WiFi/WiFiMQTTSecure/WiFiMQTTSecure.ino b/examples/WiFi/WiFiMQTTSecure/WiFiMQTTSecure.ino index 00f74dd..d631857 100644 --- a/examples/WiFi/WiFiMQTTSecure/WiFiMQTTSecure.ino +++ b/examples/WiFi/WiFiMQTTSecure/WiFiMQTTSecure.ino @@ -13,7 +13,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #if !( defined(ESP8266) || defined(ESP32) ) #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif @@ -60,26 +60,28 @@ void printWifiStatus() Serial.println(F(" dBm")); } -void setup() +void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTTSecure on ")); Serial.println(ARDUINO_BOARD); + Serial.print(F("\nStart WiFiMQTTSecure on ")); + Serial.println(ARDUINO_BOARD); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -87,17 +89,19 @@ void setup() // you're connected now, so print out the data printWifiStatus(); - Serial.print("Connecting to secured-host:port = "); Serial.print(MQTT_SERVER); - Serial.print(":"); Serial.println(MQTT_PORT); - + Serial.print("Connecting to secured-host:port = "); + Serial.print(MQTT_SERVER); + Serial.print(":"); + Serial.println(MQTT_PORT); + client.setInsecure(); // skip verification - + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -117,7 +121,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -125,23 +129,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFi/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino b/examples/WiFi/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino index f823f45..0bdb046 100644 --- a/examples/WiFi/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino +++ b/examples/WiFi/WiFiMQTTSecureAWS/WiFiMQTTSecureAWS.ino @@ -87,9 +87,11 @@ void printWifiStatus() void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTTSecureAWS on ")); Serial.println(ARDUINO_BOARD); + Serial.print(F("\nStart WiFiMQTTSecureAWS on ")); + Serial.println(ARDUINO_BOARD); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); Serial.print(F("Connecting to SSID: ")); @@ -112,40 +114,42 @@ void setup() printWifiStatus(); Serial.print("connecting to host..."); - Serial.print("Connecting to secured-host:port = "); Serial.print(AWS_IOT_ENDPOINT); - Serial.print(":"); Serial.println(AWS_IOT_PORT); - + Serial.print("Connecting to secured-host:port = "); + Serial.print(AWS_IOT_ENDPOINT); + Serial.print(":"); + Serial.println(AWS_IOT_PORT); + // connect to aws endpoint with certificates and keys client.setCACert(AWS_CERT_CA); client.setCertificate(AWS_CERT_CRT); client.setPrivateKey(AWS_CERT_PRIVATE); - - while (!client.connect(AWS_IOT_ENDPOINT, AWS_IOT_PORT)) + + while (!client.connect(AWS_IOT_ENDPOINT, AWS_IOT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client mqttClient.begin(client); Serial.print("connecting to AWS MQTT broker..."); - - while (!mqttClient.connect(DEVICE_NAME)) + + while (!mqttClient.connect(DEVICE_NAME)) { Serial.print("."); delay(1000); } - + Serial.println(" connected!"); // subscribe callback which is called when every packet has come mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -153,20 +157,21 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); } -void loop() +void loop() { mqttClient.update(); static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); diff --git a/examples/WiFi/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino b/examples/WiFi/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino index 900f901..f7cf084 100644 --- a/examples/WiFi/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino +++ b/examples/WiFi/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino @@ -1,19 +1,19 @@ /**************************************************************************************************************************** WiFiMQTToverWebSocket.ino - + MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #include "defines.h" int status = WL_IDLE_STATUS; // the Wifi radio's status @@ -57,14 +57,17 @@ void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTToverWebSocket on ")); Serial.print(BOARD_NAME); - Serial.print(F(" with ")); Serial.println(SHIELD_TYPE); + Serial.print(F("\nStart WiFiMQTToverWebSocket on ")); + Serial.print(BOARD_NAME); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); Serial.println(WIFI_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); - #if WIFI_USING_ESP_AT +#if WIFI_USING_ESP_AT // initialize serial for ESP module EspSerial.begin(115200); @@ -72,46 +75,49 @@ void setup() WiFi.init(&EspSerial); Serial.println(F("WiFi shield init done")); - + #endif #if !(ESP32 || ESP8266) - + // check for the presence of the shield - #if USE_WIFI_NINA - if (WiFi.status() == WL_NO_MODULE) - #else - if (WiFi.status() == WL_NO_SHIELD) - #endif - { - Serial.println(F("WiFi shield not present")); - // don't continue - while (true); - } - - #if USE_WIFI_NINA - String fv = WiFi.firmwareVersion(); - - if (fv < WIFI_FIRMWARE_LATEST_VERSION) - { - Serial.println(F("Please upgrade the firmware")); - } - #endif - +#if USE_WIFI_NINA + + if (WiFi.status() == WL_NO_MODULE) +#else + if (WiFi.status() == WL_NO_SHIELD) +#endif + { + Serial.println(F("WiFi shield not present")); + + // don't continue + while (true); + } + +#if USE_WIFI_NINA + String fv = WiFi.firmwareVersion(); + + if (fv < WIFI_FIRMWARE_LATEST_VERSION) + { + Serial.println(F("Please upgrade the firmware")); + } + +#endif + #endif Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -120,8 +126,10 @@ void setup() printWifiStatus(); // server address, port and URL - Serial.print("Connecting to WebSockets Server @ "); Serial.print(WS_SERVER); - Serial.print(", port "); Serial.println(WS_PORT); + Serial.print("Connecting to WebSockets Server @ "); + Serial.print(WS_SERVER); + Serial.print(", port "); + Serial.println(WS_PORT); client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required @@ -150,21 +158,22 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFi/WiFiMQTToverWebSocket/defines.h b/examples/WiFi/WiFiMQTToverWebSocket/defines.h index a8ec66b..69cb7f4 100644 --- a/examples/WiFi/WiFiMQTToverWebSocket/defines.h +++ b/examples/WiFi/WiFiMQTToverWebSocket/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -41,21 +41,21 @@ #define USE_WIFI_NINA false - // To use the default WiFi library here + // To use the default WiFi library here #define USE_WIFI_CUSTOM false - + #elif (ESP32) #define USE_WIFI_NINA false - // To use the default WiFi library here + // To use the default WiFi library here #define USE_WIFI_CUSTOM false #elif (ESP8266) #define USE_WIFI_NINA false - // To use the default WiFi library here + // To use the default WiFi library here #define USE_WIFI_CUSTOM true #elif ( defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) ) @@ -71,17 +71,17 @@ #define USE_WIFI_CUSTOM false #elif defined(ARDUINO_AVR_UNO_WIFI_REV2) - + #define USE_WIFI_NINA false #define USE_WIFI101 true #define USE_WIFI_CUSTOM false - + #else #define USE_WIFI_NINA false #define USE_WIFI101 false - - // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here + + // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here #define USE_WIFI_CUSTOM true #endif @@ -111,21 +111,21 @@ #define SHIELD_TYPE "WiFi101 using WiFi101 Library" #elif (ESP32 || ESP8266) #warning Using ESP WiFi with WiFi Library - #define SHIELD_TYPE "ESP WiFi using WiFi Library" + #define SHIELD_TYPE "ESP WiFi using WiFi Library" #elif USE_WIFI_CUSTOM #warning Using Custom WiFi using Custom WiFi Library #define SHIELD_TYPE "Custom WiFi using Custom WiFi Library" #else - #define SHIELD_TYPE "Unknown WiFi shield/Library" + #define SHIELD_TYPE "Unknown WiFi shield/Library" #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(WIFI_USE_NRF528XX) - #undef WIFI_USE_NRF528XX - #endif - #define WIFI_USE_NRF528XX true +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -133,10 +133,10 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(WIFI_USE_SAMD) - #undef WIFI_USE_SAMD - #endif - #define WIFI_USE_SAMD true +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -150,10 +150,10 @@ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) - #if defined(WIFI_USE_STM32) - #undef WIFI_USE_STM32 - #endif - #define WIFI_USE_STM32 true +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true #endif #ifdef CORE_TEENSY @@ -376,7 +376,7 @@ #define BOARD_NAME BOARD_TYPE #else #define BOARD_NAME "Unknown Board" - #endif + #endif #endif #include diff --git a/examples/WiFi/WiFiMQTToverWebSocketSecure/WiFiMQTToverWebSocketSecure.ino b/examples/WiFi/WiFiMQTToverWebSocketSecure/WiFiMQTToverWebSocketSecure.ino index c7ec039..b213d8b 100644 --- a/examples/WiFi/WiFiMQTToverWebSocketSecure/WiFiMQTToverWebSocketSecure.ino +++ b/examples/WiFi/WiFiMQTToverWebSocketSecure/WiFiMQTToverWebSocketSecure.ino @@ -68,9 +68,11 @@ void printWifiStatus() void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiMQTToverWebSocketSecure on ")); Serial.println(ARDUINO_BOARD); + Serial.print(F("\nStart WiFiMQTToverWebSocketSecure on ")); + Serial.println(ARDUINO_BOARD); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); Serial.print(F("Connecting to SSID: ")); @@ -92,11 +94,13 @@ void setup() // you're connected now, so print out the data printWifiStatus(); - Serial.print("Connecting to secured-host:port = "); Serial.print(WS_SERVER); - Serial.print(":"); Serial.println(WS_PORT); - - client.beginSSL(WS_SERVER, WS_PORT); - + Serial.print("Connecting to secured-host:port = "); + Serial.print(WS_SERVER); + Serial.print(":"); + Serial.println(WS_PORT); + + client.beginSSL(WS_SERVER, WS_PORT); + client.setReconnectInterval(2000); // initialize mqtt client @@ -116,7 +120,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -124,23 +128,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFi101/WiFiMQTT/WiFiMQTT.ino b/examples/WiFi101/WiFiMQTT/WiFiMQTT.ino new file mode 100644 index 0000000..30ade7e --- /dev/null +++ b/examples/WiFi101/WiFiMQTT/WiFiMQTT.ino @@ -0,0 +1,180 @@ +/**************************************************************************************************************************** + WiFiMQTT.ino + + MQTT and MQTT over WebSoket Client for Arduino + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 + + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. + + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) + + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic + Licensed under MIT license + *****************************************************************************************************************************/ + +#include "defines.h" + +int status = WL_IDLE_STATUS; // the Wifi radio's status + +#include + +WiFiClient client; +MQTTPubSubClient mqttClient; + +//#define MQTT_SERVER "192.168.2.30" +#define MQTT_SERVER "public.cloud.shiftr.io" +#define MQTT_PORT 1883 + +const char *PubTopic = "/mqttPubSub"; // Topic to publish +const char *PubMessage = "Hello from " BOARD_NAME " with " SHIELD_TYPE; // Topic Message to publish + +void printWifiStatus() +{ + // print the SSID of the network you're attached to: + // you're connected now, so print out the data + Serial.print(F("You're connected to the network, IP = ")); + Serial.println(WiFi.localIP()); + + Serial.print(F("SSID: ")); + Serial.print(WiFi.SSID()); + + // print the received signal strength: + int32_t rssi = WiFi.RSSI(); + Serial.print(F(", Signal strength (RSSI):")); + Serial.print(rssi); + Serial.println(F(" dBm")); +} + +void setup() +{ + // Debug console + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + Serial.print(F("\nStart WiFiMQTT on ")); + Serial.print(BOARD_NAME); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); + Serial.println(WIFI_WEBSERVER_VERSION); + Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); + +#if WIFI_USING_ESP_AT + + // initialize serial for ESP module + EspSerial.begin(115200); + // initialize ESP module + WiFi.init(&EspSerial); + + Serial.println(F("WiFi shield init done")); + +#endif + +#if !(ESP32 || ESP8266) + + // check for the presence of the shield +#if USE_WIFI_NINA + + if (WiFi.status() == WL_NO_MODULE) +#else + if (WiFi.status() == WL_NO_SHIELD) +#endif + { + Serial.println(F("WiFi shield not present")); + + // don't continue + while (true); + } + +#if USE_WIFI_NINA + String fv = WiFi.firmwareVersion(); + + if (fv < WIFI_FIRMWARE_LATEST_VERSION) + { + Serial.println(F("Please upgrade the firmware")); + } + +#endif + +#endif + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + // you're connected now, so print out the data + printWifiStatus(); + + Serial.print("Connecting to host "); + Serial.println(MQTT_SERVER); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) + { + Serial.print("."); + delay(1000); + } + + Serial.println("\nConnected!"); + + // initialize mqtt client + mqttClient.begin(client); + + Serial.print("Connecting to mqtt broker..."); + + while (!mqttClient.connect("arduino", "public", "public")) + { + Serial.print("."); + delay(1000); + } + + Serial.println(" connected!"); + + // subscribe callback which is called when every packet has come + mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) + { + (void) size; + + Serial.println("MQTT received: " + topic + " - " + payload); + }); + + // subscribe topic and callback which is called when /hello has come + mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) + { + (void) size; + + Serial.print("Subcribed to "); + Serial.print(PubTopic); + Serial.print(" => "); + Serial.println(payload); + }); + + mqttClient.publish(PubTopic, PubMessage); +} + +void loop() +{ + mqttClient.update(); // should be called + + // publish message + static uint32_t prev_ms = millis(); + + if (millis() > prev_ms + 30000) + { + prev_ms = millis(); + mqttClient.publish(PubTopic, PubMessage); + } +} diff --git a/examples/WiFi101/WiFiMQTT/defines.h b/examples/WiFi101/WiFiMQTT/defines.h new file mode 100644 index 0000000..3ac8c0a --- /dev/null +++ b/examples/WiFi101/WiFiMQTT/defines.h @@ -0,0 +1,314 @@ +/**************************************************************************************************************************** + defines.h + MQTT and MQTT over WebSoket Client for Arduino + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 + + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. + + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) + + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic + Licensed under MIT license + ***************************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +#define DEBUG_WIFI_WEBSERVER_PORT Serial + +// Debug Level from 0 to 4 +#define _WIFI_LOGLEVEL_ 1 +#define _WIFININA_LOGLEVEL_ 3 +#define _MQTT_PUBSUB_LOGLEVEL_ 4 + +#if ( defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) ) + + #define USE_WIFI_NINA false + #define USE_WIFI101 true + #define USE_WIFI_CUSTOM false + +#elif defined(ARDUINO_AVR_UNO_WIFI_REV2) + + #define USE_WIFI_NINA false + #define USE_WIFI101 true + #define USE_WIFI_CUSTOM false + +#else + + #error Only for WiFi101 + +#endif + +#if USE_WIFI101 + #warning Using WiFi101 using WiFi101 Library + #define SHIELD_TYPE "WiFi101 using WiFi101 Library" +#endif + +#if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ + defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ + defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true +#endif + +#if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ + || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \ + || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ + || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ + || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true +#endif + +#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) + #if defined(WIFI_USE_SAM_DUE) + #undef WIFI_USE_SAM_DUE + #endif + #define WIFI_USE_SAM_DUE true + #warning Use SAM_DUE architecture +#endif + +#if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true +#endif + +#ifdef CORE_TEENSY + #if defined(__IMXRT1062__) + // For Teensy 4.1/4.0 + #define BOARD_TYPE "TEENSY 4.1/4.0" + #elif defined(__MK66FX1M0__) + #define BOARD_TYPE "Teensy 3.6" + #elif defined(__MK64FX512__) + #define BOARD_TYPE "Teensy 3.5" + #elif defined(__MKL26Z64__) + #define BOARD_TYPE "Teensy LC" + #elif defined(__MK20DX256__) + #define BOARD_TYPE "Teensy 3.2" // and Teensy 3.1 (obsolete) + #elif defined(__MK20DX128__) + #define BOARD_TYPE "Teensy 3.0" + #elif defined(__AVR_AT90USB1286__) + #error Teensy 2.0++ not supported yet + #elif defined(__AVR_ATmega32U4__) + #error Teensy 2.0 not supported yet + #else + // For Other Boards + #define BOARD_TYPE "Unknown Teensy Board" + #endif + +#elif defined(WIFI_USE_NRF528XX) + #if defined(NRF52840_FEATHER) + #define BOARD_TYPE "NRF52840_FEATHER_EXPRESS" + #elif defined(NRF52832_FEATHER) + #define BOARD_TYPE "NRF52832_FEATHER" + #elif defined(NRF52840_FEATHER_SENSE) + #define BOARD_TYPE "NRF52840_FEATHER_SENSE" + #elif defined(NRF52840_ITSYBITSY) + #define BOARD_TYPE "NRF52840_ITSYBITSY_EXPRESS" + #elif defined(NRF52840_CIRCUITPLAY) + #define BOARD_TYPE "NRF52840_CIRCUIT_PLAYGROUND" + #elif defined(NRF52840_CLUE) + #define BOARD_TYPE "NRF52840_CLUE" + #elif defined(NRF52840_METRO) + #define BOARD_TYPE "NRF52840_METRO_EXPRESS" + #elif defined(NRF52840_PCA10056) + #define BOARD_TYPE "NORDIC_NRF52840DK" + #elif defined(NINA_B302_ublox) + #define BOARD_TYPE "NINA_B302_ublox" + #elif defined(NINA_B112_ublox) + #define BOARD_TYPE "NINA_B112_ublox" + #elif defined(PARTICLE_XENON) + #define BOARD_TYPE "PARTICLE_XENON" + #elif defined(MDBT50Q_RX) + #define BOARD_TYPE "RAYTAC_MDBT50Q_RX" + #elif defined(ARDUINO_NRF52_ADAFRUIT) + #define BOARD_TYPE "ARDUINO_NRF52_ADAFRUIT" + #else + #define BOARD_TYPE "nRF52 Unknown" + #endif + +#elif defined(WIFI_USE_SAMD) + #if defined(ARDUINO_SAMD_ZERO) + #define BOARD_TYPE "SAMD Zero" + #elif defined(ARDUINO_SAMD_MKR1000) + #define BOARD_TYPE "SAMD MKR1000" + #elif defined(ARDUINO_SAMD_MKRWIFI1010) + #define BOARD_TYPE "SAMD MKRWIFI1010" + #elif defined(ARDUINO_SAMD_NANO_33_IOT) + #define BOARD_TYPE "SAMD NANO_33_IOT" + #elif defined(ARDUINO_SAMD_MKRFox1200) + #define BOARD_TYPE "SAMD MKRFox1200" + #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) ) + #define BOARD_TYPE "SAMD MKRWAN13X0" + #elif defined(ARDUINO_SAMD_MKRGSM1400) + #define BOARD_TYPE "SAMD MKRGSM1400" + #elif defined(ARDUINO_SAMD_MKRNB1500) + #define BOARD_TYPE "SAMD MKRNB1500" + #elif defined(ARDUINO_SAMD_MKRVIDOR4000) + #define BOARD_TYPE "SAMD MKRVIDOR4000" + #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) + #define BOARD_TYPE "SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS" + #elif defined(ADAFRUIT_FEATHER_M0_EXPRESS) + #define BOARD_TYPE "SAMD21 ADAFRUIT_FEATHER_M0_EXPRESS" + #elif defined(ADAFRUIT_METRO_M0_EXPRESS) + #define BOARD_TYPE "SAMD21 ADAFRUIT_METRO_M0_EXPRESS" + #elif defined(ADAFRUIT_CIRCUITPLAYGROUND_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_CIRCUITPLAYGROUND_M0" + #elif defined(ADAFRUIT_GEMMA_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_GEMMA_M0" + #elif defined(ADAFRUIT_TRINKET_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_TRINKET_M0" + #elif defined(ADAFRUIT_ITSYBITSY_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_ITSYBITSY_M0" + #elif defined(ARDUINO_SAMD_HALLOWING_M0) + #define BOARD_TYPE "SAMD21 ARDUINO_SAMD_HALLOWING_M0" + #elif defined(ADAFRUIT_METRO_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_METRO_M4_EXPRESS" + #elif defined(ADAFRUIT_GRAND_CENTRAL_M4) + #define BOARD_TYPE "SAMD51 ADAFRUIT_GRAND_CENTRAL_M4" + #elif defined(ADAFRUIT_FEATHER_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_FEATHER_M4_EXPRESS" + #elif defined(ADAFRUIT_ITSYBITSY_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_ITSYBITSY_M4_EXPRESS" + #elif defined(ADAFRUIT_TRELLIS_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_TRELLIS_M4_EXPRESS" + #elif defined(ADAFRUIT_PYPORTAL) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYPORTAL" + #elif defined(ADAFRUIT_PYPORTAL_M4_TITANO) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYPORTAL_M4_TITANO" + #elif defined(ADAFRUIT_PYBADGE_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYBADGE_M4_EXPRESS" + #elif defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) + #define BOARD_TYPE "SAMD51 ADAFRUIT_METRO_M4_AIRLIFT_LITE" + #elif defined(ADAFRUIT_PYGAMER_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYGAMER_M4_EXPRESS" + #elif defined(ADAFRUIT_PYGAMER_ADVANCE_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYGAMER_ADVANCE_M4_EXPRESS" + #elif defined(ADAFRUIT_PYBADGE_AIRLIFT_M4) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYBADGE_AIRLIFT_M4" + #elif defined(ADAFRUIT_MONSTER_M4SK_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_MONSTER_M4SK_EXPRESS" + #elif defined(ADAFRUIT_HALLOWING_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_HALLOWING_M4_EXPRESS" + #elif defined(SEEED_WIO_TERMINAL) + #define BOARD_TYPE "SAMD SEEED_WIO_TERMINAL" + #elif defined(SEEED_FEMTO_M0) + #define BOARD_TYPE "SAMD SEEED_FEMTO_M0" + #elif defined(SEEED_XIAO_M0) + #define BOARD_TYPE "SAMD SEEED_XIAO_M0" + #elif defined(Wio_Lite_MG126) + #define BOARD_TYPE "SAMD SEEED Wio_Lite_MG126" + #elif defined(WIO_GPS_BOARD) + #define BOARD_TYPE "SAMD SEEED WIO_GPS_BOARD" + #elif defined(SEEEDUINO_ZERO) + #define BOARD_TYPE "SAMD SEEEDUINO_ZERO" + #elif defined(SEEEDUINO_LORAWAN) + #define BOARD_TYPE "SAMD SEEEDUINO_LORAWAN" + #elif defined(SEEED_GROVE_UI_WIRELESS) + #define BOARD_TYPE "SAMD SEEED_GROVE_UI_WIRELESS" + #elif defined(__SAMD21E18A__) + #define BOARD_TYPE "SAMD21E18A" + #elif defined(__SAMD21G18A__) + #define BOARD_TYPE "SAMD21G18A" + #elif defined(__SAMD51G19A__) + #define BOARD_TYPE "SAMD51G19A" + #elif defined(__SAMD51J19A__) + #define BOARD_TYPE "SAMD51J19A" + #elif defined(__SAMD51P19A__) + #define BOARD_TYPE "__SAMD51P19A__" + #elif defined(__SAMD51J20A__) + #define BOARD_TYPE "SAMD51J20A" + #elif defined(__SAM3X8E__) + #define BOARD_TYPE "SAM3X8E" + #elif defined(__CPU_ARC__) + #define BOARD_TYPE "CPU_ARC" + #elif defined(__SAMD51__) + #define BOARD_TYPE "SAMD51" + #else + #define BOARD_TYPE "SAMD Unknown" + #endif + +#elif defined(WIFI_USE_STM32) + #if defined(STM32F0) + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" + #elif defined(STM32F1) + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" + #elif defined(STM32F2) + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" + #elif defined(STM32F3) + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" + #elif defined(STM32F4) + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" + #elif defined(STM32F7) + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" + #elif defined(STM32L0) + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" + #elif defined(STM32L1) + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" + #elif defined(STM32L4) + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" + #elif defined(STM32H7) + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" + #elif defined(STM32G0) + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" + #elif defined(STM32G4) + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" + #elif defined(STM32WB) + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" + #elif defined(STM32MP1) + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" + #else + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" + #endif + +#elif defined(ESP32) + #warning ESP32 board selected + #define BOARD_TYPE "ESP32" +#elif defined(ESP8266) + #warning ESP8266 board selected + #define BOARD_TYPE "ESP8266" +#else + #define BOARD_TYPE "AVR Mega" +#endif + +#ifndef BOARD_NAME + #if defined(ARDUINO_BOARD) + #define BOARD_NAME ARDUINO_BOARD + #elif defined(BOARD_TYPE) + #define BOARD_NAME BOARD_TYPE + #else + #define BOARD_NAME "Unknown Board" + #endif +#endif + +#include + +char ssid[] = "your_ssid"; // your network SSID (name) +char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+ + +#endif //defines_h diff --git a/examples/WiFi101/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino b/examples/WiFi101/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino new file mode 100644 index 0000000..cf39287 --- /dev/null +++ b/examples/WiFi101/WiFiMQTToverWebSocket/WiFiMQTToverWebSocket.ino @@ -0,0 +1,185 @@ +/**************************************************************************************************************************** + WiFiMQTToverWebSocket.ino + + MQTT and MQTT over WebSoket Client for Arduino + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 + + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. + + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) + + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic + Licensed under MIT license + *****************************************************************************************************************************/ + +#include "defines.h" + +int status = WL_IDLE_STATUS; // the Wifi radio's status + +#include // include before MQTTPubSubClient.h + +#define MQTTPUBSUBCLIENT_USE_WEBSOCKETS true + +#include + +WebSocketsClient client; + +//MQTTPubSub::PubSubClient<256> mqttClient; +MQTTPubSubClient mqttClient; + +//#define WS_SERVER "192.168.2.30" +#define WS_SERVER "test.mosquitto.org" +#define WS_PORT 8080 + +const char *PubTopic = "/mqttPubSub"; // Topic to publish +const char *PubMessage = "Hello from " BOARD_NAME " with " SHIELD_TYPE; // Topic Message to publish + +void printWifiStatus() +{ + // print the SSID of the network you're attached to: + // you're connected now, so print out the data + Serial.print(F("You're connected to the network, IP = ")); + Serial.println(WiFi.localIP()); + + Serial.print(F("SSID: ")); + Serial.print(WiFi.SSID()); + + // print the received signal strength: + int32_t rssi = WiFi.RSSI(); + Serial.print(F(", Signal strength (RSSI):")); + Serial.print(rssi); + Serial.println(F(" dBm")); +} + +void setup() +{ + // Debug console + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + Serial.print(F("\nStart WiFiMQTToverWebSocket on ")); + Serial.print(BOARD_NAME); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); + Serial.println(WIFI_WEBSERVER_VERSION); + Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); + +#if WIFI_USING_ESP_AT + + // initialize serial for ESP module + EspSerial.begin(115200); + // initialize ESP module + WiFi.init(&EspSerial); + + Serial.println(F("WiFi shield init done")); + +#endif + +#if !(ESP32 || ESP8266) + + // check for the presence of the shield +#if USE_WIFI_NINA + + if (WiFi.status() == WL_NO_MODULE) +#else + if (WiFi.status() == WL_NO_SHIELD) +#endif + { + Serial.println(F("WiFi shield not present")); + + // don't continue + while (true); + } + +#if USE_WIFI_NINA + String fv = WiFi.firmwareVersion(); + + if (fv < WIFI_FIRMWARE_LATEST_VERSION) + { + Serial.println(F("Please upgrade the firmware")); + } + +#endif + +#endif + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + // you're connected now, so print out the data + printWifiStatus(); + + // server address, port and URL + Serial.print("Connecting to WebSockets Server @ "); + Serial.print(WS_SERVER); + Serial.print(", port "); + Serial.println(WS_PORT); + + client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required + + client.setReconnectInterval(2000); + + // initialize mqtt client + mqttClient.begin(client); + + Serial.print("Connecting to mqtt broker..."); + + while (!mqttClient.connect("arduino", "public", "public")) + { + Serial.print("."); + delay(1000); + } + + Serial.println(" connected!"); + + // subscribe callback which is called when every packet has come + mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) + { + (void) size; + + Serial.println("MQTT received: " + topic + " - " + payload); + }); + + // subscribe topic and callback which is called when /hello has come + mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) + { + (void) size; + + Serial.print("Subcribed to "); + Serial.print(PubTopic); + Serial.print(" => "); + Serial.println(payload); + }); + + mqttClient.publish(PubTopic, PubMessage); +} + +void loop() +{ + mqttClient.update(); // should be called + + // publish message + static uint32_t prev_ms = millis(); + + if (millis() > prev_ms + 30000) + { + prev_ms = millis(); + mqttClient.publish(PubTopic, PubMessage); + } +} diff --git a/examples/WiFi101/WiFiMQTToverWebSocket/defines.h b/examples/WiFi101/WiFiMQTToverWebSocket/defines.h new file mode 100644 index 0000000..3ac8c0a --- /dev/null +++ b/examples/WiFi101/WiFiMQTToverWebSocket/defines.h @@ -0,0 +1,314 @@ +/**************************************************************************************************************************** + defines.h + MQTT and MQTT over WebSoket Client for Arduino + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 + + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. + + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) + + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic + Licensed under MIT license + ***************************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +#define DEBUG_WIFI_WEBSERVER_PORT Serial + +// Debug Level from 0 to 4 +#define _WIFI_LOGLEVEL_ 1 +#define _WIFININA_LOGLEVEL_ 3 +#define _MQTT_PUBSUB_LOGLEVEL_ 4 + +#if ( defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) ) + + #define USE_WIFI_NINA false + #define USE_WIFI101 true + #define USE_WIFI_CUSTOM false + +#elif defined(ARDUINO_AVR_UNO_WIFI_REV2) + + #define USE_WIFI_NINA false + #define USE_WIFI101 true + #define USE_WIFI_CUSTOM false + +#else + + #error Only for WiFi101 + +#endif + +#if USE_WIFI101 + #warning Using WiFi101 using WiFi101 Library + #define SHIELD_TYPE "WiFi101 using WiFi101 Library" +#endif + +#if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ + defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ + defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true +#endif + +#if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ + || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \ + || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ + || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ + || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true +#endif + +#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) + #if defined(WIFI_USE_SAM_DUE) + #undef WIFI_USE_SAM_DUE + #endif + #define WIFI_USE_SAM_DUE true + #warning Use SAM_DUE architecture +#endif + +#if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true +#endif + +#ifdef CORE_TEENSY + #if defined(__IMXRT1062__) + // For Teensy 4.1/4.0 + #define BOARD_TYPE "TEENSY 4.1/4.0" + #elif defined(__MK66FX1M0__) + #define BOARD_TYPE "Teensy 3.6" + #elif defined(__MK64FX512__) + #define BOARD_TYPE "Teensy 3.5" + #elif defined(__MKL26Z64__) + #define BOARD_TYPE "Teensy LC" + #elif defined(__MK20DX256__) + #define BOARD_TYPE "Teensy 3.2" // and Teensy 3.1 (obsolete) + #elif defined(__MK20DX128__) + #define BOARD_TYPE "Teensy 3.0" + #elif defined(__AVR_AT90USB1286__) + #error Teensy 2.0++ not supported yet + #elif defined(__AVR_ATmega32U4__) + #error Teensy 2.0 not supported yet + #else + // For Other Boards + #define BOARD_TYPE "Unknown Teensy Board" + #endif + +#elif defined(WIFI_USE_NRF528XX) + #if defined(NRF52840_FEATHER) + #define BOARD_TYPE "NRF52840_FEATHER_EXPRESS" + #elif defined(NRF52832_FEATHER) + #define BOARD_TYPE "NRF52832_FEATHER" + #elif defined(NRF52840_FEATHER_SENSE) + #define BOARD_TYPE "NRF52840_FEATHER_SENSE" + #elif defined(NRF52840_ITSYBITSY) + #define BOARD_TYPE "NRF52840_ITSYBITSY_EXPRESS" + #elif defined(NRF52840_CIRCUITPLAY) + #define BOARD_TYPE "NRF52840_CIRCUIT_PLAYGROUND" + #elif defined(NRF52840_CLUE) + #define BOARD_TYPE "NRF52840_CLUE" + #elif defined(NRF52840_METRO) + #define BOARD_TYPE "NRF52840_METRO_EXPRESS" + #elif defined(NRF52840_PCA10056) + #define BOARD_TYPE "NORDIC_NRF52840DK" + #elif defined(NINA_B302_ublox) + #define BOARD_TYPE "NINA_B302_ublox" + #elif defined(NINA_B112_ublox) + #define BOARD_TYPE "NINA_B112_ublox" + #elif defined(PARTICLE_XENON) + #define BOARD_TYPE "PARTICLE_XENON" + #elif defined(MDBT50Q_RX) + #define BOARD_TYPE "RAYTAC_MDBT50Q_RX" + #elif defined(ARDUINO_NRF52_ADAFRUIT) + #define BOARD_TYPE "ARDUINO_NRF52_ADAFRUIT" + #else + #define BOARD_TYPE "nRF52 Unknown" + #endif + +#elif defined(WIFI_USE_SAMD) + #if defined(ARDUINO_SAMD_ZERO) + #define BOARD_TYPE "SAMD Zero" + #elif defined(ARDUINO_SAMD_MKR1000) + #define BOARD_TYPE "SAMD MKR1000" + #elif defined(ARDUINO_SAMD_MKRWIFI1010) + #define BOARD_TYPE "SAMD MKRWIFI1010" + #elif defined(ARDUINO_SAMD_NANO_33_IOT) + #define BOARD_TYPE "SAMD NANO_33_IOT" + #elif defined(ARDUINO_SAMD_MKRFox1200) + #define BOARD_TYPE "SAMD MKRFox1200" + #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) ) + #define BOARD_TYPE "SAMD MKRWAN13X0" + #elif defined(ARDUINO_SAMD_MKRGSM1400) + #define BOARD_TYPE "SAMD MKRGSM1400" + #elif defined(ARDUINO_SAMD_MKRNB1500) + #define BOARD_TYPE "SAMD MKRNB1500" + #elif defined(ARDUINO_SAMD_MKRVIDOR4000) + #define BOARD_TYPE "SAMD MKRVIDOR4000" + #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) + #define BOARD_TYPE "SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS" + #elif defined(ADAFRUIT_FEATHER_M0_EXPRESS) + #define BOARD_TYPE "SAMD21 ADAFRUIT_FEATHER_M0_EXPRESS" + #elif defined(ADAFRUIT_METRO_M0_EXPRESS) + #define BOARD_TYPE "SAMD21 ADAFRUIT_METRO_M0_EXPRESS" + #elif defined(ADAFRUIT_CIRCUITPLAYGROUND_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_CIRCUITPLAYGROUND_M0" + #elif defined(ADAFRUIT_GEMMA_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_GEMMA_M0" + #elif defined(ADAFRUIT_TRINKET_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_TRINKET_M0" + #elif defined(ADAFRUIT_ITSYBITSY_M0) + #define BOARD_TYPE "SAMD21 ADAFRUIT_ITSYBITSY_M0" + #elif defined(ARDUINO_SAMD_HALLOWING_M0) + #define BOARD_TYPE "SAMD21 ARDUINO_SAMD_HALLOWING_M0" + #elif defined(ADAFRUIT_METRO_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_METRO_M4_EXPRESS" + #elif defined(ADAFRUIT_GRAND_CENTRAL_M4) + #define BOARD_TYPE "SAMD51 ADAFRUIT_GRAND_CENTRAL_M4" + #elif defined(ADAFRUIT_FEATHER_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_FEATHER_M4_EXPRESS" + #elif defined(ADAFRUIT_ITSYBITSY_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_ITSYBITSY_M4_EXPRESS" + #elif defined(ADAFRUIT_TRELLIS_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_TRELLIS_M4_EXPRESS" + #elif defined(ADAFRUIT_PYPORTAL) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYPORTAL" + #elif defined(ADAFRUIT_PYPORTAL_M4_TITANO) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYPORTAL_M4_TITANO" + #elif defined(ADAFRUIT_PYBADGE_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYBADGE_M4_EXPRESS" + #elif defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) + #define BOARD_TYPE "SAMD51 ADAFRUIT_METRO_M4_AIRLIFT_LITE" + #elif defined(ADAFRUIT_PYGAMER_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYGAMER_M4_EXPRESS" + #elif defined(ADAFRUIT_PYGAMER_ADVANCE_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYGAMER_ADVANCE_M4_EXPRESS" + #elif defined(ADAFRUIT_PYBADGE_AIRLIFT_M4) + #define BOARD_TYPE "SAMD51 ADAFRUIT_PYBADGE_AIRLIFT_M4" + #elif defined(ADAFRUIT_MONSTER_M4SK_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_MONSTER_M4SK_EXPRESS" + #elif defined(ADAFRUIT_HALLOWING_M4_EXPRESS) + #define BOARD_TYPE "SAMD51 ADAFRUIT_HALLOWING_M4_EXPRESS" + #elif defined(SEEED_WIO_TERMINAL) + #define BOARD_TYPE "SAMD SEEED_WIO_TERMINAL" + #elif defined(SEEED_FEMTO_M0) + #define BOARD_TYPE "SAMD SEEED_FEMTO_M0" + #elif defined(SEEED_XIAO_M0) + #define BOARD_TYPE "SAMD SEEED_XIAO_M0" + #elif defined(Wio_Lite_MG126) + #define BOARD_TYPE "SAMD SEEED Wio_Lite_MG126" + #elif defined(WIO_GPS_BOARD) + #define BOARD_TYPE "SAMD SEEED WIO_GPS_BOARD" + #elif defined(SEEEDUINO_ZERO) + #define BOARD_TYPE "SAMD SEEEDUINO_ZERO" + #elif defined(SEEEDUINO_LORAWAN) + #define BOARD_TYPE "SAMD SEEEDUINO_LORAWAN" + #elif defined(SEEED_GROVE_UI_WIRELESS) + #define BOARD_TYPE "SAMD SEEED_GROVE_UI_WIRELESS" + #elif defined(__SAMD21E18A__) + #define BOARD_TYPE "SAMD21E18A" + #elif defined(__SAMD21G18A__) + #define BOARD_TYPE "SAMD21G18A" + #elif defined(__SAMD51G19A__) + #define BOARD_TYPE "SAMD51G19A" + #elif defined(__SAMD51J19A__) + #define BOARD_TYPE "SAMD51J19A" + #elif defined(__SAMD51P19A__) + #define BOARD_TYPE "__SAMD51P19A__" + #elif defined(__SAMD51J20A__) + #define BOARD_TYPE "SAMD51J20A" + #elif defined(__SAM3X8E__) + #define BOARD_TYPE "SAM3X8E" + #elif defined(__CPU_ARC__) + #define BOARD_TYPE "CPU_ARC" + #elif defined(__SAMD51__) + #define BOARD_TYPE "SAMD51" + #else + #define BOARD_TYPE "SAMD Unknown" + #endif + +#elif defined(WIFI_USE_STM32) + #if defined(STM32F0) + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" + #elif defined(STM32F1) + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" + #elif defined(STM32F2) + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" + #elif defined(STM32F3) + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" + #elif defined(STM32F4) + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" + #elif defined(STM32F7) + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" + #elif defined(STM32L0) + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" + #elif defined(STM32L1) + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" + #elif defined(STM32L4) + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" + #elif defined(STM32H7) + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" + #elif defined(STM32G0) + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" + #elif defined(STM32G4) + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" + #elif defined(STM32WB) + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" + #elif defined(STM32MP1) + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" + #else + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" + #endif + +#elif defined(ESP32) + #warning ESP32 board selected + #define BOARD_TYPE "ESP32" +#elif defined(ESP8266) + #warning ESP8266 board selected + #define BOARD_TYPE "ESP8266" +#else + #define BOARD_TYPE "AVR Mega" +#endif + +#ifndef BOARD_NAME + #if defined(ARDUINO_BOARD) + #define BOARD_NAME ARDUINO_BOARD + #elif defined(BOARD_TYPE) + #define BOARD_NAME BOARD_TYPE + #else + #define BOARD_NAME "Unknown Board" + #endif +#endif + +#include + +char ssid[] = "your_ssid"; // your network SSID (name) +char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+ + +#endif //defines_h diff --git a/examples/WiFiNINA/WiFiNINA_MQTT/WiFiNINA_MQTT.ino b/examples/WiFiNINA/WiFiNINA_MQTT/WiFiNINA_MQTT.ino index fb61cb5..6c1b88f 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTT/WiFiNINA_MQTT.ino +++ b/examples/WiFiNINA/WiFiNINA_MQTT/WiFiNINA_MQTT.ino @@ -51,43 +51,47 @@ void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiNINA_MQTT on ")); Serial.print(BOARD_NAME); - Serial.print(F(" with ")); Serial.println(SHIELD_TYPE); + Serial.print(F("\nStart WiFiNINA_MQTT on ")); + Serial.print(BOARD_NAME); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); Serial.println(WIFININA_GENERIC_VERSION); Serial.println(WIFI_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); ///////////////////////////////////// - + // check for the presence of the shield if (WiFi.status() == WL_NO_MODULE) { Serial.println(F("WiFi shield not present")); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println(F("Please upgrade the firmware")); } - + Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -97,14 +101,15 @@ void setup() ///////////////////////////////////// - Serial.print("Connecting to host "); Serial.println(MQTT_SERVER); - - while (!client.connect(MQTT_SERVER, MQTT_PORT)) + Serial.print("Connecting to host "); + Serial.println(MQTT_SERVER); + + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -124,7 +129,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -132,23 +137,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFiNINA/WiFiNINA_MQTT/defines.h b/examples/WiFiNINA/WiFiNINA_MQTT/defines.h index ca455bb..88565f9 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTT/defines.h +++ b/examples/WiFiNINA/WiFiNINA_MQTT/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -37,17 +37,17 @@ #define USE_WIFI_CUSTOM false #elif defined(ARDUINO_AVR_UNO_WIFI_REV2) - + #define USE_WIFI_NINA false #define USE_WIFI101 true #define USE_WIFI_CUSTOM false - + #else #define USE_WIFI_NINA false #define USE_WIFI101 false - - // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here + + // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here #define USE_WIFI_CUSTOM true #endif @@ -62,16 +62,16 @@ #warning Using Custom WiFi using Custom WiFi Library #define SHIELD_TYPE "Custom WiFi using Custom WiFi Library" #else - #define SHIELD_TYPE "Unknown WiFi shield/Library" + #define SHIELD_TYPE "Unknown WiFi shield/Library" #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(WIFI_USE_NRF528XX) - #undef WIFI_USE_NRF528XX - #endif - #define WIFI_USE_NRF528XX true +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -79,10 +79,10 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(WIFI_USE_SAMD) - #undef WIFI_USE_SAMD - #endif - #define WIFI_USE_SAMD true +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -96,10 +96,10 @@ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) - #if defined(WIFI_USE_STM32) - #undef WIFI_USE_STM32 - #endif - #define WIFI_USE_STM32 true +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true #endif #ifdef CORE_TEENSY @@ -316,7 +316,7 @@ #define BOARD_NAME BOARD_TYPE #else #define BOARD_NAME "Unknown Board" - #endif + #endif #endif #include diff --git a/examples/WiFiNINA/WiFiNINA_MQTTSecure/WiFiNINA_MQTTSecure.ino b/examples/WiFiNINA/WiFiNINA_MQTTSecure/WiFiNINA_MQTTSecure.ino index 1ba5e83..c5a5ef0 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTTSecure/WiFiNINA_MQTTSecure.ino +++ b/examples/WiFiNINA/WiFiNINA_MQTTSecure/WiFiNINA_MQTTSecure.ino @@ -13,7 +13,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #include "defines.h" #include @@ -54,45 +54,48 @@ void printWifiStatus() Serial.println(F(" dBm")); } -void setup() +void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiNINA_MQTTSecure on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiNINA_MQTTSecure on ")); + Serial.println(BOARD_NAME); Serial.println(WIFININA_GENERIC_VERSION); Serial.println(WIFI_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); ///////////////////////////////////// - + // check for the presence of the shield if (WiFi.status() == WL_NO_MODULE) { Serial.println(F("WiFi shield not present")); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println(F("Please upgrade the firmware")); } - + Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -101,16 +104,18 @@ void setup() printWifiStatus(); ///////////////////////////////////// - - Serial.print("Connecting to secured-host:port = "); Serial.print(MQTT_SERVER); - Serial.print(":"); Serial.println(MQTT_PORT); - + + Serial.print("Connecting to secured-host:port = "); + Serial.print(MQTT_SERVER); + Serial.print(":"); + Serial.println(MQTT_PORT); + while (!client.connect(MQTT_SERVER, MQTT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client @@ -130,7 +135,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -138,23 +143,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFiNINA/WiFiNINA_MQTTSecure/defines.h b/examples/WiFiNINA/WiFiNINA_MQTTSecure/defines.h index ca455bb..88565f9 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTTSecure/defines.h +++ b/examples/WiFiNINA/WiFiNINA_MQTTSecure/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -37,17 +37,17 @@ #define USE_WIFI_CUSTOM false #elif defined(ARDUINO_AVR_UNO_WIFI_REV2) - + #define USE_WIFI_NINA false #define USE_WIFI101 true #define USE_WIFI_CUSTOM false - + #else #define USE_WIFI_NINA false #define USE_WIFI101 false - - // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here + + // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here #define USE_WIFI_CUSTOM true #endif @@ -62,16 +62,16 @@ #warning Using Custom WiFi using Custom WiFi Library #define SHIELD_TYPE "Custom WiFi using Custom WiFi Library" #else - #define SHIELD_TYPE "Unknown WiFi shield/Library" + #define SHIELD_TYPE "Unknown WiFi shield/Library" #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(WIFI_USE_NRF528XX) - #undef WIFI_USE_NRF528XX - #endif - #define WIFI_USE_NRF528XX true +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -79,10 +79,10 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(WIFI_USE_SAMD) - #undef WIFI_USE_SAMD - #endif - #define WIFI_USE_SAMD true +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -96,10 +96,10 @@ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) - #if defined(WIFI_USE_STM32) - #undef WIFI_USE_STM32 - #endif - #define WIFI_USE_STM32 true +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true #endif #ifdef CORE_TEENSY @@ -316,7 +316,7 @@ #define BOARD_NAME BOARD_TYPE #else #define BOARD_NAME "Unknown Board" - #endif + #endif #endif #include diff --git a/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/WiFiNINA_MQTTSecureAWS.ino b/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/WiFiNINA_MQTTSecureAWS.ino index e3f9b1d..69a70c0 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/WiFiNINA_MQTTSecureAWS.ino +++ b/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/WiFiNINA_MQTTSecureAWS.ino @@ -66,42 +66,45 @@ void printWifiStatus() void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiNINA_MQTTSecureAWS on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiNINA_MQTTSecureAWS on ")); + Serial.println(BOARD_NAME); Serial.println(WIFININA_GENERIC_VERSION); Serial.println(WIFI_WEBSERVER_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); ///////////////////////////////////// - + // check for the presence of the shield if (WiFi.status() == WL_NO_MODULE) { Serial.println(F("WiFi shield not present")); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println(F("Please upgrade the firmware")); } - + Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -112,35 +115,37 @@ void setup() ///////////////////////////////////// Serial.print("connecting to host..."); - Serial.print("Connecting to secured-host:port = "); Serial.print(AWS_IOT_ENDPOINT); - Serial.print(":"); Serial.println(AWS_IOT_PORT); - - while (!client.connect(AWS_IOT_ENDPOINT, AWS_IOT_PORT)) + Serial.print("Connecting to secured-host:port = "); + Serial.print(AWS_IOT_ENDPOINT); + Serial.print(":"); + Serial.println(AWS_IOT_PORT); + + while (!client.connect(AWS_IOT_ENDPOINT, AWS_IOT_PORT)) { Serial.print("."); delay(1000); } - + Serial.println("\nConnected!"); // initialize mqtt client mqttClient.begin(client); Serial.print("connecting to AWS MQTT broker..."); - - while (!mqttClient.connect(DEVICE_NAME)) + + while (!mqttClient.connect(DEVICE_NAME)) { Serial.print("."); delay(1000); } - + Serial.println(" connected!"); // subscribe callback which is called when every packet has come mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -148,20 +153,21 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); } -void loop() +void loop() { mqttClient.update(); static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); diff --git a/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/defines.h b/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/defines.h index ca455bb..88565f9 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/defines.h +++ b/examples/WiFiNINA/WiFiNINA_MQTTSecureAWS/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -37,17 +37,17 @@ #define USE_WIFI_CUSTOM false #elif defined(ARDUINO_AVR_UNO_WIFI_REV2) - + #define USE_WIFI_NINA false #define USE_WIFI101 true #define USE_WIFI_CUSTOM false - + #else #define USE_WIFI_NINA false #define USE_WIFI101 false - - // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here + + // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here #define USE_WIFI_CUSTOM true #endif @@ -62,16 +62,16 @@ #warning Using Custom WiFi using Custom WiFi Library #define SHIELD_TYPE "Custom WiFi using Custom WiFi Library" #else - #define SHIELD_TYPE "Unknown WiFi shield/Library" + #define SHIELD_TYPE "Unknown WiFi shield/Library" #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(WIFI_USE_NRF528XX) - #undef WIFI_USE_NRF528XX - #endif - #define WIFI_USE_NRF528XX true +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -79,10 +79,10 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(WIFI_USE_SAMD) - #undef WIFI_USE_SAMD - #endif - #define WIFI_USE_SAMD true +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -96,10 +96,10 @@ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) - #if defined(WIFI_USE_STM32) - #undef WIFI_USE_STM32 - #endif - #define WIFI_USE_STM32 true +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true #endif #ifdef CORE_TEENSY @@ -316,7 +316,7 @@ #define BOARD_NAME BOARD_TYPE #else #define BOARD_NAME "Unknown Board" - #endif + #endif #endif #include diff --git a/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/WiFiNINA_MQTToverWebSocket.ino b/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/WiFiNINA_MQTToverWebSocket.ino index 3c6b3a0..e832ebc 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/WiFiNINA_MQTToverWebSocket.ino +++ b/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/WiFiNINA_MQTToverWebSocket.ino @@ -1,19 +1,19 @@ /**************************************************************************************************************************** WiFiNINA_MQTToverWebSocket.ino - + MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license *****************************************************************************************************************************/ - + #include "defines.h" #define WEBSOCKETS_NETWORK_TYPE NETWORK_WIFININA @@ -62,44 +62,48 @@ void setup() { // Debug console Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiNINA_MQTToverWebSocket on ")); Serial.print(BOARD_NAME); - Serial.print(F(" with ")); Serial.println(SHIELD_TYPE); + Serial.print(F("\nStart WiFiNINA_MQTToverWebSocket on ")); + Serial.print(BOARD_NAME); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); Serial.println(WIFININA_GENERIC_VERSION); Serial.println(WIFI_WEBSERVER_VERSION); Serial.println(WEBSOCKETS_GENERIC_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); -///////////////////////////////////// - + ///////////////////////////////////// + // check for the presence of the shield if (WiFi.status() == WL_NO_MODULE) { Serial.println(F("WiFi shield not present")); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println(F("Please upgrade the firmware")); } - + Serial.print(F("Connecting to SSID: ")); Serial.println(ssid); - + status = WiFi.begin(ssid, pass); delay(1000); - + // attempt to connect to WiFi network while ( status != WL_CONNECTED) { delay(500); - + // Connect to WPA/WPA2 network status = WiFi.status(); } @@ -110,8 +114,10 @@ void setup() ///////////////////////////////////// // server address, port and URL - Serial.print("Connecting to WebSockets Server @ "); Serial.print(WS_SERVER); - Serial.print(", port "); Serial.println(WS_PORT); + Serial.print("Connecting to WebSockets Server @ "); + Serial.print(WS_SERVER); + Serial.print(", port "); + Serial.println(WS_PORT); client.begin(WS_SERVER, WS_PORT, "/", "mqtt"); // "mqtt" is required @@ -140,21 +146,22 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/defines.h b/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/defines.h index ca455bb..88565f9 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/defines.h +++ b/examples/WiFiNINA/WiFiNINA_MQTToverWebSocket/defines.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** defines.h MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license ***************************************************************************************************************************************/ @@ -37,17 +37,17 @@ #define USE_WIFI_CUSTOM false #elif defined(ARDUINO_AVR_UNO_WIFI_REV2) - + #define USE_WIFI_NINA false #define USE_WIFI101 true #define USE_WIFI_CUSTOM false - + #else #define USE_WIFI_NINA false #define USE_WIFI101 false - - // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here + + // If not USE_WIFI_NINA, you can USE_WIFI_CUSTOM, then include the custom WiFi library here #define USE_WIFI_CUSTOM true #endif @@ -62,16 +62,16 @@ #warning Using Custom WiFi using Custom WiFi Library #define SHIELD_TYPE "Custom WiFi using Custom WiFi Library" #else - #define SHIELD_TYPE "Unknown WiFi shield/Library" + #define SHIELD_TYPE "Unknown WiFi shield/Library" #endif #if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) ) - #if defined(WIFI_USE_NRF528XX) - #undef WIFI_USE_NRF528XX - #endif - #define WIFI_USE_NRF528XX true +#if defined(WIFI_USE_NRF528XX) + #undef WIFI_USE_NRF528XX +#endif +#define WIFI_USE_NRF528XX true #endif #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ @@ -79,10 +79,10 @@ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) || defined(__SAMD21G18A__) ) - #if defined(WIFI_USE_SAMD) - #undef WIFI_USE_SAMD - #endif - #define WIFI_USE_SAMD true +#if defined(WIFI_USE_SAMD) + #undef WIFI_USE_SAMD +#endif +#define WIFI_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) @@ -96,10 +96,10 @@ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) && ! ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) - #if defined(WIFI_USE_STM32) - #undef WIFI_USE_STM32 - #endif - #define WIFI_USE_STM32 true +#if defined(WIFI_USE_STM32) + #undef WIFI_USE_STM32 +#endif +#define WIFI_USE_STM32 true #endif #ifdef CORE_TEENSY @@ -316,7 +316,7 @@ #define BOARD_NAME BOARD_TYPE #else #define BOARD_NAME "Unknown Board" - #endif + #endif #endif #include diff --git a/examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure/WiFiNINA_MQTToverWebSocketSecure.ino b/examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure/WiFiNINA_MQTToverWebSocketSecure.ino index 4d48893..932f66a 100644 --- a/examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure/WiFiNINA_MQTToverWebSocketSecure.ino +++ b/examples/WiFiNINA/WiFiNINA_MQTToverWebSocketSecure/WiFiNINA_MQTToverWebSocketSecure.ino @@ -67,9 +67,11 @@ void printWifiStatus() void setup() { Serial.begin(115200); + while (!Serial && millis() < 5000); - Serial.print(F("\nStart WiFiNINA_MQTToverWebSocketSecure on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStart WiFiNINA_MQTToverWebSocketSecure on ")); + Serial.println(BOARD_NAME); Serial.println(WIFININA_GENERIC_VERSION); Serial.println(WEBSOCKETS_GENERIC_VERSION); Serial.println(MQTT_PUBSUB_CLIENT_GENERIC_VERSION); @@ -93,11 +95,13 @@ void setup() // you're connected now, so print out the data printWifiStatus(); - Serial.print("Connecting to secured-host:port = "); Serial.print(WS_SERVER); - Serial.print(":"); Serial.println(WS_PORT); - - client.beginSSL(WS_SERVER, WS_PORT); - + Serial.print("Connecting to secured-host:port = "); + Serial.print(WS_SERVER); + Serial.print(":"); + Serial.println(WS_PORT); + + client.beginSSL(WS_SERVER, WS_PORT); + client.setReconnectInterval(2000); // initialize mqtt client @@ -117,7 +121,7 @@ void setup() mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) { (void) size; - + Serial.println("MQTT received: " + topic + " - " + payload); }); @@ -125,23 +129,24 @@ void setup() mqttClient.subscribe(PubTopic, [](const String & payload, const size_t size) { (void) size; - + Serial.print("Subcribed to "); - Serial.print(PubTopic); Serial.print(" => "); + Serial.print(PubTopic); + Serial.print(" => "); Serial.println(payload); }); mqttClient.publish(PubTopic, PubMessage); } -void loop() +void loop() { mqttClient.update(); // should be called // publish message static uint32_t prev_ms = millis(); - - if (millis() > prev_ms + 30000) + + if (millis() > prev_ms + 30000) { prev_ms = millis(); mqttClient.publish(PubTopic, PubMessage); diff --git a/library.json b/library.json index bb1e259..41dd65f 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "MQTTPubSubClient_Generic", - "version": "1.2.0", + "version": "1.2.1", "description": "MQTT, MQTT_over_WebSockets and Secured MQTT_over_WebSockets Client for Arduino. Supporting nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01. Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. Ethernet_Generic library is used as default for W5x00. Now with newly-added support to Nano_RP2040_Connect using WiFiNINA_Generic library and RP2040W using CYW43439 WiFi", "keywords": "mqtt, websocket, wifi, ethernet, tcp", "authors": @@ -34,19 +34,19 @@ { "owner": "khoih-prog", "name": "WiFiNINA_Generic", - "version": ">=1.8.14-6", + "version": ">=1.8.15-1", "platforms": ["*"] }, { "owner": "khoih-prog", "name": "WiFiWebServer", - "version": ">=1.9.5", + "version": ">=1.10.1", "platforms": ["*"] }, { "owner": "khoih-prog", "name": "EthernetWebServer", - "version": ">=2.2.3", + "version": ">=2.3.0", "platforms": ["*"] }, { @@ -94,23 +94,25 @@ { "owner": "khoih-prog", "name": "Ethernet_Generic", - "version": "^2.6.1", + "version": "^2.7.1", "platforms": ["*"] }, { "owner": "khoih-prog", "name": "WebSockets_Generic", - "version": "^2.16.0", + "version": "^2.16.1", "platforms": ["*"] }, { - "name": "WiFi101", - "version": "https://github.com/khoih-prog/WiFi101" + "owner": "khoih-prog", + "name": "WiFi101_Generic", + "version": "^1.0.0", + "platforms": ["*"] }, { "owner": "ssilverman", "name": "QNEthernet", - "version": ">=0.15.0", + "version": ">=0.16.0", "platforms": ["teensy"] } ], diff --git a/library.properties b/library.properties index 48cb0d0..efa7172 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MQTTPubSubClient_Generic -version=1.2.0 +version=1.2.1 license=MIT author=hideakitai, Khoi Hoang maintainer=Khoi Hoang diff --git a/platformio/platformio.ini b/platformio/platformio.ini index 39e8bc5..2683bb0 100644 --- a/platformio/platformio.ini +++ b/platformio/platformio.ini @@ -43,37 +43,37 @@ lib_ldf_mode = chain+ lib_deps = ; PlatformIO 4.x -; WiFiNINA_Generic@~1.8.14-6 -; WiFiWebServer@~1.9.5 -; EthernetWebServer@~2.2.3 +; WiFiNINA_Generic@~1.8.15-1 +; WiFiWebServer@~1.10.1 +; EthernetWebServer@~2.3.0 ; WebServer_WT32_ETH01@~1.5.1 ; EthernetWebServer_STM32@>=1.5.0 -; Ethernet_Generic@>=2.6.1 -; EthernetENC@~2.0.2 +; Ethernet_Generic@>=2.7.1 +; EthernetENC@~2.0.3 ; UIPEthernet@~2.0.12 ; STM32duino LwIP@>=2.1.2 ; STM32duino STM32Ethernet@>=1.3.0 -; QNEthernet@>=0.15.0 -; https://github.com/khoih-prog/WiFi101 +; QNEthernet@>=0.16.0 +; WiFi101_Generic@~1.0.0 ; ESPAsyncTCP@>=1.2.2 -; WebSockets_Generic@>=2.16.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform -; khoih-prog/WiFiNINA_Generic@~1.8.14-6 -; khoih-prog/WiFiWebServer@~1.9.5 -; khoih-prog/EthernetWebServer@~2.2.3 +; khoih-prog/WiFiNINA_Generic@~1.8.15-1 +; khoih-prog/WiFiWebServer@~1.10.1 +; khoih-prog/EthernetWebServer@~2.3.0 ; khoih-prog/WebServer_WT32_ETH01@~1.5.1 ; khoih-prog/EthernetWebServer_STM32@>=1.5.0 -; khoih-prog/Ethernet_Generic@~2.6.1 +; khoih-prog/Ethernet_Generic@~2.7.1 ; jandrassy/EthernetENC@~2.0.3 ; UIPEthernet/UIPEthernet@~2.0.12 ; stm32duino/STM32duino LwIP@>=2.1.2 ; stm32duino/STM32duino STM32Ethernet@>=1.3.0 -; ssilverman/QNEthernet@>=0.15.0 -; https://github.com/khoih-prog/WiFi101 +; ssilverman/QNEthernet@>=0.16.0 +; khoih-prog/WiFi101_Generic@~1.0.0 ; me-no-dev/ESPAsyncTCP@>=1.2.2 -; khoih-prog/WebSockets_Generic@>=2.16.0 +; khoih-prog/WebSockets_Generic@>=2.16.1 build_flags = @@ -88,21 +88,21 @@ framework = arduino lib_deps = ; PlatformIO 4.x -; WiFiWebServer@~1.9.5 -; EthernetWebServer@~2.2.3 -; Ethernet_Generic@>=2.6.1 -; EthernetENC@~2.0.2 +; WiFiWebServer@~1.10.1 +; EthernetWebServer@~2.3.0 +; Ethernet_Generic@>=2.7.1 +; EthernetENC@~2.0.3 ; UIPEthernet@~2.0.12 -; WebSockets_Generic@>=2.16.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform - khoih-prog/WiFiWebServer@~1.9.5 - khoih-prog/EthernetWebServer@~2.2.3 - khoih-prog/Ethernet_Generic@~2.6.1 + khoih-prog/WiFiWebServer@~1.10.1 + khoih-prog/EthernetWebServer@~2.3.0 + khoih-prog/Ethernet_Generic@~2.7.1 jandrassy/EthernetENC@~2.0.3 UIPEthernet/UIPEthernet@~2.0.12 - khoih-prog/WebSockets_Generic@>=2.16.0 + khoih-prog/WiFi101_Generic@~1.0.0 ; ============================================================ ; Board configuration @@ -152,23 +152,23 @@ framework = arduino, espidf lib_deps = ; PlatformIO 4.x -; WiFiWebServer@~1.9.5 -; EthernetWebServer@~2.2.3 +; WiFiWebServer@~1.10.1 +; EthernetWebServer@~2.3.0 ; WebServer_WT32_ETH01@~1.5.1 -; Ethernet_Generic@>=2.6.1 -; EthernetENC@~2.0.2 +; Ethernet_Generic@>=2.7.1 +; EthernetENC@~2.0.3 ; UIPEthernet@~2.0.12 -; WebSockets_Generic@>=2.16.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform - khoih-prog/WiFiWebServer@~1.9.5 - khoih-prog/EthernetWebServer@~2.2.3 + khoih-prog/WiFiWebServer@~1.10.1 + khoih-prog/EthernetWebServer@~2.3.0 khoih-prog/WebServer_WT32_ETH01@~1.5.1 - khoih-prog/Ethernet_Generic@~2.6.1 + khoih-prog/Ethernet_Generic@~2.7.1 jandrassy/EthernetENC@~2.0.3 UIPEthernet/UIPEthernet@~2.0.12 - khoih-prog/WebSockets_Generic@>=2.16.0 + khoih-prog/WebSockets_Generic@>=2.16.1 ; ============================================================ @@ -215,25 +215,25 @@ framework = arduino lib_deps = ; PlatformIO 4.x -; WiFiNINA_Generic@~1.8.14-6 -; WiFiWebServer@~1.9.5 -; EthernetWebServer@~2.2.3 -; Ethernet_Generic@>=2.6.1 -; EthernetENC@~2.0.2 +; WiFiNINA_Generic@~1.8.15-1 +; WiFiWebServer@~1.10.1 +; EthernetWebServer@~2.3.0 +; Ethernet_Generic@>=2.7.1 +; EthernetENC@~2.0.3 ; UIPEthernet@~2.0.12 -; https://github.com/khoih-prog/WiFi101 -; WebSockets_Generic@>=2.16.0 +; WiFi101_Generic@~1.0.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform - khoih-prog/WiFiNINA_Generic@~1.8.14-6 - khoih-prog/WiFiWebServer@~1.9.5 - khoih-prog/EthernetWebServer@~2.2.3 - khoih-prog/Ethernet_Generic@~2.6.1 + khoih-prog/WiFiNINA_Generic@~1.8.15-1 + khoih-prog/WiFiWebServer@~1.10.1 + khoih-prog/EthernetWebServer@~2.3.0 + khoih-prog/Ethernet_Generic@~2.7.1 jandrassy/EthernetENC@~2.0.3 UIPEthernet/UIPEthernet@~2.0.12 - https://github.com/khoih-prog/WiFi101 - khoih-prog/WebSockets_Generic@>=2.16.0 + khoih-prog/WiFi101_Generic@~1.0.0 + khoih-prog/WebSockets_Generic@>=2.16.1 ; ============================================================ ; Choose your board by uncommenting one of the following lines @@ -312,23 +312,23 @@ framework = arduino lib_deps = ; PlatformIO 4.x -; WiFiNINA_Generic@~1.8.14-6 -; WiFiWebServer@~1.9.5 -; EthernetWebServer@~2.2.3 -; Ethernet_Generic@>=2.6.1 -; EthernetENC@~2.0.2 +; WiFiNINA_Generic@~1.8.15-1 +; WiFiWebServer@~1.10.1 +; EthernetWebServer@~2.3.0 +; Ethernet_Generic@>=2.7.1 +; EthernetENC@~2.0.3 ; UIPEthernet@~2.0.12 -; WebSockets_Generic@>=2.16.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform - khoih-prog/WiFiNINA_Generic@~1.8.14-6 - khoih-prog/WiFiWebServer@~1.9.5 - khoih-prog/EthernetWebServer@~2.2.3 - khoih-prog/Ethernet_Generic@~2.6.1 + khoih-prog/WiFiNINA_Generic@~1.8.15-1 + khoih-prog/WiFiWebServer@~1.10.1 + khoih-prog/EthernetWebServer@~2.3.0 + khoih-prog/Ethernet_Generic@~2.7.1 jandrassy/EthernetENC@~2.0.3 UIPEthernet/UIPEthernet@~2.0.12 - khoih-prog/WebSockets_Generic@>=2.16.0 + khoih-prog/WebSockets_Generic@>=2.16.1 ; ============================================================ @@ -356,13 +356,13 @@ lib_deps = ; PlatformIO 4.x ; STM32duino LwIP@>=2.1.2 ; STM32duino STM32Ethernet@>=1.3.0 -; WebSockets_Generic@>=2.16.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform stm32duino/STM32duino LwIP@>=2.1.2 stm32duino/STM32duino STM32Ethernet@>=1.3.0 - khoih-prog/WebSockets_Generic@>=2.16.0 + khoih-prog/WebSockets_Generic@>=2.16.1 ; ============================================================ @@ -480,23 +480,22 @@ upload_protocol = picotool lib_deps = ; PlatformIO 4.x -; WiFiNINA_Generic@~1.8.14-6 -; WiFiWebServer@~1.9.5 -; EthernetWebServer@~2.2.3 -; Ethernet_Generic@>=2.6.1 -; EthernetENC@~2.0.2 +; WiFiNINA_Generic@~1.8.15-1 +; WiFiWebServer@~1.10.1 +; EthernetWebServer@~2.3.0 +; Ethernet_Generic@>=2.7.1 +; EthernetENC@~2.0.3 ; UIPEthernet@~2.0.12 -; WebSockets_Generic@>=2.16.0 +; WebSockets_Generic@>=2.16.1 ; PlatformIO 5.x ; To modify or add as necessary for each platform - khoih-prog/WiFiNINA_Generic@~1.8.14-6 - khoih-prog/WiFiWebServer@~1.9.5 - khoih-prog/EthernetWebServer@~2.2.3 - khoih-prog/Ethernet_Generic@~2.6.1 + khoih-prog/WiFiNINA_Generic@~1.8.15-1 + khoih-prog/WiFiWebServer@~1.10.1 + khoih-prog/EthernetWebServer@~2.3.0 jandrassy/EthernetENC@~2.0.3 UIPEthernet/UIPEthernet@~2.0.12 - khoih-prog/WebSockets_Generic@>=2.16.0 + khoih-prog/WebSockets_Generic@>=2.16.1 ; ============================================================ ; Board configuration Many more Boards to be filled diff --git a/src/MQTTPubSubClient_Generic.h b/src/MQTTPubSubClient_Generic.h index fce4a86..f41be1c 100644 --- a/src/MQTTPubSubClient_Generic.h +++ b/src/MQTTPubSubClient_Generic.h @@ -1,20 +1,20 @@ /**************************************************************************************************************************** MQTTPubSubClient_Generic.h - + MQTT and MQTT over WebSoket Client for Arduino - - For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, + + For nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP32, ESP32_S2, ESP32_S3 and ESP32_C3) and WT32_ETH01 - + Ethernet shields W5100, W5200, W5500, ENC28J60, Teensy 4.1 NativeEthernet/QNEthernet. - + Based on and modified from MQTTPubSubClient Library (https://github.com/hideakitai/MQTTPubSubClient) - + Built by Khoi Hoang https://github.com/khoih-prog/MQTTPubSubClient_Generic Licensed under MIT license - + Version: 1.2.0 - + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 23/03/2022 Initial porting and coding to add support to many boards, using Ethernet or WiFi @@ -45,9 +45,9 @@ #include "MQTTPubSubClient_Generic_Debug.h" -extern "C" +extern "C" { - #include "MQTTPubSubClient_Generic/lwmqtt/lwmqtt.h" +#include "MQTTPubSubClient_Generic/lwmqtt/lwmqtt.h" } #include "MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits.h" @@ -63,9 +63,9 @@ extern "C" //////////////////////////////////////// -namespace arduino +namespace arduino { -namespace mqtt +namespace mqtt { #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS @@ -78,680 +78,694 @@ namespace mqtt //////////////////////////////////////// template -class PubSubClient +class PubSubClient { - // ---------- lwmqtt interface types ---------- - // these structs are saved to inside of lwmqtt and invoked as (void*) - // and inside of our static methods, these void* are converted to this struct and used - - typedef uint32_t (*MQTTClientClockSource)(); - - //////////////////////////////////////// - - typedef struct - { - uint32_t start_ms; - uint32_t timeout_ms; - MQTTClientClockSource millis; - } lwmqtt_arduino_timer_t; - - //////////////////////////////////////// - - typedef struct - { - ClientType* client; - + // ---------- lwmqtt interface types ---------- + // these structs are saved to inside of lwmqtt and invoked as (void*) + // and inside of our static methods, these void* are converted to this struct and used + + typedef uint32_t (*MQTTClientClockSource)(); + + //////////////////////////////////////// + + typedef struct + { + uint32_t start_ms; + uint32_t timeout_ms; + MQTTClientClockSource millis; + } lwmqtt_arduino_timer_t; + + //////////////////////////////////////// + + typedef struct + { + ClientType* client; + #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11 +#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11 std::deque buffer; - #else +#else arx::deque buffer; - #endif #endif - } lwmqtt_arduino_network_t; +#endif + } lwmqtt_arduino_network_t; - //////////////////////////////////////// + //////////////////////////////////////// - typedef struct - { - PubSubClient* mqtt_client; - } lwmqtt_arduino_client_callback_t; + typedef struct + { + PubSubClient* mqtt_client; + } lwmqtt_arduino_client_callback_t; + + //////////////////////////////////////// - //////////////////////////////////////// + // ---------- interfaces between lwmqtt and library ---------- - // ---------- interfaces between lwmqtt and library ---------- + uint8_t lwmqtt_read_buf[BUFFER_SIZE]; + uint8_t lwmqtt_write_buf[BUFFER_SIZE]; - uint8_t lwmqtt_read_buf[BUFFER_SIZE]; - uint8_t lwmqtt_write_buf[BUFFER_SIZE]; - #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - lwmqtt_arduino_network_t lwmqtt_network {nullptr, {0}}; + lwmqtt_arduino_network_t lwmqtt_network {nullptr, {0}}; #else - lwmqtt_arduino_network_t lwmqtt_network {nullptr}; + lwmqtt_arduino_network_t lwmqtt_network {nullptr}; #endif - lwmqtt_arduino_timer_t lwmqtt_timer1 {0, 0, nullptr}; - lwmqtt_arduino_timer_t lwmqtt_timer2 {0, 0, nullptr}; - lwmqtt_client_t lwmqtt_client {lwmqtt_client_t()}; - lwmqtt_arduino_client_callback_t lwmqtt_callback; + lwmqtt_arduino_timer_t lwmqtt_timer1 {0, 0, nullptr}; + lwmqtt_arduino_timer_t lwmqtt_timer2 {0, 0, nullptr}; + lwmqtt_client_t lwmqtt_client {lwmqtt_client_t()}; + lwmqtt_arduino_client_callback_t lwmqtt_callback; + + // ---------- PubSubClient members ---------- - // ---------- PubSubClient members ---------- + using global_callback_t = std::function; + using topic_callback_t = std::function; - using global_callback_t = std::function; - using topic_callback_t = std::function; - #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11 - using TopicCallbacks = std::map; + using TopicCallbacks = std::map; #else - using TopicCallbacks = arx::map; + using TopicCallbacks = arx::map; #endif - // required variables - ClientType* client {nullptr}; - lwmqtt_will_t* will {nullptr}; - String will_topic; - String will_payload; - global_callback_t global_callback; - TopicCallbacks callbacks; - uint32_t prev_keep_alive_ms {0}; - uint32_t keep_alive_interval_ms {10}; + // required variables + ClientType* client {nullptr}; + lwmqtt_will_t* will {nullptr}; + String will_topic; + String will_payload; + global_callback_t global_callback; + TopicCallbacks callbacks; + uint32_t prev_keep_alive_ms {0}; + uint32_t keep_alive_interval_ms {10}; - // options - uint32_t timeout_ms {1000}; - uint16_t keep_alive_timeout_sec {100}; - bool should_clean_session {true}; + // options + uint32_t timeout_ms {1000}; + uint16_t keep_alive_timeout_sec {100}; + bool should_clean_session {true}; - // status - bool is_connected {false}; - lwmqtt_return_code_t return_code {(lwmqtt_return_code_t)0}; - lwmqtt_err_t last_error {(lwmqtt_err_t)0}; + // status + bool is_connected {false}; + lwmqtt_return_code_t return_code {(lwmqtt_return_code_t)0}; + lwmqtt_err_t last_error {(lwmqtt_err_t)0}; -public: + public: - //////////////////////////////////////// + //////////////////////////////////////// - ~PubSubClient() - { - clearWill(); - } + ~PubSubClient() + { + clearWill(); + } - //////////////////////////////////////// + //////////////////////////////////////// - void begin(ClientType& client) - { - init(client); - } + void begin(ClientType& client) + { + init(client); + } - //////////////////////////////////////// + //////////////////////////////////////// - bool connect(const String& client_id, const String& user = "", const String& pass = "") - { + bool connect(const String& client_id, const String& user = "", const String& pass = "") + { #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - while (!client->isConnected()) - { - client->loop(); - - delay(10); - } + + while (!client->isConnected()) + { + client->loop(); + + delay(10); + } + #endif - lwmqtt_options_t options = lwmqtt_default_options; - options.keep_alive = keep_alive_timeout_sec; - options.clean_session = should_clean_session; - options.client_id = lwmqtt_string(client_id.c_str()); - - if (user.length()) - { - options.username = lwmqtt_string(user.c_str()); - - if (pass.length()) + lwmqtt_options_t options = lwmqtt_default_options; + options.keep_alive = keep_alive_timeout_sec; + options.clean_session = should_clean_session; + options.client_id = lwmqtt_string(client_id.c_str()); + + if (user.length()) { - options.password = lwmqtt_string(pass.c_str()); + options.username = lwmqtt_string(user.c_str()); + + if (pass.length()) + { + options.password = lwmqtt_string(pass.c_str()); + } + } + + last_error = lwmqtt_connect(&lwmqtt_client, options, will, &return_code, timeout_ms); + + MQTT_LOGDEBUG1("connect: last_error =", last_error); + + if (last_error != LWMQTT_SUCCESS) + { + close(); + + return false; } + + is_connected = true; + + return true; } - last_error = lwmqtt_connect(&lwmqtt_client, options, will, &return_code, timeout_ms); - - MQTT_LOGDEBUG1("connect: last_error =", last_error); - - if (last_error != LWMQTT_SUCCESS) + //////////////////////////////////////// + + bool publish(const String& topic, const String& payload, const bool retained = false, const int& qos = 0) { - close(); - - return false; - } - - is_connected = true; - - return true; - } - - //////////////////////////////////////// - - bool publish(const String& topic, const String& payload, const bool retained = false, const int& qos = 0) - { - return publish(topic.c_str(), (uint8_t*)payload.c_str(), payload.length(), retained, qos); - } - - //////////////////////////////////////// - - bool publish(const String& topic, uint8_t* payload, const size_t length, const bool retained = false, const uint8_t qos = 0) - { - if (!isConnected()) - { - MQTT_LOGERROR("PubSubClient::publish: Error, not connected"); - - return false; + return publish(topic.c_str(), (uint8_t*)payload.c_str(), payload.length(), retained, qos); } - lwmqtt_message_t message = lwmqtt_default_message; - message.payload = payload; - message.payload_len = length; - message.retained = retained; - message.qos = lwmqtt_qos_t(qos); + //////////////////////////////////////// - last_error = lwmqtt_publish(&lwmqtt_client, lwmqtt_string(topic.c_str()), message, timeout_ms); - - if (last_error != LWMQTT_SUCCESS) + bool publish(const String& topic, uint8_t* payload, const size_t length, const bool retained = false, + const uint8_t qos = 0) { - MQTT_LOGERROR("PubSubClient::publish: Error, not LWMQTT_SUCCESS"); - - close(); - - return false; - } - - return true; - } + if (!isConnected()) + { + MQTT_LOGERROR("PubSubClient::publish: Error, not connected"); - //////////////////////////////////////// + return false; + } - void subscribe(const global_callback_t& cb) - { - global_callback = cb; - } + lwmqtt_message_t message = lwmqtt_default_message; + message.payload = payload; + message.payload_len = length; + message.retained = retained; + message.qos = lwmqtt_qos_t(qos); - //////////////////////////////////////// + last_error = lwmqtt_publish(&lwmqtt_client, lwmqtt_string(topic.c_str()), message, timeout_ms); - bool subscribe(const String& topic, const topic_callback_t& cb) - { - return subscribe(topic, 0, cb); - } + if (last_error != LWMQTT_SUCCESS) + { + MQTT_LOGERROR("PubSubClient::publish: Error, not LWMQTT_SUCCESS"); - //////////////////////////////////////// + close(); - bool subscribe(const String& topic, const uint8_t& qos, const topic_callback_t& cb) - { - if (!isConnected()) - { - MQTT_LOGERROR("PubSubClient::subscribe: Error, not connected"); - - return false; - } + return false; + } - last_error = lwmqtt_subscribe_one(&lwmqtt_client, lwmqtt_string(topic.c_str()), (lwmqtt_qos_t)qos, timeout_ms); - - if (last_error != LWMQTT_SUCCESS) - { - MQTT_LOGERROR("PubSubClient::subscribe: Error, not LWMQTT_SUCCESS"); - - close(); - - return false; + return true; } - - callbacks[topic] = cb; - - return true; - } - //////////////////////////////////////// + //////////////////////////////////////// - bool unsubscribe(const String& topic) - { - if (!isConnected()) + void subscribe(const global_callback_t& cb) { - MQTT_LOGERROR("PubSubClient::unsubscribe: Error, not connected"); - - return false; + global_callback = cb; } - last_error = lwmqtt_unsubscribe_one(&lwmqtt_client, lwmqtt_string(topic.c_str()), timeout_ms); - - if (last_error != LWMQTT_SUCCESS) + //////////////////////////////////////// + + bool subscribe(const String& topic, const topic_callback_t& cb) { - MQTT_LOGERROR("PubSubClient::unsubscribe: Error, not LWMQTT_SUCCESS"); - - close(); - - return false; + return subscribe(topic, 0, cb); } - - callbacks.erase(topic); - - return true; - } - //////////////////////////////////////// + //////////////////////////////////////// - bool update() - { - if (!isConnected()) + bool subscribe(const String& topic, const uint8_t& qos, const topic_callback_t& cb) { - MQTT_LOGERROR("PubSubClient::update: Error, not connected"); - - return false; - } + if (!isConnected()) + { + MQTT_LOGERROR("PubSubClient::subscribe: Error, not connected"); -#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - client->loop(); - // data is already received in onEvent callback - auto available = lwmqtt_network.buffer.size(); -#else - auto available = (size_t)client->available(); -#endif + return false; + } - if (available > 0) - { - last_error = lwmqtt_yield(&lwmqtt_client, available, timeout_ms); - - if (last_error != LWMQTT_SUCCESS) + last_error = lwmqtt_subscribe_one(&lwmqtt_client, lwmqtt_string(topic.c_str()), (lwmqtt_qos_t)qos, timeout_ms); + + if (last_error != LWMQTT_SUCCESS) { - MQTT_LOGERROR("PubSubClient::update: Error1, not LWMQTT_SUCCESS"); - + MQTT_LOGERROR("PubSubClient::subscribe: Error, not LWMQTT_SUCCESS"); + close(); - + return false; } + + callbacks[topic] = cb; + + return true; } - uint32_t now_ms = millis(); - - if (now_ms > prev_keep_alive_ms + keep_alive_interval_ms) + //////////////////////////////////////// + + bool unsubscribe(const String& topic) { - prev_keep_alive_ms = now_ms; - last_error = lwmqtt_keep_alive(&lwmqtt_client, timeout_ms); - - if (last_error != LWMQTT_SUCCESS) + if (!isConnected()) { - MQTT_LOGERROR("PubSubClient::update: Error2, not LWMQTT_SUCCESS"); - + MQTT_LOGERROR("PubSubClient::unsubscribe: Error, not connected"); + + return false; + } + + last_error = lwmqtt_unsubscribe_one(&lwmqtt_client, lwmqtt_string(topic.c_str()), timeout_ms); + + if (last_error != LWMQTT_SUCCESS) + { + MQTT_LOGERROR("PubSubClient::unsubscribe: Error, not LWMQTT_SUCCESS"); + close(); + return false; - } - } - - return true; - } + } - //////////////////////////////////////// + callbacks.erase(topic); - void setClockSource(const MQTTClientClockSource& cb) - { - lwmqtt_timer1.millis = cb; - lwmqtt_timer2.millis = cb; - } + return true; + } - //////////////////////////////////////// + //////////////////////////////////////// - void setWill(const String& topic, const String& payload = "", const bool retained = false, const uint8_t& qos = 0) - { - if (topic.length() == 0) - return; + bool update() + { + if (!isConnected()) + { + MQTT_LOGERROR("PubSubClient::update: Error, not connected"); - clearWill(); + return false; + } - will = (lwmqtt_will_t*)malloc(sizeof(lwmqtt_will_t)); - memset(will, 0, sizeof(lwmqtt_will_t)); +#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS + client->loop(); + // data is already received in onEvent callback + auto available = lwmqtt_network.buffer.size(); +#else + auto available = (size_t)client->available(); +#endif - // save topic and payload to use them later - will_topic = topic; - will_payload = payload; + if (available > 0) + { + last_error = lwmqtt_yield(&lwmqtt_client, available, timeout_ms); - will->topic = lwmqtt_string(will_topic.c_str()); - - if (will_payload.length() > 0) - will->payload = lwmqtt_string(will_payload.c_str()); - - will->retained = retained; - will->qos = (lwmqtt_qos_t)qos; - } + if (last_error != LWMQTT_SUCCESS) + { + MQTT_LOGERROR("PubSubClient::update: Error1, not LWMQTT_SUCCESS"); - //////////////////////////////////////// + close(); - void clearWill() - { - if (will != nullptr) - { - free(will); - will = nullptr; - } - } + return false; + } + } - //////////////////////////////////////// + uint32_t now_ms = millis(); - void setKeepAliveSendInterval(const uint32_t& ms) - { - keep_alive_interval_ms = ms; - } + if (now_ms > prev_keep_alive_ms + keep_alive_interval_ms) + { + prev_keep_alive_ms = now_ms; + last_error = lwmqtt_keep_alive(&lwmqtt_client, timeout_ms); - //////////////////////////////////////// + if (last_error != LWMQTT_SUCCESS) + { + MQTT_LOGERROR("PubSubClient::update: Error2, not LWMQTT_SUCCESS"); - void setKeepAliveTimeout(const uint16_t& sec) - { - keep_alive_timeout_sec = sec; - } + close(); + return false; + } + } - //////////////////////////////////////// + return true; + } + + //////////////////////////////////////// - void setCleanSession(const bool b) - { - should_clean_session = b; - } + void setClockSource(const MQTTClientClockSource& cb) + { + lwmqtt_timer1.millis = cb; + lwmqtt_timer2.millis = cb; + } - //////////////////////////////////////// + //////////////////////////////////////// - void setTimeout(const uint32_t& ms) - { - timeout_ms = ms; - } + void setWill(const String& topic, const String& payload = "", const bool retained = false, const uint8_t& qos = 0) + { + if (topic.length() == 0) + return; - //////////////////////////////////////// + clearWill(); - void setOptions(const uint16_t& keep_alive_timeout_sec, const bool b_clean_session, const uint32_t& timeout_ms) - { - setKeepAliveTimeout(keep_alive_timeout_sec); - setCleanSession(b_clean_session); - setTimeout(timeout_ms); - } + will = (lwmqtt_will_t*)malloc(sizeof(lwmqtt_will_t)); + memset(will, 0, sizeof(lwmqtt_will_t)); - //////////////////////////////////////// + // save topic and payload to use them later + will_topic = topic; + will_payload = payload; - bool isConnected() const - { -#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - return client->isConnected() && (is_connected); -#else - return (client->connected() == 1) && (is_connected); -#endif - } + will->topic = lwmqtt_string(will_topic.c_str()); - //////////////////////////////////////// + if (will_payload.length() > 0) + will->payload = lwmqtt_string(will_payload.c_str()); - bool disconnect() - { - if (!isConnected()) - { - MQTT_LOGERROR("PubSubClient::disconnect: Error, not connected"); - - return false; + will->retained = retained; + will->qos = (lwmqtt_qos_t)qos; } - last_error = lwmqtt_disconnect(&lwmqtt_client, timeout_ms); - close(); - - return last_error == LWMQTT_SUCCESS; - } - - //////////////////////////////////////// - - lwmqtt_err_t getLastError() const - { - return last_error; - } - - //////////////////////////////////////// - - lwmqtt_return_code_t getReturnCode() const - { - return return_code; - } - - //////////////////////////////////////// - -private: - - //////////////////////////////////////// - - void init(ClientType& c) - { - client = &c; - lwmqtt_network.client = &c; - lwmqtt_callback.mqtt_client = this; - lwmqtt_init(&lwmqtt_client, lwmqtt_write_buf, BUFFER_SIZE, lwmqtt_read_buf, BUFFER_SIZE); - lwmqtt_set_timers(&lwmqtt_client, &lwmqtt_timer1, &lwmqtt_timer2, lwmqtt_arduino_timer_set, lwmqtt_arduino_timer_get); - lwmqtt_set_network(&lwmqtt_client, &lwmqtt_network, lwmqtt_arduino_network_read, lwmqtt_arduino_network_write); - lwmqtt_set_callback(&lwmqtt_client, (void*)&lwmqtt_callback, lwmqtt_arduino_mqtt_client_handler); - -#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - c.onEvent([&](WStype_t type, uint8_t* payload, size_t length) + //////////////////////////////////////// + + void clearWill() { - switch (type) + if (will != nullptr) { - case WStype_DISCONNECTED: - break; - case WStype_CONNECTED: - break; - case WStype_TEXT: - case WStype_BIN: - // copy to temprary buffer - for (size_t i = 0; i < length; ++i) - lwmqtt_network.buffer.emplace_back(payload[i]); - break; - case WStype_ERROR: - case WStype_FRAGMENT_TEXT_START: - case WStype_FRAGMENT_BIN_START: - case WStype_FRAGMENT: - case WStype_FRAGMENT_FIN: - case WStype_PING: - case WStype_PONG: - break; - default: - break; + free(will); + will = nullptr; } - }); -#endif - } - - //////////////////////////////////////// + } - void close() - { - is_connected = false; - -#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - client->disconnect(); -#else - client->stop(); -#endif - } + //////////////////////////////////////// - //////////////////////////////////////// + void setKeepAliveSendInterval(const uint32_t& ms) + { + keep_alive_interval_ms = ms; + } - // ---------- lwmqtt callbacks ---------- + //////////////////////////////////////// - static void lwmqtt_arduino_timer_set(void* ref, uint32_t timeout_ms) - { - // cast timer reference - auto t = (lwmqtt_arduino_timer_t*)ref; - - // set timeout - t->timeout_ms = timeout_ms; - - // set start - if (t->millis != nullptr) + void setKeepAliveTimeout(const uint16_t& sec) { - t->start_ms = t->millis(); - } - else + keep_alive_timeout_sec = sec; + } + + //////////////////////////////////////// + + void setCleanSession(const bool b) { - t->start_ms = millis(); + should_clean_session = b; } - } - //////////////////////////////////////// + //////////////////////////////////////// - static int32_t lwmqtt_arduino_timer_get(void* ref) - { - // cast timer reference - auto t = (lwmqtt_arduino_timer_t*)ref; - // get now - uint32_t now; - - if (t->millis != nullptr) + void setTimeout(const uint32_t& ms) { - now = t->millis(); - } - else + timeout_ms = ms; + } + + //////////////////////////////////////// + + void setOptions(const uint16_t& keep_alive_timeout_sec, const bool b_clean_session, const uint32_t& timeout_ms) { - now = millis(); + setKeepAliveTimeout(keep_alive_timeout_sec); + setCleanSession(b_clean_session); + setTimeout(timeout_ms); } - - // get difference (account for roll-overs) - uint32_t diff; - - if (now < t->start_ms) + + //////////////////////////////////////// + + bool isConnected() const { - diff = UINT32_MAX - t->start_ms + now; - } - else +#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS + return client->isConnected() && (is_connected); +#else + return (client->connected() == 1) && (is_connected); +#endif + } + + //////////////////////////////////////// + + bool disconnect() { - diff = now - t->start_ms; + if (!isConnected()) + { + MQTT_LOGERROR("PubSubClient::disconnect: Error, not connected"); + + return false; + } + + last_error = lwmqtt_disconnect(&lwmqtt_client, timeout_ms); + close(); + + return last_error == LWMQTT_SUCCESS; } - - // return relative time - if (diff > t->timeout_ms) + + //////////////////////////////////////// + + lwmqtt_err_t getLastError() const { - return -(diff - t->timeout_ms); - } - else + return last_error; + } + + //////////////////////////////////////// + + lwmqtt_return_code_t getReturnCode() const { - return t->timeout_ms - diff; + return return_code; } - } - - //////////////////////////////////////// - - static lwmqtt_err_t lwmqtt_arduino_network_read(void* ref, uint8_t* buffer, size_t len, size_t* read, uint32_t timeout_ms) - { - // cast network reference - auto n = (lwmqtt_arduino_network_t*)ref; - // set timeout - uint32_t start_ms = millis(); - // reset counter - *read = 0; - - // read until all bytes have been read or timeout has been reached - while (len > 0 && (millis() < start_ms + timeout_ms)) + + //////////////////////////////////////// + + private: + + //////////////////////////////////////// + + void init(ClientType& c) { + client = &c; + lwmqtt_network.client = &c; + lwmqtt_callback.mqtt_client = this; + lwmqtt_init(&lwmqtt_client, lwmqtt_write_buf, BUFFER_SIZE, lwmqtt_read_buf, BUFFER_SIZE); + lwmqtt_set_timers(&lwmqtt_client, &lwmqtt_timer1, &lwmqtt_timer2, lwmqtt_arduino_timer_set, lwmqtt_arduino_timer_get); + lwmqtt_set_network(&lwmqtt_client, &lwmqtt_network, lwmqtt_arduino_network_read, lwmqtt_arduino_network_write); + lwmqtt_set_callback(&lwmqtt_client, (void*)&lwmqtt_callback, lwmqtt_arduino_mqtt_client_handler); + #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - n->client->loop(); - // received data are already read to read_buf in onEvent callback - int r = 0; - - if (n->buffer.size() >= len) + c.onEvent([&](WStype_t type, uint8_t* payload, size_t length) { - for (size_t i = 0; i < len; ++i) - buffer[i] = n->buffer[i]; - - for (size_t i = 0; i < len; ++i) - n->buffer.pop_front(); - - r = len; - } -#else - // read from connection - int r = n->client->read(buffer, len); + switch (type) + { + case WStype_DISCONNECTED: + break; + + case WStype_CONNECTED: + break; + + case WStype_TEXT: + case WStype_BIN: + + // copy to temprary buffer + for (size_t i = 0; i < length; ++i) + lwmqtt_network.buffer.emplace_back(payload[i]); + + break; + + case WStype_ERROR: + case WStype_FRAGMENT_TEXT_START: + case WStype_FRAGMENT_BIN_START: + case WStype_FRAGMENT: + case WStype_FRAGMENT_FIN: + case WStype_PING: + case WStype_PONG: + break; + + default: + break; + } + }); #endif - // handle read data - if (r > 0) - { - buffer += r; - *read += r; - len -= r; - - continue; - } + } + + //////////////////////////////////////// + + void close() + { + is_connected = false; - // wait/unblock for some time (RTOS based boards may otherwise fail since - // the wifi task cannot provide the data) - delay(0); - - // otherwise check status #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - if (!n->client->isConnected()) + client->disconnect(); #else - if (!n->client->connected()) + client->stop(); #endif + } + + //////////////////////////////////////// + + // ---------- lwmqtt callbacks ---------- + + static void lwmqtt_arduino_timer_set(void* ref, uint32_t timeout_ms) + { + // cast timer reference + auto t = (lwmqtt_arduino_timer_t*)ref; + + // set timeout + t->timeout_ms = timeout_ms; + + // set start + if (t->millis != nullptr) + { + t->start_ms = t->millis(); + } + else { - return LWMQTT_NETWORK_FAILED_READ; + t->start_ms = millis(); } } - - // check counter - if (*read == 0) + + //////////////////////////////////////// + + static int32_t lwmqtt_arduino_timer_get(void* ref) { - return LWMQTT_NETWORK_TIMEOUT; + // cast timer reference + auto t = (lwmqtt_arduino_timer_t*)ref; + // get now + uint32_t now; + + if (t->millis != nullptr) + { + now = t->millis(); + } + else + { + now = millis(); + } + + // get difference (account for roll-overs) + uint32_t diff; + + if (now < t->start_ms) + { + diff = UINT32_MAX - t->start_ms + now; + } + else + { + diff = now - t->start_ms; + } + + // return relative time + if (diff > t->timeout_ms) + { + return -(diff - t->timeout_ms); + } + else + { + return t->timeout_ms - diff; + } } - - return LWMQTT_SUCCESS; - } - - //////////////////////////////////////// - - static lwmqtt_err_t lwmqtt_arduino_network_write(void* ref, uint8_t* buffer, size_t len, size_t* sent, uint32_t /*timeout_ms*/) - { - // cast network reference - auto n = (lwmqtt_arduino_network_t*)ref; - - // write bytes + + //////////////////////////////////////// + + static lwmqtt_err_t lwmqtt_arduino_network_read(void* ref, uint8_t* buffer, size_t len, size_t* read, + uint32_t timeout_ms) + { + // cast network reference + auto n = (lwmqtt_arduino_network_t*)ref; + // set timeout + uint32_t start_ms = millis(); + // reset counter + *read = 0; + + // read until all bytes have been read or timeout has been reached + while (len > 0 && (millis() < start_ms + timeout_ms)) + { #ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS - bool b = n->client->sendBIN(buffer, len); - *sent = b ? len : 0; + n->client->loop(); + // received data are already read to read_buf in onEvent callback + int r = 0; + + if (n->buffer.size() >= len) + { + for (size_t i = 0; i < len; ++i) + buffer[i] = n->buffer[i]; + + for (size_t i = 0; i < len; ++i) + n->buffer.pop_front(); + + r = len; + } + #else - *sent = n->client->write(buffer, len); + // read from connection + int r = n->client->read(buffer, len); #endif - if (*sent <= 0) - { - return LWMQTT_NETWORK_FAILED_WRITE; - } - - return LWMQTT_SUCCESS; - } + // handle read data + if (r > 0) + { + buffer += r; + *read += r; + len -= r; - //////////////////////////////////////// + continue; + } - // this function will be called inside of lwmqtt when packet has come - static void lwmqtt_arduino_mqtt_client_handler(lwmqtt_client_t* /*client*/, void* ref, - lwmqtt_string_t topic, lwmqtt_message_t message) - { - // null terminate topic - char terminated_topic[topic.len + 1]; - memcpy(terminated_topic, topic.data, topic.len); - terminated_topic[topic.len] = '\0'; + // wait/unblock for some time (RTOS based boards may otherwise fail since + // the wifi task cannot provide the data) + delay(0); - // null terminate payload if available - if (message.payload != nullptr) - { - message.payload[message.payload_len] = '\0'; + // otherwise check status +#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS + + if (!n->client->isConnected()) +#else + if (!n->client->connected()) +#endif + { + return LWMQTT_NETWORK_FAILED_READ; + } + } + + // check counter + if (*read == 0) + { + return LWMQTT_NETWORK_TIMEOUT; + } + + return LWMQTT_SUCCESS; } - auto cb = (lwmqtt_arduino_client_callback_t*)ref; - - if (cb->mqtt_client->global_callback) + //////////////////////////////////////// + + static lwmqtt_err_t lwmqtt_arduino_network_write(void* ref, uint8_t* buffer, size_t len, size_t* sent, + uint32_t /*timeout_ms*/) { - cb->mqtt_client->global_callback(terminated_topic, (const char*)message.payload, message.payload_len); + // cast network reference + auto n = (lwmqtt_arduino_network_t*)ref; + + // write bytes +#ifdef MQTTPUBSUBCLIENT_USE_WEBSOCKETS + bool b = n->client->sendBIN(buffer, len); + *sent = b ? len : 0; +#else + *sent = n->client->write(buffer, len); +#endif + + if (*sent <= 0) + { + return LWMQTT_NETWORK_FAILED_WRITE; + } + + return LWMQTT_SUCCESS; } - - for (const auto& c : cb->mqtt_client->callbacks) + + //////////////////////////////////////// + + // this function will be called inside of lwmqtt when packet has come + static void lwmqtt_arduino_mqtt_client_handler(lwmqtt_client_t* /*client*/, void* ref, + lwmqtt_string_t topic, lwmqtt_message_t message) { - if (c.first == terminated_topic) + // null terminate topic + char terminated_topic[topic.len + 1]; + memcpy(terminated_topic, topic.data, topic.len); + terminated_topic[topic.len] = '\0'; + + // null terminate payload if available + if (message.payload != nullptr) + { + message.payload[message.payload_len] = '\0'; + } + + auto cb = (lwmqtt_arduino_client_callback_t*)ref; + + if (cb->mqtt_client->global_callback) + { + cb->mqtt_client->global_callback(terminated_topic, (const char*)message.payload, message.payload_len); + } + + for (const auto& c : cb->mqtt_client->callbacks) { - c.second((const char*)message.payload, message.payload_len); + if (c.first == terminated_topic) + { + c.second((const char*)message.payload, message.payload_len); + } } } - } }; //////////////////////////////////////// diff --git a/src/MQTTPubSubClient_Generic/lwmqtt/client.c b/src/MQTTPubSubClient_Generic/lwmqtt/client.c index 3cac301..2a5376f 100644 --- a/src/MQTTPubSubClient_Generic/lwmqtt/client.c +++ b/src/MQTTPubSubClient_Generic/lwmqtt/client.c @@ -101,6 +101,7 @@ static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t off size_t partial_read = 0; lwmqtt_err_t err = client->network_read(client->network, client->read_buf + offset + read, len - read, &partial_read, (uint32_t)remaining_time); + if (err != LWMQTT_SUCCESS) { return err; @@ -171,7 +172,7 @@ static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offs size_t partial_write = 0; lwmqtt_err_t err = client->network_write(client->network, client->write_buf + offset + written, len - written, - &partial_write, (uint32_t)remaining_time); + &partial_write, (uint32_t)remaining_time); if (err != LWMQTT_SUCCESS) { @@ -185,7 +186,8 @@ static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offs return LWMQTT_SUCCESS; } -static lwmqtt_err_t lwmqtt_read_packet_in_buffer(lwmqtt_client_t *client, size_t *read, lwmqtt_packet_type_t *packet_type) +static lwmqtt_err_t lwmqtt_read_packet_in_buffer(lwmqtt_client_t *client, size_t *read, + lwmqtt_packet_type_t *packet_type) { // preset packet type *packet_type = LWMQTT_NO_PACKET; @@ -313,173 +315,173 @@ static lwmqtt_err_t lwmqtt_cycle(lwmqtt_client_t *client, size_t *read, lwmqtt_p { // handle publish packets case LWMQTT_PUBLISH_PACKET: + { + // decode publish packet + bool dup; + uint16_t packet_id; + lwmqtt_string_t topic; + lwmqtt_message_t msg; + err = lwmqtt_decode_publish(client->read_buf, client->read_buf_size, &dup, &packet_id, &topic, &msg); + + if (err != LWMQTT_SUCCESS) { - // decode publish packet - bool dup; - uint16_t packet_id; - lwmqtt_string_t topic; - lwmqtt_message_t msg; - err = lwmqtt_decode_publish(client->read_buf, client->read_buf_size, &dup, &packet_id, &topic, &msg); - - if (err != LWMQTT_SUCCESS) - { - return err; - } - - // call callback if set - if (client->callback != NULL) - { - client->callback(client, client->callback_ref, topic, msg); - } - - // break early on qos zero - if (msg.qos == LWMQTT_QOS0) - { - break; - } - - // define ack packet - lwmqtt_packet_type_t ack_type = LWMQTT_NO_PACKET; - - if (msg.qos == LWMQTT_QOS1) - { - ack_type = LWMQTT_PUBACK_PACKET; - } - else if (msg.qos == LWMQTT_QOS2) - { - ack_type = LWMQTT_PUBREC_PACKET; - } - - // encode ack packet - size_t len; - err = lwmqtt_encode_ack(client->write_buf, client->write_buf_size, &len, ack_type, false, packet_id); - - if (err != LWMQTT_SUCCESS) - { - return err; - } - - // send ack packet - err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) - { - return err; - } + return err; + } + // call callback if set + if (client->callback != NULL) + { + client->callback(client, client->callback_ref, topic, msg); + } + + // break early on qos zero + if (msg.qos == LWMQTT_QOS0) + { break; } - // handle pubrec packets - case LWMQTT_PUBREC_PACKET: + // define ack packet + lwmqtt_packet_type_t ack_type = LWMQTT_NO_PACKET; + + if (msg.qos == LWMQTT_QOS1) { - // decode pubrec packet - bool dup; - uint16_t packet_id; - err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_PUBREC_PACKET, &dup, &packet_id); + ack_type = LWMQTT_PUBACK_PACKET; + } + else if (msg.qos == LWMQTT_QOS2) + { + ack_type = LWMQTT_PUBREC_PACKET; + } - if (err != LWMQTT_SUCCESS) - { - return err; - } + // encode ack packet + size_t len; + err = lwmqtt_encode_ack(client->write_buf, client->write_buf_size, &len, ack_type, false, packet_id); - // encode pubrel packet - size_t len; - err = lwmqtt_encode_ack(client->write_buf, client->write_buf_size, &len, LWMQTT_PUBREL_PACKET, 0, packet_id); + if (err != LWMQTT_SUCCESS) + { + return err; + } - if (err != LWMQTT_SUCCESS) - { - return err; - } + // send ack packet + err = lwmqtt_send_packet_in_buffer(client, len); - // send pubrel packet - err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) + { + return err; + } - if (err != LWMQTT_SUCCESS) - { - return err; - } + break; + } - break; + // handle pubrec packets + case LWMQTT_PUBREC_PACKET: + { + // decode pubrec packet + bool dup; + uint16_t packet_id; + err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_PUBREC_PACKET, &dup, &packet_id); + + if (err != LWMQTT_SUCCESS) + { + return err; } - // handle pubrel packets - case LWMQTT_PUBREL_PACKET: + // encode pubrel packet + size_t len; + err = lwmqtt_encode_ack(client->write_buf, client->write_buf_size, &len, LWMQTT_PUBREL_PACKET, 0, packet_id); + + if (err != LWMQTT_SUCCESS) { - // decode pubrec packet - bool dup; - uint16_t packet_id; - err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_PUBREL_PACKET, &dup, &packet_id); + return err; + } - if (err != LWMQTT_SUCCESS) - { - return err; - } + // send pubrel packet + err = lwmqtt_send_packet_in_buffer(client, len); - // encode pubcomp packet - size_t len; - err = lwmqtt_encode_ack(client->write_buf, client->write_buf_size, &len, LWMQTT_PUBCOMP_PACKET, 0, packet_id); + if (err != LWMQTT_SUCCESS) + { + return err; + } - if (err != LWMQTT_SUCCESS) - { - return err; - } + break; + } + + // handle pubrel packets + case LWMQTT_PUBREL_PACKET: + { + // decode pubrec packet + bool dup; + uint16_t packet_id; + err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_PUBREL_PACKET, &dup, &packet_id); - // send pubcomp packet - err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) + { + return err; + } - if (err != LWMQTT_SUCCESS) - { - return err; - } + // encode pubcomp packet + size_t len; + err = lwmqtt_encode_ack(client->write_buf, client->write_buf_size, &len, LWMQTT_PUBCOMP_PACKET, 0, packet_id); - break; + if (err != LWMQTT_SUCCESS) + { + return err; } + // send pubcomp packet + err = lwmqtt_send_packet_in_buffer(client, len); + + if (err != LWMQTT_SUCCESS) + { + return err; + } + + break; + } + // handle pingresp packets case LWMQTT_PINGRESP_PACKET: - { - // set flag - client->pong_pending = false; + { + // set flag + client->pong_pending = false; - break; - } + break; + } // handle all other packets default: - { - break; - } + { + break; + } } return LWMQTT_SUCCESS; } static lwmqtt_err_t lwmqtt_cycle_until(lwmqtt_client_t *client, lwmqtt_packet_type_t *packet_type, size_t available, - lwmqtt_packet_type_t needle) + lwmqtt_packet_type_t needle) { // prepare counter size_t read = 0; // loop until timeout has been reached - do + do { // do one cycle lwmqtt_err_t err = lwmqtt_cycle(client, &read, packet_type); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // return when one packet has been successfully read when no availability has been given - if (needle == LWMQTT_NO_PACKET && available == 0) + if (needle == LWMQTT_NO_PACKET && available == 0) { return LWMQTT_SUCCESS; } // otherwise check if needle has been found - if (*packet_type == needle) + if (*packet_type == needle) { return LWMQTT_SUCCESS; } @@ -488,7 +490,7 @@ static lwmqtt_err_t lwmqtt_cycle_until(lwmqtt_client_t *client, lwmqtt_packet_ty return LWMQTT_SUCCESS; } -lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t timeout) +lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); @@ -496,8 +498,8 @@ lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t ti // cycle until timeout has been reached lwmqtt_packet_type_t packet_type = LWMQTT_NO_PACKET; lwmqtt_err_t err = lwmqtt_cycle_until(client, &packet_type, available, LWMQTT_NO_PACKET); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -506,7 +508,7 @@ lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t ti } lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, lwmqtt_will_t *will, - lwmqtt_return_code_t *return_code, uint32_t timeout) + lwmqtt_return_code_t *return_code, uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); @@ -526,16 +528,16 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l // encode connect packet size_t len; lwmqtt_err_t err = lwmqtt_encode_connect(client->write_buf, client->write_buf_size, &len, options, will); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // send packet err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -543,12 +545,12 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l // wait for connack packet lwmqtt_packet_type_t packet_type = LWMQTT_NO_PACKET; err = lwmqtt_cycle_until(client, &packet_type, 0, LWMQTT_CONNACK_PACKET); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; - } - else if (packet_type != LWMQTT_CONNACK_PACKET) + } + else if (packet_type != LWMQTT_CONNACK_PACKET) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -556,14 +558,14 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l // decode connack packet bool session_present; err = lwmqtt_decode_connack(client->read_buf, client->read_buf_size, &session_present, return_code); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // return error if connection was not accepted - if (*return_code != LWMQTT_CONNECTION_ACCEPTED) + if (*return_code != LWMQTT_CONNECTION_ACCEPTED) { return LWMQTT_CONNECTION_DENIED; } @@ -572,7 +574,7 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l } lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, lwmqtt_qos_t *qos, - uint32_t timeout) + uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); @@ -580,17 +582,17 @@ lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_ // encode subscribe packet size_t len; lwmqtt_err_t err = lwmqtt_encode_subscribe(client->write_buf, client->write_buf_size, &len, - lwmqtt_get_next_packet_id(client), count, topic_filter, qos); - - if (err != LWMQTT_SUCCESS) + lwmqtt_get_next_packet_id(client), count, topic_filter, qos); + + if (err != LWMQTT_SUCCESS) { return err; } // send packet err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -598,12 +600,12 @@ lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_ // wait for suback packet lwmqtt_packet_type_t packet_type = LWMQTT_NO_PACKET; err = lwmqtt_cycle_until(client, &packet_type, 0, LWMQTT_SUBACK_PACKET); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; - } - else if (packet_type != LWMQTT_SUBACK_PACKET) + } + else if (packet_type != LWMQTT_SUBACK_PACKET) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -613,16 +615,16 @@ lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_ lwmqtt_qos_t granted_qos[count]; uint16_t packet_id; err = lwmqtt_decode_suback(client->read_buf, client->read_buf_size, &packet_id, count, &suback_count, granted_qos); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check suback codes - for (int i = 0; i < suback_count; i++) + for (int i = 0; i < suback_count; i++) { - if (granted_qos[i] == LWMQTT_QOS_FAILURE) + if (granted_qos[i] == LWMQTT_QOS_FAILURE) { return LWMQTT_FAILED_SUBSCRIPTION; } @@ -632,12 +634,12 @@ lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_ } lwmqtt_err_t lwmqtt_subscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, lwmqtt_qos_t qos, - uint32_t timeout) + uint32_t timeout) { return lwmqtt_subscribe(client, 1, &topic_filter, &qos, timeout); } -lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, uint32_t timeout) +lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); @@ -645,17 +647,17 @@ lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_strin // encode unsubscribe packet size_t len; lwmqtt_err_t err = lwmqtt_encode_unsubscribe(client->write_buf, client->write_buf_size, &len, - lwmqtt_get_next_packet_id(client), count, topic_filter); - - if (err != LWMQTT_SUCCESS) + lwmqtt_get_next_packet_id(client), count, topic_filter); + + if (err != LWMQTT_SUCCESS) { return err; } // send unsubscribe packet err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -663,12 +665,12 @@ lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_strin // wait for unsuback packet lwmqtt_packet_type_t packet_type = LWMQTT_NO_PACKET; err = lwmqtt_cycle_until(client, &packet_type, 0, LWMQTT_UNSUBACK_PACKET); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; - } - else if (packet_type != LWMQTT_UNSUBACK_PACKET) + } + else if (packet_type != LWMQTT_UNSUBACK_PACKET) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -677,8 +679,8 @@ lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_strin bool dup; uint16_t packet_id; err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_UNSUBACK_PACKET, &dup, &packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -686,21 +688,21 @@ lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_strin return LWMQTT_SUCCESS; } -lwmqtt_err_t lwmqtt_unsubscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, uint32_t timeout) +lwmqtt_err_t lwmqtt_unsubscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, uint32_t timeout) { return lwmqtt_unsubscribe(client, 1, &topic_filter, timeout); } lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmqtt_message_t message, - uint32_t timeout) + uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); // add packet id if at least qos 1 uint16_t packet_id = 0; - - if (message.qos == LWMQTT_QOS1 || message.qos == LWMQTT_QOS2) + + if (message.qos == LWMQTT_QOS1 || message.qos == LWMQTT_QOS2) { packet_id = lwmqtt_get_next_packet_id(client); } @@ -708,34 +710,34 @@ lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmq // encode publish packet size_t len = 0; lwmqtt_err_t err = lwmqtt_encode_publish(client->write_buf, client->write_buf_size, &len, 0, packet_id, topic, message); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // send packet err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // immediately return on qos zero - if (message.qos == LWMQTT_QOS0) + if (message.qos == LWMQTT_QOS0) { return LWMQTT_SUCCESS; } // define ack packet lwmqtt_packet_type_t ack_type = LWMQTT_NO_PACKET; - - if (message.qos == LWMQTT_QOS1) + + if (message.qos == LWMQTT_QOS1) { ack_type = LWMQTT_PUBACK_PACKET; - } - else if (message.qos == LWMQTT_QOS2) + } + else if (message.qos == LWMQTT_QOS2) { ack_type = LWMQTT_PUBCOMP_PACKET; } @@ -743,12 +745,12 @@ lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmq // wait for ack packet lwmqtt_packet_type_t packet_type = LWMQTT_NO_PACKET; err = lwmqtt_cycle_until(client, &packet_type, 0, ack_type); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; - } - else if (packet_type != ack_type) + } + else if (packet_type != ack_type) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -756,8 +758,8 @@ lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmq // decode ack packet bool dup; err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, ack_type, &dup, &packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -765,7 +767,7 @@ lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmq return LWMQTT_SUCCESS; } -lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) +lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); @@ -773,16 +775,16 @@ lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) // encode disconnect packet size_t len; lwmqtt_err_t err = lwmqtt_encode_zero(client->write_buf, client->write_buf_size, &len, LWMQTT_DISCONNECT_PACKET); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // send disconnected packet err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -790,19 +792,19 @@ lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) return LWMQTT_SUCCESS; } -lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout) +lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout) { // set command timer client->timer_set(client->command_timer, timeout); // return immediately if keep alive interval is zero - if (client->keep_alive_interval == 0) + if (client->keep_alive_interval == 0) { return LWMQTT_SUCCESS; } // return immediately if no ping is due - if (client->timer_get(client->keep_alive_timer) > 0) + if (client->timer_get(client->keep_alive_timer) > 0) { return LWMQTT_SUCCESS; } @@ -810,7 +812,7 @@ lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout) // a ping is due // fail immediately if a pong is already pending - if (client->pong_pending) + if (client->pong_pending) { return LWMQTT_PONG_TIMEOUT; } @@ -818,16 +820,16 @@ lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout) // encode pingreq packet size_t len; lwmqtt_err_t err = lwmqtt_encode_zero(client->write_buf, client->write_buf_size, &len, LWMQTT_PINGREQ_PACKET); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // send packet err = lwmqtt_send_packet_in_buffer(client, len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } diff --git a/src/MQTTPubSubClient_Generic/lwmqtt/packet.c b/src/MQTTPubSubClient_Generic/lwmqtt/packet.c index fd482d5..8a15538 100644 --- a/src/MQTTPubSubClient_Generic/lwmqtt/packet.c +++ b/src/MQTTPubSubClient_Generic/lwmqtt/packet.c @@ -1,6 +1,6 @@ #include "packet.h" -lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_t *packet_type) +lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_t *packet_type) { // set default packet type *packet_type = LWMQTT_NO_PACKET; @@ -14,8 +14,8 @@ lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_pack // read header lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -24,7 +24,7 @@ lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_pack *packet_type = (lwmqtt_packet_type_t)lwmqtt_read_bits(header, 4, 4); // check if packet type is correct and can be received - switch (*packet_type) + switch (*packet_type) { case LWMQTT_CONNACK_PACKET: case LWMQTT_PUBLISH_PACKET: @@ -36,26 +36,27 @@ lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_pack case LWMQTT_UNSUBACK_PACKET: case LWMQTT_PINGRESP_PACKET: return LWMQTT_SUCCESS; + default: *packet_type = LWMQTT_NO_PACKET; return LWMQTT_MISSING_OR_WRONG_PACKET; } } -lwmqtt_err_t lwmqtt_detect_remaining_length(uint8_t *buf, size_t buf_len, uint32_t *rem_len) +lwmqtt_err_t lwmqtt_detect_remaining_length(uint8_t *buf, size_t buf_len, uint32_t *rem_len) { // prepare pointer uint8_t *ptr = buf; // attempt to decode remaining length lwmqtt_err_t err = lwmqtt_read_varnum(&ptr, buf + buf_len, rem_len); - - if (err == LWMQTT_VARNUM_OVERFLOW) + + if (err == LWMQTT_VARNUM_OVERFLOW) { *rem_len = 0; return LWMQTT_REMAINING_LENGTH_OVERFLOW; - } - else if (err != LWMQTT_SUCCESS) + } + else if (err != LWMQTT_SUCCESS) { *rem_len = 0; return err; @@ -65,7 +66,7 @@ lwmqtt_err_t lwmqtt_detect_remaining_length(uint8_t *buf, size_t buf_len, uint32 } lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_options_t options, - lwmqtt_will_t *will) + lwmqtt_will_t *will) { // prepare pointers uint8_t *buf_ptr = buf; @@ -78,18 +79,18 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw rem_len += options.client_id.len + 2; // add will if present to remaining length - if (will != NULL) + if (will != NULL) { rem_len += will->topic.len + 2 + will->payload.len + 2; } // add username if present to remaining length - if (options.username.len > 0) + if (options.username.len > 0) { rem_len += options.username.len + 2; // add password if present to remaining length - if (options.password.len > 0) + if (options.password.len > 0) { rem_len += options.password.len + 2; } @@ -98,8 +99,8 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw // check remaining length length int rem_len_len; lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); - - if (err == LWMQTT_VARNUM_OVERFLOW) + + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -110,32 +111,33 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw // write header err = lwmqtt_write_byte(&buf_ptr, buf_end, header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write remaining length err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write version string err = lwmqtt_write_string(&buf_ptr, buf_end, lwmqtt_string("MQTT")); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write version number - + err = lwmqtt_write_byte(&buf_ptr, buf_end, 4); - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -147,7 +149,7 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw lwmqtt_write_bits(&flags, (uint8_t)(options.clean_session), 1, 1); // set will flags if present - if (will != NULL) + if (will != NULL) { lwmqtt_write_bits(&flags, 1, 2, 1); lwmqtt_write_bits(&flags, will->qos, 3, 2); @@ -155,12 +157,12 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw } // set username flag if present - if (options.username.len > 0) + if (options.username.len > 0) { lwmqtt_write_bits(&flags, 1, 7, 1); // set password flag if present - if (options.password.len > 0) + if (options.password.len > 0) { lwmqtt_write_bits(&flags, 1, 6, 1); } @@ -168,73 +170,73 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw // write flags err = lwmqtt_write_byte(&buf_ptr, buf_end, flags); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write keep alive err = lwmqtt_write_num(&buf_ptr, buf_end, options.keep_alive); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write client id err = lwmqtt_write_string(&buf_ptr, buf_end, options.client_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write will if present - if (will != NULL) + if (will != NULL) { // write topic err = lwmqtt_write_string(&buf_ptr, buf_end, will->topic); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write payload length err = lwmqtt_write_num(&buf_ptr, buf_end, (uint16_t)will->payload.len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write payload err = lwmqtt_write_data(&buf_ptr, buf_end, (uint8_t *)will->payload.data, will->payload.len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } } // write username if present - if (options.username.len > 0) + if (options.username.len > 0) { err = lwmqtt_write_string(&buf_ptr, buf_end, options.username); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } } // write password if present - if (options.username.len > 0 && options.password.len > 0) + if (options.username.len > 0 && options.password.len > 0) { err = lwmqtt_write_string(&buf_ptr, buf_end, options.password); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -247,7 +249,7 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw } lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_present, - lwmqtt_return_code_t *return_code) + lwmqtt_return_code_t *return_code) { // prepare pointers uint8_t *buf_ptr = buf; @@ -256,14 +258,14 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p // read header uint8_t header; lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check packet type - if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_CONNACK_PACKET) + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_CONNACK_PACKET) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -271,14 +273,14 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p // read remaining length uint32_t rem_len; err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check remaining length - if (rem_len != 2) + if (rem_len != 2) { return LWMQTT_REMAINING_LENGTH_MISMATCH; } @@ -286,8 +288,8 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p // read flags uint8_t flags; err = lwmqtt_read_byte(&buf_ptr, buf_end, &flags); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -295,8 +297,8 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p // read return code uint8_t raw_return_code; err = lwmqtt_read_byte(&buf_ptr, buf_end, &raw_return_code); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -305,25 +307,32 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p *session_present = lwmqtt_read_bits(flags, 7, 1) == 1; // get return code - switch (raw_return_code) { + switch (raw_return_code) + { case 0: *return_code = LWMQTT_CONNECTION_ACCEPTED; break; + case 1: *return_code = LWMQTT_UNACCEPTABLE_PROTOCOL; break; + case 2: *return_code = LWMQTT_IDENTIFIER_REJECTED; break; + case 3: *return_code = LWMQTT_SERVER_UNAVAILABLE; break; + case 4: *return_code = LWMQTT_BAD_USERNAME_OR_PASSWORD; break; + case 5: *return_code = LWMQTT_NOT_AUTHORIZED; break; + default: *return_code = LWMQTT_UNKNOWN_RETURN_CODE; } @@ -331,7 +340,7 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p return LWMQTT_SUCCESS; } -lwmqtt_err_t lwmqtt_encode_zero(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_packet_type_t packet_type) +lwmqtt_err_t lwmqtt_encode_zero(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_packet_type_t packet_type) { // prepare pointer uint8_t *buf_ptr = buf; @@ -341,16 +350,16 @@ lwmqtt_err_t lwmqtt_encode_zero(uint8_t *buf, size_t buf_len, size_t *len, lwmqt uint8_t header = 0; lwmqtt_write_bits(&header, packet_type, 4, 4); lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write remaining length err = lwmqtt_write_varnum(&buf_ptr, buf_end, 0); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -362,7 +371,7 @@ lwmqtt_err_t lwmqtt_encode_zero(uint8_t *buf, size_t buf_len, size_t *len, lwmqt } lwmqtt_err_t lwmqtt_decode_ack(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_t packet_type, bool *dup, - uint16_t *packet_id) + uint16_t *packet_id) { // prepare pointer uint8_t *buf_ptr = buf; @@ -371,14 +380,14 @@ lwmqtt_err_t lwmqtt_decode_ack(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_ // read header uint8_t header = 0; lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check packet type - if (lwmqtt_read_bits(header, 4, 4) != packet_type) + if (lwmqtt_read_bits(header, 4, 4) != packet_type) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -389,22 +398,22 @@ lwmqtt_err_t lwmqtt_decode_ack(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_ // read remaining length uint32_t rem_len; err = lwmqtt_read_varnum(&buf_ptr, buf + buf_len, &rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check remaining length - if (rem_len != 2) + if (rem_len != 2) { return LWMQTT_REMAINING_LENGTH_MISMATCH; } // read packet id err = lwmqtt_read_num(&buf_ptr, buf_end, packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -413,7 +422,7 @@ lwmqtt_err_t lwmqtt_decode_ack(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_ } lwmqtt_err_t lwmqtt_encode_ack(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_packet_type_t packet_type, bool dup, - uint16_t packet_id) + uint16_t packet_id) { // prepare pointer uint8_t *buf_ptr = buf; @@ -433,24 +442,24 @@ lwmqtt_err_t lwmqtt_encode_ack(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt // write header lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write remaining length err = lwmqtt_write_varnum(&buf_ptr, buf_end, 2); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write packet id err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -462,7 +471,7 @@ lwmqtt_err_t lwmqtt_encode_ack(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt } lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint16_t *packet_id, lwmqtt_string_t *topic, - lwmqtt_message_t *msg) + lwmqtt_message_t *msg) { // prepare pointer uint8_t *buf_ptr = buf; @@ -471,14 +480,14 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint // read header uint8_t header; lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check packet type - if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_PUBLISH_PACKET) + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_PUBLISH_PACKET) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -490,17 +499,20 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint msg->retained = lwmqtt_read_bits(header, 0, 1) == 1; // get qos - switch (lwmqtt_read_bits(header, 1, 2)) + switch (lwmqtt_read_bits(header, 1, 2)) { case 0: msg->qos = LWMQTT_QOS0; break; + case 1: msg->qos = LWMQTT_QOS1; break; + case 2: msg->qos = LWMQTT_QOS2; break; + default: msg->qos = LWMQTT_QOS0; break; @@ -509,20 +521,20 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint // read remaining length uint32_t rem_len; err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check remaining length (topic length) - if (rem_len < 2) + if (rem_len < 2) { return LWMQTT_REMAINING_LENGTH_MISMATCH; } // check buffer capacity - if ((uint32_t)(buf_end - buf_ptr) < rem_len) + if ((uint32_t)(buf_end - buf_ptr) < rem_len) { return LWMQTT_BUFFER_TOO_SHORT; } @@ -532,22 +544,23 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint // read topic err = lwmqtt_read_string(&buf_ptr, buf_end, topic); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // read packet id if qos is at least 1 - if (msg->qos > 0) + if (msg->qos > 0) { err = lwmqtt_read_num(&buf_ptr, buf_end, packet_id); - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } - } - else + } + else { *packet_id = 0; } @@ -557,8 +570,8 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint // read payload err = lwmqtt_read_data(&buf_ptr, buf_end, &msg->payload, buf_end - buf_ptr); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -567,7 +580,7 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint } lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bool dup, uint16_t packet_id, - lwmqtt_string_t topic, lwmqtt_message_t msg) + lwmqtt_string_t topic, lwmqtt_message_t msg) { // prepare pointer uint8_t *buf_ptr = buf; @@ -575,8 +588,8 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo // calculate remaining length uint32_t rem_len = 2 + topic.len + (uint32_t)msg.payload_len; - - if (msg.qos > 0) + + if (msg.qos > 0) { rem_len += 2; } @@ -584,8 +597,8 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo // check remaining length length int rem_len_len; lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); - - if (err == LWMQTT_VARNUM_OVERFLOW) + + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -607,34 +620,34 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo // write header err = lwmqtt_write_byte(&buf_ptr, buf_end, header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write remaining length err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write topic err = lwmqtt_write_string(&buf_ptr, buf_end, topic); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write packet id if qos is at least 1 - if (msg.qos > 0) + if (msg.qos > 0) { err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -642,8 +655,8 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo // write payload err = lwmqtt_write_data(&buf_ptr, buf_end, msg.payload, msg.payload_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -655,7 +668,7 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo } lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, uint16_t packet_id, int count, - lwmqtt_string_t *topic_filters, lwmqtt_qos_t *qos_levels) + lwmqtt_string_t *topic_filters, lwmqtt_qos_t *qos_levels) { // prepare pointer uint8_t *buf_ptr = buf; @@ -663,8 +676,8 @@ lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, // calculate remaining length uint32_t rem_len = 2; - - for (int i = 0; i < count; i++) + + for (int i = 0; i < count; i++) { rem_len += 2 + topic_filters[i].len + 1; } @@ -672,8 +685,8 @@ lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, // check remaining length length int rem_len_len; lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); - - if (err == LWMQTT_VARNUM_OVERFLOW) + + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -689,43 +702,43 @@ lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, // write header err = lwmqtt_write_byte(&buf_ptr, buf_end, header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write remaining length err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write packet id err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write all subscriptions - for (int i = 0; i < count; i++) + for (int i = 0; i < count; i++) { // write topic err = lwmqtt_write_string(&buf_ptr, buf_end, topic_filters[i]); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write qos level err = lwmqtt_write_byte(&buf_ptr, buf_end, (uint8_t)qos_levels[i]); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } @@ -738,7 +751,7 @@ lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, } lwmqtt_err_t lwmqtt_decode_suback(uint8_t *buf, size_t buf_len, uint16_t *packet_id, int max_count, int *count, - lwmqtt_qos_t *granted_qos_levels) + lwmqtt_qos_t *granted_qos_levels) { // prepare pointer uint8_t *buf_ptr = buf; @@ -747,14 +760,14 @@ lwmqtt_err_t lwmqtt_decode_suback(uint8_t *buf, size_t buf_len, uint16_t *packet // read header uint8_t header; lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check packet type - if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_SUBACK_PACKET) + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_SUBACK_PACKET) { return LWMQTT_MISSING_OR_WRONG_PACKET; } @@ -762,31 +775,31 @@ lwmqtt_err_t lwmqtt_decode_suback(uint8_t *buf, size_t buf_len, uint16_t *packet // read remaining length uint32_t rem_len; err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // check remaining length (packet id + min. one suback code) - if (rem_len < 3) + if (rem_len < 3) { return LWMQTT_REMAINING_LENGTH_MISMATCH; } // read packet id err = lwmqtt_read_num(&buf_ptr, buf_end, packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // read all suback codes - for (*count = 0; *count < (int)rem_len - 2; (*count)++) + for (*count = 0; *count < (int)rem_len - 2; (*count)++) { // check max count - if (*count > max_count) + if (*count > max_count) { return LWMQTT_SUBACK_ARRAY_OVERFLOW; } @@ -794,24 +807,27 @@ lwmqtt_err_t lwmqtt_decode_suback(uint8_t *buf, size_t buf_len, uint16_t *packet // read qos level uint8_t raw_qos_level; err = lwmqtt_read_byte(&buf_ptr, buf_end, &raw_qos_level); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // set qos level - switch (raw_qos_level) + switch (raw_qos_level) { case 0: granted_qos_levels[*count] = LWMQTT_QOS0; break; + case 1: granted_qos_levels[*count] = LWMQTT_QOS1; break; + case 2: granted_qos_levels[*count] = LWMQTT_QOS2; break; + default: granted_qos_levels[*count] = LWMQTT_QOS_FAILURE; break; @@ -822,7 +838,7 @@ lwmqtt_err_t lwmqtt_decode_suback(uint8_t *buf, size_t buf_len, uint16_t *packet } lwmqtt_err_t lwmqtt_encode_unsubscribe(uint8_t *buf, size_t buf_len, size_t *len, uint16_t packet_id, int count, - lwmqtt_string_t *topic_filters) + lwmqtt_string_t *topic_filters) { // prepare pointer uint8_t *buf_ptr = buf; @@ -830,8 +846,8 @@ lwmqtt_err_t lwmqtt_encode_unsubscribe(uint8_t *buf, size_t buf_len, size_t *len // calculate remaining length uint32_t rem_len = 2; - - for (int i = 0; i < count; i++) + + for (int i = 0; i < count; i++) { rem_len += 2 + topic_filters[i].len; } @@ -839,8 +855,8 @@ lwmqtt_err_t lwmqtt_encode_unsubscribe(uint8_t *buf, size_t buf_len, size_t *len // check remaining length length int rem_len_len; lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); - - if (err == LWMQTT_VARNUM_OVERFLOW) + + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -856,33 +872,34 @@ lwmqtt_err_t lwmqtt_encode_unsubscribe(uint8_t *buf, size_t buf_len, size_t *len // write header err = lwmqtt_write_byte(&buf_ptr, buf_end, header); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write remaining length err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write packet id err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); - - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } // write topics - for (int i = 0; i < count; i++) + for (int i = 0; i < count; i++) { err = lwmqtt_write_string(&buf_ptr, buf_end, topic_filters[i]); - if (err != LWMQTT_SUCCESS) + + if (err != LWMQTT_SUCCESS) { return err; } diff --git a/src/MQTTPubSubClient_Generic/lwmqtt/packet.h b/src/MQTTPubSubClient_Generic/lwmqtt/packet.h index 23417bc..6b73d5e 100644 --- a/src/MQTTPubSubClient_Generic/lwmqtt/packet.h +++ b/src/MQTTPubSubClient_Generic/lwmqtt/packet.h @@ -6,7 +6,7 @@ /** The available packet types. */ -typedef enum +typedef enum { LWMQTT_NO_PACKET = 0, LWMQTT_CONNECT_PACKET = 1, diff --git a/src/MQTTPubSubClient_Generic/lwmqtt/string.c b/src/MQTTPubSubClient_Generic/lwmqtt/string.c index 3c3a444..9b9e9a1 100644 --- a/src/MQTTPubSubClient_Generic/lwmqtt/string.c +++ b/src/MQTTPubSubClient_Generic/lwmqtt/string.c @@ -2,12 +2,12 @@ #include "lwmqtt.h" -lwmqtt_string_t lwmqtt_string(const char *str) +lwmqtt_string_t lwmqtt_string(const char *str) { // check for null - if (str == NULL) + if (str == NULL) { - return (lwmqtt_string_t) + return (lwmqtt_string_t) { 0, NULL }; @@ -17,33 +17,33 @@ lwmqtt_string_t lwmqtt_string(const char *str) uint16_t len = (uint16_t)strlen(str); // check zero length - if (len == 0) + if (len == 0) { - return (lwmqtt_string_t) + return (lwmqtt_string_t) { 0, NULL }; } - return (lwmqtt_string_t) + return (lwmqtt_string_t) { len, (char *)str }; } -int lwmqtt_strcmp(lwmqtt_string_t a, const char *b) +int lwmqtt_strcmp(lwmqtt_string_t a, const char *b) { // get string of b lwmqtt_string_t b_str = lwmqtt_string(b); // return if both are zero length - if (a.len == 0 && b_str.len == 0) + if (a.len == 0 && b_str.len == 0) { return 0; } // return if lengths are different - if (a.len != b_str.len) + if (a.len != b_str.len) { return -1; } diff --git a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer.h b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer.h index 026cbf8..894e09c 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer.h +++ b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer.h @@ -4,14 +4,14 @@ #define ARX_RINGBUFFER_H #if __cplusplus < 201103L -#error "C++11 must be enabled in the compiler for this library to work, please check your compiler flags" + #error "C++11 must be enabled in the compiler for this library to work, please check your compiler flags" #endif #include "ArxContainer/has_include.h" #include "ArxContainer/has_libstdcplusplus.h" #ifdef ARDUINO -#include + #include #endif #include "ArxContainer/replace_minmax_macros.h" @@ -28,29 +28,37 @@ #include #ifndef ARX_VECTOR_DEFAULT_SIZE -#define ARX_VECTOR_DEFAULT_SIZE 16 + #define ARX_VECTOR_DEFAULT_SIZE 16 #endif // ARX_VECTOR_DEFAULT_SIZE #ifndef ARX_DEQUE_DEFAULT_SIZE -#define ARX_DEQUE_DEFAULT_SIZE 16 + #define ARX_DEQUE_DEFAULT_SIZE 16 #endif // ARX_DEQUE_DEFAULT_SIZE #ifndef ARX_MAP_DEFAULT_SIZE -#define ARX_MAP_DEFAULT_SIZE 16 + #define ARX_MAP_DEFAULT_SIZE 16 #endif // ARX_MAP_DEFAULT_SIZE -namespace arx { - -namespace container { - namespace detail { - template - T&& move(T& t) { return static_cast(t); } - } // namespace detail +namespace arx +{ + +namespace container +{ +namespace detail +{ +template +T&& move(T& t) +{ + return static_cast < T && > (t); +} +} // namespace detail } // namespace container template -class RingBuffer { - class Iterator { +class RingBuffer +{ + class Iterator + { friend RingBuffer; T* ptr {nullptr}; // pointer to the first element @@ -58,458 +66,634 @@ class RingBuffer { Iterator() {} Iterator(T* ptr, int pos) - : ptr(ptr), pos(pos) {} + : ptr(ptr), pos(pos) {} - public: - Iterator(const Iterator& it) { - this->ptr = it.ptr; - this->pos = it.pos; + public: + Iterator(const Iterator& it) + { + this->ptr = it.ptr; + this->pos = it.pos; } - Iterator(Iterator&& it) { - this->ptr = container::detail::move(it.ptr); - this->pos = container::detail::move(it.pos); + Iterator(Iterator&& it) + { + this->ptr = container::detail::move(it.ptr); + this->pos = container::detail::move(it.pos); } - int index() const { - if (pos >= 0) - return pos % N; - else - return N - (abs(pos) % (N + 1)); + int index() const + { + if (pos >= 0) + return pos % N; + else + return N - (abs(pos) % (N + 1)); } - int index_with_offset(const int i) const { - const int p = pos + i; - if (p >= 0) - return p % N; - else - return N - (abs(p) % (N + 1)); + int index_with_offset(const int i) const + { + const int p = pos + i; + + if (p >= 0) + return p % N; + else + return N - (abs(p) % (N + 1)); } - T& operator*() { - return *(ptr + index()); + T& operator*() + { + return *(ptr + index()); } - const T& operator*() const { - return *(ptr + index()); + const T& operator*() const + { + return *(ptr + index()); } - T* operator->() { - return ptr + index(); + T* operator->() + { + return ptr + index(); } - const T* operator->() const { - return ptr + index(); + const T* operator->() const + { + return ptr + index(); } - Iterator operator+(const Iterator& rhs) { - Iterator it(this->ptr, this->pos + rhs.pos); - return it; + Iterator operator+(const Iterator& rhs) + { + Iterator it(this->ptr, this->pos + rhs.pos); + return it; } - Iterator operator+(const int n) { - Iterator it(this->ptr, this->pos + n); - return it; + Iterator operator+(const int n) + { + Iterator it(this->ptr, this->pos + n); + return it; } - Iterator operator-(const Iterator& rhs) { - Iterator it(this->ptr, this->pos - rhs.pos); - return it; + Iterator operator-(const Iterator& rhs) + { + Iterator it(this->ptr, this->pos - rhs.pos); + return it; } - Iterator operator-(const int n) { - Iterator it(this->ptr, this->pos - n); - return it; + Iterator operator-(const int n) + { + Iterator it(this->ptr, this->pos - n); + return it; } - Iterator& operator+=(const Iterator& rhs) { - this->pos += rhs.pos; - return *this; + Iterator& operator+=(const Iterator& rhs) + { + this->pos += rhs.pos; + return *this; } - Iterator& operator+=(const int n) { - this->pos += n; - return *this; + Iterator& operator+=(const int n) + { + this->pos += n; + return *this; } - Iterator& operator-=(const Iterator& rhs) { - this->pos -= rhs.pos; - return *this; + Iterator& operator-=(const Iterator& rhs) + { + this->pos -= rhs.pos; + return *this; } - Iterator& operator-=(const int n) { - this->pos -= n; - return *this; + Iterator& operator-=(const int n) + { + this->pos -= n; + return *this; } // prefix increment/decrement - Iterator& operator++() { - ++pos; - return *this; + Iterator& operator++() + { + ++pos; + return *this; } - Iterator& operator--() { - --pos; - return *this; + Iterator& operator--() + { + --pos; + return *this; } // postfix increment/decrement - Iterator operator++(int) { - Iterator it = *this; - ++pos; - return it; + Iterator operator++(int) + { + Iterator it = *this; + ++pos; + return it; } - Iterator operator--(int) { - Iterator it = *this; - --pos; - return it; + Iterator operator--(int) + { + Iterator it = *this; + --pos; + return it; } - Iterator& operator=(const Iterator& rhs) { - this->ptr = rhs.ptr; - this->pos = rhs.pos; - return *this; + Iterator& operator=(const Iterator& rhs) + { + this->ptr = rhs.ptr; + this->pos = rhs.pos; + return *this; } - Iterator& operator=(Iterator&& rhs) { - this->ptr = container::detail::move(rhs.ptr); - this->pos = container::detail::move(rhs.pos); - return *this; + Iterator& operator=(Iterator&& rhs) + { + this->ptr = container::detail::move(rhs.ptr); + this->pos = container::detail::move(rhs.pos); + return *this; } - bool operator==(const Iterator& rhs) const { - return (rhs.ptr == ptr) && (rhs.pos == pos); + bool operator==(const Iterator& rhs) const + { + return (rhs.ptr == ptr) && (rhs.pos == pos); } - bool operator!=(const Iterator& rhs) const { - return !(*this == rhs); + bool operator!=(const Iterator& rhs) const + { + return !(*this == rhs); } - bool operator<(const Iterator& rhs) const { - return pos < rhs.pos; + bool operator<(const Iterator& rhs) const + { + return pos < rhs.pos; } - bool operator<=(const Iterator& rhs) const { - return pos <= rhs.pos; + bool operator<=(const Iterator& rhs) const + { + return pos <= rhs.pos; } - bool operator>(const Iterator& rhs) const { - return pos > rhs.pos; + bool operator>(const Iterator& rhs) const + { + return pos > rhs.pos; } - bool operator>=(const Iterator& rhs) const { - return pos >= rhs.pos; + bool operator>=(const Iterator& rhs) const + { + return pos >= rhs.pos; } - private: - int raw_pos() const { - return pos; + private: + int raw_pos() const + { + return pos; } - void set(const int i) { - pos = i; + void set(const int i) + { + pos = i; } - void reset() { - pos = 0; + void reset() + { + pos = 0; } }; -protected: + protected: friend class Iterator; T queue_[N]; Iterator head_; Iterator tail_; -public: + public: using iterator = Iterator; using const_iterator = const Iterator; RingBuffer() - : queue_() - , head_(queue_, 0) - , tail_(queue_, 0) { + : queue_() + , head_(queue_, 0) + , tail_(queue_, 0) + { } RingBuffer(std::initializer_list lst) - : queue_() - , head_(queue_, 0) - , tail_(queue_, 0) { - for (auto it = lst.begin(); it != lst.end(); ++it) { - push_back(*it); - } + : queue_() + , head_(queue_, 0) + , tail_(queue_, 0) + { + for (auto it = lst.begin(); it != lst.end(); ++it) + { + push_back(*it); + } } // copy explicit RingBuffer(const RingBuffer& r) - : queue_() - , head_(r.head_) - , tail_(r.tail_) { - for (size_t i = 0; i < r.size(); ++i) - queue_[i] = r.queue_[i]; + : queue_() + , head_(r.head_) + , tail_(r.tail_) + { + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = r.queue_[i]; } - RingBuffer& operator=(const RingBuffer& r) { - head_ = r.head_; - tail_ = r.tail_; - for (size_t i = 0; i < r.size(); ++i) - queue_[i] = r.queue_[i]; - return *this; + RingBuffer& operator=(const RingBuffer& r) + { + head_ = r.head_; + tail_ = r.tail_; + + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = r.queue_[i]; + + return *this; } // move - RingBuffer(RingBuffer&& r) { - head_ = container::detail::move(r.head_); - tail_ = container::detail::move(r.tail_); - for (size_t i = 0; i < r.size(); ++i) - queue_[i] = container::detail::move(r.queue_[i]); + RingBuffer(RingBuffer&& r) + { + head_ = container::detail::move(r.head_); + tail_ = container::detail::move(r.tail_); + + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = container::detail::move(r.queue_[i]); } - RingBuffer& operator=(RingBuffer&& r) { - head_ = container::detail::move(r.head_); - tail_ = container::detail::move(r.tail_); - for (size_t i = 0; i < r.size(); ++i) - queue_[i] = container::detail::move(r.queue_[i]); - return *this; + RingBuffer& operator=(RingBuffer&& r) + { + head_ = container::detail::move(r.head_); + tail_ = container::detail::move(r.tail_); + + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = container::detail::move(r.queue_[i]); + + return *this; } virtual ~RingBuffer() {} - size_t capacity() const { return N; }; - size_t size() const { return abs(tail_.raw_pos() - head_.raw_pos()); } - inline const T* data() const { return &(get(head_)); } - T* data() { return &(get(head_)); } - bool empty() const { return tail_ == head_; } - void clear() { - head_.reset(); - tail_.reset(); + size_t capacity() const + { + return N; + }; + size_t size() const + { + return abs(tail_.raw_pos() - head_.raw_pos()); + } + inline const T* data() const + { + return &(get(head_)); + } + T* data() + { + return &(get(head_)); + } + bool empty() const + { + return tail_ == head_; + } + void clear() + { + head_.reset(); + tail_.reset(); } - void pop() { - pop_front(); + void pop() + { + pop_front(); } - void pop_front() { - if (size() == 0) return; - if (size() == 1) - clear(); - else - increment_head(); + void pop_front() + { + if (size() == 0) + return; + + if (size() == 1) + clear(); + else + increment_head(); } - void pop_back() { - if (size() == 0) return; - if (size() == 1) - clear(); - else - decrement_tail(); + void pop_back() + { + if (size() == 0) + return; + + if (size() == 1) + clear(); + else + decrement_tail(); } - void push(const T& data) { - push_back(data); + void push(const T& data) + { + push_back(data); } - void push(T&& data) { - push_back(data); + void push(T&& data) + { + push_back(data); } - void push_back(const T& data) { - get(tail_) = data; - increment_tail(); + void push_back(const T& data) + { + get(tail_) = data; + increment_tail(); }; - void push_back(T&& data) { - get(tail_) = data; - increment_tail(); + void push_back(T&& data) + { + get(tail_) = data; + increment_tail(); }; - void push_front(const T& data) { - get(head_) = data; - decrement_head(); + void push_front(const T& data) + { + get(head_) = data; + decrement_head(); }; - void push_front(T&& data) { - get(head_) = data; - decrement_head(); + void push_front(T&& data) + { + get(head_) = data; + decrement_head(); }; - void emplace(const T& data) { push(data); } - void emplace(T&& data) { push(data); } - void emplace_back(const T& data) { push_back(data); } - void emplace_back(T&& data) { push_back(data); } + void emplace(const T& data) + { + push(data); + } + void emplace(T&& data) + { + push(data); + } + void emplace_back(const T& data) + { + push_back(data); + } + void emplace_back(T&& data) + { + push_back(data); + } + + const T& front() const + { + return get(head_); + }; + T& front() + { + return get(head_); + }; + + const T& back() const + { + return get(size() - 1); + } + T& back() + { + return get(size() - 1); + } - const T& front() const { return get(head_); }; - T& front() { return get(head_); }; + const T& operator[](size_t index) const + { + return get((int)index); + } + T& operator[](size_t index) + { + return get((int)index); + } - const T& back() const { return get(size() - 1); } - T& back() { return get(size() - 1); } + iterator begin() + { + return empty() ? Iterator() : head_; + } + iterator end() + { + return empty() ? Iterator() : tail_; + } + const_iterator begin() const + { + return empty() ? Iterator() : head_; + } + const_iterator end() const + { + return empty() ? Iterator() : tail_; + } - const T& operator[](size_t index) const { return get((int)index); } - T& operator[](size_t index) { return get((int)index); } + iterator erase(const iterator& p) + { + if (!is_valid(p)) + return end(); - iterator begin() { return empty() ? Iterator() : head_; } - iterator end() { return empty() ? Iterator() : tail_; } - const_iterator begin() const { return empty() ? Iterator() : head_; } - const_iterator end() const { return empty() ? Iterator() : tail_; } + iterator it_last = begin() + size() - 1; - iterator erase(const iterator& p) { - if (!is_valid(p)) return end(); + for (iterator it = p; it != it_last; ++it) + *it = *(it + 1); - iterator it_last = begin() + size() - 1; - for (iterator it = p; it != it_last; ++it) - *it = *(it + 1); - *it_last = T(); - decrement_tail(); - return empty() ? end() : p; + *it_last = T(); + decrement_tail(); + return empty() ? end() : p; } - void resize(size_t sz) { - size_t s = size(); - if (sz > size()) { - for (size_t i = 0; i < sz - s; ++i) push(T()); - } else if (sz < size()) { - for (size_t i = 0; i < s - sz; ++i) pop(); - } + void resize(size_t sz) + { + size_t s = size(); + + if (sz > size()) + { + for (size_t i = 0; i < sz - s; ++i) + push(T()); + } + else if (sz < size()) + { + for (size_t i = 0; i < s - sz; ++i) + pop(); + } } - void assign(const_iterator first, const_iterator end) { - clear(); - while (first != end) push(*(first++)); + void assign(const_iterator first, const_iterator end) + { + clear(); + + while (first != end) + push(*(first++)); } - void assign(const T* first, const T* end) { - clear(); - // T* it = first; - while (first != end) push(*(first++)); + void assign(const T* first, const T* end) + { + clear(); + + // T* it = first; + while (first != end) + push(*(first++)); } - void shrink_to_fit() { - // dummy + void shrink_to_fit() + { + // dummy } - void reserve(size_t n) { - (void)n; - // dummy + void reserve(size_t n) + { + (void)n; + // dummy } - void insert(const_iterator pos, const_iterator first, const_iterator last) { - if (pos != end()) { - size_t sz = 0; - { - for (iterator it = first; it != last; ++it) ++sz; - } - iterator it = end() + sz - 1; - for (int i = sz; i > 0; --i, --it) - *it = *(it - sz); - it = pos; - for (size_t i = 0; i < sz; ++i) - *it = *(first + i); - } else { - for (iterator it = first; it != last; ++it) - push_back(*it); + void insert(const_iterator pos, const_iterator first, const_iterator last) + { + if (pos != end()) + { + size_t sz = 0; + { + for (iterator it = first; it != last; ++it) + ++sz; } + iterator it = end() + sz - 1; + + for (int i = sz; i > 0; --i, --it) + *it = *(it - sz); + + it = pos; + + for (size_t i = 0; i < sz; ++i) + *it = *(first + i); + } + else + { + for (iterator it = first; it != last; ++it) + push_back(*it); + } } - void insert(const_iterator pos, const T* first, const T* last) { - if (pos != end()) { - size_t sz = 0; - { - for (const T* it = first; it != last; ++it) ++sz; - } - iterator it = end() + sz - 1; - for (int i = sz; i > 0; --i, --it) - *it = *(it - sz); - it = pos; - for (size_t i = 0; i < sz; ++i) - *it = *(first + i); - } else { - for (const T* it = first; it != last; ++it) - push_back(*it); + void insert(const_iterator pos, const T* first, const T* last) + { + if (pos != end()) + { + size_t sz = 0; + { + for (const T* it = first; it != last; ++it) + ++sz; } + iterator it = end() + sz - 1; + + for (int i = sz; i > 0; --i, --it) + *it = *(it - sz); + + it = pos; + + for (size_t i = 0; i < sz; ++i) + *it = *(first + i); + } + else + { + for (const T* it = first; it != last; ++it) + push_back(*it); + } } -private: - T& get(const Iterator& it) { - return queue_[it.index()]; + private: + T& get(const Iterator& it) + { + return queue_[it.index()]; } - const T& get(const Iterator& it) const { - return queue_[it.index()]; + const T& get(const Iterator& it) const + { + return queue_[it.index()]; } - T& get(const int index) { - return queue_[head_.index_with_offset(index)]; + T& get(const int index) + { + return queue_[head_.index_with_offset(index)]; } - const T& get(const int index) const { - return queue_[head_.index_with_offset(index)]; + const T& get(const int index) const + { + return queue_[head_.index_with_offset(index)]; } - T* ptr(const Iterator& it) { - return (T*)(queue_ + it.index()); + T* ptr(const Iterator& it) + { + return (T*)(queue_ + it.index()); } - const T* ptr(const Iterator& it) const { - return (T*)(queue_ + it.index()); + const T* ptr(const Iterator& it) const + { + return (T*)(queue_ + it.index()); } - T* ptr(const int index) { - return (T*)(queue_ + head_.index_with_offset(index)); + T* ptr(const int index) + { + return (T*)(queue_ + head_.index_with_offset(index)); } - const T* ptr(const int index) const { - return (T*)(queue_ + head_.index_with_offset(index)); + const T* ptr(const int index) const + { + return (T*)(queue_ + head_.index_with_offset(index)); } - void increment_head() { - ++head_; - resolve_overflow(); + void increment_head() + { + ++head_; + resolve_overflow(); } - void increment_tail() { - ++tail_; - resolve_overflow(); - if (size() > N) - increment_head(); + void increment_tail() + { + ++tail_; + resolve_overflow(); + + if (size() > N) + increment_head(); } - void decrement_head() { - --head_; - resolve_overflow(); - if (size() > N) - decrement_tail(); + void decrement_head() + { + --head_; + resolve_overflow(); + + if (size() > N) + decrement_tail(); } - void decrement_tail() { - --tail_; - resolve_overflow(); + void decrement_tail() + { + --tail_; + resolve_overflow(); } - void resolve_overflow() { - if (empty()) - clear(); - else if (head_.raw_pos() > tail_.raw_pos()) { - // the same value will be obtained regardless of which of the head tail overflows - int len = (INT_MAX - head_.raw_pos()) + (tail_.raw_pos() - INT_MIN); - clear(); - tail_.set(len); - } + void resolve_overflow() + { + if (empty()) + clear(); + else if (head_.raw_pos() > tail_.raw_pos()) + { + // the same value will be obtained regardless of which of the head tail overflows + int len = (INT_MAX - head_.raw_pos()) + (tail_.raw_pos() - INT_MIN); + clear(); + tail_.set(len); + } } - bool is_valid(const iterator& it) { - return (it.raw_pos() >= head_.raw_pos()) && (it.raw_pos() < tail_.raw_pos()); + bool is_valid(const iterator& it) + { + return (it.raw_pos() >= head_.raw_pos()) && (it.raw_pos() < tail_.raw_pos()); } }; template -bool operator==(const RingBuffer& x, const RingBuffer& y) { - if (x.size() != y.size()) return false; - for (size_t i = 0; i < x.size(); ++i) - if (x[i] != y[i]) return false; - return true; +bool operator==(const RingBuffer& x, const RingBuffer& y) +{ + if (x.size() != y.size()) + return false; + + for (size_t i = 0; i < x.size(); ++i) + if (x[i] != y[i]) + return false; + + return true; } template -bool operator!=(const RingBuffer& x, const RingBuffer& y) { - return !(x == y); +bool operator!=(const RingBuffer& x, const RingBuffer& y) +{ + return !(x == y); } template -struct vector : public RingBuffer { +struct vector : public RingBuffer +{ using iterator = typename RingBuffer::iterator; using const_iterator = typename RingBuffer::const_iterator; vector() - : RingBuffer() {} + : RingBuffer() {} vector(std::initializer_list lst) - : RingBuffer(lst) {} + : RingBuffer(lst) {} // copy vector(const vector& r) - : RingBuffer(r) {} + : RingBuffer(r) {} - vector& operator=(const vector& r) { - RingBuffer::operator=(r); - return *this; + vector& operator=(const vector& r) + { + RingBuffer::operator=(r); + return *this; } // move vector(vector&& r) - : RingBuffer(r) {} + : RingBuffer(r) {} - vector& operator=(vector&& r) { - RingBuffer::operator=(r); - return *this; + vector& operator=(vector&& r) + { + RingBuffer::operator=(r); + return *this; } virtual ~vector() {} -private: + private: using RingBuffer::pop; using RingBuffer::pop_front; using RingBuffer::push; @@ -518,36 +702,39 @@ struct vector : public RingBuffer { }; template -struct deque : public RingBuffer { +struct deque : public RingBuffer +{ using iterator = typename RingBuffer::iterator; using const_iterator = typename RingBuffer::const_iterator; deque() - : RingBuffer() {} + : RingBuffer() {} deque(std::initializer_list lst) - : RingBuffer(lst) {} + : RingBuffer(lst) {} // copy deque(const deque& r) - : RingBuffer(r) {} + : RingBuffer(r) {} - deque& operator=(const deque& r) { - RingBuffer::operator=(r); - return *this; + deque& operator=(const deque& r) + { + RingBuffer::operator=(r); + return *this; } // move deque(deque&& r) - : RingBuffer(r) {} + : RingBuffer(r) {} - deque& operator=(deque&& r) { - RingBuffer::operator=(r); - return *this; + deque& operator=(deque&& r) + { + RingBuffer::operator=(r); + return *this; } virtual ~deque() {} -private: + private: using RingBuffer::capacity; using RingBuffer::pop; using RingBuffer::push; @@ -558,146 +745,181 @@ struct deque : public RingBuffer { }; template -struct pair { - T1 first; - T2 second; +struct pair +{ + T1 first; + T2 second; }; template -pair make_pair(const T1& t1, const T2& t2) { - return {t1, t2}; +pair make_pair(const T1& t1, const T2& t2) +{ + return {t1, t2}; }; template -bool operator==(const pair& x, const pair& y) { - return (x.first == y.first) && (x.second == y.second); +bool operator==(const pair& x, const pair& y) +{ + return (x.first == y.first) && (x.second == y.second); } template -bool operator!=(const pair& x, const pair& y) { - return !(x == y); +bool operator!=(const pair& x, const pair& y) +{ + return !(x == y); } template -struct map : public RingBuffer, N> { +struct map : public RingBuffer, N> +{ using iterator = typename RingBuffer, N>::iterator; using const_iterator = typename RingBuffer, N>::const_iterator; using base = RingBuffer, N>; map() - : base() {} + : base() {} map(std::initializer_list > lst) - : base(lst) {} + : base(lst) {} // copy map(const map& r) - : base(r) {} + : base(r) {} - map& operator=(const map& r) { - base::operator=(r); - return *this; + map& operator=(const map& r) + { + base::operator=(r); + return *this; } // move map(map&& r) - : RingBuffer(r) {} + : RingBuffer(r) {} - map& operator=(map&& r) { - base::operator=(r); - return *this; + map& operator=(map&& r) + { + base::operator=(r); + return *this; } virtual ~map() {} - const_iterator find(const Key& key) const { - for (size_t i = 0; i < this->size(); ++i) { - const_iterator it = this->begin() + i; - if (key == it->first) - return it; - } - return this->end(); + const_iterator find(const Key& key) const + { + for (size_t i = 0; i < this->size(); ++i) + { + const_iterator it = this->begin() + i; + + if (key == it->first) + return it; + } + + return this->end(); } - iterator find(const Key& key) { - for (size_t i = 0; i < this->size(); ++i) { - iterator it = this->begin() + i; - if (key == it->first) - return it; - } - return this->end(); + iterator find(const Key& key) + { + for (size_t i = 0; i < this->size(); ++i) + { + iterator it = this->begin() + i; + + if (key == it->first) + return it; + } + + return this->end(); } - pair insert(const Key& key, const T& t) { - bool b {false}; - iterator it = find(key); - if (it == this->end()) { - this->push(make_pair(key, t)); - b = true; - it = this->begin() + this->size() - 1; - } - return {it, b}; + pair insert(const Key& key, const T& t) + { + bool b {false}; + iterator it = find(key); + + if (it == this->end()) + { + this->push(make_pair(key, t)); + b = true; + it = this->begin() + this->size() - 1; + } + + return {it, b}; } - pair insert(const pair& p) { - bool b {false}; - iterator it = find(p.first); - if (it == this->end()) { - this->push(p); - b = true; - it = this->begin() + this->size() - 1; - } - return {it, b}; + pair insert(const pair& p) + { + bool b {false}; + iterator it = find(p.first); + + if (it == this->end()) + { + this->push(p); + b = true; + it = this->begin() + this->size() - 1; + } + + return {it, b}; } - pair emplace(const Key& key, const T& t) { - return insert(key, t); + pair emplace(const Key& key, const T& t) + { + return insert(key, t); } - pair emplace(const pair& p) { - return insert(p); + pair emplace(const pair& p) + { + return insert(p); } - const T& at(const Key& key) const { - // iterator it = find(key); - // if (it != this->end()) return it->second; - // return T(); - return find(key)->second; + const T& at(const Key& key) const + { + // iterator it = find(key); + // if (it != this->end()) return it->second; + // return T(); + return find(key)->second; } - T& at(const Key& key) { - // iterator it = find(key); - // if (it != this->end()) return it->second; - // return T(); - return find(key)->second; + T& at(const Key& key) + { + // iterator it = find(key); + // if (it != this->end()) return it->second; + // return T(); + return find(key)->second; } - iterator erase(const iterator& it) { - iterator i = (iterator)find(it->first); - return base::erase(i); + iterator erase(const iterator& it) + { + iterator i = (iterator)find(it->first); + return base::erase(i); } - iterator erase(const Key& key) { - iterator i = find(key); - return base::erase(i); + iterator erase(const Key& key) + { + iterator i = find(key); + return base::erase(i); } - iterator erase(const size_t index) { - if (index < this->size()) { - iterator it = this->begin() + index; - erase(it); - } - return this->end(); + iterator erase(const size_t index) + { + if (index < this->size()) + { + iterator it = this->begin() + index; + erase(it); + } + + return this->end(); } - T& operator[](const Key& key) { - iterator it = find(key); - if (it != this->end()) return it->second; + T& operator[](const Key& key) + { + iterator it = find(key); + + if (it != this->end()) + return it->second; - insert(::arx::make_pair(key, T())); - return this->back().second; + insert(::arx::make_pair(key, T())); + return this->back().second; } -private: + private: using base::assign; using base::back; using base::capacity; diff --git a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_include.h b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_include.h index d605d71..978dcca 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_include.h +++ b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_include.h @@ -3,28 +3,28 @@ #ifndef ARX_TYPE_TRAITS_HAS_INCLUDE_H #define ARX_TYPE_TRAITS_HAS_INCLUDE_H - // Check whether __has_include is available, but also check the GCC - // version (__has_include was introduced in gcc 5) to catch - // environments (such as ESP8266) where gcc is old, but some system - // header provides a fake __has_include. We also need to check - // against __clang__ here, since clang pretends to be GCC - // 4.something and would otherwise be detected incorrectly here... - #if !defined(__has_include) || defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__) - #if defined(ARDUINO_ARCH_ESP8266) - // ESP8266 does not have a working __has_include, but we - // know it does have a working libstdc++ with all the - // headers we care about, so provide a fake has_include - #define ARX_SYSTEM_HAS_INCLUDE(x) 1 - #elif defined(ARDUINO_SAM_DUE) - // Arduino DUE's GCC version is 4.8.3 (GCC < 5.0). - // If libstdc++ is used, std::function causes error - // so currently we disable libstdc++ and use ArxTypeTraits - #define ARX_SYSTEM_HAS_INCLUDE(x) 0 - #else - #error "Compiler does not support __has_include, please report a bug against the ArxTypeTraits library about this." - #endif - #else - #define ARX_SYSTEM_HAS_INCLUDE(x) __has_include(x) - #endif +// Check whether __has_include is available, but also check the GCC +// version (__has_include was introduced in gcc 5) to catch +// environments (such as ESP8266) where gcc is old, but some system +// header provides a fake __has_include. We also need to check +// against __clang__ here, since clang pretends to be GCC +// 4.something and would otherwise be detected incorrectly here... +#if !defined(__has_include) || defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__) + #if defined(ARDUINO_ARCH_ESP8266) + // ESP8266 does not have a working __has_include, but we + // know it does have a working libstdc++ with all the + // headers we care about, so provide a fake has_include + #define ARX_SYSTEM_HAS_INCLUDE(x) 1 + #elif defined(ARDUINO_SAM_DUE) + // Arduino DUE's GCC version is 4.8.3 (GCC < 5.0). + // If libstdc++ is used, std::function causes error + // so currently we disable libstdc++ and use ArxTypeTraits + #define ARX_SYSTEM_HAS_INCLUDE(x) 0 + #else + #error "Compiler does not support __has_include, please report a bug against the ArxTypeTraits library about this." + #endif +#else + #define ARX_SYSTEM_HAS_INCLUDE(x) __has_include(x) +#endif #endif // ARX_TYPE_TRAITS_HAS_INCLUDE_H diff --git a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_libstdcplusplus.h b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_libstdcplusplus.h index 77d77be..c4d9157 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_libstdcplusplus.h +++ b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/has_libstdcplusplus.h @@ -4,22 +4,22 @@ #define ARX_TYPE_TRAITS_HAS_LIBSTDCPLUSPLUS_H #if !defined(ARX_HAVE_LIBSTDCPLUSPLUS) - #if ARX_SYSTEM_HAS_INCLUDE() - #include - #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) - // For gcc's libstdc++ and clang's libc++, assume that - // __cplusplus tells us what the standard includes support - #define ARX_HAVE_LIBSTDCPLUSPLUS __cplusplus - #elif defined(__UCLIBCXX_MAJOR__) - // For uclibc++, assume C++98 support only. - #define ARX_HAVE_LIBSTDCPLUSPLUS 199711L - #else - #error "Unknown C++ library found, please report a bug against the ArxTypeTraits library about this." - #endif + #if ARX_SYSTEM_HAS_INCLUDE() + #include + #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) + // For gcc's libstdc++ and clang's libc++, assume that + // __cplusplus tells us what the standard includes support + #define ARX_HAVE_LIBSTDCPLUSPLUS __cplusplus + #elif defined(__UCLIBCXX_MAJOR__) + // For uclibc++, assume C++98 support only. + #define ARX_HAVE_LIBSTDCPLUSPLUS 199711L #else - // Assume no standard library is available at all (e.g. on AVR) - #define ARX_HAVE_LIBSTDCPLUSPLUS 0 + #error "Unknown C++ library found, please report a bug against the ArxTypeTraits library about this." #endif + #else + // Assume no standard library is available at all (e.g. on AVR) + #define ARX_HAVE_LIBSTDCPLUSPLUS 0 + #endif #endif #endif // ARX_TYPE_TRAITS_HAS_LIBSTDCPLUSPLUS_H diff --git a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/initializer_list.h b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/initializer_list.h index 374aa27..26673ea 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/initializer_list.h +++ b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/initializer_list.h @@ -10,21 +10,32 @@ #if ARX_SYSTEM_HAS_INCLUDE() #include #else -namespace std { +namespace std +{ template -class initializer_list { -private: +class initializer_list +{ + private: const T* array; size_t len; initializer_list(const T* a, size_t l) - : array(a), len(l) {} + : array(a), len(l) {} -public: + public: initializer_list() - : array(nullptr), len(0) {} - size_t size() const { return len; } - const T* begin() const { return array; } - const T* end() const { return array + len; } + : array(nullptr), len(0) {} + size_t size() const + { + return len; + } + const T* begin() const + { + return array; + } + const T* end() const + { + return array + len; + } }; } // namespace std #endif diff --git a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/replace_minmax_macros.h b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/replace_minmax_macros.h index f8ff86e..c24d56e 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/replace_minmax_macros.h +++ b/src/MQTTPubSubClient_Generic/util/ArxContainer/ArxContainer/replace_minmax_macros.h @@ -7,29 +7,29 @@ // included later and break *uses* of the min/max methods, rather than // the declarations of it. #ifdef ARDUINO - #include + #include #endif // These macros are defined by Arduino.h on some platforms, and conflict // with min/max methods defined or included by ArxTypeTraits, so replace // them with macros here. #ifdef max - #undef max - template - constexpr auto max(T1 x, T2 y) - -> decltype(x + y) - { - return (x > y) ? x : y; - } +#undef max +template +constexpr auto max(T1 x, T2 y) +- > decltype(x + y) +{ + return (x > y) ? x : y; +} #endif #ifdef min - #undef min - template - constexpr auto min(T1 x, T2 y) - -> decltype(x + y) - { - return (x < y) ? x : y; - } +#undef min +template +constexpr auto min(T1 x, T2 y) +- > decltype(x + y) +{ + return (x < y) ? x : y; +} #endif #endif // ARX_TYPE_TRAITS_REPLACE_MINMAX_MACROS_H diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits.h index b0adb68..fead374 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits.h @@ -4,7 +4,7 @@ #define ARX_TYPE_TRAITS_H #if __cplusplus < 201103L - #error "C++11 must be enabled in the compiler for this library to work, please check your compiler flags" + #error "C++11 must be enabled in the compiler for this library to work, please check your compiler flags" #endif #include "ArxTypeTraits/has_include.h" @@ -19,10 +19,12 @@ namespace std { } // This includes everything yet to be defined, so we can do this early // (and must do so, to allow e.g. the C++14 additions in the arx::std // namespace to reference the C++11 stuff from the system headers. -namespace arx { - namespace stdx { - using namespace ::std; - } +namespace arx +{ +namespace stdx +{ +using namespace ::std; +} } // Import everything from arx::std back into the normal std namespace. @@ -31,8 +33,9 @@ namespace arx { // versions for things not supplied by the standard library. Only when // you really need the arx version (e.g. for constexpr numeric_limits // when also using ArduinoSTL), you need to qualify with arx::stdx:: -namespace std { - using namespace ::arx::stdx; +namespace std +{ +using namespace ::arx::stdx; } #include "ArxTypeTraits/replace_minmax_macros.h" diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/functional.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/functional.h index ed25943..fc9f604 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/functional.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/functional.h @@ -25,140 +25,150 @@ // header, so we can just define it inline here without conflicts. // Note that there is no need to include anything to declare the // non-placement new operators, since those are implicit. -inline void* operator new (const size_t size, void* ptr) noexcept { (void)size; return ptr; } +inline void* operator new (const size_t size, void* ptr) noexcept +{ + (void)size; + return ptr; +} #endif -namespace arx { namespace stdx { +namespace arx +{ +namespace stdx +{ - // reference: - // stack overflow https://stackoverflow.com/questions/32074410/stdfunction-bind-like-type-erasure-without-standard-c-library +// reference: +// stack overflow https://stackoverflow.com/questions/32074410/stdfunction-bind-like-type-erasure-without-standard-c-library - template - class function; +template +class function; - template - class function +template +class function +{ + struct vtable_t { - struct vtable_t + void (*mover)(void* src, void* dest); + void (*destroyer)(void*); + R (*invoke)(void const* t, Args&&... args); + + template + static vtable_t const* get() + { + static const vtable_t table = { - void (*mover)(void* src, void* dest); - void (*destroyer)(void*); - R (*invoke)(void const* t, Args&&... args); - - template - static vtable_t const* get() - { - static const vtable_t table = - { - // mover - [] (void* src, void* dest) - { - new(dest) T(move(*static_cast(src))); - }, - // destroyer - [] (void* t) - { - static_cast(t)->~T(); - }, - // invoke - [] (void const* t, Args&&... args) -> R - { - return (*static_cast(t))(std::forward(args)...); - } - }; - return &table; - } + // mover + [] (void* src, void* dest) + { + new (dest) T(move(*static_cast(src))); + }, + // destroyer + [] (void* t) + { + static_cast(t)->~T(); + }, + // invoke + [] (void const * t, Args&&... args) -> R + { + return (*static_cast(t))(std::forward(args)...); + } }; + return &table; + } + }; - vtable_t const* table {nullptr}; - void* data {nullptr}; - - public: - - template < - class Func, - class dF = typename std::decay::type, - typename enable_if {}>::type* = nullptr, - typename enable_if ::type, R>{}>::type* = nullptr - > - function(const Func& f) - : table(vtable_t::template get()) - { - data = reinterpret_cast(new Func(f)); - } - function(const function& o) - : table(o.table) - { - data = o.data; - } - function(function&& o) - : table(o.table) - { - if (table) table->mover(o.data, data); - } - function() - { - } - ~function() - { - if (table) table->destroyer(data); - } - - function& operator= (const function& o) - { - this->~function(); - new(this) function(move(o)); - return *this; - } - function& operator= (function&& o) - { - this->~function(); - new(this) function(move(o)); - return *this; - } - function& operator= (std::nullptr_t p) - { - (void)p; - this->~function(); - return *this; - } + vtable_t const* table {nullptr}; + void* data {nullptr}; - explicit operator bool() const - { - return table; - } + public: - R operator()(Args...args) const - { - return table->invoke(data, forward(args)...); - } - }; - - template - inline bool operator== (const function& f, std::nullptr_t) + template < + class Func, + class dF = typename std::decay::type, + typename enable_if < !std::is_same {} >::type* = nullptr, + typename enable_if < std::is_convertible::type, R> {} >::type* = nullptr + > + function(const Func& f) + : table(vtable_t::template get()) { - return !static_cast(f); + data = reinterpret_cast(new Func(f)); } - - template - inline bool operator== (std::nullptr_t, const function& f) + function(const function& o) + : table(o.table) + { + data = o.data; + } + function(function&& o) + : table(o.table) + { + if (table) + table->mover(o.data, data); + } + function() + { + } + ~function() { - return !static_cast(f); + if (table) + table->destroyer(data); } - template - inline bool operator!= (const function& f, std::nullptr_t) + function& operator= (const function& o) + { + this->~function(); + new (this) function(move(o)); + return *this; + } + function& operator= (function&& o) { - return static_cast(f); + this->~function(); + new (this) function(move(o)); + return *this; + } + function& operator= (std::nullptr_t p) + { + (void)p; + this->~function(); + return *this; } - template - inline bool operator!= (std::nullptr_t, const function& f) + explicit operator bool() const { - return static_cast(f); + return table; } -} } // namespace arx::std + R operator()(Args...args) const + { + return table->invoke(data, forward(args)...); + } +}; + +template +inline bool operator== (const function& f, std::nullptr_t) +{ + return !static_cast(f); +} + +template +inline bool operator== (std::nullptr_t, const function& f) +{ + return !static_cast(f); +} + +template +inline bool operator!= (const function& f, std::nullptr_t) +{ + return static_cast(f); +} + +template +inline bool operator!= (std::nullptr_t, const function& f) +{ + return static_cast(f); +} + +} +} // namespace arx::std #endif // Do not have libstdc++11 diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_include.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_include.h index d605d71..978dcca 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_include.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_include.h @@ -3,28 +3,28 @@ #ifndef ARX_TYPE_TRAITS_HAS_INCLUDE_H #define ARX_TYPE_TRAITS_HAS_INCLUDE_H - // Check whether __has_include is available, but also check the GCC - // version (__has_include was introduced in gcc 5) to catch - // environments (such as ESP8266) where gcc is old, but some system - // header provides a fake __has_include. We also need to check - // against __clang__ here, since clang pretends to be GCC - // 4.something and would otherwise be detected incorrectly here... - #if !defined(__has_include) || defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__) - #if defined(ARDUINO_ARCH_ESP8266) - // ESP8266 does not have a working __has_include, but we - // know it does have a working libstdc++ with all the - // headers we care about, so provide a fake has_include - #define ARX_SYSTEM_HAS_INCLUDE(x) 1 - #elif defined(ARDUINO_SAM_DUE) - // Arduino DUE's GCC version is 4.8.3 (GCC < 5.0). - // If libstdc++ is used, std::function causes error - // so currently we disable libstdc++ and use ArxTypeTraits - #define ARX_SYSTEM_HAS_INCLUDE(x) 0 - #else - #error "Compiler does not support __has_include, please report a bug against the ArxTypeTraits library about this." - #endif - #else - #define ARX_SYSTEM_HAS_INCLUDE(x) __has_include(x) - #endif +// Check whether __has_include is available, but also check the GCC +// version (__has_include was introduced in gcc 5) to catch +// environments (such as ESP8266) where gcc is old, but some system +// header provides a fake __has_include. We also need to check +// against __clang__ here, since clang pretends to be GCC +// 4.something and would otherwise be detected incorrectly here... +#if !defined(__has_include) || defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__) + #if defined(ARDUINO_ARCH_ESP8266) + // ESP8266 does not have a working __has_include, but we + // know it does have a working libstdc++ with all the + // headers we care about, so provide a fake has_include + #define ARX_SYSTEM_HAS_INCLUDE(x) 1 + #elif defined(ARDUINO_SAM_DUE) + // Arduino DUE's GCC version is 4.8.3 (GCC < 5.0). + // If libstdc++ is used, std::function causes error + // so currently we disable libstdc++ and use ArxTypeTraits + #define ARX_SYSTEM_HAS_INCLUDE(x) 0 + #else + #error "Compiler does not support __has_include, please report a bug against the ArxTypeTraits library about this." + #endif +#else + #define ARX_SYSTEM_HAS_INCLUDE(x) __has_include(x) +#endif #endif // ARX_TYPE_TRAITS_HAS_INCLUDE_H diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_libstdcplusplus.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_libstdcplusplus.h index 77d77be..c4d9157 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_libstdcplusplus.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/has_libstdcplusplus.h @@ -4,22 +4,22 @@ #define ARX_TYPE_TRAITS_HAS_LIBSTDCPLUSPLUS_H #if !defined(ARX_HAVE_LIBSTDCPLUSPLUS) - #if ARX_SYSTEM_HAS_INCLUDE() - #include - #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) - // For gcc's libstdc++ and clang's libc++, assume that - // __cplusplus tells us what the standard includes support - #define ARX_HAVE_LIBSTDCPLUSPLUS __cplusplus - #elif defined(__UCLIBCXX_MAJOR__) - // For uclibc++, assume C++98 support only. - #define ARX_HAVE_LIBSTDCPLUSPLUS 199711L - #else - #error "Unknown C++ library found, please report a bug against the ArxTypeTraits library about this." - #endif + #if ARX_SYSTEM_HAS_INCLUDE() + #include + #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) + // For gcc's libstdc++ and clang's libc++, assume that + // __cplusplus tells us what the standard includes support + #define ARX_HAVE_LIBSTDCPLUSPLUS __cplusplus + #elif defined(__UCLIBCXX_MAJOR__) + // For uclibc++, assume C++98 support only. + #define ARX_HAVE_LIBSTDCPLUSPLUS 199711L #else - // Assume no standard library is available at all (e.g. on AVR) - #define ARX_HAVE_LIBSTDCPLUSPLUS 0 + #error "Unknown C++ library found, please report a bug against the ArxTypeTraits library about this." #endif + #else + // Assume no standard library is available at all (e.g. on AVR) + #define ARX_HAVE_LIBSTDCPLUSPLUS 0 + #endif #endif #endif // ARX_TYPE_TRAITS_HAS_LIBSTDCPLUSPLUS_H diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/initializer_list.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/initializer_list.h index b1cd74f..1d1375c 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/initializer_list.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/initializer_list.h @@ -10,20 +10,30 @@ #if ARX_SYSTEM_HAS_INCLUDE() #include #else -namespace std { - template - class initializer_list +namespace std +{ +template +class initializer_list +{ + private: + const T* array; + size_t len; + initializer_list(const T* a, size_t l) : array(a), len(l) {} + public: + initializer_list() : array(nullptr), len(0) {} + size_t size() const { - private: - const T* array; - size_t len; - initializer_list(const T* a, size_t l) : array(a), len(l) {} - public: - initializer_list() : array(nullptr), len(0) {} - size_t size() const { return len; } - const T *begin() const { return array; } - const T *end() const { return array + len; } - }; + return len; + } + const T *begin() const + { + return array; + } + const T *end() const + { + return array + len; + } +}; } // namespace std #endif diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/replace_minmax_macros.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/replace_minmax_macros.h index f8ff86e..c24d56e 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/replace_minmax_macros.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/replace_minmax_macros.h @@ -7,29 +7,29 @@ // included later and break *uses* of the min/max methods, rather than // the declarations of it. #ifdef ARDUINO - #include + #include #endif // These macros are defined by Arduino.h on some platforms, and conflict // with min/max methods defined or included by ArxTypeTraits, so replace // them with macros here. #ifdef max - #undef max - template - constexpr auto max(T1 x, T2 y) - -> decltype(x + y) - { - return (x > y) ? x : y; - } +#undef max +template +constexpr auto max(T1 x, T2 y) +- > decltype(x + y) +{ + return (x > y) ? x : y; +} #endif #ifdef min - #undef min - template - constexpr auto min(T1 x, T2 y) - -> decltype(x + y) - { - return (x < y) ? x : y; - } +#undef min +template +constexpr auto min(T1 x, T2 y) +- > decltype(x + y) +{ + return (x < y) ? x : y; +} #endif #endif // ARX_TYPE_TRAITS_REPLACE_MINMAX_MACROS_H diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/tuple.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/tuple.h index 2c13d95..adc3844 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/tuple.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/tuple.h @@ -9,73 +9,78 @@ #else // Do not have libstdc++11 -namespace arx { namespace stdx { +namespace arx +{ +namespace stdx +{ - // https://theolizer.com/cpp-school2/cpp-school2-15/ - // https://wandbox.org/permlink/C0BWIzjqg4iO3kKZ +// https://theolizer.com/cpp-school2/cpp-school2-15/ +// https://wandbox.org/permlink/C0BWIzjqg4iO3kKZ - template - struct tuple - { - tuple() {} - }; +template +struct tuple +{ + tuple() {} +}; - template - class tuple : public tuple - { - template friend struct get_helper; - tFirst mMember; - public: - tuple(tFirst const& iFirst, tRest const&... iRest) - : tuple(iRest...) - , mMember(iFirst) - { } - constexpr tuple() {} - }; +template +class tuple : public tuple +{ + template friend struct get_helper; + tFirst mMember; + public: + tuple(tFirst const& iFirst, tRest const&... iRest) + : tuple(iRest...) + , mMember(iFirst) + { } + constexpr tuple() {} +}; - template - struct get_helper { }; - template - struct get_helper<0, tFirst, tRest...> - { - typedef tFirst type; - static type& get(tuple& iTuple) - { - return iTuple.mMember; - } - }; - template - struct get_helper - { - typedef typename get_helper::type type; - static type& get(tuple& iTuple) - { - return get_helper::get(iTuple); - } - }; +template +struct get_helper { }; +template +struct get_helper<0, tFirst, tRest...> +{ + typedef tFirst type; + static type& get(tuple& iTuple) + { + return iTuple.mMember; + } +}; +template +struct get_helper +{ + typedef typename get_helper < N - 1, tRest... >::type type; + static type& get(tuple& iTuple) + { + return get_helper < N - 1, tRest... >::get(iTuple); + } +}; - template - typename get_helper::type& get(tuple& iTuple) - { - return get_helper::get(iTuple); - } +template +typename get_helper::type& get(tuple& iTuple) +{ + return get_helper::get(iTuple); +} - template class tuple_size; - template class tuple_size; - template class tuple_size; - template class tuple_size; - template - class tuple_size > - : public integral_constant {}; +template class tuple_size; +template class tuple_size; +template class tuple_size; +template class tuple_size; +template +class tuple_size > + : public integral_constant {}; - template - auto make_tuple(Types&&... args) - -> std::tuple::type...> - { - return std::tuple::type...>(std::forward::type>(args)...); - } +template +auto make_tuple(Types&&... args) +-> std::tuple::type...> +{ + return std::tuple::type...> + (std::forward::type>(args)...); +} -} } // namespace arx::std +} +} // namespace arx::std #endif // Do not have libstdc++11 diff --git a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/type_traits.h b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/type_traits.h index a24c946..974ea17 100644 --- a/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/type_traits.h +++ b/src/MQTTPubSubClient_Generic/util/ArxTypeTraits/ArxTypeTraits/type_traits.h @@ -9,16 +9,20 @@ #else // Do not have libstdc++98 -namespace arx { namespace stdx { - - template - void swap(T& a, T& b) - { - T t = move(a); - a = move(b); - b = move(t); - } -} } // namespace arx::stdx +namespace arx +{ +namespace stdx +{ + +template +void swap(T& a, T& b) +{ + T t = move(a); + a = move(b); + b = move(t); +} +} +} // namespace arx::stdx #endif // Do not have libstdc++98 @@ -34,350 +38,537 @@ namespace arx { namespace stdx { #include #include -namespace arx { namespace stdx { - - using nullptr_t = decltype(nullptr); - - // numeric_limits - - template - struct numeric_limits - { - static constexpr T max() { return T(); } - static constexpr T min() { return T(); } - }; - template <> constexpr bool numeric_limits::max() { return true; } - template <> constexpr char numeric_limits::max() { return CHAR_MAX; } - template <> constexpr signed char numeric_limits::max() { return SCHAR_MAX; } - template <> constexpr unsigned char numeric_limits::max() { return UCHAR_MAX; } - template <> constexpr wchar_t numeric_limits::max() { return WCHAR_MAX; } - // template <> constexpr char8_t numeric_limits::max() { return UCHAR_MAX; } - template <> constexpr char16_t numeric_limits::max() { return UINT_LEAST16_MAX; } - template <> constexpr char32_t numeric_limits::max() { return UINT_LEAST32_MAX; } - template <> constexpr short numeric_limits::max() { return SHRT_MAX; } - template <> constexpr unsigned short numeric_limits::max() { return USHRT_MAX; } - template <> constexpr int numeric_limits::max() { return INT_MAX; } - template <> constexpr unsigned int numeric_limits::max() { return UINT_MAX; } - template <> constexpr long numeric_limits::max() { return LONG_MAX; } - template <> constexpr unsigned long numeric_limits::max() { return ULONG_MAX; } - // template <> constexpr long long numeric_limits::max() { return LLONG_MAX; } - // template <> constexpr unsigned long long numeric_limits::max() { return ULLONG_MAX; } - template <> constexpr float numeric_limits::max() { return FLT_MAX; } - template <> constexpr double numeric_limits::max() { return DBL_MAX; } - template <> constexpr long double numeric_limits::max() { return LDBL_MAX; } - - template <> constexpr bool numeric_limits::min() { return false; } - template <> constexpr char numeric_limits::min() { return CHAR_MIN; } - template <> constexpr signed char numeric_limits::min() { return SCHAR_MIN; } - template <> constexpr unsigned char numeric_limits::min() { return 0; } - template <> constexpr wchar_t numeric_limits::min() { return WCHAR_MIN; } - // template <> constexpr char8_t numeric_limits::min() { return 0; } - template <> constexpr char16_t numeric_limits::min() { return 0; } - template <> constexpr char32_t numeric_limits::min() { return 0; } - template <> constexpr short numeric_limits::min() { return SHRT_MIN; } - template <> constexpr unsigned short numeric_limits::min() { return 0; } - template <> constexpr int numeric_limits::min() { return INT_MIN; } - template <> constexpr unsigned int numeric_limits::min() { return 0; } - template <> constexpr long numeric_limits::min() { return LONG_MIN; } - template <> constexpr unsigned long numeric_limits::min() { return 0; } - // template <> constexpr long long numeric_limits::min() { return LLONG_MIN; } - // template <> constexpr unsigned long long numeric_limits::min() { return 0; } - template <> constexpr float numeric_limits::min() { return FLT_MIN; } - template <> constexpr double numeric_limits::min() { return DBL_MIN; } - template <> constexpr long double numeric_limits::min() { return LDBL_MIN; } - - - // integral_constant - - template - struct integral_constant - { - static constexpr T value = v; - using value_type = T; - using type = integral_constant; - constexpr operator value_type() const noexcept { return value; } - constexpr value_type operator()() const noexcept { return value; } - }; - - using true_type = integral_constant; - using false_type = integral_constant; - - - template - T declval(); // no implementation - - - template - struct enable_if; - template - struct enable_if { using type = T; }; - - - template - struct conditional { using type = T; }; - template - struct conditional { using type = F; }; - - - template struct remove_cv { using type = T; }; - template struct remove_cv { using type = T; }; - template struct remove_cv { using type = T; }; - template struct remove_cv { using type = T; }; - - template struct remove_const { using type = T; }; - template struct remove_const { using type = T; }; - - template struct remove_volatile { using type = T; }; - template struct remove_volatile { using type = T; }; - - template struct remove_pointer { using type = T; }; - template struct remove_pointer { using type = T; }; - template struct remove_pointer { using type = T; }; - template struct remove_pointer { using type = T; }; - template struct remove_pointer { using type = T; }; - - template struct remove_reference { using type = T; }; - template struct remove_reference { using type = T; }; - template struct remove_reference { using type = T; }; - - template struct remove_extent { typedef T type; }; - template struct remove_extent { typedef T type; }; - template struct remove_extent { typedef T type; }; - - - template - constexpr T&& forward(typename remove_reference::type& t) noexcept - { - return static_cast(t); - } - template - constexpr T&& forward(typename remove_reference::type&& t) noexcept - { - return static_cast(t); - } - - - namespace detail - { - template - struct type_identity { using type = T; }; - template - auto try_add_pointer(int) -> type_identity::type*>; - template - auto try_add_pointer(...) -> type_identity; - } - template - struct add_pointer : decltype(detail::try_add_pointer(0)) {}; - - - template - struct is_same : false_type {}; - template - struct is_same : true_type {}; - - - template - struct is_integral : false_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - template <> struct is_integral : true_type {}; - - - template - struct is_floating_point : false_type {}; - template <> struct is_floating_point : true_type {}; - template <> struct is_floating_point : true_type {}; - template <> struct is_floating_point : true_type {}; - - - template - struct is_arithmetic - : conditional< - is_integral::value || is_floating_point::value, - true_type, - false_type +namespace arx +{ +namespace stdx +{ + +using nullptr_t = decltype(nullptr); + +// numeric_limits + +template +struct numeric_limits +{ + static constexpr T max() + { + return T(); + } + static constexpr T min() + { + return T(); + } +}; +template <> constexpr bool numeric_limits::max() +{ + return true; +} +template <> constexpr char numeric_limits::max() +{ + return CHAR_MAX; +} +template <> constexpr signed char numeric_limits::max() +{ + return SCHAR_MAX; +} +template <> constexpr unsigned char numeric_limits::max() +{ + return UCHAR_MAX; +} +template <> constexpr wchar_t numeric_limits::max() +{ + return WCHAR_MAX; +} +// template <> constexpr char8_t numeric_limits::max() { return UCHAR_MAX; } +template <> constexpr char16_t numeric_limits::max() +{ + return UINT_LEAST16_MAX; +} +template <> constexpr char32_t numeric_limits::max() +{ + return UINT_LEAST32_MAX; +} +template <> constexpr short numeric_limits::max() +{ + return SHRT_MAX; +} +template <> constexpr unsigned short numeric_limits::max() +{ + return USHRT_MAX; +} +template <> constexpr int numeric_limits::max() +{ + return INT_MAX; +} +template <> constexpr unsigned int numeric_limits::max() +{ + return UINT_MAX; +} +template <> constexpr long numeric_limits::max() +{ + return LONG_MAX; +} +template <> constexpr unsigned long numeric_limits::max() +{ + return ULONG_MAX; +} +// template <> constexpr long long numeric_limits::max() { return LLONG_MAX; } +// template <> constexpr unsigned long long numeric_limits::max() { return ULLONG_MAX; } +template <> constexpr float numeric_limits::max() +{ + return FLT_MAX; +} +template <> constexpr double numeric_limits::max() +{ + return DBL_MAX; +} +template <> constexpr long double numeric_limits::max() +{ + return LDBL_MAX; +} + +template <> constexpr bool numeric_limits::min() +{ + return false; +} +template <> constexpr char numeric_limits::min() +{ + return CHAR_MIN; +} +template <> constexpr signed char numeric_limits::min() +{ + return SCHAR_MIN; +} +template <> constexpr unsigned char numeric_limits::min() +{ + return 0; +} +template <> constexpr wchar_t numeric_limits::min() +{ + return WCHAR_MIN; +} +// template <> constexpr char8_t numeric_limits::min() { return 0; } +template <> constexpr char16_t numeric_limits::min() +{ + return 0; +} +template <> constexpr char32_t numeric_limits::min() +{ + return 0; +} +template <> constexpr short numeric_limits::min() +{ + return SHRT_MIN; +} +template <> constexpr unsigned short numeric_limits::min() +{ + return 0; +} +template <> constexpr int numeric_limits::min() +{ + return INT_MIN; +} +template <> constexpr unsigned int numeric_limits::min() +{ + return 0; +} +template <> constexpr long numeric_limits::min() +{ + return LONG_MIN; +} +template <> constexpr unsigned long numeric_limits::min() +{ + return 0; +} +// template <> constexpr long long numeric_limits::min() { return LLONG_MIN; } +// template <> constexpr unsigned long long numeric_limits::min() { return 0; } +template <> constexpr float numeric_limits::min() +{ + return FLT_MIN; +} +template <> constexpr double numeric_limits::min() +{ + return DBL_MIN; +} +template <> constexpr long double numeric_limits::min() +{ + return LDBL_MIN; +} + + +// integral_constant + +template +struct integral_constant +{ + static constexpr T value = v; + using value_type = T; + using type = integral_constant; + constexpr operator value_type() const noexcept + { + return value; + } + constexpr value_type operator()() const noexcept + { + return value; + } +}; + +using true_type = integral_constant; +using false_type = integral_constant; + + +template +T declval(); // no implementation + + +template +struct enable_if; +template +struct enable_if +{ + using type = T; +}; + + +template +struct conditional +{ + using type = T; +}; +template +struct conditional +{ + using type = F; +}; + + +template struct remove_cv +{ + using type = T; +}; +template struct remove_cv +{ + using type = T; +}; +template struct remove_cv +{ + using type = T; +}; +template struct remove_cv +{ + using type = T; +}; + +template struct remove_const +{ + using type = T; +}; +template struct remove_const +{ + using type = T; +}; + +template struct remove_volatile +{ + using type = T; +}; +template struct remove_volatile +{ + using type = T; +}; + +template struct remove_pointer +{ + using type = T; +}; +template struct remove_pointer +{ + using type = T; +}; +template struct remove_pointer +{ + using type = T; +}; +template struct remove_pointer +{ + using type = T; +}; +template struct remove_pointer +{ + using type = T; +}; + +template struct remove_reference +{ + using type = T; +}; +template struct remove_reference +{ + using type = T; +}; +template struct remove_reference < T&& > +{ + using type = T; +}; + +template struct remove_extent +{ + typedef T type; +}; +template struct remove_extent +{ + typedef T type; +}; +template struct remove_extent +{ + typedef T type; +}; + + +template +constexpr T&& forward(typename remove_reference::type& t) noexcept +{ + return static_cast < T && > (t); +} +template +constexpr T&& forward(typename remove_reference::type&& t) noexcept +{ + return static_cast < T && > (t); +} + + +namespace detail +{ +template +struct type_identity +{ + using type = T; +}; +template +auto try_add_pointer(int) -> type_identity::type*>; +template +auto try_add_pointer(...) -> type_identity; +} +template +struct add_pointer : decltype(detail::try_add_pointer(0)) {}; + + +template +struct is_same : false_type {}; +template +struct is_same : true_type {}; + + +template +struct is_integral : false_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; +template <> struct is_integral : true_type {}; + + +template +struct is_floating_point : false_type {}; +template <> struct is_floating_point : true_type {}; +template <> struct is_floating_point : true_type {}; +template <> struct is_floating_point : true_type {}; + + +template +struct is_arithmetic + : conditional < + is_integral::value || is_floating_point::value, + true_type, + false_type + >::type +{}; + + +namespace detail +{ +template ::value> +struct is_signed : integral_constant < bool, T(-1) < T(0)> {}; +template +struct is_signed : false_type {}; +} +template +struct is_signed : detail::is_signed::type {}; + + +namespace detail +{ +template::value> +struct is_unsigned : integral_constant < bool, T(0) < T(-1) > {}; +template +struct is_unsigned : false_type {}; +} +template +struct is_unsigned : detail::is_unsigned::type {}; + + +template struct is_pointer_helper : false_type {}; +template struct is_pointer_helper : true_type {}; +template struct is_pointer : is_pointer_helper::type> {}; + + +template +struct is_array : false_type {}; +template +struct is_array : true_type {}; +template +struct is_array : true_type {}; + + +namespace details +{ +template +struct Tester +{ + using type = void; +}; +template +using void_t = typename Tester::type; +templateclass Z, class, class...Ts> +struct can_apply : false_type {}; +templateclass Z, class...Ts> +struct can_apply>, Ts...> : true_type {}; + +template +using try_convert = decltype(To{declval()}); +} +templateclass Z, class...Ts> +using can_apply = details::can_apply; + +template +struct is_convertible + : conditional < + can_apply ::value + , true_type + , typename conditional < + is_arithmetic::value && is_arithmetic::value, + true_type, + false_type >::type - {}; - - - namespace detail - { - template ::value> - struct is_signed : integral_constant {}; - template - struct is_signed : false_type {}; - } - template - struct is_signed : detail::is_signed::type {}; - - - namespace detail - { - template::value> - struct is_unsigned : integral_constant {}; - template - struct is_unsigned : false_type {}; - } - template - struct is_unsigned : detail::is_unsigned::type {}; - - - template struct is_pointer_helper : false_type {}; - template struct is_pointer_helper : true_type {}; - template struct is_pointer : is_pointer_helper::type> {}; - - - template - struct is_array : false_type {}; - template - struct is_array : true_type {}; - template - struct is_array : true_type {}; - - - namespace details - { - template - struct Tester { using type = void; }; - template - using void_t = typename Tester::type; - templateclass Z, class, class...Ts> - struct can_apply : false_type{}; - templateclass Z, class...Ts> - struct can_apply>, Ts...> : true_type{}; - - template - using try_convert = decltype(To{declval()}); - } - templateclass Z, class...Ts> - using can_apply = details::can_apply; - - template - struct is_convertible - : conditional < - can_apply ::value - , true_type - , typename conditional < - is_arithmetic::value && is_arithmetic::value, - true_type, - false_type - >::type >::type - {}; - - template<> - struct is_convertible : true_type{}; - - - // primary template - template - struct is_function : false_type { }; - // specialization for regular functions - template - struct is_function : true_type {}; - // specialization for variadic functions such as printf - template - struct is_function : true_type {}; - // specialization for function types that have cv-qualifiers - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - // specialization for function types that have ref-qualifiers - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - template - struct is_function : true_type {}; - - - template - struct is_empty : public integral_constant { }; - - - template - class decay - { - typedef typename remove_reference::type U; - public: - typedef typename conditional< - is_array::value, - typename remove_extent::type*, - typename conditional< - is_function::value, - typename add_pointer::type, - typename remove_cv::type - >::type - >::type type; - }; - - - namespace details - { - template struct tag { using type=T; }; - template using type_t = typename Tag::type; - - template - using invoke_t = decltype( declval()(declval()...) ); - - template - struct result_of {}; - template - struct result_of>> - : tag > - {}; - } - template - using result_of = details::result_of; - -} } // namespace arx::stdx +{}; + +template<> +struct is_convertible : true_type {}; + + +// primary template +template +struct is_function : false_type { }; +// specialization for regular functions +template +struct is_function : true_type {}; +// specialization for variadic functions such as printf +template +struct is_function : true_type {}; +// specialization for function types that have cv-qualifiers +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +// specialization for function types that have ref-qualifiers +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function : true_type {}; +template +struct is_function < Ret(Args...) && > : true_type {}; +template +struct is_function < Ret(Args...) const && > : true_type {}; +template +struct is_function < Ret(Args...) volatile && > : true_type {}; +template +struct is_function < Ret(Args...) const volatile && > : true_type {}; +template +struct is_function < Ret(Args......) && > : true_type {}; +template +struct is_function < Ret(Args......) const && > : true_type {}; +template +struct is_function < Ret(Args......) volatile && > : true_type {}; +template +struct is_function < Ret(Args......) const volatile && > : true_type {}; + + +template +struct is_empty : public integral_constant { }; + + +template +class decay +{ + typedef typename remove_reference::type U; + public: + typedef typename conditional < + is_array::value, + typename remove_extent::type *, + typename conditional < + is_function::value, + typename add_pointer::type, + typename remove_cv::type + >::type + >::type type; +}; + + +namespace details +{ +template struct tag +{ + using type = T; +}; +template using type_t = typename Tag::type; + +template +using invoke_t = decltype( declval()(declval()...) ); + +template +struct result_of {}; +template +struct result_of>> +: tag > +{}; +} +template +using result_of = details::result_of; + +} +} // namespace arx::stdx #endif // Do not have libstdc++11 @@ -386,18 +577,22 @@ namespace arx { namespace stdx { #else // Do not have libstdc++14 -namespace arx { namespace stdx { +namespace arx +{ +namespace stdx +{ - // `move` must be declared before including `functional.h` - // C++14 constexpr version should be inside of C++14, - // but moved before `functional.h` - template - constexpr typename remove_reference::type&& move(T&& t) noexcept - { - return static_cast::type&&>(t); - } +// `move` must be declared before including `functional.h` +// C++14 constexpr version should be inside of C++14, +// but moved before `functional.h` +template +constexpr typename remove_reference::type && move(T && t) noexcept +{ + return static_cast < typename remove_reference::type && > (t); +} -} } // namespace arx::stdx +} +} // namespace arx::stdx #endif // Do not have libstdc++14 @@ -407,227 +602,255 @@ namespace arx { namespace stdx { #include "functional.h" #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201402L // Have libstdc++14 - // Nothing to include here, relevant header files were already included - // for C++11 above. +// Nothing to include here, relevant header files were already included +// for C++11 above. #else // Do not have libstdc++14 -namespace arx { namespace stdx { - - template - using enable_if_t = typename enable_if::type; - - template - using decay_t = typename decay::type; - - template - using remove_cv_t = typename remove_cv::type; - template - using remove_const_t = typename remove_const::type; - template - using remove_volatile_t = typename remove_volatile::type; - template - using remove_reference_t = typename remove_reference::type; - template - using remove_pointer_t = typename remove_pointer::type; - - template - struct integer_sequence - { - using type = integer_sequence; - using value_type = T; - static constexpr size_t size() noexcept { return sizeof...(Ts); } - }; - template - using index_sequence = integer_sequence; - - // https://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence - - template - struct concat_impl; - template - using concat = typename concat_impl::type; - - template - struct concat_impl , index_sequence> - : index_sequence {}; - template - struct make_index_sequence_impl; - template - using make_index_sequence = typename make_index_sequence_impl::type; - template - struct make_index_sequence_impl - : concat, make_index_sequence > {}; - template<> - struct make_index_sequence_impl <0> : index_sequence<>{}; - template<> - struct make_index_sequence_impl <1> : index_sequence<0>{}; - - template - using index_sequence_for = make_index_sequence; - -} } // namespace arx::stdx +namespace arx +{ +namespace stdx +{ + +template +using enable_if_t = typename enable_if::type; + +template +using decay_t = typename decay::type; + +template +using remove_cv_t = typename remove_cv::type; +template +using remove_const_t = typename remove_const::type; +template +using remove_volatile_t = typename remove_volatile::type; +template +using remove_reference_t = typename remove_reference::type; +template +using remove_pointer_t = typename remove_pointer::type; + +template +struct integer_sequence +{ + using type = integer_sequence; + using value_type = T; + static constexpr size_t size() noexcept + { + return sizeof...(Ts); + } +}; +template +using index_sequence = integer_sequence; + +// https://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence + +template +struct concat_impl; +template +using concat = typename concat_impl::type; + +template +struct concat_impl , index_sequence> + : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; +template +struct make_index_sequence_impl; +template +using make_index_sequence = typename make_index_sequence_impl::type; +template +struct make_index_sequence_impl + : concat < make_index_sequence < N / 2 >, make_index_sequence < N - N / 2 >> {}; +template<> +struct make_index_sequence_impl <0> : index_sequence<> {}; +template<> +struct make_index_sequence_impl <1> : index_sequence<0> {}; + +template +using index_sequence_for = make_index_sequence; + +} +} // namespace arx::stdx #endif // Do not have libstdc++14 #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201703L // Have libstdc++17 - // Nothing to include here, relevant header files were already included - // for C++11 above. +// Nothing to include here, relevant header files were already included +// for C++11 above. #else // Do not have libstdc++17 -namespace arx { namespace stdx { - - template - struct Tester { using type = void; }; - template - using void_t = typename Tester::type; - - template - struct disjunction : false_type {}; - template - struct disjunction : Arg::type {}; - template - struct disjunction : conditional>::type {}; - - template - struct conjunction : true_type {}; - template - struct conjunction : Arg::type {}; - template - struct conjunction : conditional, Arg>::type {}; - - template - struct negation : integral_constant {}; - - // https://qiita.com/_EnumHack/items/92e6e135174f1f781dbb - // without decltype(auto) - - template - constexpr auto apply_impl(F&& f, Tuple&& t, index_sequence) - -> decltype(f(get(forward(t))...)) - { - return f(get(forward(t))...); - } - template - constexpr auto apply(F&& f, Tuple&& t) - -> decltype(apply_impl( - forward(f), - forward(t), - make_index_sequence>::value>{} - )) - { - return apply_impl( - forward(f), - forward(t), - make_index_sequence>::value>() - ); - } - -} } // namespace arx::stdx +namespace arx +{ +namespace stdx +{ + +template +struct Tester +{ + using type = void; +}; +template +using void_t = typename Tester::type; + +template +struct disjunction : false_type {}; +template +struct disjunction : Arg::type {}; +template +struct disjunction : conditional>::type {}; + +template +struct conjunction : true_type {}; +template +struct conjunction : Arg::type {}; +template +struct conjunction : conditional, Arg>::type {}; + +template +struct negation : integral_constant < bool, !T::value > {}; + +// https://qiita.com/_EnumHack/items/92e6e135174f1f781dbb +// without decltype(auto) + +template +constexpr auto apply_impl(F&& f, Tuple&& t, index_sequence) +-> decltype(f(get(forward(t))...)) +{ + return f(get(forward(t))...); +} +template +constexpr auto apply(F&& f, Tuple&& t) +-> decltype(apply_impl( + forward(f), + forward(t), +make_index_sequence>::value> {} + )) +{ + return apply_impl( + forward(f), + forward(t), + make_index_sequence>::value>() + ); +} + +} +} // namespace arx::stdx #endif // Do not have libstdc++17 #if ARX_HAVE_LIBSTDCPLUSPLUS > 201703L // Have libstdc++2a - // Nothing to include here, relevant header files were already included - // for C++11 above. +// Nothing to include here, relevant header files were already included +// for C++11 above. #else // Do not have libstdc++2a -namespace arx { namespace stdx { +namespace arx +{ +namespace stdx +{ - template - struct remove_cvref - { - typedef remove_cv_t> type; - }; +template +struct remove_cvref +{ + typedef remove_cv_t> type; +}; - template< class T > - using remove_cvref_t = typename remove_cvref::type; +template< class T > +using remove_cvref_t = typename remove_cvref::type; -} } // namespace arx::stdx +} +} // namespace arx::stdx #endif // Do not have libstdc++2a -namespace arx { // others - - template class Check, class... T> - struct is_detected_impl : std::false_type {}; - template