From 8298499a8616a380b91a0701ed9005ca76807f29 Mon Sep 17 00:00:00 2001 From: HendrikVE Date: Tue, 12 Jun 2018 11:13:06 +0200 Subject: [PATCH 1/9] added mqtt library --- libraries/arduino-mqtt-2.3.1/.editorconfig | 7 + libraries/arduino-mqtt-2.3.1/.gitignore | 2 + libraries/arduino-mqtt-2.3.1/.travis.yml | 46 ++ libraries/arduino-mqtt-2.3.1/CMakeLists.txt | 40 + libraries/arduino-mqtt-2.3.1/LICENSE.md | 21 + libraries/arduino-mqtt-2.3.1/Makefile | 14 + libraries/arduino-mqtt-2.3.1/README.md | 225 ++++++ .../AdafruitHuzzahESP8266.ino | 69 ++ .../AdafruitHuzzahESP8266_SSL.ino | 71 ++ .../ArduinoEthernetShield.ino | 62 ++ .../ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino | 84 ++ .../ArduinoMKRGSM1400_SSL.ino | 86 ++ .../ArduinoWiFi101/ArduinoWiFi101.ino | 70 ++ .../ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino | 75 ++ .../ArduinoWiFiShield/ArduinoWiFiShield.ino | 68 ++ .../examples/ArduinoYun/ArduinoYun.ino | 60 ++ .../ArduinoYun_SSL/ArduinoYun_SSL.ino | 62 ++ .../ESP32DevelopmentBoard.ino | 69 ++ .../ESP32DevelopmentBoard_SSL.ino | 71 ++ .../arduino-mqtt-2.3.1/library.properties | 9 + libraries/arduino-mqtt-2.3.1/src/MQTT.h | 6 + libraries/arduino-mqtt-2.3.1/src/MQTTClient.h | 363 +++++++++ .../arduino-mqtt-2.3.1/src/lwmqtt/client.c | 615 +++++++++++++++ .../arduino-mqtt-2.3.1/src/lwmqtt/helpers.c | 251 ++++++ .../arduino-mqtt-2.3.1/src/lwmqtt/helpers.h | 137 ++++ .../arduino-mqtt-2.3.1/src/lwmqtt/lwmqtt.h | 373 +++++++++ .../arduino-mqtt-2.3.1/src/lwmqtt/packet.c | 742 ++++++++++++++++++ .../arduino-mqtt-2.3.1/src/lwmqtt/packet.h | 185 +++++ .../arduino-mqtt-2.3.1/src/lwmqtt/string.c | 38 + libraries/arduino-mqtt-2.3.1/src/system.cpp | 48 ++ libraries/arduino-mqtt-2.3.1/src/system.h | 26 + libraries/lwmqtt/client.c | 615 +++++++++++++++ libraries/lwmqtt/helpers.c | 251 ++++++ libraries/lwmqtt/helpers.h | 137 ++++ libraries/lwmqtt/lwmqtt.h | 373 +++++++++ libraries/lwmqtt/packet.c | 742 ++++++++++++++++++ libraries/lwmqtt/packet.h | 185 +++++ libraries/lwmqtt/string.c | 38 + 38 files changed, 6336 insertions(+) create mode 100644 libraries/arduino-mqtt-2.3.1/.editorconfig create mode 100644 libraries/arduino-mqtt-2.3.1/.gitignore create mode 100644 libraries/arduino-mqtt-2.3.1/.travis.yml create mode 100644 libraries/arduino-mqtt-2.3.1/CMakeLists.txt create mode 100644 libraries/arduino-mqtt-2.3.1/LICENSE.md create mode 100644 libraries/arduino-mqtt-2.3.1/Makefile create mode 100644 libraries/arduino-mqtt-2.3.1/README.md create mode 100644 libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101/ArduinoWiFi101.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoYun/ArduinoYun.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino create mode 100644 libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino create mode 100644 libraries/arduino-mqtt-2.3.1/library.properties create mode 100644 libraries/arduino-mqtt-2.3.1/src/MQTT.h create mode 100644 libraries/arduino-mqtt-2.3.1/src/MQTTClient.h create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/client.c create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.c create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.h create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/lwmqtt.h create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.c create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.h create mode 100644 libraries/arduino-mqtt-2.3.1/src/lwmqtt/string.c create mode 100644 libraries/arduino-mqtt-2.3.1/src/system.cpp create mode 100644 libraries/arduino-mqtt-2.3.1/src/system.h create mode 100644 libraries/lwmqtt/client.c create mode 100644 libraries/lwmqtt/helpers.c create mode 100644 libraries/lwmqtt/helpers.h create mode 100644 libraries/lwmqtt/lwmqtt.h create mode 100644 libraries/lwmqtt/packet.c create mode 100644 libraries/lwmqtt/packet.h create mode 100644 libraries/lwmqtt/string.c diff --git a/libraries/arduino-mqtt-2.3.1/.editorconfig b/libraries/arduino-mqtt-2.3.1/.editorconfig new file mode 100644 index 00000000000..3edae7b539a --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/.editorconfig @@ -0,0 +1,7 @@ +[Makefile] +indent_style = tab +indent_size = 4 + +[src/*.h,src/*.cpp,examples/**.ino] +indent_style = space +indent_size = 2 diff --git a/libraries/arduino-mqtt-2.3.1/.gitignore b/libraries/arduino-mqtt-2.3.1/.gitignore new file mode 100644 index 00000000000..0c4fe4711d3 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +cmake-build-debug/ diff --git a/libraries/arduino-mqtt-2.3.1/.travis.yml b/libraries/arduino-mqtt-2.3.1/.travis.yml new file mode 100644 index 00000000000..82f28bb50f8 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/.travis.yml @@ -0,0 +1,46 @@ +language: generic +env: + global: + - IDE_VERSION=1.8.5 + matrix: + - EXAMPLE="AdafruitHuzzahESP8266" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80" + - EXAMPLE="AdafruitHuzzahESP8266_SSL" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80" + - EXAMPLE="ArduinoEthernetShield" BOARD="arduino:avr:uno" + - EXAMPLE="ArduinoMKRGSM1400" BOARD="arduino:samd:mkrgsm1400" + - EXAMPLE="ArduinoMKRGSM1400_SSL" BOARD="arduino:samd:mkrgsm1400" + - EXAMPLE="ArduinoWiFi101_SSL" BOARD="arduino:avr:uno" + - EXAMPLE="ArduinoWiFiShield" BOARD="arduino:avr:uno" + - EXAMPLE="ArduinoYun" BOARD="arduino:avr:yun" + - EXAMPLE="ArduinoYun_SSL" BOARD="arduino:avr:yun" + - EXAMPLE="ESP32DevelopmentBoard" BOARD="espressif:esp32:esp32:FlashFreq=80" + - EXAMPLE="ESP32DevelopmentBoard_SSL" BOARD="espressif:esp32:esp32:FlashFreq=80" +before_install: + - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16 + - sleep 3 + - export DISPLAY=:1.0 + - wget http://downloads.arduino.cc/arduino-$IDE_VERSION-linux64.tar.xz + - tar xf arduino-$IDE_VERSION-linux64.tar.xz + - mv arduino-$IDE_VERSION ~/arduino-ide + - export PATH=$PATH:~/arduino-ide + - if [[ "$BOARD" =~ "esp8266:esp8266:" ]]; then + arduino --pref "boardsmanager.additional.urls=http://arduino.esp8266.com/stable/package_esp8266com_index.json" --install-boards esp8266:esp8266; + arduino --pref "boardsmanager.additional.urls=" --save-prefs; + fi + - if [[ "$BOARD" =~ "espressif:esp32:" ]]; then + mkdir -p ~/Arduino/hardware/espressif && + cd ~/Arduino/hardware/espressif && + git clone https://github.com/espressif/arduino-esp32.git esp32 && + cd esp32/tools/ && + python get.py && + cd $TRAVIS_BUILD_DIR; + fi + - if [[ "$BOARD" =~ "arduino:samd:mkrgsm1400" ]]; then + arduino --install-boards arduino:samd; + arduino --install-library MKRGSM; + fi + - arduino --install-library WiFi101 +install: + - mkdir -p ~/Arduino/libraries + - ln -s $PWD ~/Arduino/libraries/. +script: + - arduino --verbose-build --verify --board $BOARD $PWD/examples/$EXAMPLE/$EXAMPLE.ino; diff --git a/libraries/arduino-mqtt-2.3.1/CMakeLists.txt b/libraries/arduino-mqtt-2.3.1/CMakeLists.txt new file mode 100644 index 00000000000..e9a7798673e --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/CMakeLists.txt @@ -0,0 +1,40 @@ +# Uncompilable CMake File to enable project editing with CLion IDE + +cmake_minimum_required(VERSION 2.8.4) +project(arduino-mqtt) + +include_directories( + /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/ + /Users/256dpi/Development/Arduino/libraries/Ethernet/src + /Users/256dpi/Development/Arduino/libraries/WiFi101/src + /Users/256dpi/Development/Arduino/libraries/MKRGSM/src + /Applications/Arduino.app/Contents/Java/libraries/Bridge/src + /Users/256dpi/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src + /Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFi/src + /Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFiClientSecure/src + src/) + +include_directories(src/) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES + examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino + examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino + examples/ArduinoEthernetShield/ArduinoEthernetShield.ino + examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino + examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino + examples/ArduinoWiFi101/ArduinoWiFi101.ino + examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino + examples/ArduinoWiFiShield/ArduinoWiFiShield.ino + examples/ArduinoYun/ArduinoYun.ino + examples/ArduinoYun_SSL/ArduinoYun_SSL.ino + examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino + examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino + src/lwmqtt + src/MQTT.h + src/MQTTClient.h + src/system.cpp + src/system.h) + +add_executable(arduino-mqtt ${SOURCE_FILES}) diff --git a/libraries/arduino-mqtt-2.3.1/LICENSE.md b/libraries/arduino-mqtt-2.3.1/LICENSE.md new file mode 100644 index 00000000000..325e07cffe5 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Joël Gähwiler + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libraries/arduino-mqtt-2.3.1/Makefile b/libraries/arduino-mqtt-2.3.1/Makefile new file mode 100644 index 00000000000..0e94cc41262 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/Makefile @@ -0,0 +1,14 @@ +all: fmt + +fmt: + clang-format -i src/*.cpp src/*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}" + +update: + rm -rf ./lwmqtt + git clone --branch v0.5.6 https://github.com/256dpi/lwmqtt.git ./lwmqtt + mkdir -p ./src/lwmqtt + cp -r ./lwmqtt/src/*.c ./src/lwmqtt/ + cp -r ./lwmqtt/src/*.h ./src/lwmqtt/ + cp -r ./lwmqtt/include/*.h ./src/lwmqtt/ + rm -rf ./lwmqtt + sed -i '' "s//\"lwmqtt.h\"/g" ./src/lwmqtt/* diff --git a/libraries/arduino-mqtt-2.3.1/README.md b/libraries/arduino-mqtt-2.3.1/README.md new file mode 100644 index 00000000000..b294c9fb1b4 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/README.md @@ -0,0 +1,225 @@ +# arduino-mqtt + +[![Build Status](https://travis-ci.org/256dpi/arduino-mqtt.svg?branch=master)](https://travis-ci.org/256dpi/arduino-mqtt) +[![GitHub release](https://img.shields.io/github/release/256dpi/arduino-mqtt.svg)](https://github.com/256dpi/arduino-mqtt/releases) + +This library bundles the [lwmqtt](https://github.com/256dpi/lwmqtt) MQTT 3.1.1 client and adds a thin wrapper to get an Arduino like API. + +Download the latest version from the [release](https://github.com/256dpi/arduino-mqtt/releases) section. Or even better use the builtin Library Manager in the Arduino IDE and search for "MQTT". + +The library is also available on [PlatformIO](https://platformio.org/lib/show/617/MQTT). You can install it by running: `pio lib install "MQTT"`. + +## Compatibility + +The following examples show how you can use the library with various Arduino compatible hardware: + +- [Arduino Yun & Yun-Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun/ArduinoYun.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino)) +- [Arduino Ethernet Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino) +- [Arduino WiFi Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino) +- [Adafruit HUZZAH ESP8266](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino)) +- [Arduino/Genuino WiFi101 Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFi101/ArduinoWiFi101.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino)) +- [Arduino MKR GSM 1400](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino)) +- [ESP32 Development Board](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino)) + +Other shields and boards should also work if they provide a [Client](https://www.arduino.cc/en/Reference/ClientConstructor) based network implementation. + +## Notes + +- The maximum size for packets being published and received is set by default to 128 bytes. To change the buffer sizes, you need to use `MQTTClient client(256)` instead of just `MQTTClient client` on the top of your sketch. The passed value denotes the read and write buffer size. + +- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections. + +- To use the library with shiftr.io, you need to provide the token key (username) and token secret (password) as the second and third argument to `client.connect(name, key, secret)`. + +## Example + +The following example uses an Arduino MKR1000 to connect to shiftr.io. You can check on your device after a successful connection here: https://shiftr.io/try. + +```c++ +#include +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} +``` + +## API + +Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport: + +```c++ +void begin(const char hostname[], Client &client); +void begin(const char hostname[], int port, Client &client); +``` + +- Specify port `8883` when using SSL clients for secure connections. +- Local domain names (e.g. `Computer.local` on OSX) are not supported by Arduino. You need to set the IP address directly. + +The hostname and port can also be changed after calling `begin()`: + +```c++ +void setHost(const char hostname[]); +void setHost(const char hostname[], int port); +``` + +Set a will message (last testament) that gets registered on the broker after connecting: + +```c++ +void setWill(const char topic[]); +void setWill(const char topic[], const char payload[]); +void setWill(const char topic[], const char payload[], bool retained, int qos); +void clearWill(); +``` + +Register a callback to receive messages: + +```c++ +void onMessage(MQTTClientCallbackSimple); +// Callback signature: void messageReceived(String &topic, String &payload) {} + +void onMessageAdvanced(MQTTClientCallbackAdvanced); +// Callback signature: void messageReceived(MQTTClient *client, char[] topic, char payload[], int payload_length) {} +``` + +- The set callback is mostly called during a call to `loop()` but may also be called during a call to `subscribe()`, `unsubscribe()` or `publish() // QoS > 0` if messages have been received before receiving the required acknowledgement. Therefore, it is strongly recommended to not call `subscribe()`, `unsubscribe()` or `publish() // QoS > 0` directly in the callback. + +Set more advanced options: + +```c++ +void setOptions(int keepAlive, bool cleanSession, int timeout); +``` + +- The `keepAlive` option controls the keep alive interval (default: 10). +- The `cleanSession` option controls the session retention on the broker side (default: true). +- The `timeout` option controls the default timeout for all commands in milliseconds (default: 1000). + +Connect to broker using the supplied client id and an optional username and password: + +```c++ +bool connect(const char clientId[]); +bool connect(const char clientId[], const char username[]); +bool connect(const char clientId[], const char username[], const char password[]); +``` + +- This functions returns a boolean that indicates if the connection has been established successfully. + +Publishes a message to the broker with an optional payload: + +```c++ +bool publish(const String &topic); +bool publish(const char topic[]); +bool publish(const String &topic, const String &payload); +bool publish(const String &topic, const String &payload, bool retained, int qos); +bool publish(const char topic[], const String &payload); +bool publish(const char topic[], const String &payload, bool retained, int qos); +bool publish(const char topic[], const char payload[]); +bool publish(const char topic[], const char payload[], bool retained, int qos); +bool publish(const char topic[], const char payload[], int length); +bool publish(const char topic[], const char payload[], int length, bool retained, int qos); +``` + +Subscribe to a topic: + +```c++ +bool subscribe(const String &topic); +bool subscribe(const String &topic, int qos); +bool subscribe(const char topic[]); +bool subscribe(const char topic[], int qos); +``` + +Unsubscribe from a topic: + +```c++ +bool unsubscribe(const String &topic); +bool unsubscribe(const char topic[]); +``` + +Sends and receives packets: + +```c++ +bool loop(); +``` + +- This function should be called in every `loop`. + +Check if the client is currently connected: + +```c++ +bool connected(); +``` + +Access low-level information for debugging: + +```c++ +lwmqtt_err_t lastError(); +lwmqtt_return_code_t returnCode(); +``` + +- The error codes can be found [here](https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L11). +- The return codes can be found [here](https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L243). + +Disconnect from the broker: + +```c++ +bool disconnect(); +``` + +## Release Management + +- Update version in `library.properties`. +- Create release on GitHub. diff --git a/libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino b/libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino new file mode 100644 index 00000000000..ca5a2260779 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino @@ -0,0 +1,69 @@ +// This example uses an Adafruit Huzzah ESP8266 +// to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + delay(10); // <- fixes some issues with WiFi stability + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino b/libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino new file mode 100644 index 00000000000..1def5678dac --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino @@ -0,0 +1,71 @@ +// This example uses an Adafruit Huzzah ESP8266 +// to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClientSecure net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + // + // MQTT brokers usually use port 8883 for secure connections. + client.begin("broker.shiftr.io", 8883, net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + delay(10); // <- fixes some issues with WiFi stability + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino new file mode 100644 index 00000000000..8386c77881a --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino @@ -0,0 +1,62 @@ +// This example uses an Arduino Uno together with +// an Ethernet Shield to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; +byte ip[] = {192, 168, 1, 177}; // <- change to match your network + +EthernetClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("connecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + Ethernet.begin(mac, ip); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino new file mode 100644 index 00000000000..dd56d5e21e7 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino @@ -0,0 +1,84 @@ +// This example uses an Arduino MKR GSM 1400 board +// to connect to shiftr.io. +// +// IMPORTANT: This example uses the new MKRGSM library. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Sandeep Mistry +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char pin[] = ""; +const char apn[] = "apn"; +const char login[] = "login"; +const char password[] = "password"; + +GSMClient net; +GPRS gprs; +GSM gsmAccess; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + // connection state + bool connected = false; + + Serial.print("connecting to cellular network ..."); + + // After starting the modem with gsmAccess.begin() + // attach to the GPRS network with the APN, login and password + while (!connected) { + if ((gsmAccess.begin(pin) == GSM_READY) && + (gprs.attachGPRS(apn, login, password) == GPRS_READY)) { + connected = true; + } else { + Serial.print("."); + delay(1000); + } + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino new file mode 100644 index 00000000000..1b172ab6eaf --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino @@ -0,0 +1,86 @@ +// This example uses an Arduino MKR GSM 1400 board +// to securely connect to shiftr.io. +// +// IMPORTANT: This example uses the new MKRGSM library. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Sandeep Mistry +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char pin[] = ""; +const char apn[] = "apn"; +const char login[] = "login"; +const char password[] = "password"; + +GSMSSLClient net; +GPRS gprs; +GSM gsmAccess; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + // connection state + bool connected = false; + + Serial.print("connecting to cellular network ..."); + + // After starting the modem with gsmAccess.begin() + // attach to the GPRS network with the APN, login and password + while (!connected) { + if ((gsmAccess.begin(pin) == GSM_READY) && + (gprs.attachGPRS(apn, login, password) == GPRS_READY)) { + connected = true; + } else { + Serial.print("."); + delay(1000); + } + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + // + // MQTT brokers usually use port 8883 for secure connections. + client.begin("broker.shiftr.io", 8883, net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101/ArduinoWiFi101.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101/ArduinoWiFi101.ino new file mode 100644 index 00000000000..a36bd65aa67 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101/ArduinoWiFi101.ino @@ -0,0 +1,70 @@ +// This example uses an Arduino/Genuino Zero together with +// a WiFi101 Shield or a MKR1000 to connect to shiftr.io. +// +// IMPORTANT: This example uses the new WiFi101 library. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Gilberto Conti +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino new file mode 100644 index 00000000000..c21e7ae997f --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino @@ -0,0 +1,75 @@ +// This example uses an Arduino/Genuino Zero together with +// a WiFi101 Shield or a MKR1000 to connect to shiftr.io. +// +// IMPORTANT: This example uses the new WiFi101 library. +// +// IMPORTANT: You need to install/update the SSL certificates first: +// https://github.com/arduino-libraries/WiFi101-FirmwareUpdater#to-update-ssl-certificates +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Gilberto Conti +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiSSLClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + // + // MQTT brokers usually use port 8883 for secure connections. + client.begin("broker.shiftr.io", 8883, net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino new file mode 100644 index 00000000000..4aff769f4d5 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino @@ -0,0 +1,68 @@ +// This example uses an Arduino Uno together with +// a WiFi Shield to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoYun/ArduinoYun.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoYun/ArduinoYun.ino new file mode 100644 index 00000000000..823bdff3660 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoYun/ArduinoYun.ino @@ -0,0 +1,60 @@ +// This example uses an Arduino Yun or a Yun-Shield +// and the MQTTClient to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include +#include + +BridgeClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("connecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Bridge.begin(); + Serial.begin(115200); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino b/libraries/arduino-mqtt-2.3.1/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino new file mode 100644 index 00000000000..46c068ab230 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino @@ -0,0 +1,62 @@ +// This example uses an Arduino Yun or a Yun-Shield +// and the MQTTClient to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include +#include + +BridgeSSLClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("connecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Bridge.begin(); + Serial.begin(115200); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + // + // MQTT brokers usually use port 8883 for secure connections. + client.begin("broker.shiftr.io", 8883, net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino b/libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino new file mode 100644 index 00000000000..c6919280df0 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino @@ -0,0 +1,69 @@ +// This example uses an ESP32 Development Board +// to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClient net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + client.begin("broker.shiftr.io", net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + delay(10); // <- fixes some issues with WiFi stability + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino b/libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino new file mode 100644 index 00000000000..cff75379ec6 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino @@ -0,0 +1,71 @@ +// This example uses an ESP32 Development Board +// to connect to shiftr.io. +// +// You can check on your device after a successful +// connection here: https://shiftr.io/try. +// +// by Joël Gähwiler +// https://github.com/256dpi/arduino-mqtt + +#include +#include + +const char ssid[] = "ssid"; +const char pass[] = "pass"; + +WiFiClientSecure net; +MQTTClient client; + +unsigned long lastMillis = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect("arduino", "try", "try")) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + + client.subscribe("/hello"); + // client.unsubscribe("/hello"); +} + +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. + // You need to set the IP address directly. + // + // MQTT brokers usually use port 8883 for secure connections. + client.begin("broker.shiftr.io", 8883, net); + client.onMessage(messageReceived); + + connect(); +} + +void loop() { + client.loop(); + delay(10); // <- fixes some issues with WiFi stability + + if (!client.connected()) { + connect(); + } + + // publish a message roughly every second. + if (millis() - lastMillis > 1000) { + lastMillis = millis(); + client.publish("/hello", "world"); + } +} diff --git a/libraries/arduino-mqtt-2.3.1/library.properties b/libraries/arduino-mqtt-2.3.1/library.properties new file mode 100644 index 00000000000..86be671dfeb --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/library.properties @@ -0,0 +1,9 @@ +name=MQTT +version=2.3.1 +author=Joel Gaehwiler +maintainer=Joel Gaehwiler +sentence=MQTT library for Arduino +paragraph=This library bundles the lwmqtt client and adds a thin wrapper to get an Arduino like API. +category=Communication +url=https://github.com/256dpi/arduino-mqtt +architectures=* diff --git a/libraries/arduino-mqtt-2.3.1/src/MQTT.h b/libraries/arduino-mqtt-2.3.1/src/MQTT.h new file mode 100644 index 00000000000..35652c45f9c --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/MQTT.h @@ -0,0 +1,6 @@ +#ifndef MQTT_H +#define MQTT_H + +#include "MQTTClient.h" + +#endif diff --git a/libraries/arduino-mqtt-2.3.1/src/MQTTClient.h b/libraries/arduino-mqtt-2.3.1/src/MQTTClient.h new file mode 100644 index 00000000000..5552f09413e --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/MQTTClient.h @@ -0,0 +1,363 @@ +#ifndef MQTT_CLIENT_H +#define MQTT_CLIENT_H + +#include +#include +#include + +#include "system.h" + +class MQTTClient; + +typedef void (*MQTTClientCallbackSimple)(String &topic, String &payload); +typedef void (*MQTTClientCallbackAdvanced)(MQTTClient *client, char topic[], char bytes[], int length); + +typedef struct { + MQTTClient *client = nullptr; + MQTTClientCallbackSimple simple = nullptr; + MQTTClientCallbackAdvanced advanced = nullptr; +} MQTTClientCallback; + +static void MQTTClientHandler(lwmqtt_client_t * /*client*/, void *ref, lwmqtt_string_t topic, + lwmqtt_message_t message) { + // get callback + auto cb = (MQTTClientCallback *)ref; + + // 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'; + } + + // call the advanced callback and return if available + if (cb->advanced != nullptr) { + cb->advanced(cb->client, terminated_topic, (char *)message.payload, (int)message.payload_len); + return; + } + + // return if simple callback is not set + if (cb->simple == nullptr) { + return; + } + + // create topic string + String str_topic = String(terminated_topic); + + // create payload string + String str_payload; + if (message.payload != nullptr) { + str_payload = String((const char *)message.payload); + } + + // call simple callback + cb->simple(str_topic, str_payload); +} + +class MQTTClient { + private: + size_t bufSize = 0; + uint8_t *readBuf = nullptr; + uint8_t *writeBuf = nullptr; + + uint16_t keepAlive = 10; + bool cleanSession = true; + uint32_t timeout = 1000; + + Client *netClient = nullptr; + const char *hostname = nullptr; + int port = 0; + lwmqtt_will_t will = lwmqtt_default_will; + bool hasWill = false; + MQTTClientCallback callback; + + lwmqtt_arduino_network_t network = {nullptr}; + lwmqtt_arduino_timer_t timer1 = {0}; + lwmqtt_arduino_timer_t timer2 = {0}; + lwmqtt_client_t client; + + bool _connected = false; + lwmqtt_return_code_t _returnCode = (lwmqtt_return_code_t)0; + lwmqtt_err_t _lastError = (lwmqtt_err_t)0; + + public: + explicit MQTTClient(int bufSize = 128) { + memset(&client, 0, sizeof(client)); + this->bufSize = (size_t)bufSize; + this->readBuf = (uint8_t *)malloc((size_t)bufSize + 1); + this->writeBuf = (uint8_t *)malloc((size_t)bufSize); + } + + ~MQTTClient() { + free(this->readBuf); + free(this->writeBuf); + } + + void begin(const char hostname[], Client &client) { this->begin(hostname, 1883, client); } + + void begin(const char hostname[], int port, Client &client) { + // set config + this->hostname = hostname; + this->port = port; + this->netClient = &client; + + // initialize client + lwmqtt_init(&this->client, this->writeBuf, this->bufSize, this->readBuf, this->bufSize); + + // set timers + lwmqtt_set_timers(&this->client, &this->timer1, &this->timer2, lwmqtt_arduino_timer_set, lwmqtt_arduino_timer_get); + + // set network + lwmqtt_set_network(&this->client, &this->network, lwmqtt_arduino_network_read, lwmqtt_arduino_network_write); + + // set callback + lwmqtt_set_callback(&this->client, (void *)&this->callback, MQTTClientHandler); + } + + void onMessage(MQTTClientCallbackSimple cb) { + // set callback + this->callback.client = this; + this->callback.simple = cb; + this->callback.advanced = nullptr; + } + + void onMessageAdvanced(MQTTClientCallbackAdvanced cb) { + // set callback + this->callback.client = this; + this->callback.simple = nullptr; + this->callback.advanced = cb; + } + + void setHost(const char hostname[]) { this->setHost(hostname, 1883); } + + void setHost(const char hostname[], int port) { + this->hostname = hostname; + this->port = port; + } + + void setWill(const char topic[]) { this->setWill(topic, ""); } + + void setWill(const char topic[], const char payload[]) { this->setWill(topic, payload, false, 0); } + + void setWill(const char topic[], const char payload[], bool retained, int qos) { + this->hasWill = true; + this->will.topic = lwmqtt_string(topic); + this->will.payload = lwmqtt_string(payload); + this->will.retained = retained; + this->will.qos = (lwmqtt_qos_t)qos; + } + + void clearWill() { this->hasWill = false; } + + void setOptions(int keepAlive, bool cleanSession, int timeout) { + this->keepAlive = (uint16_t)keepAlive; + this->cleanSession = cleanSession; + this->timeout = (uint32_t)timeout; + } + + bool connect(const char clientId[]) { return this->connect(clientId, nullptr, nullptr); } + + bool connect(const char clientId[], const char username[]) { return this->connect(clientId, username, nullptr); } + + bool connect(const char clientId[], const char username[], const char password[]) { + // close left open connection if still connected + if (this->connected()) { + this->close(); + } + + // save client + this->network.client = this->netClient; + + // connect to host + if (this->netClient->connect(this->hostname, (uint16_t)this->port) < 0) { + return false; + } + + // prepare options + lwmqtt_options_t options = lwmqtt_default_options; + options.keep_alive = this->keepAlive; + options.clean_session = this->cleanSession; + options.client_id = lwmqtt_string(clientId); + + // set username and password if available + if (username != nullptr) { + options.username = lwmqtt_string(username); + + if (password != nullptr) { + options.password = lwmqtt_string(password); + } + } + + // prepare will reference + lwmqtt_will_t *will = nullptr; + if (this->hasWill) { + will = &this->will; + } + + // connect to broker + this->_lastError = lwmqtt_connect(&this->client, options, will, &this->_returnCode, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + return this->close(); + } + + // set flag + this->_connected = true; + + return true; + } + + bool publish(const String &topic) { return this->publish(topic.c_str(), ""); } + + bool publish(const char topic[]) { return this->publish(topic, ""); } + + bool publish(const String &topic, const String &payload) { return this->publish(topic.c_str(), payload.c_str()); } + + bool publish(const String &topic, const String &payload, bool retained, int qos) { + return this->publish(topic.c_str(), payload.c_str(), retained, qos); + } + + bool publish(const char topic[], const String &payload) { return this->publish(topic, payload.c_str()); } + + bool publish(const char topic[], const String &payload, bool retained, int qos) { + return this->publish(topic, payload.c_str(), retained, qos); + } + + bool publish(const char topic[], const char payload[]) { + return this->publish(topic, (char *)payload, (int)strlen(payload)); + } + + bool publish(const char topic[], const char payload[], bool retained, int qos) { + return this->publish(topic, (char *)payload, (int)strlen(payload), retained, qos); + } + + bool publish(const char topic[], const char payload[], int length) { + return this->publish(topic, payload, length, false, 0); + } + + bool publish(const char topic[], const char payload[], int length, bool retained, int qos) { + // return immediately if not connected + if (!this->connected()) { + return false; + } + + // prepare message + lwmqtt_message_t message = lwmqtt_default_message; + message.payload = (uint8_t *)payload; + message.payload_len = (size_t)length; + message.retained = retained; + message.qos = lwmqtt_qos_t(qos); + + // publish message + this->_lastError = lwmqtt_publish(&this->client, lwmqtt_string(topic), message, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + return this->close(); + } + + return true; + } + + bool subscribe(const String &topic) { return this->subscribe(topic.c_str()); } + + bool subscribe(const String &topic, int qos) { return this->subscribe(topic.c_str(), qos); } + + bool subscribe(const char topic[]) { return this->subscribe(topic, 0); } + + bool subscribe(const char topic[], int qos) { + // return immediately if not connected + if (!this->connected()) { + return false; + } + + // subscribe to topic + this->_lastError = lwmqtt_subscribe_one(&this->client, lwmqtt_string(topic), (lwmqtt_qos_t)qos, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + return this->close(); + } + + return true; + } + + bool unsubscribe(const String &topic) { return this->unsubscribe(topic.c_str()); } + + bool unsubscribe(const char topic[]) { + // return immediately if not connected + if (!this->connected()) { + return false; + } + + // unsubscribe from topic + this->_lastError = lwmqtt_unsubscribe_one(&this->client, lwmqtt_string(topic), this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + return this->close(); + } + + return true; + } + + bool loop() { + // return immediately if not connected + if (!this->connected()) { + return false; + } + + // get available bytes on the network + auto available = (size_t)this->netClient->available(); + + // yield if data is available + if (available > 0) { + this->_lastError = lwmqtt_yield(&this->client, available, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + return this->close(); + } + } + + // keep the connection alive + this->_lastError = lwmqtt_keep_alive(&this->client, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + return this->close(); + } + + return true; + } + + bool connected() { + // a client is connected if the network is connected, a client is available and + // the connection has been properly initiated + return this->netClient != nullptr && this->netClient->connected() == 1 && this->_connected; + } + + lwmqtt_err_t lastError() { return this->_lastError; } + + lwmqtt_return_code_t returnCode() { return this->_returnCode; } + + bool disconnect() { + // return immediately if not connected anymore + if (!this->connected()) { + return false; + } + + // cleanly disconnect + this->_lastError = lwmqtt_disconnect(&this->client, this->timeout); + + // close + this->close(); + + return this->_lastError == LWMQTT_SUCCESS; + } + + private: + bool close() { + // set flag + this->_connected = false; + + // close network + this->netClient->stop(); + + return false; + } +}; + +#endif diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/client.c b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/client.c new file mode 100644 index 00000000000..8775ab6e149 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/client.c @@ -0,0 +1,615 @@ +#include "packet.h" + +void lwmqtt_init(lwmqtt_client_t *client, uint8_t *write_buf, size_t write_buf_size, uint8_t *read_buf, + size_t read_buf_size) { + client->last_packet_id = 1; + client->keep_alive_interval = 0; + client->pong_pending = false; + + client->write_buf = write_buf; + client->write_buf_size = write_buf_size; + client->read_buf = read_buf; + client->read_buf_size = read_buf_size; + + client->callback = NULL; + client->callback_ref = NULL; + + client->network = NULL; + client->network_read = NULL; + client->network_write = NULL; + + client->keep_alive_timer = NULL; + client->command_timer = NULL; + client->timer_set = NULL; + client->timer_get = NULL; +} + +void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_t read, lwmqtt_network_write_t write) { + client->network = ref; + client->network_read = read; + client->network_write = write; +} + +void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *command_timer, lwmqtt_timer_set_t set, + lwmqtt_timer_get_t get) { + client->keep_alive_timer = keep_alive_timer; + client->command_timer = command_timer; + client->timer_set = set; + client->timer_get = get; + + client->timer_set(client->keep_alive_timer, 0); + client->timer_set(client->command_timer, 0); +} + +void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t cb) { + client->callback_ref = ref; + client->callback = cb; +} + +static uint16_t lwmqtt_get_next_packet_id(lwmqtt_client_t *client) { + // check overflow + if (client->last_packet_id == 65535) { + client->last_packet_id = 1; + return 1; + } + + // increment packet id + client->last_packet_id++; + + return client->last_packet_id; +} + +static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t offset, size_t len) { + // check read buffer capacity + if (client->read_buf_size < offset + len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // prepare counter + size_t read = 0; + + // read while data is missing + while (read < len) { + // check remaining time + int32_t remaining_time = client->timer_get(client->command_timer); + if (remaining_time <= 0) { + return LWMQTT_NETWORK_TIMEOUT; + } + + // read + 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; + } + + // increment counter + read += partial_read; + } + + return LWMQTT_SUCCESS; +} + +static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offset, size_t len) { + // prepare counter + size_t written = 0; + + // write while data is left + while (written < len) { + // check remaining time + int32_t remaining_time = client->timer_get(client->command_timer); + if (remaining_time <= 0) { + return LWMQTT_NETWORK_TIMEOUT; + } + + // write + 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); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // increment counter + written += partial_write; + } + + 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) { + // preset packet type + *packet_type = LWMQTT_NO_PACKET; + + // read or wait for header byte + lwmqtt_err_t err = lwmqtt_read_from_network(client, 0, 1); + if (err == LWMQTT_NETWORK_TIMEOUT) { + // this is ok as no data has been read at all + return LWMQTT_SUCCESS; + } else if (err != LWMQTT_SUCCESS) { + return err; + } + + // detect packet type + err = lwmqtt_detect_packet_type(client->read_buf, 1, packet_type); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // prepare variables + size_t len = 0; + uint32_t rem_len = 0; + + do { + // adjust len + len++; + + // read next byte + err = lwmqtt_read_from_network(client, len, 1); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // attempt to detect remaining length + err = lwmqtt_detect_remaining_length(client->read_buf + 1, len, &rem_len); + } while (err == LWMQTT_BUFFER_TOO_SHORT); + + // check final error + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read the rest of the buffer if needed + if (rem_len > 0) { + err = lwmqtt_read_from_network(client, 1 + len, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // adjust counter + *read += 1 + len + rem_len; + + return LWMQTT_SUCCESS; +} + +static lwmqtt_err_t lwmqtt_send_packet_in_buffer(lwmqtt_client_t *client, size_t length) { + // write to network + lwmqtt_err_t err = lwmqtt_write_to_network(client, 0, length); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // reset keep alive timer + client->timer_set(client->keep_alive_timer, client->keep_alive_interval); + + return LWMQTT_SUCCESS; +} + +static lwmqtt_err_t lwmqtt_cycle(lwmqtt_client_t *client, size_t *read, lwmqtt_packet_type_t *packet_type) { + // read next packet from the network + lwmqtt_err_t err = lwmqtt_read_packet_in_buffer(client, read, packet_type); + if (err != LWMQTT_SUCCESS) { + return err; + } else if (*packet_type == LWMQTT_NO_PACKET) { + return LWMQTT_SUCCESS; + } + + switch (*packet_type) { + // 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) { + 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; + } + + 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; + } + + // 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; + } + + // send pubrel packet + err = lwmqtt_send_packet_in_buffer(client, len); + 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); + 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); + 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; + + break; + } + + // handle all other packets + default: { 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) { + // prepare counter + size_t read = 0; + + // loop until timeout has been reached + do { + // do one cycle + lwmqtt_err_t err = lwmqtt_cycle(client, &read, packet_type); + 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) { + return LWMQTT_SUCCESS; + } + + // otherwise check if needle has been found + if (*packet_type == needle) { + return LWMQTT_SUCCESS; + } + } while (client->timer_get(client->command_timer) > 0 && (available == 0 || read < available)); + + return LWMQTT_SUCCESS; +} + +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); + + // 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // set command timer + client->timer_set(client->command_timer, timeout); + + // save keep alive interval (take 75% to be a little earlier than actually needed) + client->keep_alive_interval = (uint32_t)(options.keep_alive * 750); + + // set keep alive timer + client->timer_set(client->keep_alive_timer, client->keep_alive_interval); + + // reset pong pending flag + client->pong_pending = false; + + // 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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // 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) { + return err; + } else if (packet_type != LWMQTT_CONNACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // 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) { + return err; + } + + // return error if connection was not accepted + if (*return_code != LWMQTT_CONNECTION_ACCEPTED) { + return LWMQTT_CONNECTION_DENIED; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, lwmqtt_qos_t *qos, + uint32_t timeout) { + // set command timer + client->timer_set(client->command_timer, timeout); + + // 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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // 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) { + return err; + } else if (packet_type != LWMQTT_SUBACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // decode packet + int suback_count = 0; + 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) { + return err; + } + + // check suback codes + for (int i = 0; i < suback_count; i++) { + if (granted_qos[i] == LWMQTT_QOS_FAILURE) { + return LWMQTT_FAILED_SUBSCRIPTION; + } + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_subscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, lwmqtt_qos_t qos, + 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) { + // set command timer + client->timer_set(client->command_timer, timeout); + + // 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) { + return err; + } + + // send unsubscribe packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // 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) { + return err; + } else if (packet_type != LWMQTT_UNSUBACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // decode unsuback packet + 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // 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) { + packet_id = lwmqtt_get_next_packet_id(client); + } + + // 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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // immediately return on qos zero + 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) { + ack_type = LWMQTT_PUBACK_PACKET; + } else if (message.qos == LWMQTT_QOS2) { + ack_type = LWMQTT_PUBCOMP_PACKET; + } + + // 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) { + return err; + } else if (packet_type != ack_type) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) { + // set command timer + client->timer_set(client->command_timer, 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) { + return err; + } + + // send disconnected packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + return LWMQTT_SUCCESS; + } + + // return immediately if no ping is due + if (client->timer_get(client->keep_alive_timer) > 0) { + return LWMQTT_SUCCESS; + } + + // a ping is due + + // fail immediately if a pong is already pending + if (client->pong_pending) { + return LWMQTT_PONG_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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set flag + client->pong_pending = true; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.c b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.c new file mode 100644 index 00000000000..8b9d37e5b4f --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.c @@ -0,0 +1,251 @@ +#include + +#include "helpers.h" + +uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num) { + return (byte & (uint8_t)((~(0xFF << num)) << pos)) >> pos; +} + +void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num) { + *byte = (*byte & ~(uint8_t)((~(0xFF << num)) << pos)) | (value << pos); +} + +lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len) { + // check zero length + if (len == 0) { + *data = NULL; + return LWMQTT_SUCCESS; + } + + // check buffer size + if ((size_t)(buf_end - (*buf)) < len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // read data + *data = *buf; + + // advance pointer + *buf += len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_data(uint8_t **buf, const uint8_t *buf_end, uint8_t *data, size_t len) { + // check zero length + if (len == 0) { + return LWMQTT_SUCCESS; + } + + // check buffer size + if ((size_t)(buf_end - (*buf)) < len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // write data + memcpy(*buf, data, len); + + // advance pointer + *buf += len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_read_num(uint8_t **buf, const uint8_t *buf_end, uint16_t *num) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 2) { + *num = 0; + return LWMQTT_BUFFER_TOO_SHORT; + } + + // read two byte integer + *num = (uint16_t)256 * (*buf)[0] + (*buf)[1]; + + // adjust pointer + *buf += 2; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_num(uint8_t **buf, const uint8_t *buf_end, uint16_t num) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 2) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // write bytes + (*buf)[0] = (uint8_t)(num / 256); + (*buf)[1] = (uint8_t)(num % 256); + + // adjust pointer + *buf += 2; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_read_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t *str) { + // read length + uint16_t len; + lwmqtt_err_t err = lwmqtt_read_num(buf, buf_end, &len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read data + err = lwmqtt_read_data(buf, buf_end, (uint8_t **)&str->data, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set length + str->len = len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t str) { + // write string length + lwmqtt_err_t err = lwmqtt_write_num(buf, buf_end, str.len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write data + err = lwmqtt_write_data(buf, buf_end, (uint8_t *)str.data, str.len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_read_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t *byte) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 1) { + *byte = 0; + return LWMQTT_BUFFER_TOO_SHORT; + } + + // read byte + *byte = (*buf)[0]; + + // adjust pointer + *buf += 1; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t byte) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 1) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // write byte + (*buf)[0] = byte; + + // adjust pointer + *buf += 1; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_varnum_length(uint32_t varnum, int *len) { + if (varnum < 128) { + *len = 1; + return LWMQTT_SUCCESS; + } else if (varnum < 16384) { + *len = 2; + return LWMQTT_SUCCESS; + } else if (varnum < 2097151) { + *len = 3; + return LWMQTT_SUCCESS; + } else if (varnum < 268435455) { + *len = 4; + return LWMQTT_SUCCESS; + } else { + *len = 0; + return LWMQTT_VARNUM_OVERFLOW; + } +} + +lwmqtt_err_t lwmqtt_read_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t *varnum) { + // prepare last byte + uint8_t byte; + + // prepare multiplier + uint32_t multiplier = 1; + + // prepare length + size_t len = 0; + + // initialize number + *varnum = 0; + + // decode variadic number + do { + // increment length + len++; + + // return error if buffer is to small + if ((size_t)(buf_end - (*buf)) < len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // return error if the length has overflowed + if (len > 4) { + return LWMQTT_VARNUM_OVERFLOW; + } + + // read byte + byte = (*buf)[len - 1]; + + // add byte to number + *varnum += (byte & 127) * multiplier; + + // increase multiplier + multiplier *= 128; + } while ((byte & 128) != 0); + + // adjust pointer + *buf += len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t varnum) { + // init len counter + size_t len = 0; + + // encode variadic number + do { + // check overflow + if (len == 4) { + return LWMQTT_VARNUM_OVERFLOW; + } + + // return error if buffer is to small + if ((size_t)(buf_end - (*buf)) < len + 1) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // calculate current byte + uint8_t byte = (uint8_t)(varnum % 128); + + // change remaining length + varnum /= 128; + + // set the top bit of this byte if there are more to encode + if (varnum > 0) { + byte |= 0x80; + } + + // write byte + (*buf)[len++] = byte; + } while (varnum > 0); + + // adjust pointer + *buf += len; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.h b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.h new file mode 100644 index 00000000000..978eaf4a5ad --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/helpers.h @@ -0,0 +1,137 @@ +#ifndef LWMQTT_HELPERS_H +#define LWMQTT_HELPERS_H + +#include "lwmqtt.h" + +/** + * Reads bits from a byte. + * + * @param byte - The byte to read from. + * @param pos - The position of the first bit. + * @param num - The number of bits to read. + * @return The read bits as a byte. + */ +uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num); + +/** + * Write bits to a byte. + * + * @param byte - The byte to write bits to. + * @param value - The bits to write as a byte. + * @param pos - The position of the first bit. + * @param num - The number of bits to write. + */ +void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num); + +/** + * Reads arbitrary data from the specified buffer. The pointer is incremented by bytes read. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param data - Pointer to beginning of data. + * @param len - The amount of data to read. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len); + +/** + * Writes arbitrary data to the specified buffer. The pointer is incremented by the bytes written. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param data - Pointer to the to be written data. + * @param len - The amount of data to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_data(uint8_t **buf, const uint8_t *buf_end, uint8_t *data, size_t len); + +/** + * Reads two byte number from the specified buffer. The pointer is incremented by two. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param num - The read number. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_num(uint8_t **buf, const uint8_t *buf_end, uint16_t *num); + +/** + * Writes a two byte number to the specified buffer. The pointer is incremented by two. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param num - The number to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_num(uint8_t **buf, const uint8_t *buf_end, uint16_t num); + +/** + * Reads a string from the specified buffer into the passed object. The pointer is incremented by the bytes read. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param str - The object into which the data is to be read. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t *str); + +/** + * Writes a string to the specified buffer. The pointer is incremented by the bytes written. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param str - The string to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t str); + +/** + * Reads one byte from the buffer. The pointer is incremented by one. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param byte - The read byte. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t *byte); + +/** + * Writes one byte to the specified buffer. The pointer is incremented by one. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param byte - The byte to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t byte); + +/** + * Returns the amount of bytes required by the variable number. + * + * @param varnum - The number to check. + * @param len - The required length; + * @return LWMQTT_SUCCESS or LWMQTT_VARNUM_OVERFLOW. + */ +lwmqtt_err_t lwmqtt_varnum_length(uint32_t varnum, int *len); + +/** + * Reads a variable number from the specified buffer. The pointer is incremented by the bytes read. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param varnum - The read varnum. + * @return LWMQTT_SUCCESS, LWMQTT_BUFFER_TOO_SHORT or LWMQTT_VARNUM_OVERFLOW. + */ +lwmqtt_err_t lwmqtt_read_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t *varnum); + +/** + * Writes a variable number to the specified buffer. The pointer is incremented by the bytes written. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param varnum - The number to write. + * @return LWMQTT_SUCCESS, LWMQTT_BUFFER_TOO_SHORT or LWMQTT_VARNUM_OVERFLOW. + */ +lwmqtt_err_t lwmqtt_write_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t varnum); + +#endif diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/lwmqtt.h b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/lwmqtt.h new file mode 100644 index 00000000000..e63ad100473 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/lwmqtt.h @@ -0,0 +1,373 @@ +#ifndef LWMQTT_H +#define LWMQTT_H + +#include +#include +#include + +/** + * The error type used by all exposed APIs. + */ +typedef enum { + LWMQTT_SUCCESS = 0, + LWMQTT_BUFFER_TOO_SHORT = -1, + LWMQTT_VARNUM_OVERFLOW = -2, + LWMQTT_NETWORK_FAILED_CONNECT = -3, + LWMQTT_NETWORK_TIMEOUT = -4, + LWMQTT_NETWORK_FAILED_READ = -5, + LWMQTT_NETWORK_FAILED_WRITE = -6, + LWMQTT_REMAINING_LENGTH_OVERFLOW = -7, + LWMQTT_REMAINING_LENGTH_MISMATCH = -8, + LWMQTT_MISSING_OR_WRONG_PACKET = -9, + LWMQTT_CONNECTION_DENIED = -10, + LWMQTT_FAILED_SUBSCRIPTION = -11, + LWMQTT_SUBACK_ARRAY_OVERFLOW = -12, + LWMQTT_PONG_TIMEOUT = -13, +} lwmqtt_err_t; + +/** + * A common string object. + */ +typedef struct { + uint16_t len; + char *data; +} lwmqtt_string_t; + +/** + * The initializer for string objects. + */ +#define lwmqtt_default_string \ + { 0, NULL } + +/** + * Returns a string object for the passed C string. + * + * @param str - The C string. + * @return A string object. + */ +lwmqtt_string_t lwmqtt_string(const char *str); + +/** + * Compares a string object to a C string. + * + * @param a - The string object to compare. + * @param b - The C string to compare. + * @return Similarity e.g. strcmp(). + */ +int lwmqtt_strcmp(lwmqtt_string_t a, const char *b); + +/** + * The available QOS levels. + */ +typedef enum { LWMQTT_QOS0 = 0, LWMQTT_QOS1 = 1, LWMQTT_QOS2 = 2, LWMQTT_QOS_FAILURE = 128 } lwmqtt_qos_t; + +/** + * The message object used to publish and receive messages. + */ +typedef struct { + lwmqtt_qos_t qos; + bool retained; + uint8_t *payload; + size_t payload_len; +} lwmqtt_message_t; + +/** + * The initializer for message objects. + */ +#define lwmqtt_default_message \ + { LWMQTT_QOS0, false, NULL, 0 } + +/** + * Forward declaration of the client object. + */ +typedef struct lwmqtt_client_t lwmqtt_client_t; + +/** + * The callback used to read from a network object. + * + * The callbacks is expected to read up to the amount of bytes in to the passed buffer. It should block the specified + * timeout and wait for more incoming data. + * + * @param ref - A custom reference. + * @param buf - The buffer. + * @param len - The length of the buffer. + * @param read - Variable that must be set with the amount of read bytes. + * @param timeout - The timeout in milliseconds for the operation. + */ +typedef lwmqtt_err_t (*lwmqtt_network_read_t)(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout); + +/** + * The callback used to write to a network object. + * + * The callback is expected to write up to the amount of bytes from the passed buffer. It should wait up to the + * specified timeout to write the specified data to the network. + * + * @param ref - A custom reference. + * @param buf - The buffer. + * @param len - The length of the buffer. + * @param sent - Variable that must be set with the amount of written bytes. + * @param timeout - The timeout in milliseconds for the operation. + */ +typedef lwmqtt_err_t (*lwmqtt_network_write_t)(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout); + +/** + * The callback used to set a timer. + * + * @param ref - A custom reference. + * @param timeout - The amount of milliseconds until the deadline. + */ +typedef void (*lwmqtt_timer_set_t)(void *ref, uint32_t timeout); + +/** + * The callback used to get a timers value. + * + * @param - A custom reference. + * @return The amount of milliseconds until the deadline. May return negative numbers if the deadline has been reached. + */ +typedef int32_t (*lwmqtt_timer_get_t)(void *ref); + +/** + * The callback used to forward incoming messages. + * + * Note: The callback is mostly executed because of a call to lwmqtt_yield() that processes incoming messages. However, + * it is possible that the callback is also executed during a call to lwmqtt_subscribe(), lwmqtt_publish() or + * lwmqtt_unsubscribe() if incoming messages are received between the required acknowledgements. It is therefore not + * recommended to call any further lwmqtt methods in the callback as this might result in weird call stacks. The + * callback should place the received messages in a queue and dispatch them after the caller has returned. + */ +typedef void (*lwmqtt_callback_t)(lwmqtt_client_t *client, void *ref, lwmqtt_string_t str, lwmqtt_message_t msg); + +/** + * The client object. + */ +struct lwmqtt_client_t { + uint16_t last_packet_id; + uint32_t keep_alive_interval; + bool pong_pending; + + size_t write_buf_size, read_buf_size; + uint8_t *write_buf, *read_buf; + + lwmqtt_callback_t callback; + void *callback_ref; + + void *network; + lwmqtt_network_read_t network_read; + lwmqtt_network_write_t network_write; + + void *keep_alive_timer; + void *command_timer; + lwmqtt_timer_set_t timer_set; + lwmqtt_timer_get_t timer_get; +}; + +/** + * Will initialize the specified client object. + * + * @param client - The client object. + * @param write_buf - The write buffer. + * @param write_buf_size - The write buffer size. + * @param read_buf - The read buffer. + * @param read_buf_size - The read buffer size. + */ +void lwmqtt_init(lwmqtt_client_t *client, uint8_t *write_buf, size_t write_buf_size, uint8_t *read_buf, + size_t read_buf_size); + +/** + * Will set the network reference and callbacks for this client object. + * + * @param client - The client object. + * @param ref - The reference to the network object. + * @param read - The read callback. + * @param write - The write callback. + */ +void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_t read, lwmqtt_network_write_t write); + +/** + * Will set the timer references and callbacks for this client object. + * + * @param client - The client object. + * @param keep_alive_timer - The reference to the keep alive timer. + * @param command_timer - The reference to the command timer. + * @param set - The set callback. + * @param get - The get callback. + */ +void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *command_timer, lwmqtt_timer_set_t set, + lwmqtt_timer_get_t get); + +/** + * Will set the callback used to receive incoming messages. + * + * @param client - The client object. + * @param ref - A custom reference that will passed to the callback. + * @param cb - The callback to be called. + */ +void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t cb); + +/** + * The object defining the last will of a client. + */ +typedef struct { + lwmqtt_string_t topic; + lwmqtt_qos_t qos; + bool retained; + lwmqtt_string_t payload; +} lwmqtt_will_t; + +/** + * The default initializer for the will object. + */ +#define lwmqtt_default_will \ + { lwmqtt_default_string, LWMQTT_QOS0, false, lwmqtt_default_string } + +/** + * The object containing the connection options for a client. + */ +typedef struct { + lwmqtt_string_t client_id; + uint16_t keep_alive; + bool clean_session; + lwmqtt_string_t username; + lwmqtt_string_t password; +} lwmqtt_options_t; + +/** + * The default initializer for the options object. + */ +#define lwmqtt_default_options \ + { lwmqtt_default_string, 60, true, lwmqtt_default_string, lwmqtt_default_string } + +/** + * The available return codes transported by the connack packet. + */ +typedef enum { + LWMQTT_CONNECTION_ACCEPTED = 0, + LWMQTT_UNACCEPTABLE_PROTOCOL = 1, + LWMQTT_IDENTIFIER_REJECTED = 2, + LWMQTT_SERVER_UNAVAILABLE = 3, + LWMQTT_BAD_USERNAME_OR_PASSWORD = 4, + LWMQTT_NOT_AUTHORIZED = 5, + LWMQTT_UNKNOWN_RETURN_CODE = 6 +} lwmqtt_return_code_t; + +/** + * Will send a connect packet and wait for a connack response and set the return code. + * + * The network object must already be connected to the server. An error is returned if the broker rejects the + * connection. + * + * @param client - The client object. + * @param options - The options object. + * @param will - The will object. + * @param return_code - The variable that will receive the return code. + * @param timeout - The command timeout. + * @return An error value. + */ +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); + +/** + * Will send a publish packet and wait for all acks to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param topic - The topic. + * @param message - The message. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmqtt_message_t msg, uint32_t timeout); + +/** + * Will send a subscribe packet with multiple topic filters plus QOS levels and wait for the suback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param count - The number of topic filters and QOS levels. + * @param topic_filter - The list of topic filters. + * @param qos - The list of QOS levels. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, lwmqtt_qos_t *qos, + uint32_t timeout); + +/** + * Will send a subscribe packet with a single topic filter plus QOS level and wait for the suback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param topic_filter - The topic filter. + * @param qos - The QOS level. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_subscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, lwmqtt_qos_t qos, + uint32_t timeout); + +/** + * Will send an unsubscribe packet with multiple topic filters and wait for the unsuback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param count - The number of topic filters. + * @param topic_filter - The topic filter. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, uint32_t timeout); + +/** + * Will send an unsubscribe packet with a single topic filter and wait for the unsuback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param topic_filter - The topic filter. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_unsubscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, uint32_t timeout); + +/** + * Will send a disconnect packet and finish the client. + * + * @param client - The client object. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout); + +/** + * Will yield control to the client and receive incoming packets from the network. + * + * Applications may peek on the network if there is data available to read before calling yield and potentially block + * until the timeout is reached. Furthermore, applications may specify the amount of bytes available to read in order + * to constrain the yield to only receive packets that are already inflight. + * + * If no availability data is given the yield will return after one packet has been successfully read or the deadline + * has been reached but no single bytes has been received. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param available - The available bytes to read. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t timeout); + +/** + * Will yield control to the client to keep the connection alive. + * + * @param client - The client object. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout); + +#endif // LWMQTT_H diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.c b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.c new file mode 100644 index 00000000000..5ed579fd2ef --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.c @@ -0,0 +1,742 @@ +#include "packet.h" + +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; + + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // prepare header + uint8_t header; + + // read header + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // get packet type + *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) { + case LWMQTT_CONNACK_PACKET: + case LWMQTT_PUBLISH_PACKET: + case LWMQTT_PUBACK_PACKET: + case LWMQTT_PUBREC_PACKET: + case LWMQTT_PUBREL_PACKET: + case LWMQTT_PUBCOMP_PACKET: + case LWMQTT_SUBACK_PACKET: + 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) { + // 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) { + *rem_len = 0; + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } else if (err != LWMQTT_SUCCESS) { + *rem_len = 0; + return err; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_options_t options, + lwmqtt_will_t *will) { + // prepare pointers + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // fixed header is 10 + uint32_t rem_len = 10; + + // add client id to remaining length + rem_len += options.client_id.len + 2; + + // add will if present to remaining length + 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) { + rem_len += options.username.len + 2; + + // add password if present to remaining length + if (options.password.len > 0) { + rem_len += options.password.len + 2; + } + } + + // 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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + lwmqtt_write_bits(&header, LWMQTT_CONNECT_PACKET, 4, 4); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write version string + err = lwmqtt_write_string(&buf_ptr, buf_end, lwmqtt_string("MQTT")); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write version number + err = lwmqtt_write_byte(&buf_ptr, buf_end, 4); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // prepare flags + uint8_t flags = 0; + + // set clean session + lwmqtt_write_bits(&flags, (uint8_t)(options.clean_session), 1, 1); + + // set will flags if present + if (will != NULL) { + lwmqtt_write_bits(&flags, 1, 2, 1); + lwmqtt_write_bits(&flags, will->qos, 3, 2); + lwmqtt_write_bits(&flags, (uint8_t)(will->retained), 5, 1); + } + + // set username flag if present + if (options.username.len > 0) { + lwmqtt_write_bits(&flags, 1, 6, 1); + + // set password flag if present + if (options.password.len > 0) { + lwmqtt_write_bits(&flags, 1, 7, 1); + } + } + + // write flags + err = lwmqtt_write_byte(&buf_ptr, buf_end, flags); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write keep alive + err = lwmqtt_write_num(&buf_ptr, buf_end, options.keep_alive); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write client id + err = lwmqtt_write_string(&buf_ptr, buf_end, options.client_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write will if present + if (will != NULL) { + // write topic + err = lwmqtt_write_string(&buf_ptr, buf_end, will->topic); + 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) { + return err; + } + + // write payload + err = lwmqtt_write_data(&buf_ptr, buf_end, (uint8_t *)will->payload.data, will->payload.len); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // write username if present + if (options.username.len > 0) { + err = lwmqtt_write_string(&buf_ptr, buf_end, options.username); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // write password if present + if (options.username.len > 0 && options.password.len > 0) { + err = lwmqtt_write_string(&buf_ptr, buf_end, options.password); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // set written length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_present, + lwmqtt_return_code_t *return_code) { + // prepare pointers + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_CONNACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length + if (rem_len != 2) { + return LWMQTT_REMAINING_LENGTH_MISMATCH; + } + + // read flags + uint8_t flags; + err = lwmqtt_read_byte(&buf_ptr, buf_end, &flags); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read return code + uint8_t raw_return_code; + err = lwmqtt_read_byte(&buf_ptr, buf_end, &raw_return_code); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // get session present + *session_present = lwmqtt_read_bits(flags, 7, 1) == 1; + + // get 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; + } + + 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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // write header + 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) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, 0); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header = 0; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != packet_type) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // get dup + *dup = lwmqtt_read_bits(header, 3, 1) == 1; + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf + buf_len, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length + 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, packet_type, 4, 4); + + // set dup + lwmqtt_write_bits(&header, (uint8_t)(dup), 3, 1); + + // set qos + lwmqtt_write_bits(&header, (uint8_t)(packet_type == LWMQTT_PUBREL_PACKET ? LWMQTT_QOS1 : LWMQTT_QOS0), 1, 2); + + // write header + lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, 2); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set written length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_PUBLISH_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // get dup + *dup = lwmqtt_read_bits(header, 3, 1) == 1; + + // get retained + msg->retained = lwmqtt_read_bits(header, 0, 1) == 1; + + // get qos + 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; + } + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length (topic length) + if (rem_len < 2) { + return LWMQTT_REMAINING_LENGTH_MISMATCH; + } + + // check buffer capacity + if ((uint32_t)(buf_end - buf_ptr) < rem_len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // reset buf end + buf_end = buf_ptr + rem_len; + + // read topic + err = lwmqtt_read_string(&buf_ptr, buf_end, topic); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read packet id if qos is at least 1 + if (msg->qos > 0) { + err = lwmqtt_read_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + } else { + *packet_id = 0; + } + + // set payload length + msg->payload_len = buf_end - buf_ptr; + + // read payload + err = lwmqtt_read_data(&buf_ptr, buf_end, &msg->payload, buf_end - buf_ptr); + if (err != LWMQTT_SUCCESS) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // calculate remaining length + uint32_t rem_len = 2 + topic.len + (uint32_t)msg.payload_len; + if (msg.qos > 0) { + rem_len += 2; + } + + // 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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, LWMQTT_PUBLISH_PACKET, 4, 4); + + // set dup + lwmqtt_write_bits(&header, (uint8_t)(dup), 3, 1); + + // set qos + lwmqtt_write_bits(&header, msg.qos, 1, 2); + + // set retained + lwmqtt_write_bits(&header, (uint8_t)(msg.retained), 0, 1); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write topic + err = lwmqtt_write_string(&buf_ptr, buf_end, topic); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id if qos is at least 1 + if (msg.qos > 0) { + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // write payload + err = lwmqtt_write_data(&buf_ptr, buf_end, msg.payload, msg.payload_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // calculate remaining length + uint32_t rem_len = 2; + for (int i = 0; i < count; i++) { + rem_len += 2 + topic_filters[i].len + 1; + } + + // 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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, LWMQTT_SUBSCRIBE_PACKET, 4, 4); + + // set qos + lwmqtt_write_bits(&header, LWMQTT_QOS1, 1, 2); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write all subscriptions + for (int i = 0; i < count; i++) { + // write topic + err = lwmqtt_write_string(&buf_ptr, buf_end, topic_filters[i]); + 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) { + return err; + } + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_SUBACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length (packet id + min. one suback code) + 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) { + return err; + } + + // read all suback codes + for (*count = 0; *count < (int)rem_len - 2; (*count)++) { + // check max count + if (*count > max_count) { + return LWMQTT_SUBACK_ARRAY_OVERFLOW; + } + + // read qos level + uint8_t raw_qos_level; + err = lwmqtt_read_byte(&buf_ptr, buf_end, &raw_qos_level); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set 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; + } + } + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // calculate remaining length + uint32_t rem_len = 2; + for (int i = 0; i < count; i++) { + rem_len += 2 + topic_filters[i].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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, LWMQTT_UNSUBSCRIBE_PACKET, 4, 4); + + // set qos + lwmqtt_write_bits(&header, LWMQTT_QOS1, 1, 2); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write topics + for (int i = 0; i < count; i++) { + err = lwmqtt_write_string(&buf_ptr, buf_end, topic_filters[i]); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.h b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.h new file mode 100644 index 00000000000..387a18f8b83 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/packet.h @@ -0,0 +1,185 @@ +#ifndef LWMQTT_PACKET_H +#define LWMQTT_PACKET_H + +#include "helpers.h" + +/** + * The available packet types. + */ +typedef enum { + LWMQTT_NO_PACKET = 0, + LWMQTT_CONNECT_PACKET = 1, + LWMQTT_CONNACK_PACKET, + LWMQTT_PUBLISH_PACKET, + LWMQTT_PUBACK_PACKET, + LWMQTT_PUBREC_PACKET, + LWMQTT_PUBREL_PACKET, + LWMQTT_PUBCOMP_PACKET, + LWMQTT_SUBSCRIBE_PACKET, + LWMQTT_SUBACK_PACKET, + LWMQTT_UNSUBSCRIBE_PACKET, + LWMQTT_UNSUBACK_PACKET, + LWMQTT_PINGREQ_PACKET, + LWMQTT_PINGRESP_PACKET, + LWMQTT_DISCONNECT_PACKET +} lwmqtt_packet_type_t; + +/** + * Will detect the packet type from the at least one byte long buffer. + * + * @param buf - The buffer from which the packet type will be detected. + * @param buf_len - The length of the specified buffer. + * @param packet_type - The packet type. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_t *packet_type); + +/** + * Will detect the remaining length form the at least on byte long buffer. + * + * It will return LWMQTT_BUFFER_TOO_SHORT if the buffer is to short and an additional byte should be read from the + * network. In case the remaining length is overflowed it will return LWMQTT_REMAINING_LENGTH_OVERFLOW. + * + * @param buf - The buffer from which the remaining length will be detected. + * @param buf_len - The length of the specified buffer. + * @param rem_len - The detected remaining length. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_detect_remaining_length(uint8_t *buf, size_t buf_len, uint32_t *rem_len); + +/** + * Encodes a connect packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param options - The options to be used to build the connect packet. + * @param will - The last will and testament. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_options_t options, + lwmqtt_will_t *will); + +/** + * Decodes a connack packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param session_present - The session present flag. + * @param return_code - The return code. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_present, + lwmqtt_return_code_t *return_code); + +/** + * Encodes a zero (disconnect, pingreq) packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_type - The packets type. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_encode_zero(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_packet_type_t packet_type); + +/** + * Decodes an ack (puback, pubrec, pubrel, pubcomp, unsuback) packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param packet_type - The packet type. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @return An error value. + */ +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); + +/** + * Encodes an ack (puback, pubrec, pubrel, pubcomp) packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_type - The packets type. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @return An error value. + */ +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); + +/** + * Decodes a publish packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @param topic - The topic. + * @parma msg - The message. + * @return An error value. + */ +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); + +/** + * Encodes a publish packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @param topic - The topic. + * @param msg - The message. + * @return An error value. + */ +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); + +/** + * Encodes a subscribe packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_id - The packet id. + * @param count - The number of members in the topic_filters and qos_levels array. + * @param topic_filters - The array of topic filter. + * @param qos_levels - The array of requested QoS levels. + * @return An error value. + */ +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); + +/** + * Decodes a suback packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param packet_id - The packet id. + * @param max_count - The maximum number of members allowed in the granted_qos_levels array. + * @param count - The number of members in the granted_qos_levels array. + * @param granted_qos_levels - The granted QoS levels. + * @return An error value. + */ +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); + +/** + * Encodes the supplied unsubscribe data into the supplied buffer, ready for sending + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_id - The packet id. + * @param count - The number of members in the topic_filters array. + * @param topic_filters - The array of topic filters. + * @return An error value. + */ +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); + +#endif // LWMQTT_PACKET_H diff --git a/libraries/arduino-mqtt-2.3.1/src/lwmqtt/string.c b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/string.c new file mode 100644 index 00000000000..c27dc94e3d1 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/lwmqtt/string.c @@ -0,0 +1,38 @@ +#include + +#include "lwmqtt.h" + +lwmqtt_string_t lwmqtt_string(const char *str) { + // check for null + if (str == NULL) { + return (lwmqtt_string_t){0, NULL}; + } + + // get length + uint16_t len = (uint16_t)strlen(str); + + // check zero length + if (len == 0) { + return (lwmqtt_string_t){0, NULL}; + } + + return (lwmqtt_string_t){len, (char *)str}; +} + +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) { + return 0; + } + + // return if lengths are different + if (a.len != b_str.len) { + return -1; + } + + // compare memory of same length + return strncmp(a.data, b_str.data, a.len); +} diff --git a/libraries/arduino-mqtt-2.3.1/src/system.cpp b/libraries/arduino-mqtt-2.3.1/src/system.cpp new file mode 100644 index 00000000000..cfd8c47407d --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/system.cpp @@ -0,0 +1,48 @@ +#include + +#include "system.h" + +void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) { + // cast timer reference + auto t = (lwmqtt_arduino_timer_t *)ref; + + // set future end time + t->end = (uint32_t)(millis() + timeout); +} + +int32_t lwmqtt_arduino_timer_get(void *ref) { + // cast timer reference + auto t = (lwmqtt_arduino_timer_t *)ref; + + // get difference to end time + return (int32_t)t->end - (int32_t)millis(); +} + +lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) { + // cast network reference + auto n = (lwmqtt_arduino_network_t *)ref; + + // set timeout + n->client->setTimeout(timeout); + + // read bytes + *read = n->client->readBytes(buffer, len); + if (*read <= 0) { + return LWMQTT_NETWORK_FAILED_READ; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t /*timeout*/) { + // cast network reference + auto n = (lwmqtt_arduino_network_t *)ref; + + // write bytes + *sent = n->client->write(buffer, len); + if (*sent <= 0) { + return LWMQTT_NETWORK_FAILED_WRITE; + }; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/arduino-mqtt-2.3.1/src/system.h b/libraries/arduino-mqtt-2.3.1/src/system.h new file mode 100644 index 00000000000..e51739fb894 --- /dev/null +++ b/libraries/arduino-mqtt-2.3.1/src/system.h @@ -0,0 +1,26 @@ +#ifndef LWMQTT_ARDUINO_H +#define LWMQTT_ARDUINO_H + +#include +#include + +extern "C" { +#include "lwmqtt/lwmqtt.h" +}; + +typedef struct { + uint32_t end; +} lwmqtt_arduino_timer_t; + +void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout); + +int32_t lwmqtt_arduino_timer_get(void *ref); + +typedef struct { + Client *client; +} lwmqtt_arduino_network_t; + +lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout); +lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout); + +#endif // LWMQTT_ARDUINO_H diff --git a/libraries/lwmqtt/client.c b/libraries/lwmqtt/client.c new file mode 100644 index 00000000000..8775ab6e149 --- /dev/null +++ b/libraries/lwmqtt/client.c @@ -0,0 +1,615 @@ +#include "packet.h" + +void lwmqtt_init(lwmqtt_client_t *client, uint8_t *write_buf, size_t write_buf_size, uint8_t *read_buf, + size_t read_buf_size) { + client->last_packet_id = 1; + client->keep_alive_interval = 0; + client->pong_pending = false; + + client->write_buf = write_buf; + client->write_buf_size = write_buf_size; + client->read_buf = read_buf; + client->read_buf_size = read_buf_size; + + client->callback = NULL; + client->callback_ref = NULL; + + client->network = NULL; + client->network_read = NULL; + client->network_write = NULL; + + client->keep_alive_timer = NULL; + client->command_timer = NULL; + client->timer_set = NULL; + client->timer_get = NULL; +} + +void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_t read, lwmqtt_network_write_t write) { + client->network = ref; + client->network_read = read; + client->network_write = write; +} + +void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *command_timer, lwmqtt_timer_set_t set, + lwmqtt_timer_get_t get) { + client->keep_alive_timer = keep_alive_timer; + client->command_timer = command_timer; + client->timer_set = set; + client->timer_get = get; + + client->timer_set(client->keep_alive_timer, 0); + client->timer_set(client->command_timer, 0); +} + +void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t cb) { + client->callback_ref = ref; + client->callback = cb; +} + +static uint16_t lwmqtt_get_next_packet_id(lwmqtt_client_t *client) { + // check overflow + if (client->last_packet_id == 65535) { + client->last_packet_id = 1; + return 1; + } + + // increment packet id + client->last_packet_id++; + + return client->last_packet_id; +} + +static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t offset, size_t len) { + // check read buffer capacity + if (client->read_buf_size < offset + len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // prepare counter + size_t read = 0; + + // read while data is missing + while (read < len) { + // check remaining time + int32_t remaining_time = client->timer_get(client->command_timer); + if (remaining_time <= 0) { + return LWMQTT_NETWORK_TIMEOUT; + } + + // read + 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; + } + + // increment counter + read += partial_read; + } + + return LWMQTT_SUCCESS; +} + +static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offset, size_t len) { + // prepare counter + size_t written = 0; + + // write while data is left + while (written < len) { + // check remaining time + int32_t remaining_time = client->timer_get(client->command_timer); + if (remaining_time <= 0) { + return LWMQTT_NETWORK_TIMEOUT; + } + + // write + 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); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // increment counter + written += partial_write; + } + + 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) { + // preset packet type + *packet_type = LWMQTT_NO_PACKET; + + // read or wait for header byte + lwmqtt_err_t err = lwmqtt_read_from_network(client, 0, 1); + if (err == LWMQTT_NETWORK_TIMEOUT) { + // this is ok as no data has been read at all + return LWMQTT_SUCCESS; + } else if (err != LWMQTT_SUCCESS) { + return err; + } + + // detect packet type + err = lwmqtt_detect_packet_type(client->read_buf, 1, packet_type); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // prepare variables + size_t len = 0; + uint32_t rem_len = 0; + + do { + // adjust len + len++; + + // read next byte + err = lwmqtt_read_from_network(client, len, 1); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // attempt to detect remaining length + err = lwmqtt_detect_remaining_length(client->read_buf + 1, len, &rem_len); + } while (err == LWMQTT_BUFFER_TOO_SHORT); + + // check final error + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read the rest of the buffer if needed + if (rem_len > 0) { + err = lwmqtt_read_from_network(client, 1 + len, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // adjust counter + *read += 1 + len + rem_len; + + return LWMQTT_SUCCESS; +} + +static lwmqtt_err_t lwmqtt_send_packet_in_buffer(lwmqtt_client_t *client, size_t length) { + // write to network + lwmqtt_err_t err = lwmqtt_write_to_network(client, 0, length); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // reset keep alive timer + client->timer_set(client->keep_alive_timer, client->keep_alive_interval); + + return LWMQTT_SUCCESS; +} + +static lwmqtt_err_t lwmqtt_cycle(lwmqtt_client_t *client, size_t *read, lwmqtt_packet_type_t *packet_type) { + // read next packet from the network + lwmqtt_err_t err = lwmqtt_read_packet_in_buffer(client, read, packet_type); + if (err != LWMQTT_SUCCESS) { + return err; + } else if (*packet_type == LWMQTT_NO_PACKET) { + return LWMQTT_SUCCESS; + } + + switch (*packet_type) { + // 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) { + 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; + } + + 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; + } + + // 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; + } + + // send pubrel packet + err = lwmqtt_send_packet_in_buffer(client, len); + 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); + 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); + 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; + + break; + } + + // handle all other packets + default: { 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) { + // prepare counter + size_t read = 0; + + // loop until timeout has been reached + do { + // do one cycle + lwmqtt_err_t err = lwmqtt_cycle(client, &read, packet_type); + 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) { + return LWMQTT_SUCCESS; + } + + // otherwise check if needle has been found + if (*packet_type == needle) { + return LWMQTT_SUCCESS; + } + } while (client->timer_get(client->command_timer) > 0 && (available == 0 || read < available)); + + return LWMQTT_SUCCESS; +} + +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); + + // 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // set command timer + client->timer_set(client->command_timer, timeout); + + // save keep alive interval (take 75% to be a little earlier than actually needed) + client->keep_alive_interval = (uint32_t)(options.keep_alive * 750); + + // set keep alive timer + client->timer_set(client->keep_alive_timer, client->keep_alive_interval); + + // reset pong pending flag + client->pong_pending = false; + + // 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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // 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) { + return err; + } else if (packet_type != LWMQTT_CONNACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // 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) { + return err; + } + + // return error if connection was not accepted + if (*return_code != LWMQTT_CONNECTION_ACCEPTED) { + return LWMQTT_CONNECTION_DENIED; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, lwmqtt_qos_t *qos, + uint32_t timeout) { + // set command timer + client->timer_set(client->command_timer, timeout); + + // 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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // 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) { + return err; + } else if (packet_type != LWMQTT_SUBACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // decode packet + int suback_count = 0; + 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) { + return err; + } + + // check suback codes + for (int i = 0; i < suback_count; i++) { + if (granted_qos[i] == LWMQTT_QOS_FAILURE) { + return LWMQTT_FAILED_SUBSCRIPTION; + } + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_subscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, lwmqtt_qos_t qos, + 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) { + // set command timer + client->timer_set(client->command_timer, timeout); + + // 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) { + return err; + } + + // send unsubscribe packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // 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) { + return err; + } else if (packet_type != LWMQTT_UNSUBACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // decode unsuback packet + 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // 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) { + packet_id = lwmqtt_get_next_packet_id(client); + } + + // 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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // immediately return on qos zero + 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) { + ack_type = LWMQTT_PUBACK_PACKET; + } else if (message.qos == LWMQTT_QOS2) { + ack_type = LWMQTT_PUBCOMP_PACKET; + } + + // 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) { + return err; + } else if (packet_type != ack_type) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) { + // set command timer + client->timer_set(client->command_timer, 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) { + return err; + } + + // send disconnected packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + return LWMQTT_SUCCESS; + } + + // return immediately if no ping is due + if (client->timer_get(client->keep_alive_timer) > 0) { + return LWMQTT_SUCCESS; + } + + // a ping is due + + // fail immediately if a pong is already pending + if (client->pong_pending) { + return LWMQTT_PONG_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) { + return err; + } + + // send packet + err = lwmqtt_send_packet_in_buffer(client, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set flag + client->pong_pending = true; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/lwmqtt/helpers.c b/libraries/lwmqtt/helpers.c new file mode 100644 index 00000000000..8b9d37e5b4f --- /dev/null +++ b/libraries/lwmqtt/helpers.c @@ -0,0 +1,251 @@ +#include + +#include "helpers.h" + +uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num) { + return (byte & (uint8_t)((~(0xFF << num)) << pos)) >> pos; +} + +void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num) { + *byte = (*byte & ~(uint8_t)((~(0xFF << num)) << pos)) | (value << pos); +} + +lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len) { + // check zero length + if (len == 0) { + *data = NULL; + return LWMQTT_SUCCESS; + } + + // check buffer size + if ((size_t)(buf_end - (*buf)) < len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // read data + *data = *buf; + + // advance pointer + *buf += len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_data(uint8_t **buf, const uint8_t *buf_end, uint8_t *data, size_t len) { + // check zero length + if (len == 0) { + return LWMQTT_SUCCESS; + } + + // check buffer size + if ((size_t)(buf_end - (*buf)) < len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // write data + memcpy(*buf, data, len); + + // advance pointer + *buf += len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_read_num(uint8_t **buf, const uint8_t *buf_end, uint16_t *num) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 2) { + *num = 0; + return LWMQTT_BUFFER_TOO_SHORT; + } + + // read two byte integer + *num = (uint16_t)256 * (*buf)[0] + (*buf)[1]; + + // adjust pointer + *buf += 2; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_num(uint8_t **buf, const uint8_t *buf_end, uint16_t num) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 2) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // write bytes + (*buf)[0] = (uint8_t)(num / 256); + (*buf)[1] = (uint8_t)(num % 256); + + // adjust pointer + *buf += 2; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_read_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t *str) { + // read length + uint16_t len; + lwmqtt_err_t err = lwmqtt_read_num(buf, buf_end, &len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read data + err = lwmqtt_read_data(buf, buf_end, (uint8_t **)&str->data, len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set length + str->len = len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t str) { + // write string length + lwmqtt_err_t err = lwmqtt_write_num(buf, buf_end, str.len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write data + err = lwmqtt_write_data(buf, buf_end, (uint8_t *)str.data, str.len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_read_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t *byte) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 1) { + *byte = 0; + return LWMQTT_BUFFER_TOO_SHORT; + } + + // read byte + *byte = (*buf)[0]; + + // adjust pointer + *buf += 1; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t byte) { + // check buffer size + if ((size_t)(buf_end - (*buf)) < 1) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // write byte + (*buf)[0] = byte; + + // adjust pointer + *buf += 1; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_varnum_length(uint32_t varnum, int *len) { + if (varnum < 128) { + *len = 1; + return LWMQTT_SUCCESS; + } else if (varnum < 16384) { + *len = 2; + return LWMQTT_SUCCESS; + } else if (varnum < 2097151) { + *len = 3; + return LWMQTT_SUCCESS; + } else if (varnum < 268435455) { + *len = 4; + return LWMQTT_SUCCESS; + } else { + *len = 0; + return LWMQTT_VARNUM_OVERFLOW; + } +} + +lwmqtt_err_t lwmqtt_read_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t *varnum) { + // prepare last byte + uint8_t byte; + + // prepare multiplier + uint32_t multiplier = 1; + + // prepare length + size_t len = 0; + + // initialize number + *varnum = 0; + + // decode variadic number + do { + // increment length + len++; + + // return error if buffer is to small + if ((size_t)(buf_end - (*buf)) < len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // return error if the length has overflowed + if (len > 4) { + return LWMQTT_VARNUM_OVERFLOW; + } + + // read byte + byte = (*buf)[len - 1]; + + // add byte to number + *varnum += (byte & 127) * multiplier; + + // increase multiplier + multiplier *= 128; + } while ((byte & 128) != 0); + + // adjust pointer + *buf += len; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_write_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t varnum) { + // init len counter + size_t len = 0; + + // encode variadic number + do { + // check overflow + if (len == 4) { + return LWMQTT_VARNUM_OVERFLOW; + } + + // return error if buffer is to small + if ((size_t)(buf_end - (*buf)) < len + 1) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // calculate current byte + uint8_t byte = (uint8_t)(varnum % 128); + + // change remaining length + varnum /= 128; + + // set the top bit of this byte if there are more to encode + if (varnum > 0) { + byte |= 0x80; + } + + // write byte + (*buf)[len++] = byte; + } while (varnum > 0); + + // adjust pointer + *buf += len; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/lwmqtt/helpers.h b/libraries/lwmqtt/helpers.h new file mode 100644 index 00000000000..978eaf4a5ad --- /dev/null +++ b/libraries/lwmqtt/helpers.h @@ -0,0 +1,137 @@ +#ifndef LWMQTT_HELPERS_H +#define LWMQTT_HELPERS_H + +#include "lwmqtt.h" + +/** + * Reads bits from a byte. + * + * @param byte - The byte to read from. + * @param pos - The position of the first bit. + * @param num - The number of bits to read. + * @return The read bits as a byte. + */ +uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num); + +/** + * Write bits to a byte. + * + * @param byte - The byte to write bits to. + * @param value - The bits to write as a byte. + * @param pos - The position of the first bit. + * @param num - The number of bits to write. + */ +void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num); + +/** + * Reads arbitrary data from the specified buffer. The pointer is incremented by bytes read. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param data - Pointer to beginning of data. + * @param len - The amount of data to read. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len); + +/** + * Writes arbitrary data to the specified buffer. The pointer is incremented by the bytes written. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param data - Pointer to the to be written data. + * @param len - The amount of data to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_data(uint8_t **buf, const uint8_t *buf_end, uint8_t *data, size_t len); + +/** + * Reads two byte number from the specified buffer. The pointer is incremented by two. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param num - The read number. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_num(uint8_t **buf, const uint8_t *buf_end, uint16_t *num); + +/** + * Writes a two byte number to the specified buffer. The pointer is incremented by two. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param num - The number to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_num(uint8_t **buf, const uint8_t *buf_end, uint16_t num); + +/** + * Reads a string from the specified buffer into the passed object. The pointer is incremented by the bytes read. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param str - The object into which the data is to be read. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t *str); + +/** + * Writes a string to the specified buffer. The pointer is incremented by the bytes written. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param str - The string to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_string(uint8_t **buf, const uint8_t *buf_end, lwmqtt_string_t str); + +/** + * Reads one byte from the buffer. The pointer is incremented by one. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param byte - The read byte. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_read_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t *byte); + +/** + * Writes one byte to the specified buffer. The pointer is incremented by one. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param byte - The byte to write. + * @return LWMQTT_SUCCESS or LWMQTT_BUFFER_TOO_SHORT. + */ +lwmqtt_err_t lwmqtt_write_byte(uint8_t **buf, const uint8_t *buf_end, uint8_t byte); + +/** + * Returns the amount of bytes required by the variable number. + * + * @param varnum - The number to check. + * @param len - The required length; + * @return LWMQTT_SUCCESS or LWMQTT_VARNUM_OVERFLOW. + */ +lwmqtt_err_t lwmqtt_varnum_length(uint32_t varnum, int *len); + +/** + * Reads a variable number from the specified buffer. The pointer is incremented by the bytes read. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param varnum - The read varnum. + * @return LWMQTT_SUCCESS, LWMQTT_BUFFER_TOO_SHORT or LWMQTT_VARNUM_OVERFLOW. + */ +lwmqtt_err_t lwmqtt_read_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t *varnum); + +/** + * Writes a variable number to the specified buffer. The pointer is incremented by the bytes written. + * + * @param buf - Pointer to the buffer. + * @param buf_end - Pointer to the end of the buffer. + * @param varnum - The number to write. + * @return LWMQTT_SUCCESS, LWMQTT_BUFFER_TOO_SHORT or LWMQTT_VARNUM_OVERFLOW. + */ +lwmqtt_err_t lwmqtt_write_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t varnum); + +#endif diff --git a/libraries/lwmqtt/lwmqtt.h b/libraries/lwmqtt/lwmqtt.h new file mode 100644 index 00000000000..e63ad100473 --- /dev/null +++ b/libraries/lwmqtt/lwmqtt.h @@ -0,0 +1,373 @@ +#ifndef LWMQTT_H +#define LWMQTT_H + +#include +#include +#include + +/** + * The error type used by all exposed APIs. + */ +typedef enum { + LWMQTT_SUCCESS = 0, + LWMQTT_BUFFER_TOO_SHORT = -1, + LWMQTT_VARNUM_OVERFLOW = -2, + LWMQTT_NETWORK_FAILED_CONNECT = -3, + LWMQTT_NETWORK_TIMEOUT = -4, + LWMQTT_NETWORK_FAILED_READ = -5, + LWMQTT_NETWORK_FAILED_WRITE = -6, + LWMQTT_REMAINING_LENGTH_OVERFLOW = -7, + LWMQTT_REMAINING_LENGTH_MISMATCH = -8, + LWMQTT_MISSING_OR_WRONG_PACKET = -9, + LWMQTT_CONNECTION_DENIED = -10, + LWMQTT_FAILED_SUBSCRIPTION = -11, + LWMQTT_SUBACK_ARRAY_OVERFLOW = -12, + LWMQTT_PONG_TIMEOUT = -13, +} lwmqtt_err_t; + +/** + * A common string object. + */ +typedef struct { + uint16_t len; + char *data; +} lwmqtt_string_t; + +/** + * The initializer for string objects. + */ +#define lwmqtt_default_string \ + { 0, NULL } + +/** + * Returns a string object for the passed C string. + * + * @param str - The C string. + * @return A string object. + */ +lwmqtt_string_t lwmqtt_string(const char *str); + +/** + * Compares a string object to a C string. + * + * @param a - The string object to compare. + * @param b - The C string to compare. + * @return Similarity e.g. strcmp(). + */ +int lwmqtt_strcmp(lwmqtt_string_t a, const char *b); + +/** + * The available QOS levels. + */ +typedef enum { LWMQTT_QOS0 = 0, LWMQTT_QOS1 = 1, LWMQTT_QOS2 = 2, LWMQTT_QOS_FAILURE = 128 } lwmqtt_qos_t; + +/** + * The message object used to publish and receive messages. + */ +typedef struct { + lwmqtt_qos_t qos; + bool retained; + uint8_t *payload; + size_t payload_len; +} lwmqtt_message_t; + +/** + * The initializer for message objects. + */ +#define lwmqtt_default_message \ + { LWMQTT_QOS0, false, NULL, 0 } + +/** + * Forward declaration of the client object. + */ +typedef struct lwmqtt_client_t lwmqtt_client_t; + +/** + * The callback used to read from a network object. + * + * The callbacks is expected to read up to the amount of bytes in to the passed buffer. It should block the specified + * timeout and wait for more incoming data. + * + * @param ref - A custom reference. + * @param buf - The buffer. + * @param len - The length of the buffer. + * @param read - Variable that must be set with the amount of read bytes. + * @param timeout - The timeout in milliseconds for the operation. + */ +typedef lwmqtt_err_t (*lwmqtt_network_read_t)(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout); + +/** + * The callback used to write to a network object. + * + * The callback is expected to write up to the amount of bytes from the passed buffer. It should wait up to the + * specified timeout to write the specified data to the network. + * + * @param ref - A custom reference. + * @param buf - The buffer. + * @param len - The length of the buffer. + * @param sent - Variable that must be set with the amount of written bytes. + * @param timeout - The timeout in milliseconds for the operation. + */ +typedef lwmqtt_err_t (*lwmqtt_network_write_t)(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout); + +/** + * The callback used to set a timer. + * + * @param ref - A custom reference. + * @param timeout - The amount of milliseconds until the deadline. + */ +typedef void (*lwmqtt_timer_set_t)(void *ref, uint32_t timeout); + +/** + * The callback used to get a timers value. + * + * @param - A custom reference. + * @return The amount of milliseconds until the deadline. May return negative numbers if the deadline has been reached. + */ +typedef int32_t (*lwmqtt_timer_get_t)(void *ref); + +/** + * The callback used to forward incoming messages. + * + * Note: The callback is mostly executed because of a call to lwmqtt_yield() that processes incoming messages. However, + * it is possible that the callback is also executed during a call to lwmqtt_subscribe(), lwmqtt_publish() or + * lwmqtt_unsubscribe() if incoming messages are received between the required acknowledgements. It is therefore not + * recommended to call any further lwmqtt methods in the callback as this might result in weird call stacks. The + * callback should place the received messages in a queue and dispatch them after the caller has returned. + */ +typedef void (*lwmqtt_callback_t)(lwmqtt_client_t *client, void *ref, lwmqtt_string_t str, lwmqtt_message_t msg); + +/** + * The client object. + */ +struct lwmqtt_client_t { + uint16_t last_packet_id; + uint32_t keep_alive_interval; + bool pong_pending; + + size_t write_buf_size, read_buf_size; + uint8_t *write_buf, *read_buf; + + lwmqtt_callback_t callback; + void *callback_ref; + + void *network; + lwmqtt_network_read_t network_read; + lwmqtt_network_write_t network_write; + + void *keep_alive_timer; + void *command_timer; + lwmqtt_timer_set_t timer_set; + lwmqtt_timer_get_t timer_get; +}; + +/** + * Will initialize the specified client object. + * + * @param client - The client object. + * @param write_buf - The write buffer. + * @param write_buf_size - The write buffer size. + * @param read_buf - The read buffer. + * @param read_buf_size - The read buffer size. + */ +void lwmqtt_init(lwmqtt_client_t *client, uint8_t *write_buf, size_t write_buf_size, uint8_t *read_buf, + size_t read_buf_size); + +/** + * Will set the network reference and callbacks for this client object. + * + * @param client - The client object. + * @param ref - The reference to the network object. + * @param read - The read callback. + * @param write - The write callback. + */ +void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_t read, lwmqtt_network_write_t write); + +/** + * Will set the timer references and callbacks for this client object. + * + * @param client - The client object. + * @param keep_alive_timer - The reference to the keep alive timer. + * @param command_timer - The reference to the command timer. + * @param set - The set callback. + * @param get - The get callback. + */ +void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *command_timer, lwmqtt_timer_set_t set, + lwmqtt_timer_get_t get); + +/** + * Will set the callback used to receive incoming messages. + * + * @param client - The client object. + * @param ref - A custom reference that will passed to the callback. + * @param cb - The callback to be called. + */ +void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t cb); + +/** + * The object defining the last will of a client. + */ +typedef struct { + lwmqtt_string_t topic; + lwmqtt_qos_t qos; + bool retained; + lwmqtt_string_t payload; +} lwmqtt_will_t; + +/** + * The default initializer for the will object. + */ +#define lwmqtt_default_will \ + { lwmqtt_default_string, LWMQTT_QOS0, false, lwmqtt_default_string } + +/** + * The object containing the connection options for a client. + */ +typedef struct { + lwmqtt_string_t client_id; + uint16_t keep_alive; + bool clean_session; + lwmqtt_string_t username; + lwmqtt_string_t password; +} lwmqtt_options_t; + +/** + * The default initializer for the options object. + */ +#define lwmqtt_default_options \ + { lwmqtt_default_string, 60, true, lwmqtt_default_string, lwmqtt_default_string } + +/** + * The available return codes transported by the connack packet. + */ +typedef enum { + LWMQTT_CONNECTION_ACCEPTED = 0, + LWMQTT_UNACCEPTABLE_PROTOCOL = 1, + LWMQTT_IDENTIFIER_REJECTED = 2, + LWMQTT_SERVER_UNAVAILABLE = 3, + LWMQTT_BAD_USERNAME_OR_PASSWORD = 4, + LWMQTT_NOT_AUTHORIZED = 5, + LWMQTT_UNKNOWN_RETURN_CODE = 6 +} lwmqtt_return_code_t; + +/** + * Will send a connect packet and wait for a connack response and set the return code. + * + * The network object must already be connected to the server. An error is returned if the broker rejects the + * connection. + * + * @param client - The client object. + * @param options - The options object. + * @param will - The will object. + * @param return_code - The variable that will receive the return code. + * @param timeout - The command timeout. + * @return An error value. + */ +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); + +/** + * Will send a publish packet and wait for all acks to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param topic - The topic. + * @param message - The message. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmqtt_message_t msg, uint32_t timeout); + +/** + * Will send a subscribe packet with multiple topic filters plus QOS levels and wait for the suback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param count - The number of topic filters and QOS levels. + * @param topic_filter - The list of topic filters. + * @param qos - The list of QOS levels. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, lwmqtt_qos_t *qos, + uint32_t timeout); + +/** + * Will send a subscribe packet with a single topic filter plus QOS level and wait for the suback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param topic_filter - The topic filter. + * @param qos - The QOS level. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_subscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, lwmqtt_qos_t qos, + uint32_t timeout); + +/** + * Will send an unsubscribe packet with multiple topic filters and wait for the unsuback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param count - The number of topic filters. + * @param topic_filter - The topic filter. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, uint32_t timeout); + +/** + * Will send an unsubscribe packet with a single topic filter and wait for the unsuback to complete. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param topic_filter - The topic filter. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_unsubscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic_filter, uint32_t timeout); + +/** + * Will send a disconnect packet and finish the client. + * + * @param client - The client object. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout); + +/** + * Will yield control to the client and receive incoming packets from the network. + * + * Applications may peek on the network if there is data available to read before calling yield and potentially block + * until the timeout is reached. Furthermore, applications may specify the amount of bytes available to read in order + * to constrain the yield to only receive packets that are already inflight. + * + * If no availability data is given the yield will return after one packet has been successfully read or the deadline + * has been reached but no single bytes has been received. + * + * Note: The message callback might be called with incoming messages as part of this call. + * + * @param client - The client object. + * @param available - The available bytes to read. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t timeout); + +/** + * Will yield control to the client to keep the connection alive. + * + * @param client - The client object. + * @param timeout - The command timeout. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout); + +#endif // LWMQTT_H diff --git a/libraries/lwmqtt/packet.c b/libraries/lwmqtt/packet.c new file mode 100644 index 00000000000..5ed579fd2ef --- /dev/null +++ b/libraries/lwmqtt/packet.c @@ -0,0 +1,742 @@ +#include "packet.h" + +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; + + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // prepare header + uint8_t header; + + // read header + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // get packet type + *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) { + case LWMQTT_CONNACK_PACKET: + case LWMQTT_PUBLISH_PACKET: + case LWMQTT_PUBACK_PACKET: + case LWMQTT_PUBREC_PACKET: + case LWMQTT_PUBREL_PACKET: + case LWMQTT_PUBCOMP_PACKET: + case LWMQTT_SUBACK_PACKET: + 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) { + // 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) { + *rem_len = 0; + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } else if (err != LWMQTT_SUCCESS) { + *rem_len = 0; + return err; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_options_t options, + lwmqtt_will_t *will) { + // prepare pointers + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // fixed header is 10 + uint32_t rem_len = 10; + + // add client id to remaining length + rem_len += options.client_id.len + 2; + + // add will if present to remaining length + 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) { + rem_len += options.username.len + 2; + + // add password if present to remaining length + if (options.password.len > 0) { + rem_len += options.password.len + 2; + } + } + + // 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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + lwmqtt_write_bits(&header, LWMQTT_CONNECT_PACKET, 4, 4); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write version string + err = lwmqtt_write_string(&buf_ptr, buf_end, lwmqtt_string("MQTT")); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write version number + err = lwmqtt_write_byte(&buf_ptr, buf_end, 4); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // prepare flags + uint8_t flags = 0; + + // set clean session + lwmqtt_write_bits(&flags, (uint8_t)(options.clean_session), 1, 1); + + // set will flags if present + if (will != NULL) { + lwmqtt_write_bits(&flags, 1, 2, 1); + lwmqtt_write_bits(&flags, will->qos, 3, 2); + lwmqtt_write_bits(&flags, (uint8_t)(will->retained), 5, 1); + } + + // set username flag if present + if (options.username.len > 0) { + lwmqtt_write_bits(&flags, 1, 6, 1); + + // set password flag if present + if (options.password.len > 0) { + lwmqtt_write_bits(&flags, 1, 7, 1); + } + } + + // write flags + err = lwmqtt_write_byte(&buf_ptr, buf_end, flags); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write keep alive + err = lwmqtt_write_num(&buf_ptr, buf_end, options.keep_alive); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write client id + err = lwmqtt_write_string(&buf_ptr, buf_end, options.client_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write will if present + if (will != NULL) { + // write topic + err = lwmqtt_write_string(&buf_ptr, buf_end, will->topic); + 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) { + return err; + } + + // write payload + err = lwmqtt_write_data(&buf_ptr, buf_end, (uint8_t *)will->payload.data, will->payload.len); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // write username if present + if (options.username.len > 0) { + err = lwmqtt_write_string(&buf_ptr, buf_end, options.username); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // write password if present + if (options.username.len > 0 && options.password.len > 0) { + err = lwmqtt_write_string(&buf_ptr, buf_end, options.password); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // set written length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_present, + lwmqtt_return_code_t *return_code) { + // prepare pointers + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_CONNACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length + if (rem_len != 2) { + return LWMQTT_REMAINING_LENGTH_MISMATCH; + } + + // read flags + uint8_t flags; + err = lwmqtt_read_byte(&buf_ptr, buf_end, &flags); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read return code + uint8_t raw_return_code; + err = lwmqtt_read_byte(&buf_ptr, buf_end, &raw_return_code); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // get session present + *session_present = lwmqtt_read_bits(flags, 7, 1) == 1; + + // get 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; + } + + 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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // write header + 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) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, 0); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header = 0; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != packet_type) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // get dup + *dup = lwmqtt_read_bits(header, 3, 1) == 1; + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf + buf_len, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length + 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) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, packet_type, 4, 4); + + // set dup + lwmqtt_write_bits(&header, (uint8_t)(dup), 3, 1); + + // set qos + lwmqtt_write_bits(&header, (uint8_t)(packet_type == LWMQTT_PUBREL_PACKET ? LWMQTT_QOS1 : LWMQTT_QOS0), 1, 2); + + // write header + lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, 2); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set written length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_PUBLISH_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // get dup + *dup = lwmqtt_read_bits(header, 3, 1) == 1; + + // get retained + msg->retained = lwmqtt_read_bits(header, 0, 1) == 1; + + // get qos + 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; + } + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length (topic length) + if (rem_len < 2) { + return LWMQTT_REMAINING_LENGTH_MISMATCH; + } + + // check buffer capacity + if ((uint32_t)(buf_end - buf_ptr) < rem_len) { + return LWMQTT_BUFFER_TOO_SHORT; + } + + // reset buf end + buf_end = buf_ptr + rem_len; + + // read topic + err = lwmqtt_read_string(&buf_ptr, buf_end, topic); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // read packet id if qos is at least 1 + if (msg->qos > 0) { + err = lwmqtt_read_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + } else { + *packet_id = 0; + } + + // set payload length + msg->payload_len = buf_end - buf_ptr; + + // read payload + err = lwmqtt_read_data(&buf_ptr, buf_end, &msg->payload, buf_end - buf_ptr); + if (err != LWMQTT_SUCCESS) { + return err; + } + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // calculate remaining length + uint32_t rem_len = 2 + topic.len + (uint32_t)msg.payload_len; + if (msg.qos > 0) { + rem_len += 2; + } + + // 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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, LWMQTT_PUBLISH_PACKET, 4, 4); + + // set dup + lwmqtt_write_bits(&header, (uint8_t)(dup), 3, 1); + + // set qos + lwmqtt_write_bits(&header, msg.qos, 1, 2); + + // set retained + lwmqtt_write_bits(&header, (uint8_t)(msg.retained), 0, 1); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write topic + err = lwmqtt_write_string(&buf_ptr, buf_end, topic); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id if qos is at least 1 + if (msg.qos > 0) { + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // write payload + err = lwmqtt_write_data(&buf_ptr, buf_end, msg.payload, msg.payload_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // calculate remaining length + uint32_t rem_len = 2; + for (int i = 0; i < count; i++) { + rem_len += 2 + topic_filters[i].len + 1; + } + + // 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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, LWMQTT_SUBSCRIBE_PACKET, 4, 4); + + // set qos + lwmqtt_write_bits(&header, LWMQTT_QOS1, 1, 2); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write all subscriptions + for (int i = 0; i < count; i++) { + // write topic + err = lwmqtt_write_string(&buf_ptr, buf_end, topic_filters[i]); + 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) { + return err; + } + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // read header + uint8_t header; + lwmqtt_err_t err = lwmqtt_read_byte(&buf_ptr, buf_end, &header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check packet type + if (lwmqtt_read_bits(header, 4, 4) != LWMQTT_SUBACK_PACKET) { + return LWMQTT_MISSING_OR_WRONG_PACKET; + } + + // read remaining length + uint32_t rem_len; + err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // check remaining length (packet id + min. one suback code) + 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) { + return err; + } + + // read all suback codes + for (*count = 0; *count < (int)rem_len - 2; (*count)++) { + // check max count + if (*count > max_count) { + return LWMQTT_SUBACK_ARRAY_OVERFLOW; + } + + // read qos level + uint8_t raw_qos_level; + err = lwmqtt_read_byte(&buf_ptr, buf_end, &raw_qos_level); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // set 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; + } + } + + return LWMQTT_SUCCESS; +} + +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) { + // prepare pointer + uint8_t *buf_ptr = buf; + uint8_t *buf_end = buf + buf_len; + + // calculate remaining length + uint32_t rem_len = 2; + for (int i = 0; i < count; i++) { + rem_len += 2 + topic_filters[i].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) { + return LWMQTT_REMAINING_LENGTH_OVERFLOW; + } + + // prepare header + uint8_t header = 0; + + // set packet type + lwmqtt_write_bits(&header, LWMQTT_UNSUBSCRIBE_PACKET, 4, 4); + + // set qos + lwmqtt_write_bits(&header, LWMQTT_QOS1, 1, 2); + + // write header + err = lwmqtt_write_byte(&buf_ptr, buf_end, header); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write remaining length + err = lwmqtt_write_varnum(&buf_ptr, buf_end, rem_len); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write packet id + err = lwmqtt_write_num(&buf_ptr, buf_end, packet_id); + if (err != LWMQTT_SUCCESS) { + return err; + } + + // write topics + for (int i = 0; i < count; i++) { + err = lwmqtt_write_string(&buf_ptr, buf_end, topic_filters[i]); + if (err != LWMQTT_SUCCESS) { + return err; + } + } + + // set length + *len = buf_ptr - buf; + + return LWMQTT_SUCCESS; +} diff --git a/libraries/lwmqtt/packet.h b/libraries/lwmqtt/packet.h new file mode 100644 index 00000000000..387a18f8b83 --- /dev/null +++ b/libraries/lwmqtt/packet.h @@ -0,0 +1,185 @@ +#ifndef LWMQTT_PACKET_H +#define LWMQTT_PACKET_H + +#include "helpers.h" + +/** + * The available packet types. + */ +typedef enum { + LWMQTT_NO_PACKET = 0, + LWMQTT_CONNECT_PACKET = 1, + LWMQTT_CONNACK_PACKET, + LWMQTT_PUBLISH_PACKET, + LWMQTT_PUBACK_PACKET, + LWMQTT_PUBREC_PACKET, + LWMQTT_PUBREL_PACKET, + LWMQTT_PUBCOMP_PACKET, + LWMQTT_SUBSCRIBE_PACKET, + LWMQTT_SUBACK_PACKET, + LWMQTT_UNSUBSCRIBE_PACKET, + LWMQTT_UNSUBACK_PACKET, + LWMQTT_PINGREQ_PACKET, + LWMQTT_PINGRESP_PACKET, + LWMQTT_DISCONNECT_PACKET +} lwmqtt_packet_type_t; + +/** + * Will detect the packet type from the at least one byte long buffer. + * + * @param buf - The buffer from which the packet type will be detected. + * @param buf_len - The length of the specified buffer. + * @param packet_type - The packet type. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_detect_packet_type(uint8_t *buf, size_t buf_len, lwmqtt_packet_type_t *packet_type); + +/** + * Will detect the remaining length form the at least on byte long buffer. + * + * It will return LWMQTT_BUFFER_TOO_SHORT if the buffer is to short and an additional byte should be read from the + * network. In case the remaining length is overflowed it will return LWMQTT_REMAINING_LENGTH_OVERFLOW. + * + * @param buf - The buffer from which the remaining length will be detected. + * @param buf_len - The length of the specified buffer. + * @param rem_len - The detected remaining length. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_detect_remaining_length(uint8_t *buf, size_t buf_len, uint32_t *rem_len); + +/** + * Encodes a connect packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param options - The options to be used to build the connect packet. + * @param will - The last will and testament. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_options_t options, + lwmqtt_will_t *will); + +/** + * Decodes a connack packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param session_present - The session present flag. + * @param return_code - The return code. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_present, + lwmqtt_return_code_t *return_code); + +/** + * Encodes a zero (disconnect, pingreq) packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_type - The packets type. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_encode_zero(uint8_t *buf, size_t buf_len, size_t *len, lwmqtt_packet_type_t packet_type); + +/** + * Decodes an ack (puback, pubrec, pubrel, pubcomp, unsuback) packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param packet_type - The packet type. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @return An error value. + */ +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); + +/** + * Encodes an ack (puback, pubrec, pubrel, pubcomp) packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_type - The packets type. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @return An error value. + */ +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); + +/** + * Decodes a publish packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @param topic - The topic. + * @parma msg - The message. + * @return An error value. + */ +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); + +/** + * Encodes a publish packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param dup - The dup flag. + * @param packet_id - The packet id. + * @param topic - The topic. + * @param msg - The message. + * @return An error value. + */ +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); + +/** + * Encodes a subscribe packet into the supplied buffer. + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_id - The packet id. + * @param count - The number of members in the topic_filters and qos_levels array. + * @param topic_filters - The array of topic filter. + * @param qos_levels - The array of requested QoS levels. + * @return An error value. + */ +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); + +/** + * Decodes a suback packet from the supplied buffer. + * + * @param buf - The raw buffer data. + * @param buf_len - The length of the specified buffer. + * @param packet_id - The packet id. + * @param max_count - The maximum number of members allowed in the granted_qos_levels array. + * @param count - The number of members in the granted_qos_levels array. + * @param granted_qos_levels - The granted QoS levels. + * @return An error value. + */ +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); + +/** + * Encodes the supplied unsubscribe data into the supplied buffer, ready for sending + * + * @param buf - The buffer into which the packet will be encoded. + * @param buf_len - The length of the specified buffer. + * @param len - The encoded length of the packet. + * @param packet_id - The packet id. + * @param count - The number of members in the topic_filters array. + * @param topic_filters - The array of topic filters. + * @return An error value. + */ +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); + +#endif // LWMQTT_PACKET_H diff --git a/libraries/lwmqtt/string.c b/libraries/lwmqtt/string.c new file mode 100644 index 00000000000..c27dc94e3d1 --- /dev/null +++ b/libraries/lwmqtt/string.c @@ -0,0 +1,38 @@ +#include + +#include "lwmqtt.h" + +lwmqtt_string_t lwmqtt_string(const char *str) { + // check for null + if (str == NULL) { + return (lwmqtt_string_t){0, NULL}; + } + + // get length + uint16_t len = (uint16_t)strlen(str); + + // check zero length + if (len == 0) { + return (lwmqtt_string_t){0, NULL}; + } + + return (lwmqtt_string_t){len, (char *)str}; +} + +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) { + return 0; + } + + // return if lengths are different + if (a.len != b_str.len) { + return -1; + } + + // compare memory of same length + return strncmp(a.data, b_str.data, a.len); +} From c3cb6121bc358b168d40c99bda463090e3e2096d Mon Sep 17 00:00:00 2001 From: HendrikVE Date: Tue, 12 Jun 2018 11:18:06 +0200 Subject: [PATCH 2/9] added BME280 library --- libraries/BME280-2.3.0/LICENSE | 675 ++++++++++++++++++ libraries/BME280-2.3.0/README.md | 325 +++++++++ .../examples/BME280_Modes/BME280_Modes.ino | 142 ++++ .../BME_280_BRZO_I2C_Test.ino | 103 +++ .../BME_280_I2C_Test/BME_280_I2C_Test.ino | 91 +++ .../BME_280_Spi_Sw_Test.ino | 96 +++ .../BME_280_Spi_Test/BME_280_Spi_Test.ino | 96 +++ .../Environment_Calculations.ino | 110 +++ libraries/BME280-2.3.0/keywords.txt | 10 + libraries/BME280-2.3.0/library.properties | 9 + libraries/BME280-2.3.0/src/BME280.cpp | 414 +++++++++++ libraries/BME280-2.3.0/src/BME280.h | 336 +++++++++ libraries/BME280-2.3.0/src/BME280I2C.cpp | 84 +++ libraries/BME280-2.3.0/src/BME280I2C.h | 86 +++ libraries/BME280-2.3.0/src/BME280I2C_BRZO.cpp | 81 +++ libraries/BME280-2.3.0/src/BME280I2C_BRZO.h | 90 +++ libraries/BME280-2.3.0/src/BME280Spi.cpp | 118 +++ libraries/BME280-2.3.0/src/BME280Spi.h | 92 +++ libraries/BME280-2.3.0/src/BME280SpiSw.cpp | 137 ++++ libraries/BME280-2.3.0/src/BME280SpiSw.h | 106 +++ .../src/EnvironmentCalculations.cpp | 101 +++ .../src/EnvironmentCalculations.h | 86 +++ 22 files changed, 3388 insertions(+) create mode 100644 libraries/BME280-2.3.0/LICENSE create mode 100644 libraries/BME280-2.3.0/README.md create mode 100644 libraries/BME280-2.3.0/examples/BME280_Modes/BME280_Modes.ino create mode 100644 libraries/BME280-2.3.0/examples/BME_280_BRZO_I2C_Test/BME_280_BRZO_I2C_Test.ino create mode 100644 libraries/BME280-2.3.0/examples/BME_280_I2C_Test/BME_280_I2C_Test.ino create mode 100644 libraries/BME280-2.3.0/examples/BME_280_Spi_Sw_Test/BME_280_Spi_Sw_Test.ino create mode 100644 libraries/BME280-2.3.0/examples/BME_280_Spi_Test/BME_280_Spi_Test.ino create mode 100644 libraries/BME280-2.3.0/examples/Environment_Calculations/Environment_Calculations.ino create mode 100644 libraries/BME280-2.3.0/keywords.txt create mode 100644 libraries/BME280-2.3.0/library.properties create mode 100644 libraries/BME280-2.3.0/src/BME280.cpp create mode 100644 libraries/BME280-2.3.0/src/BME280.h create mode 100644 libraries/BME280-2.3.0/src/BME280I2C.cpp create mode 100644 libraries/BME280-2.3.0/src/BME280I2C.h create mode 100644 libraries/BME280-2.3.0/src/BME280I2C_BRZO.cpp create mode 100644 libraries/BME280-2.3.0/src/BME280I2C_BRZO.h create mode 100644 libraries/BME280-2.3.0/src/BME280Spi.cpp create mode 100644 libraries/BME280-2.3.0/src/BME280Spi.h create mode 100644 libraries/BME280-2.3.0/src/BME280SpiSw.cpp create mode 100644 libraries/BME280-2.3.0/src/BME280SpiSw.h create mode 100644 libraries/BME280-2.3.0/src/EnvironmentCalculations.cpp create mode 100644 libraries/BME280-2.3.0/src/EnvironmentCalculations.h diff --git a/libraries/BME280-2.3.0/LICENSE b/libraries/BME280-2.3.0/LICENSE new file mode 100644 index 00000000000..733c072369c --- /dev/null +++ b/libraries/BME280-2.3.0/LICENSE @@ -0,0 +1,675 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. + diff --git a/libraries/BME280-2.3.0/README.md b/libraries/BME280-2.3.0/README.md new file mode 100644 index 00000000000..265ade790f9 --- /dev/null +++ b/libraries/BME280-2.3.0/README.md @@ -0,0 +1,325 @@ +# BME280 +Provides an Arduino library for reading and interpreting Bosch BME280 data over I2C, SPI or Sw SPI. Additional environment calculation functions are provided. ESP and BRZO are now supported. + +## Table of Contents + +1. [BME280](#bme280) +2. [Table of Contents](#table-of-contents) +3. [Summary](#summary) +4. [Installation](#installation) +5. [Usage](#usage) +6. [Enumerations](#enumerations) +7. [Settings](#settings) + - [BME280I2C::Settings](#settings) + - [BME280Spi::Settings](#settings) + - [BME280SpiSw::Settings](#settings) +8. [Methods](#methods) + - [BME280I2C(const BME280I2C::Settings& settings)](#methods) + - [BME280Spi(const BME280Spi::Settings& settings)](#methods) + - [BME280SpiSw(const BME280SpiSw::Settings& settings)](#methods) + - [bool begin()](#methods) + - [void setSettings(const Settings& settings)](#methods) + - [const Settings& getSettings() const](#methods) + - [float temp(TempUnit unit)](#methods) + - [float pres(PresUnit unit)](#methods) + - [float hum()](#methods) + - [void read(float& pressure, float& temp, float& humidity, TempUnit tempUnit, PresUnit presUnit)](#methods) + - [ChipModel chipModel()](#methods) + +9. [Environment Calculations](#environment-calculations) + - [float Altitude(float pressure, bool metric = true, float seaLevelPressure = 101325)](#environment-calculations) + - [float EquivalentSeaLevelPressure(float altitude, float temp, float pres)](#environment-calculations) + - [float DewPoint(float temp, float hum, bool metric = true)](#environment-calculations) +10. [Contributing](#contributing) +11. [History](#history) +12. [Credits](#credits) +13. [License](#license) + + + +## Summary + +Reads temperature, humidity, and pressure. Calculates altitude and dew point. Provides functions for english and metric. Also reads pressure in Pa, hPa, inHg, atm, bar, torr, N/m^2 and psi. + +## Installation + +To use this library download the zip file, uncompress it to a folder named BME280. Move the folder to {Arduino Path}/libraries. + +## Usage + +Include the library at the top of your Arduino script. `#include ` +Create a global or local variable. `BME280 bme` +In your start up call `bme.begin()`. +Read the temperature, humidity, pressure, altitude and/or dew point. + +`float pres, temp, hum` +`bme.read(pres, temp, hum)` + +or + +`temp = bme.temp()` +`hum = bme.hum()` +`pres = bme.pres()` + +## Enumerations +#### TempUnit Enum + * TempUnit_Celsius + * TempUnit_Fahrenheit + +#### PresUnit Enum + * PresUnit_Pa + * PresUnit_hPa + * PresUnit_inHg + * PresUnit_atm + * PresUnit_bar + * PresUnit_torr + * PresUnit_psi + +#### OSR Enum + * OSR_X1 + * OSR_X2 + * OSR_X4 + * OSR_X8 + * OSR_X16 + +#### Mode Enum + * Mode_Sleep + * Mode_Forced + * Mode_Normal + +#### StandbyTime Enum + * StandbyTime_500us + * StandbyTime_62500us + * StandbyTime_125ms + * StandbyTime_250ms + * StandbyTime_50ms + * StandbyTime_1000ms + * StandbyTime_10ms + * StandbyTime_20ms + +#### Filter Enum + * Filter_Off + * Filter_1 + * Filter_2 + * Filter_4 + * Filter_8 + * Filter_16 + +#### ChipModel Enum + * ChipModel_Unknown + * ChipModel_BME280 + * ChipModel_BMP280 + +## Settings + +#### BME280::Settings Struct +``` + * Temperature Oversampling Rate (tempOSR): OSR Enum, default = OSR_X1 + + * Humidity Oversampling Rate (humOSR): OSR Enum, default = OSR_X1 + + * Pressure Oversampling Rate (presOSR): OSR Enum, default = OSR_X1 + + * Mode (mode): Mode Enum, default = Mode_Forced + + * Standby Time (standbyTime): StandbyTime Enum, default = StandbyTime_1000ms + + * Filter (filter): Filter Enum, default = Filter_Off + + * SPI Enable: SpiEnable Enum, default = false + values: true = enable, false = disable +``` + +#### BME280I2C::Settings Struct + + * Includes all fields in BME280 settings. +``` + * BME 280 Address (bme280Addr): uint8_t, default = 0x76 +``` +#### BME280Spi::Settings Struct + + * Includes all fields in BME280 settings. +``` + * SPI Chip Select Pin (spiCsPin): uint8_t + values: Any pin 0-31 +``` + +#### BME280Spi::Settings Struct + * Includes all fields in BME280 settings. +``` + * SPI Chip Select Pin (spiCsPin): uint8_t + values: Any pin 0-31 + + * SPI Master Out Slave In Pin (spiMosiPin): uint8_t + values: Any pin 0-31 + + * SPI Master In Slave Out Pin (spiMisoPin): uint8_t + values: Any pin 0-31 + + * SPI Serial Clock Pin (spiSckPin): uint8_t + values: Any pin 0-31 +``` +## Methods + + +#### BME280I2C(const BME280I2C::Settings& settings) + + Constructor used to create the I2C Bme class. All parameters have default values. + +#### BME280Spi(const BME280Spi::Settings& settings) + + Constructor used to create the Spi Bme class. All parameters have default values except chip select. + +#### BME280SpiSw(const BME280SpiSw::Settings& settings) + + Constructor used to create the software Spi Bme class. All parameters have default values except chip select, mosi, miso and sck. + +#### bool begin() + + Method used at start up to initialize the class. Starts the I2C or SPI interface. Can be called again to re-initialize the mode settings. + ``` + * return: bool, true = success, false = failure (no device found) + ``` + +#### void setSettings(const Settings& settings) + + Method to set the sensor settings. + + +#### const Settings& getSettings() const + + Method to get the sensor settings. + +#### float temp(TempUnit unit) + + Read the temperature from the BME280 and return a float. +``` + return: float = temperature + + * unit: tempUnit, default = TempUnit_Celsius +``` + +#### float pres(PresUnit unit) + + Read the pressure from the BME280 and return a float with the specified unit. +``` + return: float = pressure + + * unit: uint8_t, default = PresUnit_Pa +``` + +#### float hum() + + Read the humidity from the BME280 and return a percentage as a float. +``` + * return: float = percent relative humidity +``` +#### void read(float& pressure, float& temp, float& humidity, TempUnit tempUnit, PresUnit presUnit) + + Read the data from the BME280 with the specified units. +``` + return: None, however, pressure, temp and humidity are changed. + + * Pressure: float, reference + values: reference to storage float for pressure + + * Temperature: float, reference + values: reference to storage float for temperature + + * Humidity: float, reference + values: reference to storage float for humidity + + * tempUnit: tempUnit, default = TempUnit_Celsius + + * presUnit: uint8_t, default = PresUnit_Pa +``` + +#### ChipModel chipModel() +``` + * return: [ChipModel](#chipmodel-enum) enum +``` + +## Environment Calculations + +#### float Altitude(float pressure, bool metric = true, float seaLevelPressure = 101325) + + Calculate the altitude based on the pressure with the specified units. + Return: float = altitude +``` + * Pressure: float, unit = Pa + values: any float + + * Metric: bool, default = true + values: true = meters, false = feet + + * Sea Level Pressure: float, unit = Pa, default = 101325 + values: any float +``` + +#### float EquivalentSeaLevelPressure(float altitude, float temp, float pres) + + Convert current pressure to equivalent sea-level pressure. + +``` + return: The equivalent pressure at sea level. + + * altitude: float + values: meters + + * temp: float + values: celsius + + * pres: float + values: unit independent +``` + +#### float DewPoint(float temp, float hum, bool metric = true) + + Calculate the dew point based on the temperature and humidity with the specified units. +``` + return: float = dew point + + * Temperature: float, unit = Celsius if metric is true, Fahrenheit if metric is false + values: any float + + * Humidity: float, unit = % relative humidity + values: any float + + * Metric: bool, default = true + values: true = return degrees Celsius, false = return degrees Fahrenheit +``` + + +## Contributing + +1. Fork the project. +2. Create your feature branch: `git checkout -b my-new-feature` +3. Commit your changes: `git commit -am 'Add some feature'` +4. Push to the branch: `git push origin my-new-feature` +5. Submit a pull request. + +## History + +- Jan 1, 2016 - Version 1.0.0 released +- Sep 19, 2016 - Version 2.0.0 released (Restructure for I2C and SPI) +- Nov 21, 2016 - Version 2.0.1 released (Set mode support) +- Dec 19, 2016 - Version 2.1.0 released (Support for SPI) +- Dec 21, 2016 - Version 2.1.1 released (Bugs) +- Feb 17, 2017 - Version 2.1.2 released (Docs) +- Sept 9, 2017 - Version 2.1.3 released (Formatting, reorg) +- Sept 13, 2017 - Version 2.1.4 released (Examples update, bug fixes) +- Oct 7, 2017 - Version 2.2.0 released (Enums, begin restructure) +- Oct 10, 2017 - Version 2.2.1 released (Bug fixes) +- Nov 21, 2017 - Version 2.3.0 released (Examples updates, env calc fixes, bugs) + +## Credits + +Written by Tyler Glenn, 2016. + +Special thanks to Mike Glenn for editing and reviewing the code. + +## License + +GNU GPL, see License.txt + + diff --git a/libraries/BME280-2.3.0/examples/BME280_Modes/BME280_Modes.ino b/libraries/BME280-2.3.0/examples/BME280_Modes/BME280_Modes.ino new file mode 100644 index 00000000000..b1b31c2186c --- /dev/null +++ b/libraries/BME280-2.3.0/examples/BME280_Modes/BME280_Modes.ino @@ -0,0 +1,142 @@ +/* +BME280I2C Modes.ino + +This code shows how to use predefined recommended settings from Bosch for +the BME280I2C environmental sensor. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Sep 23 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro +SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro + + */ + +#include +#include // Needed for legacy versions of Arduino. + +#define SERIAL_BAUD 115200 + +/* Recommended Modes - + Based on Bosch BME280I2C environmental sensor data sheet. + +Weather Monitoring : + forced mode, 1 sample/minute + pressure ×1, temperature ×1, humidity ×1, filter off + Current Consumption = 0.16 μA + RMS Noise = 3.3 Pa/30 cm, 0.07 %RH + Data Output Rate 1/60 Hz + +Humidity Sensing : + forced mode, 1 sample/second + pressure ×0, temperature ×1, humidity ×1, filter off + Current Consumption = 2.9 μA + RMS Noise = 0.07 %RH + Data Output Rate = 1 Hz + +Indoor Navigation : + normal mode, standby time = 0.5ms + pressure ×16, temperature ×2, humidity ×1, filter = x16 + Current Consumption = 633 μA + RMS Noise = 0.2 Pa/1.7 cm + Data Output Rate = 25Hz + Filter Bandwidth = 0.53 Hz + Response Time (75%) = 0.9 s + + +Gaming : + normal mode, standby time = 0.5ms + pressure ×4, temperature ×1, humidity ×0, filter = x16 + Current Consumption = 581 μA + RMS Noise = 0.3 Pa/2.5 cm + Data Output Rate = 83 Hz + Filter Bandwidth = 1.75 Hz + Response Time (75%) = 0.3 s + +*/ + +BME280I2C::Settings settings( + BME280::OSR_X1, + BME280::OSR_X1, + BME280::OSR_X1, + BME280::Mode_Forced, + BME280::StandbyTime_1000ms, + BME280::Filter_Off, + BME280::SpiEnable_False, + 0x76 // I2C address. I2C specific. +); + +BME280I2C bme(settings); + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + Wire.begin(); + while(!bme.begin()) + { + Serial.println("Could not find BME280I2C sensor!"); + delay(1000); + } + + // bme.chipID(); // Deprecated. See chipModel(). + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } + + // Change some settings before using. + settings.tempOSR = BME280::OSR_X4; + + bme.setSettings(settings); +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(500); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->println(" Pa"); + + delay(1000); +} diff --git a/libraries/BME280-2.3.0/examples/BME_280_BRZO_I2C_Test/BME_280_BRZO_I2C_Test.ino b/libraries/BME280-2.3.0/examples/BME_280_BRZO_I2C_Test/BME_280_BRZO_I2C_Test.ino new file mode 100644 index 00000000000..d5a21a942be --- /dev/null +++ b/libraries/BME280-2.3.0/examples/BME_280_BRZO_I2C_Test/BME_280_BRZO_I2C_Test.ino @@ -0,0 +1,103 @@ +/* +BME280 BRZO I2C Test.ino + +This code shows how to record data from the BME280 environmental sensor +using I2C interface and https://github.com/pasko-zh/brzo_i2c library +on ESP8266. + +This file is an example file, part of the Arduino BME280 library. + +Copyright (C) 2016 Tyler Glenn +Forked by Alex Shavlovsky +to support https://github.com/pasko-zh/brzo_i2c library on ESP8266. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> D2 on ESP8266 +SCK (Serial Clock) -> D1 on ESP8266 + + */ +#include "Arduino.h" + +#include "brzo_i2c.h" +#define USING_BRZO 1 +#include "BME280I2C_BRZO.h" + +#define SERIAL_BAUD 115200 + +const uint32_t I2C_ACK_TIMEOUT = 2000; + +BME280I2C_BRZO bme; // Default : forced mode, standby time = 1000 ms + // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, + +bool metric = true; + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + brzo_i2c_setup(SDA,SCL,I2C_ACK_TIMEOUT); + + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + delay(1000); + } + + // bme.chipID(); // Deprecated. See chipModel(). + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(500); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->println(" Pa"); + + delay(1000); +} diff --git a/libraries/BME280-2.3.0/examples/BME_280_I2C_Test/BME_280_I2C_Test.ino b/libraries/BME280-2.3.0/examples/BME_280_I2C_Test/BME_280_I2C_Test.ino new file mode 100644 index 00000000000..a0fbd4a6cbc --- /dev/null +++ b/libraries/BME280-2.3.0/examples/BME_280_I2C_Test/BME_280_I2C_Test.ino @@ -0,0 +1,91 @@ +/* +BME280 I2C Test.ino + +This code shows how to record data from the BME280 environmental sensor +using I2C interface. This file is an example file, part of the Arduino +BME280 library. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro +SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro + + */ + +#include +#include + +#define SERIAL_BAUD 115200 + +BME280I2C bme; // Default : forced mode, standby time = 1000 ms + // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + Wire.begin(); + + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + delay(1000); + } + + // bme.chipID(); // Deprecated. See chipModel(). + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(500); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->println(" Pa"); + + delay(1000); +} diff --git a/libraries/BME280-2.3.0/examples/BME_280_Spi_Sw_Test/BME_280_Spi_Sw_Test.ino b/libraries/BME280-2.3.0/examples/BME_280_Spi_Sw_Test/BME_280_Spi_Sw_Test.ino new file mode 100644 index 00000000000..524a84eafcb --- /dev/null +++ b/libraries/BME280-2.3.0/examples/BME_280_Spi_Sw_Test/BME_280_Spi_Sw_Test.ino @@ -0,0 +1,96 @@ +/* +BME280 Spi Sw Test.ino + +This code shows how to record data from the BME280 environmental sensor +using Spi interface. This file is an example file, part of the Arduino +BME280 library. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro +SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro + + */ + +#include + +#define SERIAL_BAUD 115200 + +#define CHIP_SELECT_PIN 10 +#define MOSI_PIN 11 +#define MISO_PIN 12 +#define SCK_PIN 13 + +BME280SpiSw::Settings settings(CHIP_SELECT_PIN, MOSI_PIN, MISO_PIN, SCK_PIN); +BME280SpiSw bme(settings); + +bool metric = false; + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + delay(1000); + } + + // bme.chipID(); // Deprecated. See chipModel(). + + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(1000); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->println(" Pa"); + + delay(1000); +} diff --git a/libraries/BME280-2.3.0/examples/BME_280_Spi_Test/BME_280_Spi_Test.ino b/libraries/BME280-2.3.0/examples/BME_280_Spi_Test/BME_280_Spi_Test.ino new file mode 100644 index 00000000000..3ff216ccd6b --- /dev/null +++ b/libraries/BME280-2.3.0/examples/BME_280_Spi_Test/BME_280_Spi_Test.ino @@ -0,0 +1,96 @@ +/* +BME280 Spi Test.ino + +This code shows how to record data from the BME280 environmental sensor +using Spi interface. This file is an example file, part of the Arduino +BME280 library. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro +SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro + +*/ + +#include // Needed for legacy versions of Arduino. +#include + +#define SERIAL_BAUD 115200 + +#define DEVICE_PIN 10 + +BME280Spi::Settings settings(DEVICE_PIN); // Default : forced mode, standby time = 1000 ms + // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, + +BME280Spi bme(settings); + + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + SPI.begin(); + + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + delay(1000); + } + + // bme.chipID(); // Deprecated. See chipModel(). + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(500); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->println(" Pa"); + + delay(1000); +} diff --git a/libraries/BME280-2.3.0/examples/Environment_Calculations/Environment_Calculations.ino b/libraries/BME280-2.3.0/examples/Environment_Calculations/Environment_Calculations.ino new file mode 100644 index 00000000000..80bd0a65d63 --- /dev/null +++ b/libraries/BME280-2.3.0/examples/Environment_Calculations/Environment_Calculations.ino @@ -0,0 +1,110 @@ +/* +Environment_Calculations.ino + +This code shows how to record data from the BME280 environmental sensor +and perform various calculations. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro +SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro + + */ + +#include +#include +#include + +#define SERIAL_BAUD 115200 + +BME280I2C bme; // Default : forced mode, standby time = 1000 ms + // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + Wire.begin(); + + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + delay(1000); + } + + // bme.chipID(); // Deprecated. See chipModel(). + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(500); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? "C" :"F")); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->print(" Pa"); + + EnvironmentCalculations::AltitudeUnit envAltUnit = EnvironmentCalculations::AltitudeUnit_Meters; + EnvironmentCalculations::TempUnit envTempUnit = EnvironmentCalculations::TempUnit_Celsius; + + float altitude = EnvironmentCalculations::Altitude(pres, envAltUnit); + float dewPoint = EnvironmentCalculations::DewPoint(temp, hum, envTempUnit); + float seaLevel = EnvironmentCalculations::EquivalentSeaLevelPressure(altitude, temp, pres); + // seaLevel = EnvironmentCalculations::SealevelAlitude(altitude, temp, pres); // Deprecated. See EquivalentSeaLevelPressure(). + + client->print("\t\tAltitude: "); + client->print(altitude); + client->print((envAltUnit == EnvironmentCalculations::AltitudeUnit_Meters ? "m" : "ft")); + client->print("\t\tDew point: "); + client->print(dewPoint); + client->print("°"+ String(envTempUnit == EnvironmentCalculations::TempUnit_Celsius ? "C" :"F")); + client->print("\t\tEquivalent Sea Level Pressure: "); + client->print(seaLevel); + client->println(" Pa"); + + delay(1000); +} + diff --git a/libraries/BME280-2.3.0/keywords.txt b/libraries/BME280-2.3.0/keywords.txt new file mode 100644 index 00000000000..16a0c022c73 --- /dev/null +++ b/libraries/BME280-2.3.0/keywords.txt @@ -0,0 +1,10 @@ +BME280I2C KEYWORD1 +BME280Spi KEYWORD1 +begin KEYWORD2 +temp KEYWORD2 +pres KEYWORD2 +hum KEYWORD2 +read KEYWORD2 +Altitude KEYWORD2 +EquivalentSeaLevelPressure KEYWORD2 +DewPoint KEYWORD2 diff --git a/libraries/BME280-2.3.0/library.properties b/libraries/BME280-2.3.0/library.properties new file mode 100644 index 00000000000..a2e09fd5cd2 --- /dev/null +++ b/libraries/BME280-2.3.0/library.properties @@ -0,0 +1,9 @@ +name=BME280 +version=2.3.0 +author=Tyler Glenn +maintainer=Tyler Glenn +sentence=Provides a library for reading and interpreting Bosch BME280 environmental sensor data over I2C, SPI or Sw SPI. +paragraph=Reads temperature, humidity, and pressure. Includes environment calculations. Provides functions for english and metric. Also reads pressure in Pa, hPa, inHg, atm, bar, torr, N/m^2 and psi. ESP and BRZO I2C support. +category=Sensors +url=https://www.github.com/finitespace/BME280 +architectures=* diff --git a/libraries/BME280-2.3.0/src/BME280.cpp b/libraries/BME280-2.3.0/src/BME280.cpp new file mode 100644 index 00000000000..8190ae1a78d --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280.cpp @@ -0,0 +1,414 @@ +/* +BME280.cpp +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the Bme280 environmental sensor, +calibration code based on algorithms providedBosch, some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include + +#include "BME280.h" + + +/****************************************************************/ +BME280::BME280 +( + const Settings& settings +):m_settings(settings), + m_initialized(false) +{ +} + + +/****************************************************************/ +bool BME280::Initialize() +{ + bool success(true); + + success &= ReadChipID(); + + if(success) + { + success &= ReadTrim(); + WriteSettings(); + } + + m_initialized = success; + + return m_initialized; +} + +/****************************************************************/ +bool BME280::ReadChipID() +{ + uint8_t id[1]; + + ReadRegister(ID_ADDR, &id[0], 1); + + switch(id[0]) { + case ChipModel_BME280: + m_chip_model = ChipModel_BME280; + break; + case ChipModel_BMP280: + m_chip_model = ChipModel_BMP280; + break; + default: + m_chip_model = ChipModel_UNKNOWN; + return false; + } + + m_chip_id = id[0]; + return true; +} + + +/****************************************************************/ +bool BME280::WriteSettings() +{ + uint8_t ctrlHum, ctrlMeas, config; + + CalculateRegisters(ctrlHum, ctrlMeas, config); + + WriteRegister(CTRL_HUM_ADDR, ctrlHum); + WriteRegister(CTRL_MEAS_ADDR, ctrlMeas); + WriteRegister(CONFIG_ADDR, config); +} + + +/****************************************************************/ +void BME280::setSettings +( + const Settings& settings +) +{ + m_settings = settings; + WriteSettings(); +} + +/****************************************************************/ +const BME280::Settings& BME280::getSettings() const +{ + return m_settings; +} + + +/****************************************************************/ +bool BME280::begin +( +) +{ + bool success = Initialize(); + success &= m_initialized; + + return success; +} + +/****************************************************************/ +void BME280::CalculateRegisters +( + uint8_t& ctrlHum, + uint8_t& ctrlMeas, + uint8_t& config +) +{ + // ctrl_hum register. (ctrl_hum[2:0] = Humidity oversampling rate.) + ctrlHum = (uint8_t)m_settings.humOSR; + // ctrl_meas register. (ctrl_meas[7:5] = temperature oversampling rate, ctrl_meas[4:2] = pressure oversampling rate, ctrl_meas[1:0] = mode.) + ctrlMeas = ((uint8_t)m_settings.tempOSR << 5) | ((uint8_t)m_settings.presOSR << 2) | (uint8_t)m_settings.mode; + // config register. (config[7:5] = standby time, config[4:2] = filter, ctrl_meas[0] = spi enable.) + config = ((uint8_t)m_settings.standbyTime << 5) | ((uint8_t)m_settings.filter << 2) | (uint8_t)m_settings.spiEnable; +} + + +/****************************************************************/ +bool BME280::ReadTrim() +{ + uint8_t ord(0); + bool success = true; + + // Temp. Dig + success &= ReadRegister(TEMP_DIG_ADDR, &m_dig[ord], TEMP_DIG_LENGTH); + ord += TEMP_DIG_LENGTH; + + // Pressure Dig + success &= ReadRegister(PRESS_DIG_ADDR, &m_dig[ord], PRESS_DIG_LENGTH); + ord += PRESS_DIG_LENGTH; + + // Humidity Dig 1 + success &= ReadRegister(HUM_DIG_ADDR1, &m_dig[ord], HUM_DIG_ADDR1_LENGTH); + ord += HUM_DIG_ADDR1_LENGTH; + + // Humidity Dig 2 + success &= ReadRegister(HUM_DIG_ADDR2, &m_dig[ord], HUM_DIG_ADDR2_LENGTH); + ord += HUM_DIG_ADDR2_LENGTH; + +#ifdef DEBUG_ON + Serial.print("Dig: "); + for(int i = 0; i < 32; ++i) + { + Serial.print(m_dig[i], HEX); + Serial.print(" "); + } + Serial.println(); +#endif + + return success && ord == DIG_LENGTH; +} + + +/****************************************************************/ +bool BME280::ReadData +( + int32_t data[SENSOR_DATA_LENGTH] +) +{ + bool success; + uint8_t buffer[SENSOR_DATA_LENGTH]; + + // For forced mode we need to write the mode to BME280 register before reading + if (m_settings.mode == Mode_Forced) + { + WriteSettings(); + } + + // Registers are in order. So we can start at the pressure register and read 8 bytes. + success = ReadRegister(PRESS_ADDR, buffer, SENSOR_DATA_LENGTH); + + for(int i = 0; i < SENSOR_DATA_LENGTH; ++i) + { + data[i] = static_cast(buffer[i]); + } + +#ifdef DEBUG_ON + Serial.print("Data: "); + for(int i = 0; i < 8; ++i) + { + Serial.print(data[i], HEX); + Serial.print(" "); + } + Serial.println(); +#endif + + return success; +} + + +/****************************************************************/ +float BME280::CalculateTemperature +( + int32_t raw, + int32_t& t_fine, + TempUnit unit +) +{ + // Code based on calibration algorthim provided by Bosch. + int32_t var1, var2, final; + uint16_t dig_T1 = (m_dig[1] << 8) | m_dig[0]; + int16_t dig_T2 = (m_dig[3] << 8) | m_dig[2]; + int16_t dig_T3 = (m_dig[5] << 8) | m_dig[4]; + var1 = ((((raw >> 3) - ((int32_t)dig_T1 << 1))) * ((int32_t)dig_T2)) >> 11; + var2 = (((((raw >> 4) - ((int32_t)dig_T1)) * ((raw >> 4) - ((int32_t)dig_T1))) >> 12) * ((int32_t)dig_T3)) >> 14; + t_fine = var1 + var2; + final = (t_fine * 5 + 128) >> 8; + return unit == TempUnit_Celsius ? final/100.0 : final/100.0*9.0/5.0 + 32.0; +} + + +/****************************************************************/ +float BME280::CalculateHumidity +( + int32_t raw, + int32_t t_fine +) +{ + // Code based on calibration algorthim provided by Bosch. + int32_t var1; + uint8_t dig_H1 = m_dig[24]; + int16_t dig_H2 = (m_dig[26] << 8) | m_dig[25]; + uint8_t dig_H3 = m_dig[27]; + int16_t dig_H4 = (m_dig[28] << 4) | (0x0F & m_dig[29]); + int16_t dig_H5 = (m_dig[30] << 4) | ((m_dig[29] >> 4) & 0x0F); + int8_t dig_H6 = m_dig[31]; + + var1 = (t_fine - ((int32_t)76800)); + var1 = (((((raw << 14) - (((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * var1)) + + ((int32_t)16384)) >> 15) * (((((((var1 * ((int32_t)dig_H6)) >> 10) * (((var1 * + ((int32_t)dig_H3)) >> 11) + ((int32_t)32768))) >> 10) + ((int32_t)2097152)) * + ((int32_t)dig_H2) + 8192) >> 14)); + var1 = (var1 - (((((var1 >> 15) * (var1 >> 15)) >> 7) * ((int32_t)dig_H1)) >> 4)); + var1 = (var1 < 0 ? 0 : var1); + var1 = (var1 > 419430400 ? 419430400 : var1); + return ((uint32_t)(var1 >> 12))/1024.0; +} + + +/****************************************************************/ +float BME280::CalculatePressure +( + int32_t raw, + int32_t t_fine, + PresUnit unit +) +{ + // Code based on calibration algorthim provided by Bosch. + int64_t var1, var2, pressure; + float final; + + uint16_t dig_P1 = (m_dig[7] << 8) | m_dig[6]; + int16_t dig_P2 = (m_dig[9] << 8) | m_dig[8]; + int16_t dig_P3 = (m_dig[11] << 8) | m_dig[10]; + int16_t dig_P4 = (m_dig[13] << 8) | m_dig[12]; + int16_t dig_P5 = (m_dig[15] << 8) | m_dig[14]; + int16_t dig_P6 = (m_dig[17] << 8) | m_dig[16]; + int16_t dig_P7 = (m_dig[19] << 8) | m_dig[18]; + int16_t dig_P8 = (m_dig[21] << 8) | m_dig[20]; + int16_t dig_P9 = (m_dig[23] << 8) | m_dig[22]; + + var1 = (int64_t)t_fine - 128000; + var2 = var1 * var1 * (int64_t)dig_P6; + var2 = var2 + ((var1 * (int64_t)dig_P5) << 17); + var2 = var2 + (((int64_t)dig_P4) << 35); + var1 = ((var1 * var1 * (int64_t)dig_P3) >> 8) + ((var1 * (int64_t)dig_P2) << 12); + var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)dig_P1) >> 33; + if (var1 == 0) { return NAN; } // Don't divide by zero. + pressure = 1048576 - raw; + pressure = (((pressure << 31) - var2) * 3125)/var1; + var1 = (((int64_t)dig_P9) * (pressure >> 13) * (pressure >> 13)) >> 25; + var2 = (((int64_t)dig_P8) * pressure) >> 19; + pressure = ((pressure + var1 + var2) >> 8) + (((int64_t)dig_P7) << 4); + + final = ((uint32_t)pressure)/256.0; + + // Conversion units courtesy of www.endmemo.com. + switch(unit){ + case PresUnit_hPa: /* hPa */ + final /= 100.0; + break; + case PresUnit_inHg: /* inHg */ + final /= 3386.3752577878; /* final pa * 1inHg/3386.3752577878Pa */ + break; + case PresUnit_atm: /* atm */ + final /= 101324.99766353; /* final pa * 1 atm/101324.99766353Pa */ + break; + case PresUnit_bar: /* bar */ + final /= 100000.0; /* final pa * 1 bar/100kPa */ + break; + case PresUnit_torr: /* torr */ + final /= 133.32236534674; /* final pa * 1 torr/133.32236534674Pa */ + break; + case PresUnit_psi: /* psi */ + final /= 6894.744825494; /* final pa * 1psi/6894.744825494Pa */ + break; + default: /* Pa (case: 0) */ + break; + } + return final; +} + + +/****************************************************************/ +float BME280::temp +( + TempUnit unit +) +{ + int32_t data[8]; + int32_t t_fine; + if(!ReadData(data)){ return NAN; } + uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); + return CalculateTemperature(rawTemp, t_fine, unit); +} + + +/****************************************************************/ +float BME280::pres +( + PresUnit unit +) +{ + int32_t data[8]; + int32_t t_fine; + if(!ReadData(data)){ return NAN; } + uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); + uint32_t rawPressure = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); + CalculateTemperature(rawTemp, t_fine); + return CalculatePressure(rawPressure, t_fine, unit); +} + + +/****************************************************************/ +float BME280::hum() +{ + int32_t data[8]; + int32_t t_fine; + if(!ReadData(data)){ return NAN; } + uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); + uint32_t rawHumidity = (data[6] << 8) | data[7]; + CalculateTemperature(rawTemp, t_fine); + return CalculateHumidity(rawHumidity, t_fine); +} + + +/****************************************************************/ +void BME280::read +( + float& pressure, + float& temp, + float& humidity, + TempUnit tempUnit, + PresUnit presUnit +) +{ + int32_t data[8]; + int32_t t_fine; + if(!ReadData(data)){ + pressure = temp = humidity = NAN; + return; + } + uint32_t rawPressure = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); + uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); + uint32_t rawHumidity = (data[6] << 8) | data[7]; + temp = CalculateTemperature(rawTemp, t_fine, tempUnit); + pressure = CalculatePressure(rawPressure, t_fine, presUnit); + humidity = CalculateHumidity(rawHumidity, t_fine); +} + + +/****************************************************************/ +uint8_t BME280::chipID +( +) +{ + return m_chip_id; +} + +/****************************************************************/ +BME280::ChipModel BME280::chipModel +( +) +{ + return m_chip_model; +} diff --git a/libraries/BME280-2.3.0/src/BME280.h b/libraries/BME280-2.3.0/src/BME280.h new file mode 100644 index 00000000000..4f024e3a6f4 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280.h @@ -0,0 +1,336 @@ +/* + +BME280.h + +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +This code is licensed under the GNU LGPL and is open for ditrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + + */ + +#ifndef TG_BME_280_H +#define TG_BME_280_H + +#include "Arduino.h" + + +////////////////////////////////////////////////////////////////// +/// BME280 - Driver class for Bosch Bme280 sensor +/// +/// Based on the data sheet provided by Bosch for +/// the Bme280 environmental sensor. +/// +class BME280 +{ +public: + +/*****************************************************************/ +/* ENUMERATIONS */ +/*****************************************************************/ + +enum TempUnit { + TempUnit_Celsius, + TempUnit_Fahrenheit +}; + +enum PresUnit { + PresUnit_Pa, + PresUnit_hPa, + PresUnit_inHg, + PresUnit_atm, + PresUnit_bar, + PresUnit_torr, + PresUnit_psi +}; + +enum OSR { + OSR_X1 = 1, + OSR_X2 = 2, + OSR_X4 = 3, + OSR_X8 = 4, + OSR_X16 = 5 +}; + +enum Mode { + Mode_Sleep = 0, + Mode_Forced = 1, + Mode_Normal = 3 +}; + +enum StandbyTime { + StandbyTime_500us = 0, + StandbyTime_62500us = 1, + StandbyTime_125ms = 2, + StandbyTime_250ms = 3, + StandbyTime_50ms = 4, + StandbyTime_1000ms = 5, + StandbyTime_10ms = 6, + StandbyTime_20ms = 7 +}; + +enum Filter { + Filter_Off = 0, + Filter_2 = 1, + Filter_4 = 2, + Filter_8 = 3, + Filter_16 = 4 +}; + +enum SpiEnable{ + SpiEnable_False = 0, + SpiEnable_True = 1 +}; + +enum ChipModel { + ChipModel_UNKNOWN = 0, + ChipModel_BMP280 = 0x58, + ChipModel_BME280 = 0x60 +}; + +struct Settings { + Settings( + OSR _tosr = OSR_X1, + OSR _hosr = OSR_X1, + OSR _posr = OSR_X1, + Mode _mode = Mode_Forced, + StandbyTime _st = StandbyTime_1000ms, + Filter _filter = Filter_Off, + SpiEnable _se = SpiEnable_False + ): tempOSR(_tosr), + humOSR(_hosr), + presOSR(_posr), + mode(_mode), + standbyTime(_st), + filter(_filter), + spiEnable(_se) {} + + OSR tempOSR; + OSR humOSR; + OSR presOSR; + Mode mode; + StandbyTime standbyTime; + Filter filter; + SpiEnable spiEnable; +}; + +/*****************************************************************/ +/* INIT FUNCTIONS */ +/*****************************************************************/ + + ///////////////////////////////////////////////////////////////// + /// Constructor used to create the class. + /// All parameters have default values. + BME280( + const Settings& settings); + + ///////////////////////////////////////////////////////////////// + /// Method used to initialize the class. + bool begin(); + +/*****************************************************************/ +/* ENVIRONMENTAL FUNCTIONS */ +/*****************************************************************/ + + ////////////////////////////////////////////////// + /// Read the temperature from the BME280 and return a float. + float temp( + TempUnit unit = TempUnit_Celsius); + + ///////////////////////////////////////////////////////////////// + /// Read the pressure from the BME280 and return a float with the + /// specified unit. + float pres( + PresUnit unit = PresUnit_Pa); + + ///////////////////////////////////////////////////////////////// + /// Read the humidity from the BME280 and return a percentage + /// as a float. + float hum(); + + ///////////////////////////////////////////////////////////////// + /// Read the data from the BME280 in the specified unit. + void read( + float& pressure, + float& temperature, + float& humidity, + TempUnit tempUnit = TempUnit_Celsius, + PresUnit presUnit = PresUnit_Pa); + + +/*****************************************************************/ +/* ACCESSOR FUNCTIONS */ +/*****************************************************************/ + + ///////////////////////////////////////////////////////////////// + void setSettings( + const Settings& settings); + + ///////////////////////////////////////////////////////////////// + const Settings& getSettings() const; + + //////////////////////////////////////////////////////////////// + /// Method used to return CHIP_ID. + uint8_t chipID(); + + //////////////////////////////////////////////////////////////// + /// Method used to return ChipModel. + ChipModel chipModel(); + +protected: + +/*****************************************************************/ +/* CONSTRUCTOR INIT FUNCTIONS */ +/*****************************************************************/ + + ////////////////////////////////////////////////////////////////// + /// Write configuration to BME280, return true if successful. + virtual bool Initialize(); + + +/*****************************************************************/ +/* ACCESS FUNCTIONS */ +/*****************************************************************/ + +//uint8_t& getMode(); +//uint8_t* getDig(); +//uint8_t& getControlHumidity(); +//uint8_t& getControlMeasure(); +//uint8_t& getConfig(); + + +private: + +/*****************************************************************/ +/* CONSTANTS */ +/*****************************************************************/ + + static const uint8_t CTRL_HUM_ADDR = 0xF2; + static const uint8_t CTRL_MEAS_ADDR = 0xF4; + static const uint8_t CONFIG_ADDR = 0xF5; + static const uint8_t PRESS_ADDR = 0xF7; + static const uint8_t TEMP_ADDR = 0xFA; + static const uint8_t HUM_ADDR = 0xFD; + static const uint8_t TEMP_DIG_ADDR = 0x88; + static const uint8_t PRESS_DIG_ADDR = 0x8E; + static const uint8_t HUM_DIG_ADDR1 = 0xA1; + static const uint8_t HUM_DIG_ADDR2 = 0xE1; + static const uint8_t ID_ADDR = 0xD0; + + static const uint8_t TEMP_DIG_LENGTH = 6; + static const uint8_t PRESS_DIG_LENGTH = 18; + static const uint8_t HUM_DIG_ADDR1_LENGTH = 1; + static const uint8_t HUM_DIG_ADDR2_LENGTH = 7; + static const uint8_t DIG_LENGTH = 32; + static const uint8_t SENSOR_DATA_LENGTH = 8; + +/*****************************************************************/ +/* VARIABLES */ +/*****************************************************************/ + Settings m_settings; + + uint8_t m_dig[32]; + uint8_t m_chip_id; + ChipModel m_chip_model; + + + bool m_initialized; + +/*****************************************************************/ +/* ABSTRACT FUNCTIONS */ +/*****************************************************************/ + + ///////////////////////////////////////////////////////////////// + /// Write values to BME280 registers. + virtual bool WriteRegister( + uint8_t addr, + uint8_t data)=0; + + ///////////////////////////////////////////////////////////////// + /// Read values from BME280 registers. + virtual bool ReadRegister( + uint8_t addr, + uint8_t data[], + uint8_t length)=0; + + + +/*****************************************************************/ +/* WORKER FUNCTIONS */ +/*****************************************************************/ + + ///////////////////////////////////////////////////////////////// + /// Calculates registers based on settings. + void CalculateRegisters( + uint8_t& ctrlHum, + uint8_t& ctrlMeas, + uint8_t& config); + + ///////////////////////////////////////////////////////////////// + /// Write the settings to the chip. + bool WriteSettings(); + + + ///////////////////////////////////////////////////////////////// + /// Read the the chip id data from the BME280, return true if + /// successful and the id matches a known value. + bool ReadChipID(); + + ///////////////////////////////////////////////////////////////// + /// Read the the trim data from the BME280, return true if + /// successful. + bool ReadTrim(); + + ///////////////////////////////////////////////////////////////// + /// Read the raw data from the BME280 into an array and return + /// true if successful. + bool ReadData( + int32_t data[8]); + + ///////////////////////////////////////////////////////////////// + /// Calculate the temperature from the BME280 raw data and + /// BME280 trim, return a float. + float CalculateTemperature( + int32_t raw, + int32_t& t_fine, + TempUnit unit = TempUnit_Celsius); + + ///////////////////////////////////////////////////////////////// + /// Calculate the humidity from the BME280 raw data and BME280 + /// trim, return a float. + float CalculateHumidity( + int32_t raw, + int32_t t_fine); + + ///////////////////////////////////////////////////////////////// + /// Calculate the pressure from the BME280 raw data and BME280 + /// trim, return a float. + float CalculatePressure( + int32_t raw, + int32_t t_fine, + PresUnit unit = PresUnit_Pa); + + + +}; + +#endif // TG_BME_280_H diff --git a/libraries/BME280-2.3.0/src/BME280I2C.cpp b/libraries/BME280-2.3.0/src/BME280I2C.cpp new file mode 100644 index 00000000000..19e21ecea81 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280I2C.cpp @@ -0,0 +1,84 @@ +/* +BME280I2CI2C.cpp +This code records data from the BME280I2C sensor and provides an API. +This file is part of the Arduino BME280I2C library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 30 2015. +Last Updated: Jan 1 2016. - Happy New year! + +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the BME280I2C environmental sensor, +calibration code based on algorithms providedBosch, some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include + +#include "BME280I2C.h" + + +/****************************************************************/ +BME280I2C::BME280I2C +( + const Settings& settings +):BME280(settings), + m_bme_280_addr(settings.bme280Addr) +{ +} + + +/****************************************************************/ +bool BME280I2C::WriteRegister +( + uint8_t addr, + uint8_t data +) +{ + Wire.beginTransmission(m_bme_280_addr); + Wire.write(addr); + Wire.write(data); + Wire.endTransmission(); + + return true; // TODO: Chech return values from wire calls. +} + + +/****************************************************************/ +bool BME280I2C::ReadRegister +( + uint8_t addr, + uint8_t data[], + uint8_t length +) +{ + uint8_t ord(0); + + Wire.beginTransmission(m_bme_280_addr); + Wire.write(addr); + Wire.endTransmission(); + + Wire.requestFrom(m_bme_280_addr, length); + + while(Wire.available()) + { + data[ord++] = Wire.read(); + } + + return ord == length; +} diff --git a/libraries/BME280-2.3.0/src/BME280I2C.h b/libraries/BME280-2.3.0/src/BME280I2C.h new file mode 100644 index 00000000000..59b78fe692d --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280I2C.h @@ -0,0 +1,86 @@ +/* +BME280I2C.h +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Sep 19 2016. +Last Updated: Oct 07 2017. + +This code is licensed under the GNU LGPL and is open for ditrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the Bme280 environmental sensor. + */ + +#ifndef TG_BME_280_I2C_H +#define TG_BME_280_I2C_H + +#include "BME280.h" + +////////////////////////////////////////////////////////////////// +/// BME280I2C - I2C Implementation of BME280. +class BME280I2C: public BME280 +{ + +public: + + struct Settings : public BME280::Settings + { + Settings( + OSR _tosr = OSR_X1, + OSR _hosr = OSR_X1, + OSR _posr = OSR_X1, + Mode _mode = Mode_Forced, + StandbyTime _st = StandbyTime_1000ms, + Filter _filter = Filter_Off, + SpiEnable _se = SpiEnable_False, + uint8_t _addr = 0x76 + ): BME280::Settings(_tosr, _hosr, _posr, _mode, _st, _filter, _se), + bme280Addr(_addr) {} + + uint8_t bme280Addr; + }; + + /////////////////////////////////////////////////////////////// + /// Constructor used to create the class. All parameters have + /// default values. + BME280I2C( + const Settings& settings = Settings()); + + +protected: + +private: + + uint8_t m_bme_280_addr; + + ////////////////////////////////////////////////////////////////// + /// Write values to BME280 registers. + virtual bool WriteRegister( + uint8_t addr, + uint8_t data); + + ///////////////////////////////////////////////////////////////// + /// Read values from BME280 registers. + virtual bool ReadRegister( + uint8_t addr, + uint8_t data[], + uint8_t length); + +}; +#endif // TG_BME_280_I2C_H diff --git a/libraries/BME280-2.3.0/src/BME280I2C_BRZO.cpp b/libraries/BME280-2.3.0/src/BME280I2C_BRZO.cpp new file mode 100644 index 00000000000..6e125866598 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280I2C_BRZO.cpp @@ -0,0 +1,81 @@ +/* +BME280I2C_BRZO.cpp +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. + +Copyright (C) 2016 Tyler Glenn +Forked by Alex Shavlovsky +to support https://github.com/pasko-zh/brzo_i2c library on ESP8266. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the BME280I2C_BRZO environmental sensor, +calibration code based on algorithms providedBosch, some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include "BME280I2C_BRZO.h" + +#ifdef USING_BRZO + +#include "brzo_i2c.h" + +/****************************************************************/ +BME280I2C_BRZO::BME280I2C_BRZO +( + const Settings& settings +):BME280I2C(settings), + m_bme_280_addr(settings.bme280Addr), + m_i2c_clock_rate(settings.i2cClockRate) +{ +} + + +/****************************************************************/ +bool BME280I2C_BRZO::WriteRegister +( + uint8_t addr, + uint8_t data +) +{ + uint8_t bf[2]; + bf[0] = addr; + bf[1] = data; + brzo_i2c_start_transaction(m_bme_280_addr, m_i2c_clock_rate); + brzo_i2c_write(bf, 2, false); + return (brzo_i2c_end_transaction()==0); +} + +/****************************************************************/ +bool BME280I2C_BRZO::ReadRegister +( + uint8_t addr, + uint8_t data[], + uint8_t length +) +{ + brzo_i2c_start_transaction(m_bme_280_addr, m_i2c_clock_rate); + brzo_i2c_write(&addr, 1, true); + brzo_i2c_read(data, length, false); + brzo_i2c_end_transaction(); + return (brzo_i2c_end_transaction()==0); +} + +#endif diff --git a/libraries/BME280-2.3.0/src/BME280I2C_BRZO.h b/libraries/BME280-2.3.0/src/BME280I2C_BRZO.h new file mode 100644 index 00000000000..86f1c0d5057 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280I2C_BRZO.h @@ -0,0 +1,90 @@ +/* +BME280I2C_BRZO.h +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. + +Copyright (C) 2016 Tyler Glenn +Forked by Alex Shavlovsky +to support https://github.com/pasko-zh/brzo_i2c library on ESP8266. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Sep 19 2016. +Last Updated: Oct 07 2017. + +This code is licensed under the GNU LGPL and is open for ditrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the Bme280 environmental sensor. + */ + +#ifndef BME280I2C_BRZO_H +#define BME280I2C_BRZO_H + +#include "BME280I2C.h" + +////////////////////////////////////////////////////////////////// +/// BME280I2C_BRZO - I2C Implementation of BME280. +class BME280I2C_BRZO : public BME280I2C +{ + +public: + + struct Settings : public BME280I2C::Settings + { + Settings( + OSR _tosr = OSR_X1, + OSR _hosr = OSR_X1, + OSR _posr = OSR_X1, + Mode _mode = Mode_Forced, + StandbyTime _st = StandbyTime_1000ms, + Filter _filter = Filter_Off, + SpiEnable _se = SpiEnable_False, + uint16_t _cr = 400 + ): BME280I2C::Settings(_tosr, _hosr, _posr, _mode, _st, _filter, _se), + i2cClockRate(_cr) {} + + uint16_t i2cClockRate; + }; + + /////////////////////////////////////////////////////////////// + /// Constructor used to create the class. All parameters have + /// default values. + BME280I2C_BRZO( + const Settings& settings = Settings()); + + +protected: + +private: + + uint8_t m_bme_280_addr; + uint16_t m_i2c_clock_rate; + + ////////////////////////////////////////////////////////////////// + /// Write values to BME280 registers. + virtual bool WriteRegister( + uint8_t addr, + uint8_t data); + + ///////////////////////////////////////////////////////////////// + /// Read values from BME280 registers. + virtual bool ReadRegister( + uint8_t addr, + uint8_t data[], + uint8_t length); + +}; +#endif // BME280I2C_BRZO_H diff --git a/libraries/BME280-2.3.0/src/BME280Spi.cpp b/libraries/BME280-2.3.0/src/BME280Spi.cpp new file mode 100644 index 00000000000..3a0401549c2 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280Spi.cpp @@ -0,0 +1,118 @@ +/* +BME280Spi.cpp +This code records data from the BME280Spi sensor and provides an API. +This file is part of the Arduino BME280Spi library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 18 2016. - Happy Holidays! +Last Updated: Oct 07 2017. + +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the BME280Spi environmental sensor, +calibration code based on algorithms providedBosch, some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include "Arduino.h" +#include "BME280Spi.h" + +#include + +/****************************************************************/ +BME280Spi::BME280Spi +( + const Settings& settings +) +:BME280(settings), + csPin(settings.spiCsPin) +{ +} + + +/****************************************************************/ +bool BME280Spi::Initialize() +{ + pinMode(csPin, OUTPUT); + digitalWrite(csPin, HIGH); + + return BME280::Initialize(); +} + + +/****************************************************************/ +bool BME280Spi::ReadRegister +( + uint8_t addr, + uint8_t data[], + uint8_t len +) +{ + SPI.beginTransaction(SPISettings(500000,MSBFIRST,SPI_MODE0)); + + // bme280 uses the msb to select read and write + // combine the addr with the read/write bit + uint8_t readAddr = addr | BME280_SPI_READ; + + //select the device + digitalWrite(csPin, LOW); + // transfer the addr + SPI.transfer(readAddr); + + // read the data + for(int i = 0; i < len; ++i) + { + // transfer 0x00 to get the data + data[i] = SPI.transfer(0); + } + + // de-select the device + digitalWrite(csPin, HIGH); + + SPI.endTransaction(); + + return true; +} + + +/****************************************************************/ +bool BME280Spi::WriteRegister +( + uint8_t addr, + uint8_t data +) +{ + SPI.beginTransaction(SPISettings(500000,MSBFIRST,SPI_MODE0)); + + // bme280 uses the msb to select read and write + // combine the addr with the read/write bit + uint8_t writeAddr = addr & ~0x80; + + // select the device + digitalWrite(csPin, LOW); + + // transfer the addr and then the data to spi device + SPI.transfer(writeAddr); + SPI.transfer(data); + + // de-select the device + digitalWrite(csPin, HIGH); + + SPI.endTransaction(); + + return true; +} diff --git a/libraries/BME280-2.3.0/src/BME280Spi.h b/libraries/BME280-2.3.0/src/BME280Spi.h new file mode 100644 index 00000000000..73e25706d45 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280Spi.h @@ -0,0 +1,92 @@ +/* +BME280Spi.h +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 18 2016. - Happy Holidays! +Last Updated: Oct 07 2017. + +This code is licensed under the GNU LGPL and is open for ditrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the Bme280 environmental sensor. + */ + +#ifndef TG_BME_280_SPI_H +#define TG_BME_280_SPI_H + +#include "BME280.h" + + +class BME280Spi: public BME280 +{ +public: + + struct Settings : public BME280::Settings + { + Settings( + uint8_t _cspin, + OSR _tosr = OSR_X1, + OSR _hosr = OSR_X1, + OSR _posr = OSR_X1, + Mode _mode = Mode_Forced, + StandbyTime _st = StandbyTime_1000ms, + Filter _filter = Filter_Off, + SpiEnable _se = SpiEnable_False + ): BME280::Settings(_tosr, _hosr, _posr, _mode, _st, _filter, _se), + spiCsPin(_cspin) {} + + uint8_t spiCsPin; + }; + + //////////////////////////////////////////////////////////////// + /// Constructor used to create the class. All parameters have + /// default values. + BME280Spi( + const Settings& settings); + +protected: + + //////////////////////////////////////////////////////////////// + /// Method used at start up to initialize the class. Starts the + /// I2C interface. + virtual bool Initialize(); + +private: + + static const uint8_t BME280_SPI_WRITE = 0x7F; + static const uint8_t BME280_SPI_READ = 0x80; + + uint8_t csPin; + + //////////////////////////////////////////////////////////////// + /// Read the data from the BME280 addr into an array and + /// return true if successful. + virtual bool ReadRegister( + uint8_t addr, + uint8_t array[], + uint8_t len); + + //////////////////////////////////////////////////////////////// + /// Write values to BME280 registers. + virtual bool WriteRegister( + uint8_t addr, + uint8_t data); + +}; +#endif // TG_BME_280_SPI_H diff --git a/libraries/BME280-2.3.0/src/BME280SpiSw.cpp b/libraries/BME280-2.3.0/src/BME280SpiSw.cpp new file mode 100644 index 00000000000..0af873c01d0 --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280SpiSw.cpp @@ -0,0 +1,137 @@ +/* +BME280SpiSw.cpp +This code records data from the BME280SpiSw sensor and provides an API. +This file is part of the Arduino BME280SpiSw library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 18 2016. - Happy Holidays! +Last Updated: Dec 18 2016. - Happy Holidays! + +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the BME280SpiSw environmental sensor, +calibration code based on algorithms providedBosch, some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include "Arduino.h" +#include "BME280SpiSw.h" + + + +/****************************************************************/ +BME280SpiSw::BME280SpiSw +( + const Settings& settings +) +:BME280(settings), + csPin(settings.spiCsPin), + mosiPin(settings.spiMosiPin), + misoPin(settings.spiMisoPin), + sckPin(settings.spiSckPin) +{ +} + + +/****************************************************************/ +bool BME280SpiSw::Initialize(){ + + digitalWrite(csPin, HIGH); + pinMode(csPin, OUTPUT); + + pinMode(sckPin, OUTPUT); + pinMode(mosiPin, OUTPUT); + pinMode(misoPin, INPUT); + + return BME280::Initialize(); +} + + +/****************************************************************/ +uint8_t BME280SpiSw::SpiTransferSw +( + uint8_t data +) +{ + uint8_t resp = 0; + for (int bit = 7; bit >= 0; --bit) { + resp <<= 1; + digitalWrite(sckPin, LOW); + digitalWrite(mosiPin, data & (1 << bit)); + digitalWrite(sckPin, HIGH); + resp |= digitalRead(misoPin); + } + return resp; +} + + +/****************************************************************/ +bool BME280SpiSw::ReadRegister +( + uint8_t addr, + uint8_t data[], + uint8_t length +) +{ + + // bme280 uses the msb to select read and write + // combine the addr with the read/write bit + uint8_t readAddr = addr | BME280_SPI_READ; + + //select the device + digitalWrite(csPin, LOW); + // transfer the addr + SpiTransferSw(readAddr); + + // read the data + for(int i = 0; i < length; ++i) + { + // transfer 0x00 to get the data + data[i] = SpiTransferSw(0); + } + + // de-select the device + digitalWrite(csPin, HIGH); + + return true; +} + + +/****************************************************************/ +bool BME280SpiSw::WriteRegister +( + uint8_t addr, + uint8_t data +) +{ + // bme280 uses the msb to select read and write + // combine the addr with the read/write bit + uint8_t writeAddr = addr & ~0x80; + + // select the device + digitalWrite(csPin, LOW); + + // transfer the addr and then the data to spi device + SpiTransferSw(writeAddr); + SpiTransferSw(data); + + // de-select the device + digitalWrite(csPin, HIGH); + +return true; +} + diff --git a/libraries/BME280-2.3.0/src/BME280SpiSw.h b/libraries/BME280-2.3.0/src/BME280SpiSw.h new file mode 100644 index 00000000000..75ab12597bc --- /dev/null +++ b/libraries/BME280-2.3.0/src/BME280SpiSw.h @@ -0,0 +1,106 @@ +/* +BME280SpiSw.h +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 18 2016. - Happy Holidays! +Last Updated: Oct 07 2017. + +This code is licensed under the GNU LGPL and is open for ditrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the Bme280 environmental sensor. + */ + +#ifndef TG_BME_280_SPI_H +#define TG_BME_280_SPI_H + +#include "BME280.h" + +class BME280SpiSw: public BME280{ + + public: + struct Settings : public BME280::Settings + { + Settings( + uint8_t _cs, + uint8_t _mosi, + uint8_t _miso, + uint8_t _sck, + OSR _tosr = OSR_X1, + OSR _hosr = OSR_X1, + OSR _posr = OSR_X1, + Mode _mode = Mode_Forced, + StandbyTime _st = StandbyTime_1000ms, + Filter _filter = Filter_Off, + SpiEnable _se = SpiEnable_False + ): BME280::Settings(_tosr, _hosr, _posr, _mode, _st, _filter, _se), + spiCsPin(_cs), + spiMosiPin(_mosi), + spiMisoPin(_miso), + spiSckPin(_sck) {} + + uint8_t spiCsPin; + uint8_t spiMosiPin; + uint8_t spiMisoPin; + uint8_t spiSckPin; + }; + + //////////////////////////////////////////////////////////////// + /// Constructor for software spi + BME280SpiSw( + const Settings& settings); + +protected: + + //////////////////////////////////////////////////////////////// + /// Method used at start up to initialize the class. Starts the I2C interface. + virtual bool Initialize(); + +private: + + static const uint8_t BME280_SPI_WRITE = 0x7F; + static const uint8_t BME280_SPI_READ = 0x80; + + // TODO: Move to settings object. + uint8_t csPin; + int8_t mosiPin; + int8_t misoPin; + int8_t sckPin; + + //////////////////////////////////////////////////////////////// + /// Does a sw spi transfer. + uint8_t SpiTransferSw( + uint8_t data); + + //////////////////////////////////////////////////////////////// + /// Read the data from the BME280 addr into an array and return + /// true if successful. + virtual bool ReadRegister( + uint8_t addr, + uint8_t data[], + uint8_t length); + + //////////////////////////////////////////////////////////////// + /// Write values to BME280 registers. + virtual bool WriteRegister( + uint8_t addr, + uint8_t data); + +}; +#endif // TG_BME_280_SPI_H diff --git a/libraries/BME280-2.3.0/src/EnvironmentCalculations.cpp b/libraries/BME280-2.3.0/src/EnvironmentCalculations.cpp new file mode 100644 index 00000000000..837eaf4ffdc --- /dev/null +++ b/libraries/BME280-2.3.0/src/EnvironmentCalculations.cpp @@ -0,0 +1,101 @@ +/* +EnvironmentCalculations.cpp + +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 30 2015. +Last Updated: Jan 1 2016. - Happy New year! + +This header must be included in any derived code or copies of the code. + +Some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include + +#include "EnvironmentCalculations.h" + + +/****************************************************************/ +float EnvironmentCalculations::Altitude +( + float pressure, + bool metric, + float seaLevelPressure +) +{ + // Equations courtesy of NOAA; + float altitude = NAN; + if (!isnan(pressure) && !isnan(seaLevelPressure)){ + altitude = 1000.0 * ( seaLevelPressure - pressure ) / 3386.3752577878; + } + return metric ? altitude * 0.3048 : altitude; +} + + +/****************************************************************/ +float EnvironmentCalculations::SealevelAlitude +( + float A, + float T, + float P +) +{ + return(P / pow(1-((0.0065 *A) / (T + (0.0065 *A) + 273.15)),5.257)); +} + + +/****************************************************************/ +float EnvironmentCalculations::EquivalentSeaLevelPressure +( + float altitude, + float temp, + float pres +) +{ + return(pres / pow(1-((0.0065 *altitude) / (temp + (0.0065 *altitude) + 273.15)),5.257)); +} + + +/****************************************************************/ +float EnvironmentCalculations::DewPoint +( + float temp, + float hum, + bool metric +) +{ + // Equations courtesy of Brian McNoldy from http://andrew.rsmas.miami.edu; + float dewPoint = NAN; + + if (metric && !isnan(temp) && !isnan(hum)) + { + dewPoint = 243.04 * (log(hum/100.0) + ((17.625 * temp)/(243.04 + temp))) + /(17.625 - log(hum/100.0) - ((17.625 * temp)/(243.04 + temp))); + } + else if (!isnan(temp) && !isnan(hum)) + { + float ctemp = (temp - 32.0) * 5.0/9.0; + + dewPoint = 243.04 * (log(hum/100.0) + ((17.625 * ctemp)/(243.04 + ctemp))) + /(17.625 - log(hum/100.0) - ((17.625 * ctemp)/(243.04 + ctemp))); + + dewPoint = dewPoint * 9.0/5.0 + 32.0; + } + return dewPoint; +} diff --git a/libraries/BME280-2.3.0/src/EnvironmentCalculations.h b/libraries/BME280-2.3.0/src/EnvironmentCalculations.h new file mode 100644 index 00000000000..a182da61411 --- /dev/null +++ b/libraries/BME280-2.3.0/src/EnvironmentCalculations.h @@ -0,0 +1,86 @@ +/* + +BME280.h + +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Oct 7 2017. +Last Updated: Oct 7 2017. +This code is licensed under the GNU LGPL and is open for ditrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + + */ + +#ifndef TG_ENVIRONMENT_CALCULATIONS_H +#define TG_ENVIRONMENT_CALCULATIONS_H + +namespace EnvironmentCalculations +{ + enum TempUnit + { + TempUnit_Celsius, + TempUnit_Fahrenheit + }; + + enum AltitudeUnit + { + AltitudeUnit_Meters, + AltitudeUnit_Feet + }; + + ///////////////////////////////////////////////////////////////// + /// Calculate the altitude based on the pressure with the + /// specified units. + float Altitude( + float pressure, + bool metric = true, + float seaLevelPressure = 101325); // Pressure given in Pa. + + ///////////////////////////////////////////////////////////////// + /// Convert current pressure to sea-level pressure, returns + /// Altitude (in meters), temperature in Celsius + /// return the equivalent pressure at sea level. + /// @deprecated + float SealevelAlitude( + float alitude, + float temp, + float pres); // A: current altitude (meters). + + ///////////////////////////////////////////////////////////////// + /// Convert current pressure to equivalent sea-level pressure. + /// @param altitude in meters. + /// @param temp in Celsius. + /// @return the equivalent pressure at sea level. + float EquivalentSeaLevelPressure( + float altitude, + float temp, + float pres); + + + ///////////////////////////////////////////////////////////////// + /// Calculate the dew point based on the temperature and + /// humidity with the specified units. + float DewPoint( + float temp, + float hum, + bool metric = true); + +} + +#endif // TG_ENVIRONMENT_CALCULATIONS_H From 7a4e52401feec9af17d1b2720388d54c8aa98292 Mon Sep 17 00:00:00 2001 From: HendrikVE Date: Tue, 12 Jun 2018 11:28:49 +0200 Subject: [PATCH 3/9] fixed compiler error in bme library --- libraries/BME280-2.3.0/src/BME280.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/BME280-2.3.0/src/BME280.cpp b/libraries/BME280-2.3.0/src/BME280.cpp index 8190ae1a78d..f77f4890a11 100644 --- a/libraries/BME280-2.3.0/src/BME280.cpp +++ b/libraries/BME280-2.3.0/src/BME280.cpp @@ -95,6 +95,8 @@ bool BME280::WriteSettings() WriteRegister(CTRL_HUM_ADDR, ctrlHum); WriteRegister(CTRL_MEAS_ADDR, ctrlMeas); WriteRegister(CONFIG_ADDR, config); + + return true; } From 9879937858997bd7de43cec119ce16adeb6b30d0 Mon Sep 17 00:00:00 2001 From: HendrikVE Date: Wed, 11 Jul 2018 23:06:28 +0200 Subject: [PATCH 4/9] added adafruit libraries --- .../.github/ISSUE_TEMPLATE.md | 46 + .../.github/PULL_REQUEST_TEMPLATE.md | 26 + .../Adafruit_BME280.cpp | 530 ++++ .../Adafruit_BME280.h | 300 ++ .../Adafruit_BME280_Library-1.0.7/README.md | 59 + .../advancedsettings/advancedsettings.ino | 157 ++ .../examples/bme280test/bme280test.ino | 82 + .../library.properties | 9 + libraries/Adafruit_BME680-1.0.5/.gitignore | 1 + .../Adafruit_BME680-1.0.5/Adafruit_BME680.cpp | 605 ++++ .../Adafruit_BME680-1.0.5/Adafruit_BME680.h | 107 + libraries/Adafruit_BME680-1.0.5/Doxyfile | 2492 +++++++++++++++++ libraries/Adafruit_BME680-1.0.5/README.md | 12 + libraries/Adafruit_BME680-1.0.5/bme680.c | 1150 ++++++++ libraries/Adafruit_BME680-1.0.5/bme680.h | 225 ++ libraries/Adafruit_BME680-1.0.5/bme680_defs.h | 512 ++++ .../docs/_adafruit___b_m_e680_8h_source.html | 91 + .../Adafruit_BME680-1.0.5/docs/annotated.html | 78 + libraries/Adafruit_BME680-1.0.5/docs/bc_s.png | Bin 0 -> 676 bytes libraries/Adafruit_BME680-1.0.5/docs/bdwn.png | Bin 0 -> 147 bytes .../Adafruit_BME680-1.0.5/docs/bme680_8c.html | 157 ++ .../Adafruit_BME680-1.0.5/docs/bme680_8h.html | 129 + .../docs/bme680_8h_source.html | 87 + .../docs/bme680__defs_8h.html | 1160 ++++++++ .../docs/bme680__defs_8h_source.html | 135 + .../class_adafruit___b_m_e680-members.html | 94 + .../docs/class_adafruit___b_m_e680.html | 537 ++++ .../Adafruit_BME680-1.0.5/docs/classes.html | 82 + .../Adafruit_BME680-1.0.5/docs/closed.png | Bin 0 -> 132 bytes libraries/Adafruit_BME680-1.0.5/docs/doc.png | Bin 0 -> 746 bytes .../Adafruit_BME680-1.0.5/docs/doxygen.css | 1596 +++++++++++ .../Adafruit_BME680-1.0.5/docs/doxygen.png | Bin 0 -> 3779 bytes .../Adafruit_BME680-1.0.5/docs/dynsections.js | 97 + .../Adafruit_BME680-1.0.5/docs/files.html | 78 + .../docs/folderclosed.png | Bin 0 -> 616 bytes .../Adafruit_BME680-1.0.5/docs/folderopen.png | Bin 0 -> 597 bytes .../Adafruit_BME680-1.0.5/docs/functions.html | 122 + .../docs/functions_func.html | 110 + .../docs/functions_vars.html | 83 + .../Adafruit_BME680-1.0.5/docs/globals.html | 188 ++ .../docs/globals_defs.html | 172 ++ .../docs/globals_enum.html | 74 + .../docs/globals_eval.html | 77 + .../docs/globals_func.html | 115 + .../docs/globals_type.html | 74 + .../docs/globals_vars.html | 77 + .../docs/group___b_m_e680.html | 73 + .../Adafruit_BME680-1.0.5/docs/index.html | 73 + .../Adafruit_BME680-1.0.5/docs/jquery.js | 87 + .../docs/md__r_e_a_d_m_e.html | 80 + libraries/Adafruit_BME680-1.0.5/docs/menu.js | 26 + .../Adafruit_BME680-1.0.5/docs/menudata.js | 12 + .../Adafruit_BME680-1.0.5/docs/modules.html | 78 + .../Adafruit_BME680-1.0.5/docs/nav_f.png | Bin 0 -> 153 bytes .../Adafruit_BME680-1.0.5/docs/nav_g.png | Bin 0 -> 95 bytes .../Adafruit_BME680-1.0.5/docs/nav_h.png | Bin 0 -> 98 bytes libraries/Adafruit_BME680-1.0.5/docs/open.png | Bin 0 -> 123 bytes .../Adafruit_BME680-1.0.5/docs/pages.html | 78 + .../docs/search/all_0.html | 26 + .../docs/search/all_0.js | 4 + .../docs/search/all_1.html | 26 + .../docs/search/all_1.js | 4 + .../docs/search/all_10.html | 26 + .../docs/search/all_10.js | 4 + .../docs/search/all_2.html | 26 + .../docs/search/all_2.js | 4 + .../docs/search/all_3.html | 26 + .../docs/search/all_3.js | 4 + .../docs/search/all_4.html | 26 + .../docs/search/all_4.js | 5 + .../docs/search/all_5.html | 26 + .../docs/search/all_5.js | 9 + .../docs/search/all_6.html | 26 + .../docs/search/all_6.js | 8 + .../docs/search/all_7.html | 26 + .../docs/search/all_7.js | 4 + .../docs/search/all_8.html | 26 + .../docs/search/all_8.js | 5 + .../docs/search/all_9.html | 26 + .../docs/search/all_9.js | 5 + .../docs/search/all_a.html | 26 + .../docs/search/all_a.js | 6 + .../docs/search/all_b.html | 26 + .../docs/search/all_b.js | 28 + .../docs/search/all_c.html | 26 + .../docs/search/all_c.js | 10 + .../docs/search/all_d.html | 26 + .../docs/search/all_d.js | 8 + .../docs/search/all_e.html | 26 + .../docs/search/all_e.js | 6 + .../docs/search/all_f.html | 26 + .../docs/search/all_f.js | 4 + .../docs/search/classes_0.html | 26 + .../docs/search/classes_0.js | 4 + .../docs/search/classes_1.html | 26 + .../docs/search/classes_1.js | 8 + .../docs/search/close.png | Bin 0 -> 273 bytes .../docs/search/defines_0.html | 26 + .../docs/search/defines_0.js | 36 + .../docs/search/enums_0.html | 26 + .../docs/search/enums_0.js | 4 + .../docs/search/enumvalues_0.html | 26 + .../docs/search/enumvalues_0.js | 5 + .../docs/search/files_0.html | 26 + .../docs/search/files_0.js | 4 + .../docs/search/functions_0.html | 26 + .../docs/search/functions_0.js | 4 + .../docs/search/functions_1.html | 26 + .../docs/search/functions_1.js | 4 + .../docs/search/functions_2.html | 26 + .../docs/search/functions_2.js | 4 + .../docs/search/functions_3.html | 26 + .../docs/search/functions_3.js | 8 + .../docs/search/functions_4.html | 26 + .../docs/search/functions_4.js | 8 + .../docs/search/groups_0.html | 26 + .../docs/search/groups_0.js | 4 + .../docs/search/mag_sel.png | Bin 0 -> 563 bytes .../docs/search/nomatches.html | 12 + .../docs/search/pages_0.html | 26 + .../docs/search/pages_0.js | 4 + .../docs/search/search.css | 271 ++ .../docs/search/search.js | 791 ++++++ .../docs/search/search_l.png | Bin 0 -> 604 bytes .../docs/search/search_m.png | Bin 0 -> 158 bytes .../docs/search/search_r.png | Bin 0 -> 612 bytes .../docs/search/searchdata.js | 27 + .../docs/search/typedefs_0.html | 26 + .../docs/search/typedefs_0.js | 4 + .../docs/search/variables_0.html | 26 + .../docs/search/variables_0.js | 4 + .../docs/search/variables_1.html | 26 + .../docs/search/variables_1.js | 4 + .../docs/search/variables_2.html | 26 + .../docs/search/variables_2.js | 4 + .../docs/search/variables_3.html | 26 + .../docs/search/variables_3.js | 4 + .../docs/search/variables_4.html | 26 + .../docs/search/variables_4.js | 6 + .../docs/search/variables_5.html | 26 + .../docs/search/variables_5.js | 7 + .../docs/search/variables_6.html | 26 + .../docs/search/variables_6.js | 5 + .../docs/search/variables_7.html | 26 + .../docs/search/variables_7.js | 5 + .../docs/search/variables_8.html | 26 + .../docs/search/variables_8.js | 5 + .../docs/search/variables_9.html | 26 + .../docs/search/variables_9.js | 6 + .../docs/search/variables_a.html | 26 + .../docs/search/variables_a.js | 28 + .../docs/search/variables_b.html | 26 + .../docs/search/variables_b.js | 8 + .../docs/search/variables_c.html | 26 + .../docs/search/variables_c.js | 4 + .../docs/search/variables_d.html | 26 + .../docs/search/variables_d.js | 6 + .../docs/search/variables_e.html | 26 + .../docs/search/variables_e.js | 4 + .../docs/search/variables_f.html | 26 + .../docs/search/variables_f.js | 4 + .../Adafruit_BME680-1.0.5/docs/splitbar.png | Bin 0 -> 314 bytes .../structbme680__calib__data-members.html | 103 + .../docs/structbme680__calib__data.html | 550 ++++ .../docs/structbme680__dev-members.html | 91 + .../docs/structbme680__dev.html | 346 +++ .../structbme680__field__data-members.html | 83 + .../docs/structbme680__field__data.html | 210 ++ .../docs/structbme680__gas__sett-members.html | 81 + .../docs/structbme680__gas__sett.html | 176 ++ .../docs/structbme680__tph__sett-members.html | 80 + .../docs/structbme680__tph__sett.html | 159 ++ .../Adafruit_BME680-1.0.5/docs/sync_off.png | Bin 0 -> 853 bytes .../Adafruit_BME680-1.0.5/docs/sync_on.png | Bin 0 -> 845 bytes .../Adafruit_BME680-1.0.5/docs/tab_a.png | Bin 0 -> 142 bytes .../Adafruit_BME680-1.0.5/docs/tab_b.png | Bin 0 -> 169 bytes .../Adafruit_BME680-1.0.5/docs/tab_h.png | Bin 0 -> 177 bytes .../Adafruit_BME680-1.0.5/docs/tab_s.png | Bin 0 -> 184 bytes libraries/Adafruit_BME680-1.0.5/docs/tabs.css | 1 + .../examples/bme680oled/bme680oled.ino | 88 + .../examples/bme680test/bme680test.ino | 79 + .../Adafruit_BME680-1.0.5/library.properties | 9 + .../Adafruit_Sensor-1.0.2/Adafruit_Sensor.h | 154 + libraries/Adafruit_Sensor-1.0.2/README.md | 218 ++ .../Adafruit_Sensor-1.0.2/library.properties | 9 + 185 files changed, 17316 insertions(+) create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/.github/ISSUE_TEMPLATE.md create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/.github/PULL_REQUEST_TEMPLATE.md create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.cpp create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.h create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/README.md create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/examples/advancedsettings/advancedsettings.ino create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/examples/bme280test/bme280test.ino create mode 100644 libraries/Adafruit_BME280_Library-1.0.7/library.properties create mode 100644 libraries/Adafruit_BME680-1.0.5/.gitignore create mode 100644 libraries/Adafruit_BME680-1.0.5/Adafruit_BME680.cpp create mode 100644 libraries/Adafruit_BME680-1.0.5/Adafruit_BME680.h create mode 100644 libraries/Adafruit_BME680-1.0.5/Doxyfile create mode 100644 libraries/Adafruit_BME680-1.0.5/README.md create mode 100644 libraries/Adafruit_BME680-1.0.5/bme680.c create mode 100644 libraries/Adafruit_BME680-1.0.5/bme680.h create mode 100644 libraries/Adafruit_BME680-1.0.5/bme680_defs.h create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/_adafruit___b_m_e680_8h_source.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/annotated.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bc_s.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bdwn.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bme680_8c.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bme680_8h.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bme680_8h_source.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bme680__defs_8h.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/bme680__defs_8h_source.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/class_adafruit___b_m_e680-members.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/class_adafruit___b_m_e680.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/classes.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/closed.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/doc.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/doxygen.css create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/doxygen.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/dynsections.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/files.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/folderclosed.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/folderopen.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/functions.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/functions_func.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/functions_vars.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals_defs.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals_enum.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals_eval.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals_func.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals_type.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/globals_vars.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/group___b_m_e680.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/index.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/jquery.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/md__r_e_a_d_m_e.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/menu.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/menudata.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/modules.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/nav_f.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/nav_g.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/nav_h.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/open.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/pages.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_1.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_1.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_10.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_10.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_2.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_2.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_3.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_3.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_4.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_4.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_5.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_5.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_6.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_6.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_7.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_7.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_8.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_8.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_9.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_9.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_a.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_a.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_b.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_b.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_c.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_c.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_d.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_d.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_e.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_e.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_f.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/all_f.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/classes_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/classes_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/classes_1.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/classes_1.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/close.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/defines_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/defines_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/enums_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/enums_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/enumvalues_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/enumvalues_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/files_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/files_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_1.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_1.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_2.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_2.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_3.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_3.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_4.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/functions_4.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/groups_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/groups_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/mag_sel.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/nomatches.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/pages_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/pages_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/search.css create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/search.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/search_l.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/search_m.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/search_r.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/searchdata.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/typedefs_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/typedefs_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_0.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_0.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_1.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_1.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_2.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_2.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_3.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_3.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_4.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_4.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_5.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_5.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_6.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_6.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_7.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_7.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_8.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_8.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_9.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_9.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_a.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_a.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_b.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_b.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_c.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_c.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_d.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_d.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_e.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_e.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_f.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/search/variables_f.js create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/splitbar.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__calib__data-members.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__calib__data.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__dev-members.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__dev.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__field__data-members.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__field__data.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__gas__sett-members.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__gas__sett.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__tph__sett-members.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/structbme680__tph__sett.html create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/sync_off.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/sync_on.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/tab_a.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/tab_b.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/tab_h.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/tab_s.png create mode 100644 libraries/Adafruit_BME680-1.0.5/docs/tabs.css create mode 100644 libraries/Adafruit_BME680-1.0.5/examples/bme680oled/bme680oled.ino create mode 100644 libraries/Adafruit_BME680-1.0.5/examples/bme680test/bme680test.ino create mode 100644 libraries/Adafruit_BME680-1.0.5/library.properties create mode 100644 libraries/Adafruit_Sensor-1.0.2/Adafruit_Sensor.h create mode 100644 libraries/Adafruit_Sensor-1.0.2/README.md create mode 100644 libraries/Adafruit_Sensor-1.0.2/library.properties diff --git a/libraries/Adafruit_BME280_Library-1.0.7/.github/ISSUE_TEMPLATE.md b/libraries/Adafruit_BME280_Library-1.0.7/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000000..f0e26146fa9 --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,46 @@ +Thank you for opening an issue on an Adafruit Arduino library repository. To +improve the speed of resolution please review the following guidelines and +common troubleshooting steps below before creating the issue: + +- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use + the forums at http://forums.adafruit.com to ask questions and troubleshoot why + something isn't working as expected. In many cases the problem is a common issue + that you will more quickly receive help from the forum community. GitHub issues + are meant for known defects in the code. If you don't know if there is a defect + in the code then start with troubleshooting on the forum first. + +- **If following a tutorial or guide be sure you didn't miss a step.** Carefully + check all of the steps and commands to run have been followed. Consult the + forum if you're unsure or have questions about steps in a guide/tutorial. + +- **For Arduino projects check these very common issues to ensure they don't apply**: + + - For uploading sketches or communicating with the board make sure you're using + a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes + very hard to tell the difference between a data and charge cable! Try using the + cable with other devices or swapping to another cable to confirm it is not + the problem. + + - **Be sure you are supplying adequate power to the board.** Check the specs of + your board and plug in an external power supply. In many cases just + plugging a board into your computer is not enough to power it and other + peripherals. + + - **Double check all soldering joints and connections.** Flakey connections + cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. + + - **Ensure you are using an official Arduino or Adafruit board.** We can't + guarantee a clone board will have the same functionality and work as expected + with this code and don't support them. + +If you're sure this issue is a defect in the code and checked the steps above +please fill in the following fields to provide enough troubleshooting information. +You may delete the guideline and text above to just leave the following details: + +- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** + +- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO + VERSION HERE** + +- List the steps to reproduce the problem below (if possible attach a sketch or + copy the sketch code in too): **LIST REPRO STEPS BELOW** diff --git a/libraries/Adafruit_BME280_Library-1.0.7/.github/PULL_REQUEST_TEMPLATE.md b/libraries/Adafruit_BME280_Library-1.0.7/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000000..7b641eb862c --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,26 @@ +Thank you for creating a pull request to contribute to Adafruit's GitHub code! +Before you open the request please review the following guidelines and tips to +help it be more easily integrated: + +- **Describe the scope of your change--i.e. what the change does and what parts + of the code were modified.** This will help us understand any risks of integrating + the code. + +- **Describe any known limitations with your change.** For example if the change + doesn't apply to a supported platform of the library please mention it. + +- **Please run any tests or examples that can exercise your modified code.** We + strive to not break users of the code and running tests/examples helps with this + process. + +Thank you again for contributing! We will try to test and integrate the change +as soon as we can, but be aware we have many GitHub repositories to manage and +can't immediately respond to every request. There is no need to bump or check in +on a pull request (it will clutter the discussion of the request). + +Also don't be worried if the request is closed or not integrated--sometimes the +priorities of Adafruit's GitHub code (education, ease of use) might not match the +priorities of the pull request. Don't fret, the open source community thrives on +forks and GitHub makes it easy to keep your changes in a forked repo. + +After reviewing the guidelines above you can delete this text from the pull request. diff --git a/libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.cpp b/libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.cpp new file mode 100644 index 00000000000..373b5a7c72d --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.cpp @@ -0,0 +1,530 @@ +/*************************************************************************** + This is a library for the BME280 humidity, temperature & pressure sensor + + Designed specifically to work with the Adafruit BME280 Breakout + ----> http://www.adafruit.com/products/2650 + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#include "Arduino.h" +#include +#include +#include "Adafruit_BME280.h" + +/*************************************************************************** + PRIVATE FUNCTIONS + ***************************************************************************/ +Adafruit_BME280::Adafruit_BME280() + : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) +{ } + +Adafruit_BME280::Adafruit_BME280(int8_t cspin) + : _cs(cspin), _mosi(-1), _miso(-1), _sck(-1) +{ } + +Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin) + : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) +{ } + + +/**************************************************************************/ +/*! + @brief Initialise sensor with given parameters / settings +*/ +/**************************************************************************/ +bool Adafruit_BME280::begin(TwoWire *theWire) +{ + _wire = theWire; + _i2caddr = BME280_ADDRESS; + return init(); +} + +bool Adafruit_BME280::begin(uint8_t addr) +{ + _i2caddr = addr; + _wire = &Wire; + return init(); +} + +bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) +{ + _i2caddr = addr; + _wire = theWire; + return init(); +} + +bool Adafruit_BME280::begin(void) +{ + _i2caddr = BME280_ADDRESS; + _wire = &Wire; + return init(); +} + +bool Adafruit_BME280::init() +{ + // init I2C or SPI sensor interface + if (_cs == -1) { + // I2C + _wire -> begin(); + } else { + digitalWrite(_cs, HIGH); + pinMode(_cs, OUTPUT); + if (_sck == -1) { + // hardware SPI + SPI.begin(); + } else { + // software SPI + pinMode(_sck, OUTPUT); + pinMode(_mosi, OUTPUT); + pinMode(_miso, INPUT); + } + } + + // check if sensor, i.e. the chip ID is correct + if (read8(BME280_REGISTER_CHIPID) != 0x60) + return false; + + // reset the device using soft-reset + // this makes sure the IIR is off, etc. + write8(BME280_REGISTER_SOFTRESET, 0xB6); + + // wait for chip to wake up. + delay(300); + + // if chip is still reading calibration, delay + while (isReadingCalibration()) + delay(100); + + readCoefficients(); // read trimming parameters, see DS 4.2.2 + + setSampling(); // use defaults + + delay(100); + + return true; +} + +/**************************************************************************/ +/*! + @brief setup sensor with given parameters / settings + + This is simply a overload to the normal begin()-function, so SPI users + don't get confused about the library requiring an address. +*/ +/**************************************************************************/ + + +void Adafruit_BME280::setSampling(sensor_mode mode, + sensor_sampling tempSampling, + sensor_sampling pressSampling, + sensor_sampling humSampling, + sensor_filter filter, + standby_duration duration) { + _measReg.mode = mode; + _measReg.osrs_t = tempSampling; + _measReg.osrs_p = pressSampling; + + + _humReg.osrs_h = humSampling; + _configReg.filter = filter; + _configReg.t_sb = duration; + + + // you must make sure to also set REGISTER_CONTROL after setting the + // CONTROLHUMID register, otherwise the values won't be applied (see DS 5.4.3) + write8(BME280_REGISTER_CONTROLHUMID, _humReg.get()); + write8(BME280_REGISTER_CONFIG, _configReg.get()); + write8(BME280_REGISTER_CONTROL, _measReg.get()); +} + + +/**************************************************************************/ +/*! + @brief Encapsulate hardware and software SPI transfer into one function +*/ +/**************************************************************************/ +uint8_t Adafruit_BME280::spixfer(uint8_t x) { + // hardware SPI + if (_sck == -1) + return SPI.transfer(x); + + // software SPI + uint8_t reply = 0; + for (int i=7; i>=0; i--) { + reply <<= 1; + digitalWrite(_sck, LOW); + digitalWrite(_mosi, x & (1< beginTransmission((uint8_t)_i2caddr); + _wire -> write((uint8_t)reg); + _wire -> write((uint8_t)value); + _wire -> endTransmission(); + } else { + if (_sck == -1) + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(_cs, LOW); + spixfer(reg & ~0x80); // write, bit 7 low + spixfer(value); + digitalWrite(_cs, HIGH); + if (_sck == -1) + SPI.endTransaction(); // release the SPI bus + } +} + + +/**************************************************************************/ +/*! + @brief Reads an 8 bit value over I2C or SPI +*/ +/**************************************************************************/ +uint8_t Adafruit_BME280::read8(byte reg) { + uint8_t value; + + if (_cs == -1) { + _wire -> beginTransmission((uint8_t)_i2caddr); + _wire -> write((uint8_t)reg); + _wire -> endTransmission(); + _wire -> requestFrom((uint8_t)_i2caddr, (byte)1); + value = _wire -> read(); + } else { + if (_sck == -1) + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(_cs, LOW); + spixfer(reg | 0x80); // read, bit 7 high + value = spixfer(0); + digitalWrite(_cs, HIGH); + if (_sck == -1) + SPI.endTransaction(); // release the SPI bus + } + return value; +} + + +/**************************************************************************/ +/*! + @brief Reads a 16 bit value over I2C or SPI +*/ +/**************************************************************************/ +uint16_t Adafruit_BME280::read16(byte reg) +{ + uint16_t value; + + if (_cs == -1) { + _wire -> beginTransmission((uint8_t)_i2caddr); + _wire -> write((uint8_t)reg); + _wire -> endTransmission(); + _wire -> requestFrom((uint8_t)_i2caddr, (byte)2); + value = (_wire -> read() << 8) | _wire -> read(); + } else { + if (_sck == -1) + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(_cs, LOW); + spixfer(reg | 0x80); // read, bit 7 high + value = (spixfer(0) << 8) | spixfer(0); + digitalWrite(_cs, HIGH); + if (_sck == -1) + SPI.endTransaction(); // release the SPI bus + } + + return value; +} + + +/**************************************************************************/ +/*! + +*/ +/**************************************************************************/ +uint16_t Adafruit_BME280::read16_LE(byte reg) { + uint16_t temp = read16(reg); + return (temp >> 8) | (temp << 8); +} + + +/**************************************************************************/ +/*! + @brief Reads a signed 16 bit value over I2C or SPI +*/ +/**************************************************************************/ +int16_t Adafruit_BME280::readS16(byte reg) +{ + return (int16_t)read16(reg); +} + + +/**************************************************************************/ +/*! + +*/ +/**************************************************************************/ +int16_t Adafruit_BME280::readS16_LE(byte reg) +{ + return (int16_t)read16_LE(reg); +} + + +/**************************************************************************/ +/*! + @brief Reads a 24 bit value over I2C +*/ +/**************************************************************************/ +uint32_t Adafruit_BME280::read24(byte reg) +{ + uint32_t value; + + if (_cs == -1) { + _wire -> beginTransmission((uint8_t)_i2caddr); + _wire -> write((uint8_t)reg); + _wire -> endTransmission(); + _wire -> requestFrom((uint8_t)_i2caddr, (byte)3); + + value = _wire -> read(); + value <<= 8; + value |= _wire -> read(); + value <<= 8; + value |= _wire -> read(); + } else { + if (_sck == -1) + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(_cs, LOW); + spixfer(reg | 0x80); // read, bit 7 high + + value = spixfer(0); + value <<= 8; + value |= spixfer(0); + value <<= 8; + value |= spixfer(0); + + digitalWrite(_cs, HIGH); + if (_sck == -1) + SPI.endTransaction(); // release the SPI bus + } + + return value; +} + + +/**************************************************************************/ +/*! + @brief Take a new measurement (only possible in forced mode) +*/ +/**************************************************************************/ +void Adafruit_BME280::takeForcedMeasurement() +{ + // If we are in forced mode, the BME sensor goes back to sleep after each + // measurement and we need to set it to forced mode once at this point, so + // it will take the next measurement and then return to sleep again. + // In normal mode simply does new measurements periodically. + if (_measReg.mode == MODE_FORCED) { + // set to forced mode, i.e. "take next measurement" + write8(BME280_REGISTER_CONTROL, _measReg.get()); + // wait until measurement has been completed, otherwise we would read + // the values from the last measurement + while (read8(BME280_REGISTER_STATUS) & 0x08) + delay(1); + } +} + + +/**************************************************************************/ +/*! + @brief Reads the factory-set coefficients +*/ +/**************************************************************************/ +void Adafruit_BME280::readCoefficients(void) +{ + _bme280_calib.dig_T1 = read16_LE(BME280_REGISTER_DIG_T1); + _bme280_calib.dig_T2 = readS16_LE(BME280_REGISTER_DIG_T2); + _bme280_calib.dig_T3 = readS16_LE(BME280_REGISTER_DIG_T3); + + _bme280_calib.dig_P1 = read16_LE(BME280_REGISTER_DIG_P1); + _bme280_calib.dig_P2 = readS16_LE(BME280_REGISTER_DIG_P2); + _bme280_calib.dig_P3 = readS16_LE(BME280_REGISTER_DIG_P3); + _bme280_calib.dig_P4 = readS16_LE(BME280_REGISTER_DIG_P4); + _bme280_calib.dig_P5 = readS16_LE(BME280_REGISTER_DIG_P5); + _bme280_calib.dig_P6 = readS16_LE(BME280_REGISTER_DIG_P6); + _bme280_calib.dig_P7 = readS16_LE(BME280_REGISTER_DIG_P7); + _bme280_calib.dig_P8 = readS16_LE(BME280_REGISTER_DIG_P8); + _bme280_calib.dig_P9 = readS16_LE(BME280_REGISTER_DIG_P9); + + _bme280_calib.dig_H1 = read8(BME280_REGISTER_DIG_H1); + _bme280_calib.dig_H2 = readS16_LE(BME280_REGISTER_DIG_H2); + _bme280_calib.dig_H3 = read8(BME280_REGISTER_DIG_H3); + _bme280_calib.dig_H4 = (read8(BME280_REGISTER_DIG_H4) << 4) | (read8(BME280_REGISTER_DIG_H4+1) & 0xF); + _bme280_calib.dig_H5 = (read8(BME280_REGISTER_DIG_H5+1) << 4) | (read8(BME280_REGISTER_DIG_H5) >> 4); + _bme280_calib.dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6); +} + +/**************************************************************************/ +/*! + @brief return true if chip is busy reading cal data +*/ +/**************************************************************************/ +bool Adafruit_BME280::isReadingCalibration(void) +{ + uint8_t const rStatus = read8(BME280_REGISTER_STATUS); + + return (rStatus & (1 << 0)) != 0; +} + + +/**************************************************************************/ +/*! + @brief Returns the temperature from the sensor +*/ +/**************************************************************************/ +float Adafruit_BME280::readTemperature(void) +{ + int32_t var1, var2; + + int32_t adc_T = read24(BME280_REGISTER_TEMPDATA); + if (adc_T == 0x800000) // value in case temp measurement was disabled + return NAN; + adc_T >>= 4; + + var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) * + ((int32_t)_bme280_calib.dig_T2)) >> 11; + + var2 = (((((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1)) * + ((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1))) >> 12) * + ((int32_t)_bme280_calib.dig_T3)) >> 14; + + t_fine = var1 + var2; + + float T = (t_fine * 5 + 128) >> 8; + return T/100; +} + + +/**************************************************************************/ +/*! + @brief Returns the temperature from the sensor +*/ +/**************************************************************************/ +float Adafruit_BME280::readPressure(void) { + int64_t var1, var2, p; + + readTemperature(); // must be done first to get t_fine + + int32_t adc_P = read24(BME280_REGISTER_PRESSUREDATA); + if (adc_P == 0x800000) // value in case pressure measurement was disabled + return NAN; + adc_P >>= 4; + + var1 = ((int64_t)t_fine) - 128000; + var2 = var1 * var1 * (int64_t)_bme280_calib.dig_P6; + var2 = var2 + ((var1*(int64_t)_bme280_calib.dig_P5)<<17); + var2 = var2 + (((int64_t)_bme280_calib.dig_P4)<<35); + var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3)>>8) + + ((var1 * (int64_t)_bme280_calib.dig_P2)<<12); + var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bme280_calib.dig_P1)>>33; + + if (var1 == 0) { + return 0; // avoid exception caused by division by zero + } + p = 1048576 - adc_P; + p = (((p<<31) - var2)*3125) / var1; + var1 = (((int64_t)_bme280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25; + var2 = (((int64_t)_bme280_calib.dig_P8) * p) >> 19; + + p = ((p + var1 + var2) >> 8) + (((int64_t)_bme280_calib.dig_P7)<<4); + return (float)p/256; +} + + +/**************************************************************************/ +/*! + @brief Returns the humidity from the sensor +*/ +/**************************************************************************/ +float Adafruit_BME280::readHumidity(void) { + readTemperature(); // must be done first to get t_fine + + int32_t adc_H = read16(BME280_REGISTER_HUMIDDATA); + if (adc_H == 0x8000) // value in case humidity measurement was disabled + return NAN; + + int32_t v_x1_u32r; + + v_x1_u32r = (t_fine - ((int32_t)76800)); + + v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bme280_calib.dig_H4) << 20) - + (((int32_t)_bme280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) * + (((((((v_x1_u32r * ((int32_t)_bme280_calib.dig_H6)) >> 10) * + (((v_x1_u32r * ((int32_t)_bme280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) + + ((int32_t)2097152)) * ((int32_t)_bme280_calib.dig_H2) + 8192) >> 14)); + + v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * + ((int32_t)_bme280_calib.dig_H1)) >> 4)); + + v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r; + v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r; + float h = (v_x1_u32r>>12); + return h / 1024.0; +} + + +/**************************************************************************/ +/*! + Calculates the altitude (in meters) from the specified atmospheric + pressure (in hPa), and sea-level pressure (in hPa). + + @param seaLevel Sea-level pressure in hPa + @param atmospheric Atmospheric pressure in hPa +*/ +/**************************************************************************/ +float Adafruit_BME280::readAltitude(float seaLevel) +{ + // Equation taken from BMP180 datasheet (page 16): + // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf + + // Note that using the equation from wikipedia can give bad results + // at high altitude. See this thread for more information: + // http://forums.adafruit.com/viewtopic.php?f=22&t=58064 + + float atmospheric = readPressure() / 100.0F; + return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903)); +} + + +/**************************************************************************/ +/*! + Calculates the pressure at sea level (in hPa) from the specified altitude + (in meters), and atmospheric pressure (in hPa). + @param altitude Altitude in meters + @param atmospheric Atmospheric pressure in hPa +*/ +/**************************************************************************/ +float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric) +{ + // Equation taken from BMP180 datasheet (page 17): + // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf + + // Note that using the equation from wikipedia can give bad results + // at high altitude. See this thread for more information: + // http://forums.adafruit.com/viewtopic.php?f=22&t=58064 + + return atmospheric / pow(1.0 - (altitude/44330.0), 5.255); +} diff --git a/libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.h b/libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.h new file mode 100644 index 00000000000..61aeeed8b0e --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/Adafruit_BME280.h @@ -0,0 +1,300 @@ +/*************************************************************************** + This is a library for the BME280 humidity, temperature & pressure sensor + + Designed specifically to work with the Adafruit BME280 Breakout + ----> http://www.adafruit.com/products/2650 + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#ifndef __BME280_H__ +#define __BME280_H__ + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include +#include + +/*========================================================================= + I2C ADDRESS/BITS + -----------------------------------------------------------------------*/ + #define BME280_ADDRESS (0x77) +/*=========================================================================*/ + +/*========================================================================= + REGISTERS + -----------------------------------------------------------------------*/ + enum + { + BME280_REGISTER_DIG_T1 = 0x88, + BME280_REGISTER_DIG_T2 = 0x8A, + BME280_REGISTER_DIG_T3 = 0x8C, + + BME280_REGISTER_DIG_P1 = 0x8E, + BME280_REGISTER_DIG_P2 = 0x90, + BME280_REGISTER_DIG_P3 = 0x92, + BME280_REGISTER_DIG_P4 = 0x94, + BME280_REGISTER_DIG_P5 = 0x96, + BME280_REGISTER_DIG_P6 = 0x98, + BME280_REGISTER_DIG_P7 = 0x9A, + BME280_REGISTER_DIG_P8 = 0x9C, + BME280_REGISTER_DIG_P9 = 0x9E, + + BME280_REGISTER_DIG_H1 = 0xA1, + BME280_REGISTER_DIG_H2 = 0xE1, + BME280_REGISTER_DIG_H3 = 0xE3, + BME280_REGISTER_DIG_H4 = 0xE4, + BME280_REGISTER_DIG_H5 = 0xE5, + BME280_REGISTER_DIG_H6 = 0xE7, + + BME280_REGISTER_CHIPID = 0xD0, + BME280_REGISTER_VERSION = 0xD1, + BME280_REGISTER_SOFTRESET = 0xE0, + + BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0 + + BME280_REGISTER_CONTROLHUMID = 0xF2, + BME280_REGISTER_STATUS = 0XF3, + BME280_REGISTER_CONTROL = 0xF4, + BME280_REGISTER_CONFIG = 0xF5, + BME280_REGISTER_PRESSUREDATA = 0xF7, + BME280_REGISTER_TEMPDATA = 0xFA, + BME280_REGISTER_HUMIDDATA = 0xFD + }; + +/*=========================================================================*/ + +/*========================================================================= + CALIBRATION DATA + -----------------------------------------------------------------------*/ + typedef struct + { + uint16_t dig_T1; + int16_t dig_T2; + int16_t dig_T3; + + uint16_t dig_P1; + int16_t dig_P2; + int16_t dig_P3; + int16_t dig_P4; + int16_t dig_P5; + int16_t dig_P6; + int16_t dig_P7; + int16_t dig_P8; + int16_t dig_P9; + + uint8_t dig_H1; + int16_t dig_H2; + uint8_t dig_H3; + int16_t dig_H4; + int16_t dig_H5; + int8_t dig_H6; + } bme280_calib_data; +/*=========================================================================*/ + +/* +class Adafruit_BME280_Unified : public Adafruit_Sensor +{ + public: + Adafruit_BME280_Unified(int32_t sensorID = -1); + + bool begin(uint8_t addr = BME280_ADDRESS); + void getTemperature(float *temp); + void getPressure(float *pressure); + float pressureToAltitude(float seaLevel, float atmospheric, float temp); + float seaLevelForAltitude(float altitude, float atmospheric, float temp); + void getEvent(sensors_event_t*); + void getSensor(sensor_t*); + + private: + uint8_t _i2c_addr; + int32_t _sensorID; +}; + +*/ + +class Adafruit_BME280 { + public: + enum sensor_sampling { + SAMPLING_NONE = 0b000, + SAMPLING_X1 = 0b001, + SAMPLING_X2 = 0b010, + SAMPLING_X4 = 0b011, + SAMPLING_X8 = 0b100, + SAMPLING_X16 = 0b101 + }; + + enum sensor_mode { + MODE_SLEEP = 0b00, + MODE_FORCED = 0b01, + MODE_NORMAL = 0b11 + }; + + enum sensor_filter { + FILTER_OFF = 0b000, + FILTER_X2 = 0b001, + FILTER_X4 = 0b010, + FILTER_X8 = 0b011, + FILTER_X16 = 0b100 + }; + + // standby durations in ms + enum standby_duration { + STANDBY_MS_0_5 = 0b000, + STANDBY_MS_10 = 0b110, + STANDBY_MS_20 = 0b111, + STANDBY_MS_62_5 = 0b001, + STANDBY_MS_125 = 0b010, + STANDBY_MS_250 = 0b011, + STANDBY_MS_500 = 0b100, + STANDBY_MS_1000 = 0b101 + }; + + // constructors + Adafruit_BME280(void); + Adafruit_BME280(int8_t cspin); + Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); + + bool begin(void); + bool begin(TwoWire *theWire); + bool begin(uint8_t addr); + bool begin(uint8_t addr, TwoWire *theWire); + bool init(); + + void setSampling(sensor_mode mode = MODE_NORMAL, + sensor_sampling tempSampling = SAMPLING_X16, + sensor_sampling pressSampling = SAMPLING_X16, + sensor_sampling humSampling = SAMPLING_X16, + sensor_filter filter = FILTER_OFF, + standby_duration duration = STANDBY_MS_0_5 + ); + + void takeForcedMeasurement(); + float readTemperature(void); + float readPressure(void); + float readHumidity(void); + + float readAltitude(float seaLevel); + float seaLevelForAltitude(float altitude, float pressure); + + + private: + TwoWire *_wire; + void readCoefficients(void); + bool isReadingCalibration(void); + uint8_t spixfer(uint8_t x); + + void write8(byte reg, byte value); + uint8_t read8(byte reg); + uint16_t read16(byte reg); + uint32_t read24(byte reg); + int16_t readS16(byte reg); + uint16_t read16_LE(byte reg); // little endian + int16_t readS16_LE(byte reg); // little endian + + uint8_t _i2caddr; + int32_t _sensorID; + int32_t t_fine; + + int8_t _cs, _mosi, _miso, _sck; + + bme280_calib_data _bme280_calib; + + // The config register + struct config { + // inactive duration (standby time) in normal mode + // 000 = 0.5 ms + // 001 = 62.5 ms + // 010 = 125 ms + // 011 = 250 ms + // 100 = 500 ms + // 101 = 1000 ms + // 110 = 10 ms + // 111 = 20 ms + unsigned int t_sb : 3; + + // filter settings + // 000 = filter off + // 001 = 2x filter + // 010 = 4x filter + // 011 = 8x filter + // 100 and above = 16x filter + unsigned int filter : 3; + + // unused - don't set + unsigned int none : 1; + unsigned int spi3w_en : 1; + + unsigned int get() { + return (t_sb << 5) | (filter << 3) | spi3w_en; + } + }; + config _configReg; + + + // The ctrl_meas register + struct ctrl_meas { + // temperature oversampling + // 000 = skipped + // 001 = x1 + // 010 = x2 + // 011 = x4 + // 100 = x8 + // 101 and above = x16 + unsigned int osrs_t : 3; + + // pressure oversampling + // 000 = skipped + // 001 = x1 + // 010 = x2 + // 011 = x4 + // 100 = x8 + // 101 and above = x16 + unsigned int osrs_p : 3; + + // device mode + // 00 = sleep + // 01 or 10 = forced + // 11 = normal + unsigned int mode : 2; + + unsigned int get() { + return (osrs_t << 5) | (osrs_p << 3) | mode; + } + }; + ctrl_meas _measReg; + + + // The ctrl_hum register + struct ctrl_hum { + // unused - don't set + unsigned int none : 5; + + // pressure oversampling + // 000 = skipped + // 001 = x1 + // 010 = x2 + // 011 = x4 + // 100 = x8 + // 101 and above = x16 + unsigned int osrs_h : 3; + + unsigned int get() { + return (osrs_h); + } + }; + ctrl_hum _humReg; +}; + +#endif diff --git a/libraries/Adafruit_BME280_Library-1.0.7/README.md b/libraries/Adafruit_BME280_Library-1.0.7/README.md new file mode 100644 index 00000000000..ed49542a61e --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/README.md @@ -0,0 +1,59 @@ +This is a library for the Adafruit BME280 Humidity, Barometric Pressure + Temp sensor + +Designed specifically to work with the Adafruit BME280 Breakout + * http://www.adafruit.com/products/2652 + +These sensors use I2C or SPI to communicate, up to 4 pins are required to interface + +Use of this library also requires [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) +to be installed on your local system. + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Check out the links above for our tutorials and wiring diagrams + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, all text above must be included in any redistribution + +To download. click the DOWNLOAD ZIP button, rename the uncompressed folder Adafruit_BME280. +Check that the Adafruit_BME280 folder contains Adafruit_BME280.cpp and Adafruit_BME280.h + +Place the Adafruit_BME280 library folder your arduinosketchfolder/libraries/ folder. +You may need to create the libraries subfolder if its your first library. Restart the IDE. + +We also have a great tutorial on Arduino library installation at: +http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use + + +## Compatibility + +MCU | Tested Works | Doesn't Work | Not Tested | Notes +------------------ | :----------: | :----------: | :---------: | ----- +Atmega328 @ 16MHz | X | | | +Atmega328 @ 12MHz | X | | | +Atmega32u4 @ 16MHz | X | | | Use SDA/SCL on pins D2 & D3 +Atmega32u4 @ 8MHz | X | | | Use SDA/SCL on pins D2 & D3 +ESP8266 | X | | | I2C: just works, SPI: SDA/SCL default to pins 4 & 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL) +ESP32 | X | | | I2C: just works, SPI: SDA/SCL default to pins 4 & 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL) +Atmega2560 @ 16MHz | X | | | Use SDA/SCL on pins 20 & 21 +ATSAM3X8E | X | | | Use SDA/SCL on pins 20 & 21 +ATSAM21D | X | | | +ATtiny85 @ 16MHz | | X | | +ATtiny85 @ 8MHz | | X | | +Intel Curie @ 32MHz | | | X | +STM32F2 | | | X | + + * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini + * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V + * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 + * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro + * ESP8266 : Adafruit Huzzah + * ATmega2560 @ 16MHz : Arduino Mega + * ATSAM3X8E : Arduino Due + * ATSAM21D : Arduino Zero, M0 Pro + * ATtiny85 @ 16MHz : Adafruit Trinket 5V + * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V + + diff --git a/libraries/Adafruit_BME280_Library-1.0.7/examples/advancedsettings/advancedsettings.ino b/libraries/Adafruit_BME280_Library-1.0.7/examples/advancedsettings/advancedsettings.ino new file mode 100644 index 00000000000..68b22b9005d --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/examples/advancedsettings/advancedsettings.ino @@ -0,0 +1,157 @@ +/*************************************************************************** + This is a library for the BME280 humidity, temperature & pressure sensor + + Designed specifically to work with the Adafruit BME280 Breakout + ----> http://www.adafruit.com/products/2650 + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. The device's I2C address is either 0x76 or 0x77. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ + +#include +#include +#include +#include + +#define BME_SCK 13 +#define BME_MISO 12 +#define BME_MOSI 11 +#define BME_CS 10 + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; // I2C +//Adafruit_BME280 bme(BME_CS); // hardware SPI +//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI + +unsigned long delayTime; + +void setup() { + Serial.begin(9600); + Serial.println(F("BME280 test")); + + if (! bme.begin(&Wire1)) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + while (1); + } + + Serial.println("-- Default Test --"); + Serial.println("normal mode, 16x oversampling for all, filter off,"); + Serial.println("0.5ms standby period"); + delayTime = 5000; + + + // For more details on the following scenarious, see chapter + // 3.5 "Recommended modes of operation" in the datasheet + +/* + // weather monitoring + Serial.println("-- Weather Station Scenario --"); + Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,"); + Serial.println("filter off"); + bme.setSampling(Adafruit_BME280::MODE_FORCED, + Adafruit_BME280::SAMPLING_X1, // temperature + Adafruit_BME280::SAMPLING_X1, // pressure + Adafruit_BME280::SAMPLING_X1, // humidity + Adafruit_BME280::FILTER_OFF ); + + // suggested rate is 1/60Hz (1m) + delayTime = 60000; // in milliseconds +*/ + +/* + // humidity sensing + Serial.println("-- Humidity Sensing Scenario --"); + Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling"); + Serial.println("= pressure off, filter off"); + bme.setSampling(Adafruit_BME280::MODE_FORCED, + Adafruit_BME280::SAMPLING_X1, // temperature + Adafruit_BME280::SAMPLING_NONE, // pressure + Adafruit_BME280::SAMPLING_X1, // humidity + Adafruit_BME280::FILTER_OFF ); + + // suggested rate is 1Hz (1s) + delayTime = 1000; // in milliseconds +*/ + +/* + // indoor navigation + Serial.println("-- Indoor Navigation Scenario --"); + Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,"); + Serial.println("0.5ms standby period, filter 16x"); + bme.setSampling(Adafruit_BME280::MODE_NORMAL, + Adafruit_BME280::SAMPLING_X2, // temperature + Adafruit_BME280::SAMPLING_X16, // pressure + Adafruit_BME280::SAMPLING_X1, // humidity + Adafruit_BME280::FILTER_X16, + Adafruit_BME280::STANDBY_MS_0_5 ); + + // suggested rate is 25Hz + // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5) + // T_ovs = 2 + // P_ovs = 16 + // H_ovs = 1 + // = 40ms (25Hz) + // with standby time that should really be 24.16913... Hz + delayTime = 41; + + /* + // gaming + Serial.println("-- Gaming Scenario --"); + Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,"); + Serial.println("= humidity off, 0.5ms standby period, filter 16x"); + bme.setSampling(Adafruit_BME280::MODE_NORMAL, + Adafruit_BME280::SAMPLING_X1, // temperature + Adafruit_BME280::SAMPLING_X4, // pressure + Adafruit_BME280::SAMPLING_NONE, // humidity + Adafruit_BME280::FILTER_X16, + Adafruit_BME280::STANDBY_MS_0_5 ); + + // Suggested rate is 83Hz + // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + // T_ovs = 1 + // P_ovs = 4 + // = 11.5ms + 0.5ms standby + delayTime = 12; +*/ + + Serial.println(); +} + + +void loop() { + // Only needed in forced mode! In normal mode, you can remove the next line. + bme.takeForcedMeasurement(); // has no effect in normal mode + + printValues(); + delay(delayTime); +} + + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" *C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} \ No newline at end of file diff --git a/libraries/Adafruit_BME280_Library-1.0.7/examples/bme280test/bme280test.ino b/libraries/Adafruit_BME280_Library-1.0.7/examples/bme280test/bme280test.ino new file mode 100644 index 00000000000..e8b5563da2e --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/examples/bme280test/bme280test.ino @@ -0,0 +1,82 @@ +/*************************************************************************** + This is a library for the BME280 humidity, temperature & pressure sensor + + Designed specifically to work with the Adafruit BME280 Breakout + ----> http://www.adafruit.com/products/2650 + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. The device's I2C address is either 0x76 or 0x77. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ + +#include +#include +#include +#include + +#define BME_SCK 13 +#define BME_MISO 12 +#define BME_MOSI 11 +#define BME_CS 10 + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; // I2C +//Adafruit_BME280 bme(BME_CS); // hardware SPI +//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI + +unsigned long delayTime; + +void setup() { + Serial.begin(9600); + Serial.println(F("BME280 test")); + + bool status; + + // default settings + // (you can also pass in a Wire library object like &Wire2) + status = bme.begin(); + if (!status) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + while (1); + } + + Serial.println("-- Default Test --"); + delayTime = 1000; + + Serial.println(); +} + + +void loop() { + printValues(); + delay(delayTime); +} + + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" *C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} \ No newline at end of file diff --git a/libraries/Adafruit_BME280_Library-1.0.7/library.properties b/libraries/Adafruit_BME280_Library-1.0.7/library.properties new file mode 100644 index 00000000000..189c369af32 --- /dev/null +++ b/libraries/Adafruit_BME280_Library-1.0.7/library.properties @@ -0,0 +1,9 @@ +name=Adafruit BME280 Library +version=1.0.7 +author=Adafruit +maintainer=Adafruit +sentence=Arduino library for BME280 sensors. +paragraph=Arduino library for BME280 humidity and pressure sensors. +category=Sensors +url=https://github.com/adafruit/Adafruit_BME280_Library +architectures=* diff --git a/libraries/Adafruit_BME680-1.0.5/.gitignore b/libraries/Adafruit_BME680-1.0.5/.gitignore new file mode 100644 index 00000000000..b25c15b81fa --- /dev/null +++ b/libraries/Adafruit_BME680-1.0.5/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/libraries/Adafruit_BME680-1.0.5/Adafruit_BME680.cpp b/libraries/Adafruit_BME680-1.0.5/Adafruit_BME680.cpp new file mode 100644 index 00000000000..089065994af --- /dev/null +++ b/libraries/Adafruit_BME680-1.0.5/Adafruit_BME680.cpp @@ -0,0 +1,605 @@ +/*************************************************************************** + This is a library for the BME680 humidity, temperature & pressure sensor + + Designed specifically to work with the Adafruit BME680 Breakout + ----> http://www.adafruit.com/products/2650 + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#include "Arduino.h" +#include "Adafruit_BME680.h" + +//#define BME680_DEBUG + +// must be global in order to work with underlying library +int8_t _BME680_SoftwareSPI_MOSI, _BME680_SoftwareSPI_MISO, _BME680_SoftwareSPI_SCK; + +// Our hardware interface functions +static int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len); +static int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len); +static int8_t spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len); +static int8_t spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len); +static uint8_t spi_transfer(uint8_t x); +static void delay_msec(uint32_t ms); + +/*************************************************************************** + PUBLIC FUNCTIONS + ***************************************************************************/ + +/**************************************************************************/ +/*! + @brief Instantiates sensor with Hardware SPI or I2C. + @param cspin SPI chip select. If not passed in, I2C will be used +*/ +/**************************************************************************/ +Adafruit_BME680::Adafruit_BME680(int8_t cspin) + : _cs(cspin) +{ + _BME680_SoftwareSPI_MOSI = -1; + _BME680_SoftwareSPI_MISO = -1; + _BME680_SoftwareSPI_SCK = -1; + _filterEnabled = _tempEnabled = _humEnabled = _presEnabled = _gasEnabled = false; +} + + +/**************************************************************************/ +/*! + @brief Instantiates sensor with Software (bit-bang) SPI. + @param cspin SPI chip select + @param mosipin SPI MOSI (Data from microcontroller to sensor) + @param misopin SPI MISO (Data to microcontroller from sensor) + @param sckpin SPI Clock +*/ +/**************************************************************************/ +Adafruit_BME680::Adafruit_BME680(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin) + : _cs(cspin) +{ + _BME680_SoftwareSPI_MOSI = mosipin; + _BME680_SoftwareSPI_MISO = misopin; + _BME680_SoftwareSPI_SCK = sckpin; + _filterEnabled = _tempEnabled = _humEnabled = _presEnabled = _gasEnabled = false; +} + + + +/**************************************************************************/ +/*! + @brief Initializes the sensor + + Hardware ss initialized, verifies it is in the I2C or SPI bus, then reads + calibration data in preparation for sensor reads. + + @param addr Optional parameter for the I2C address of BME680. Default is 0x77 + @return True on sensor initialization success. False on failure. +*/ +/**************************************************************************/ +bool Adafruit_BME680::begin(uint8_t addr) { + _i2caddr = addr; + + if (_cs == -1) { + // i2c + Wire.begin(); + + gas_sensor.dev_id = addr; + gas_sensor.intf = BME680_I2C_INTF; + gas_sensor.read = &i2c_read; + gas_sensor.write = &i2c_write; + } else { + digitalWrite(_cs, HIGH); + pinMode(_cs, OUTPUT); + + if (_BME680_SoftwareSPI_SCK == -1) { + // hardware SPI + SPI.begin(); + } else { + // software SPI + pinMode(_BME680_SoftwareSPI_SCK, OUTPUT); + pinMode(_BME680_SoftwareSPI_MOSI, OUTPUT); + pinMode(_BME680_SoftwareSPI_MISO, INPUT); + } + + gas_sensor.dev_id = _cs; + gas_sensor.intf = BME680_SPI_INTF; + gas_sensor.read = &spi_read; + gas_sensor.write = &spi_write; + } + + gas_sensor.delay_ms = delay_msec; + + int8_t rslt = BME680_OK; + rslt = bme680_init(&gas_sensor); +#ifdef BME680_DEBUG + Serial.print("Result: "); Serial.println(rslt); +#endif + + if (rslt != BME680_OK) + return false; + +#ifdef BME680_DEBUG + Serial.print("T1 = "); Serial.println(gas_sensor.calib.par_t1); + Serial.print("T2 = "); Serial.println(gas_sensor.calib.par_t2); + Serial.print("T3 = "); Serial.println(gas_sensor.calib.par_t3); + Serial.print("P1 = "); Serial.println(gas_sensor.calib.par_p1); + Serial.print("P2 = "); Serial.println(gas_sensor.calib.par_p2); + Serial.print("P3 = "); Serial.println(gas_sensor.calib.par_p3); + Serial.print("P4 = "); Serial.println(gas_sensor.calib.par_p4); + Serial.print("P5 = "); Serial.println(gas_sensor.calib.par_p5); + Serial.print("P6 = "); Serial.println(gas_sensor.calib.par_p6); + Serial.print("P7 = "); Serial.println(gas_sensor.calib.par_p7); + Serial.print("P8 = "); Serial.println(gas_sensor.calib.par_p8); + Serial.print("P9 = "); Serial.println(gas_sensor.calib.par_p9); + Serial.print("P10 = "); Serial.println(gas_sensor.calib.par_p10); + Serial.print("H1 = "); Serial.println(gas_sensor.calib.par_h1); + Serial.print("H2 = "); Serial.println(gas_sensor.calib.par_h2); + Serial.print("H3 = "); Serial.println(gas_sensor.calib.par_h3); + Serial.print("H4 = "); Serial.println(gas_sensor.calib.par_h4); + Serial.print("H5 = "); Serial.println(gas_sensor.calib.par_h5); + Serial.print("H6 = "); Serial.println(gas_sensor.calib.par_h6); + Serial.print("H7 = "); Serial.println(gas_sensor.calib.par_h7); + Serial.print("G1 = "); Serial.println(gas_sensor.calib.par_gh1); + Serial.print("G2 = "); Serial.println(gas_sensor.calib.par_gh2); + Serial.print("G3 = "); Serial.println(gas_sensor.calib.par_gh3); + Serial.print("G1 = "); Serial.println(gas_sensor.calib.par_gh1); + Serial.print("G2 = "); Serial.println(gas_sensor.calib.par_gh2); + Serial.print("G3 = "); Serial.println(gas_sensor.calib.par_gh3); + Serial.print("Heat Range = "); Serial.println(gas_sensor.calib.res_heat_range); + Serial.print("Heat Val = "); Serial.println(gas_sensor.calib.res_heat_val); + Serial.print("SW Error = "); Serial.println(gas_sensor.calib.range_sw_err); +#endif + + setTemperatureOversampling(BME680_OS_8X); + setHumidityOversampling(BME680_OS_2X); + setPressureOversampling(BME680_OS_4X); + setIIRFilterSize(BME680_FILTER_SIZE_3); + setGasHeater(320, 150); // 320*C for 150 ms + + // don't do anything till we request a reading + gas_sensor.power_mode = BME680_FORCED_MODE; + + return true; +} + + + +/**************************************************************************/ +/*! + @brief Performs a reading and returns the ambient temperature. + @return Temperature in degrees Centigrade +*/ +/**************************************************************************/ +float Adafruit_BME680::readTemperature(void) { + performReading(); + return temperature; +} + + +/**************************************************************************/ +/*! + @brief Performs a reading and returns the barometric pressure. + @return Barometic pressure in Pascals +*/ +/**************************************************************************/ +float Adafruit_BME680::readPressure(void) { + performReading(); + return pressure; +} + + +/**************************************************************************/ +/*! + @brief Performs a reading and returns the relative humidity. + @return Relative humidity as floating point +*/ +/**************************************************************************/ +float Adafruit_BME680::readHumidity(void) { + performReading(); + return humidity; +} + +/**************************************************************************/ +/*! + @brief Calculates the resistance of the MOX gas sensor. + @return Resistance in Ohms +*/ +/**************************************************************************/ +uint32_t Adafruit_BME680::readGas(void) { + performReading(); + return gas_resistance; +} + + +/**************************************************************************/ +/*! + @brief Calculates the altitude (in meters). + + Reads the current atmostpheric pressure (in hPa) from the sensor and calculates + via the provided sea-level pressure (in hPa). + + @param seaLevel Sea-level pressure in hPa + @return Altitude in meters +*/ +/**************************************************************************/ +float Adafruit_BME680::readAltitude(float seaLevel) +{ + // Equation taken from BMP180 datasheet (page 16): + // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf + + // Note that using the equation from wikipedia can give bad results + // at high altitude. See this thread for more information: + // http://forums.adafruit.com/viewtopic.php?f=22&t=58064 + + float atmospheric = readPressure() / 100.0F; + return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903)); +} + +/**************************************************************************/ +/*! + @brief Performs a full reading of all 4 sensors in the BME680. + + Assigns the internal Adafruit_BME680#temperature, Adafruit_BME680#pressure, Adafruit_BME680#humidity + and Adafruit_BME680#gas_resistance member variables + + @return True on success, False on failure +*/ +/**************************************************************************/ +bool Adafruit_BME680::performReading(void) { + uint8_t set_required_settings = 0; + struct bme680_field_data data; + int8_t rslt; + + /* Select the power mode */ + /* Must be set before writing the sensor configuration */ + gas_sensor.power_mode = BME680_FORCED_MODE; + + /* Set the required sensor settings needed */ + if (_tempEnabled) + set_required_settings |= BME680_OST_SEL; + if (_humEnabled) + set_required_settings |= BME680_OSH_SEL; + if (_presEnabled) + set_required_settings |= BME680_OSP_SEL; + if (_filterEnabled) + set_required_settings |= BME680_FILTER_SEL; + if (_gasEnabled) + set_required_settings |= BME680_GAS_SENSOR_SEL; + + /* Set the desired sensor configuration */ + //Serial.println("Setting sensor settings"); + rslt = bme680_set_sensor_settings(set_required_settings, &gas_sensor); + if (rslt != BME680_OK) + return false; + + /* Set the power mode */ + //Serial.println("Setting power mode"); + rslt = bme680_set_sensor_mode(&gas_sensor); + if (rslt != BME680_OK) + return false; + + /* Get the total measurement duration so as to sleep or wait till the + * measurement is complete */ + uint16_t meas_period; + bme680_get_profile_dur(&meas_period, &gas_sensor); + //Serial.print("Waiting (ms) "); Serial.println(meas_period); + delay(meas_period * 2); /* Delay till the measurement is ready */ + + //Serial.print("t_fine = "); Serial.println(gas_sensor.calib.t_fine); + + //Serial.println("Getting sensor data"); + rslt = bme680_get_sensor_data(&data, &gas_sensor); + if (rslt != BME680_OK) + return false; + + if (_tempEnabled) { + //Serial.print("Temp: "); Serial.println(data.temperature / 100.0, 2); + temperature = data.temperature / 100.0; + } else { + temperature = NAN; + } + + if (_humEnabled) { + //Serial.print("Hum: "); Serial.println(data.humidity / 1000.0, 2); + humidity = data.humidity / 1000.0; + } else { + humidity = NAN; + } + + if (_presEnabled) { + //Serial.print("Pres: "); Serial.println(data.pressure / 100.0, 2); + pressure = data.pressure; + } else { + pressure = NAN; + } + + /* Avoid using measurements from an unstable heating setup */ + if (_gasEnabled) { + if (data.status & BME680_HEAT_STAB_MSK) { + //Serial.print("Gas resistance: "); Serial.println(data.gas_resistance); + gas_resistance = data.gas_resistance; + } else { + gas_resistance = 0; + //Serial.println("Gas reading unstable!"); + } + } + + return true; +} + +/**************************************************************************/ +/*! + @brief Enable and configure gas reading + heater + @param heaterTemp Desired temperature in degrees Centigrade + @param heaterTime Time to keep heater on in milliseconds + @return True on success, False on failure +*/ +/**************************************************************************/ +bool Adafruit_BME680::setGasHeater(uint16_t heaterTemp, uint16_t heaterTime) { + gas_sensor.gas_sett.heatr_temp = heaterTemp; + gas_sensor.gas_sett.heatr_dur = heaterTime; + + if ( (heaterTemp == 0) || (heaterTime == 0) ) { + // disabled! + gas_sensor.gas_sett.run_gas = BME680_DISABLE_GAS_MEAS; + _gasEnabled = false; + } else { + gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; + _gasEnabled = true; + } + return true; +} + + +/**************************************************************************/ +/*! + @brief Setter for Temperature oversampling + @param oversample Oversampling setting, can be BME680_OS_NONE (turn off Temperature reading), + BME680_OS_1X, BME680_OS_2X, BME680_OS_4X, BME680_OS_8X or BME680_OS_16X + @return True on success, False on failure +*/ +/**************************************************************************/ + +bool Adafruit_BME680::setTemperatureOversampling(uint8_t oversample) { + if (oversample > BME680_OS_16X) return false; + + gas_sensor.tph_sett.os_temp = oversample; + + if (oversample == BME680_OS_NONE) + _tempEnabled = false; + else + _tempEnabled = true; + + return true; +} + + +/**************************************************************************/ +/*! + @brief Setter for Humidity oversampling + @param oversample Oversampling setting, can be BME680_OS_NONE (turn off Humidity reading), + BME680_OS_1X, BME680_OS_2X, BME680_OS_4X, BME680_OS_8X or BME680_OS_16X + @return True on success, False on failure +*/ +/**************************************************************************/ + +bool Adafruit_BME680::setHumidityOversampling(uint8_t oversample) { + if (oversample > BME680_OS_16X) return false; + + gas_sensor.tph_sett.os_hum = oversample; + + if (oversample == BME680_OS_NONE) + _humEnabled = false; + else + _humEnabled = true; + + return true; +} + + +/**************************************************************************/ +/*! + @brief Setter for Pressure oversampling + @param oversample Oversampling setting, can be BME680_OS_NONE (turn off Pressure reading), + BME680_OS_1X, BME680_OS_2X, BME680_OS_4X, BME680_OS_8X or BME680_OS_16X + @return True on success, False on failure +*/ +/**************************************************************************/ +bool Adafruit_BME680::setPressureOversampling(uint8_t oversample) { + if (oversample > BME680_OS_16X) return false; + + gas_sensor.tph_sett.os_pres = oversample; + + if (oversample == BME680_OS_NONE) + _presEnabled = false; + else + _presEnabled = true; + + return true; +} + +/**************************************************************************/ +/*! + @brief Setter for IIR filter. + @param filtersize Size of the filter (in samples). Can be BME680_FILTER_SIZE_0 (no filtering), BME680_FILTER_SIZE_1, BME680_FILTER_SIZE_3, BME680_FILTER_SIZE_7, BME680_FILTER_SIZE_15, BME680_FILTER_SIZE_31, BME680_FILTER_SIZE_63, BME680_FILTER_SIZE_127 + @return True on success, False on failure + +*/ +/**************************************************************************/ +bool Adafruit_BME680::setIIRFilterSize(uint8_t filtersize) { + if (filtersize > BME680_FILTER_SIZE_127) return false; + + gas_sensor.tph_sett.filter = filtersize; + + if (filtersize == BME680_FILTER_SIZE_0) + _filterEnabled = false; + else + _filterEnabled = true; + + return true; +} + +/**************************************************************************/ +/*! + @brief Reads 8 bit values over I2C +*/ +/**************************************************************************/ +int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { +#ifdef BME680_DEBUG + Serial.print("\tI2C $"); Serial.print(reg_addr, HEX); Serial.print(" => "); +#endif + + Wire.beginTransmission((uint8_t)dev_id); + Wire.write((uint8_t)reg_addr); + Wire.endTransmission(); + if (len != Wire.requestFrom((uint8_t)dev_id, (byte)len)) { +#ifdef BME680_DEBUG + Serial.print("Failed to read "); Serial.print(len); Serial.print(" bytes from "); Serial.println(dev_id, HEX); +#endif + return 1; + } + while (len--) { + *reg_data = (uint8_t)Wire.read(); +#ifdef BME680_DEBUG + Serial.print("0x"); Serial.print(*reg_data, HEX); Serial.print(", "); +#endif + reg_data++; + } +#ifdef BME680_DEBUG + Serial.println(""); +#endif + return 0; +} + +/**************************************************************************/ +/*! + @brief Writes 8 bit values over I2C +*/ +/**************************************************************************/ +int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { +#ifdef BME680_DEBUG + Serial.print("\tI2C $"); Serial.print(reg_addr, HEX); Serial.print(" <= "); +#endif + Wire.beginTransmission((uint8_t)dev_id); + Wire.write((uint8_t)reg_addr); + while (len--) { + Wire.write(*reg_data); +#ifdef BME680_DEBUG + Serial.print("0x"); Serial.print(*reg_data, HEX); Serial.print(", "); +#endif + reg_data++; + } + Wire.endTransmission(); +#ifdef BME680_DEBUG + Serial.println(""); +#endif + return 0; +} + + + +/**************************************************************************/ +/*! + @brief Reads 8 bit values over SPI +*/ +/**************************************************************************/ +static int8_t spi_read(uint8_t cspin, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { +#ifdef BME680_DEBUG + Serial.print("\tSPI $"); Serial.print(reg_addr, HEX); Serial.print(" => "); +#endif + + digitalWrite(cspin, LOW); + + // If hardware SPI we should use transactions! + if (_BME680_SoftwareSPI_SCK == -1) { + SPI.beginTransaction(SPISettings(BME680_DEFAULT_SPIFREQ, MSBFIRST, SPI_MODE0)); + } + + spi_transfer(reg_addr); + + while (len--) { + *reg_data = spi_transfer(0x00); +#ifdef BME680_DEBUG + Serial.print("0x"); Serial.print(*reg_data, HEX); Serial.print(", "); +#endif + reg_data++; + } + + if (_BME680_SoftwareSPI_SCK == -1) { + SPI.endTransaction(); + } + + digitalWrite(cspin, HIGH); + +#ifdef BME680_DEBUG + Serial.println(""); +#endif + return 0; +} + +/**************************************************************************/ +/*! + @brief Writes 8 bit values over SPI +*/ +/**************************************************************************/ +static int8_t spi_write(uint8_t cspin, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { +#ifdef BME680_DEBUG + Serial.print("\tSPI $"); Serial.print(reg_addr, HEX); Serial.print(" <= "); +#endif + + digitalWrite(cspin, LOW); + + // If hardware SPI we should use transactions! + if (_BME680_SoftwareSPI_SCK == -1) { + SPI.beginTransaction(SPISettings(BME680_DEFAULT_SPIFREQ, MSBFIRST, SPI_MODE0)); + } + + spi_transfer(reg_addr); + while (len--) { + spi_transfer(*reg_data); +#ifdef BME680_DEBUG + Serial.print("0x"); Serial.print(*reg_data, HEX); Serial.print(", "); +#endif + reg_data++; + } + + if (_BME680_SoftwareSPI_SCK == -1) { + SPI.endTransaction(); + } + + digitalWrite(cspin, HIGH); + +#ifdef BME680_DEBUG + Serial.println(""); +#endif + return 0; +} + + +static uint8_t spi_transfer(uint8_t x) { + if (_BME680_SoftwareSPI_SCK == -1) + return SPI.transfer(x); + + // software spi + //Serial.println("Software SPI"); + uint8_t reply = 0; + for (int i=7; i>=0; i--) { + reply <<= 1; + digitalWrite(_BME680_SoftwareSPI_SCK, LOW); + digitalWrite(_BME680_SoftwareSPI_MOSI, x & (1< http://www.adafruit.com/products/XXXX + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#ifndef __BME680_H__ +#define __BME680_H__ + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif +#include +#include +#include +#include +#include "bme680.h" + + +/*========================================================================= + I2C ADDRESS/BITS + -----------------------------------------------------------------------*/ +#define BME680_DEFAULT_ADDRESS (0x77) +/*=========================================================================*/ +#define BME680_DEFAULT_SPIFREQ (1000000) + + + +/* +class Adafruit_BME680_Unified : public Adafruit_Sensor +{ +public: + Adafruit_BME680_Unified(int32_t sensorID = -1); + + bool begin(uint8_t addr = BME680_ADDRESS); + void getTemperature(float *temp); + void getPressure(float *pressure); + float pressureToAltitude(float seaLevel, float atmospheric, float temp); + float seaLevelForAltitude(float altitude, float atmospheric, float temp); + void getEvent(sensors_event_t*); + void getSensor(sensor_t*); + + private: + uint8_t _i2c_addr; + int32_t _sensorID; +}; + +*/ + +/** Adafruit_BME680 Class for both I2C and SPI usage. + * Wraps the Bosch library for Arduino usage + */ + +class Adafruit_BME680 +{ + public: + Adafruit_BME680(int8_t cspin = -1); + Adafruit_BME680(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); + + bool begin(uint8_t addr = BME680_DEFAULT_ADDRESS); + float readTemperature(void); + float readPressure(void); + float readHumidity(void); + uint32_t readGas(void); + float readAltitude(float seaLevel); + + bool setTemperatureOversampling(uint8_t os); + bool setPressureOversampling(uint8_t os); + bool setHumidityOversampling(uint8_t os); + bool setIIRFilterSize(uint8_t fs); + bool setGasHeater(uint16_t heaterTemp, uint16_t heaterTime); + + bool performReading(void); + + /// Temperature (Celsius) assigned after calling performReading() + float temperature; + /// Pressure (Pascals) assigned after calling performReading() + float pressure; + /// Humidity (RH %) assigned after calling performReading() + float humidity; + /// Gas resistor (ohms) assigned after calling performReading() + float gas_resistance; + private: + + bool _filterEnabled, _tempEnabled, _humEnabled, _presEnabled, _gasEnabled; + uint8_t _i2caddr; + int32_t _sensorID; + int8_t _cs; + + uint8_t spixfer(uint8_t x); + + struct bme680_dev gas_sensor; +}; + +#endif diff --git a/libraries/Adafruit_BME680-1.0.5/Doxyfile b/libraries/Adafruit_BME680-1.0.5/Doxyfile new file mode 100644 index 00000000000..f21c9d41dc3 --- /dev/null +++ b/libraries/Adafruit_BME680-1.0.5/Doxyfile @@ -0,0 +1,2492 @@ +# Doxyfile 1.8.13 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project. +# +# All text after a double hash (##) is considered a comment and is placed in +# front of the TAG it is preceding. +# +# All text after a single hash (#) is considered a comment and will be ignored. +# The format is: +# TAG = value [value, ...] +# For lists, items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (\" \"). + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all text +# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv +# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv +# for the list of possible encodings. +# The default value is: UTF-8. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by +# double-quotes, unless you are using Doxywizard) that should identify the +# project for which the documentation is generated. This name is used in the +# title of most generated pages and in a few other places. +# The default value is: My Project. + +PROJECT_NAME = "Adafruit BME680 Library" + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. This +# could be handy for archiving the generated documentation or if some version +# control system is used. + +PROJECT_NUMBER = + +# Using the PROJECT_BRIEF tag one can provide an optional one line description +# for a project that appears at the top of each page and should give viewer a +# quick idea about the purpose of the project. Keep the description short. + +PROJECT_BRIEF = + +# With the PROJECT_LOGO tag one can specify a logo or an icon that is included +# in the documentation. The maximum height of the logo should not exceed 55 +# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy +# the logo to the output directory. + +PROJECT_LOGO = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path +# into which the generated documentation will be written. If a relative path is +# entered, it will be relative to the location where doxygen was started. If +# left blank the current directory will be used. + +OUTPUT_DIRECTORY = + +# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub- +# directories (in 2 levels) under the output directory of each output format and +# will distribute the generated files over these directories. Enabling this +# option can be useful when feeding doxygen a huge amount of source files, where +# putting all generated files in the same directory would otherwise causes +# performance problems for the file system. +# The default value is: NO. + +CREATE_SUBDIRS = NO + +# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII +# characters to appear in the names of generated files. If set to NO, non-ASCII +# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode +# U+3044. +# The default value is: NO. + +ALLOW_UNICODE_NAMES = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese, +# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States), +# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian, +# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages), +# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian, +# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian, +# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish, +# Ukrainian and Vietnamese. +# The default value is: English. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member +# descriptions after the members that are listed in the file and class +# documentation (similar to Javadoc). Set to NO to disable this. +# The default value is: YES. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES, doxygen will prepend the brief +# description of a member or function before the detailed description +# +# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. +# The default value is: YES. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator that is +# used to form the text in various listings. Each string in this list, if found +# as the leading text of the brief description, will be stripped from the text +# and the result, after processing the whole list, is used as the annotated +# text. Otherwise, the brief description is used as-is. If left blank, the +# following values are used ($name is automatically replaced with the name of +# the entity):The $name class, The $name widget, The $name file, is, provides, +# specifies, contains, represents, a, an and the. + +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# doxygen will generate a detailed section even if there is only a brief +# description. +# The default value is: NO. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. +# The default value is: NO. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES, doxygen will prepend the full path +# before files name in the file list and in the header files. If set to NO the +# shortest path that makes the file name unique will be used +# The default value is: YES. + +FULL_PATH_NAMES = YES + +# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path. +# Stripping is only done if one of the specified strings matches the left-hand +# part of the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the path to +# strip. +# +# Note that you can specify absolute paths here, but also relative paths, which +# will be relative from the directory where doxygen is started. +# This tag requires that the tag FULL_PATH_NAMES is set to YES. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the +# path mentioned in the documentation of a class, which tells the reader which +# header file to include in order to use a class. If left blank only the name of +# the header file containing the class definition is used. Otherwise one should +# specify the list of include paths that are normally passed to the compiler +# using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but +# less readable) file names. This can be useful is your file systems doesn't +# support long names like on DOS, Mac, or CD-ROM. +# The default value is: NO. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the +# first line (until the first dot) of a Javadoc-style comment as the brief +# description. If set to NO, the Javadoc-style will behave just like regular Qt- +# style comments (thus requiring an explicit @brief command for a brief +# description.) +# The default value is: NO. + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first +# line (until the first dot) of a Qt-style comment as the brief description. If +# set to NO, the Qt-style will behave just like regular Qt-style comments (thus +# requiring an explicit \brief command for a brief description.) +# The default value is: NO. + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a +# multi-line C++ special comment block (i.e. a block of //! or /// comments) as +# a brief description. This used to be the default behavior. The new default is +# to treat a multi-line C++ comment block as a detailed description. Set this +# tag to YES if you prefer the old behavior instead. +# +# Note that setting this tag to YES also means that rational rose comments are +# not recognized any more. +# The default value is: NO. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the +# documentation from any documented member that it re-implements. +# The default value is: YES. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES then doxygen will produce a new +# page for each member. If set to NO, the documentation of a member will be part +# of the file/class/namespace that contains it. +# The default value is: NO. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen +# uses this value to replace tabs by spaces in code fragments. +# Minimum value: 1, maximum value: 16, default value: 4. + +TAB_SIZE = 4 + +# This tag can be used to specify a number of aliases that act as commands in +# the documentation. An alias has the form: +# name=value +# For example adding +# "sideeffect=@par Side Effects:\n" +# will allow you to put the command \sideeffect (or @sideeffect) in the +# documentation, which will result in a user-defined paragraph with heading +# "Side Effects:". You can put \n's in the value part of an alias to insert +# newlines. + +ALIASES = + +# This tag can be used to specify a number of word-keyword mappings (TCL only). +# A mapping has the form "name=value". For example adding "class=itcl::class" +# will allow you to use the command class in the itcl::class meaning. + +TCL_SUBST = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources +# only. Doxygen will then generate output that is more tailored for C. For +# instance, some of the names that are used will be different. The list of all +# members will be omitted, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or +# Python sources only. Doxygen will then generate output that is more tailored +# for that language. For instance, namespaces will be presented as packages, +# qualified scopes will look different, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources. Doxygen will then generate output that is tailored for Fortran. +# The default value is: NO. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for VHDL. +# The default value is: NO. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it +# parses. With this tag you can assign which parser to use for a given +# extension. Doxygen has a built-in mapping, but you can override or extend it +# using this tag. The format is ext=language, where ext is a file extension, and +# language is one of the parsers supported by doxygen: IDL, Java, Javascript, +# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran: +# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran: +# Fortran. In the later case the parser tries to guess whether the code is fixed +# or free formatted code, this is the default for Fortran type files), VHDL. For +# instance to make doxygen treat .inc files as Fortran files (default is PHP), +# and .f files as C (default is Fortran), use: inc=Fortran f=C. +# +# Note: For files without extension you can use no_extension as a placeholder. +# +# Note that for custom extensions you also need to set FILE_PATTERNS otherwise +# the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments +# according to the Markdown format, which allows for more readable +# documentation. See http://daringfireball.net/projects/markdown/ for details. +# The output of markdown processing is further processed by doxygen, so you can +# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in +# case of backward compatibilities issues. +# The default value is: YES. + +MARKDOWN_SUPPORT = YES + +# When the TOC_INCLUDE_HEADINGS tag is set to a non-zero value, all headings up +# to that level are automatically included in the table of contents, even if +# they do not have an id attribute. +# Note: This feature currently applies only to Markdown headings. +# Minimum value: 0, maximum value: 99, default value: 0. +# This tag requires that the tag MARKDOWN_SUPPORT is set to YES. + +TOC_INCLUDE_HEADINGS = 0 + +# When enabled doxygen tries to link words that correspond to documented +# classes, or namespaces to their corresponding documentation. Such a link can +# be prevented in individual cases by putting a % sign in front of the word or +# globally by setting AUTOLINK_SUPPORT to NO. +# The default value is: YES. + +AUTOLINK_SUPPORT = YES + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should set this +# tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); +# versus func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. +# The default value is: NO. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. +# The default value is: NO. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip (see: +# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen +# will parse them like normal C++ but will assume all classes use public instead +# of private inheritance when no explicit protection keyword is present. +# The default value is: NO. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate +# getter and setter methods for a property. Setting this option to YES will make +# doxygen to replace the get and set methods by a property in the documentation. +# This will only work if the methods are indeed getting or setting a simple +# type. If this is not the case, or you want to show the methods anyway, you +# should set this option to NO. +# The default value is: YES. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. +# The default value is: NO. + +DISTRIBUTE_GROUP_DOC = NO + +# If one adds a struct or class to a group and this option is enabled, then also +# any nested class or struct is added to the same group. By default this option +# is disabled and one has to add nested compounds explicitly via \ingroup. +# The default value is: NO. + +GROUP_NESTED_COMPOUNDS = NO + +# Set the SUBGROUPING tag to YES to allow class member groups of the same type +# (for instance a group of public functions) to be put as a subgroup of that +# type (e.g. under the Public Functions section). Set it to NO to prevent +# subgrouping. Alternatively, this can be done per class using the +# \nosubgrouping command. +# The default value is: YES. + +SUBGROUPING = YES + +# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions +# are shown inside the group in which they are included (e.g. using \ingroup) +# instead of on a separate page (for HTML and Man pages) or section (for LaTeX +# and RTF). +# +# Note that this feature does not work in combination with +# SEPARATE_MEMBER_PAGES. +# The default value is: NO. + +INLINE_GROUPED_CLASSES = NO + +# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions +# with only public data fields or simple typedef fields will be shown inline in +# the documentation of the scope in which they are defined (i.e. file, +# namespace, or group documentation), provided this scope is documented. If set +# to NO, structs, classes, and unions are shown on a separate page (for HTML and +# Man pages) or section (for LaTeX and RTF). +# The default value is: NO. + +INLINE_SIMPLE_STRUCTS = NO + +# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or +# enum is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically be +# useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. +# The default value is: NO. + +TYPEDEF_HIDES_STRUCT = NO + +# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This +# cache is used to resolve symbols given their name and scope. Since this can be +# an expensive process and often the same symbol appears multiple times in the +# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small +# doxygen will become slower. If the cache is too large, memory is wasted. The +# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range +# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536 +# symbols. At the end of a run doxygen will report the cache usage and suggest +# the optimal cache size from a speed point of view. +# Minimum value: 0, maximum value: 9, default value: 0. + +LOOKUP_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in +# documentation are documented, even if no documentation was available. Private +# class members and static file members will be hidden unless the +# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES. +# Note: This will also disable the warnings about undocumented members that are +# normally produced when WARNINGS is set to YES. +# The default value is: NO. + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will +# be included in the documentation. +# The default value is: NO. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_PACKAGE tag is set to YES, all members with package or internal +# scope will be included in the documentation. +# The default value is: NO. + +EXTRACT_PACKAGE = NO + +# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be +# included in the documentation. +# The default value is: NO. + +EXTRACT_STATIC = NO + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES, classes (and structs) defined +# locally in source files will be included in the documentation. If set to NO, +# only classes defined in header files are included. Does not have any effect +# for Java sources. +# The default value is: YES. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. If set to YES, local methods, +# which are defined in the implementation section but not in the interface are +# included in the documentation. If set to NO, only methods in the interface are +# included. +# The default value is: NO. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base name of +# the file that contains the anonymous namespace. By default anonymous namespace +# are hidden. +# The default value is: NO. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all +# undocumented members inside documented classes or files. If set to NO these +# members will be included in the various overviews, but no documentation +# section is generated. This option has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. If set +# to NO, these classes will be included in the various overviews. This option +# has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend +# (class|struct|union) declarations. If set to NO, these declarations will be +# included in the documentation. +# The default value is: NO. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any +# documentation blocks found inside the body of a function. If set to NO, these +# blocks will be appended to the function's detailed documentation block. +# The default value is: NO. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation that is typed after a +# \internal command is included. If the tag is set to NO then the documentation +# will be excluded. Set it to YES to include the internal documentation. +# The default value is: NO. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file +# names in lower-case letters. If set to YES, upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. +# The default value is: system dependent. + +CASE_SENSE_NAMES = NO + +# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with +# their full class and namespace scopes in the documentation. If set to YES, the +# scope will be hidden. +# The default value is: NO. + +HIDE_SCOPE_NAMES = NO + +# If the HIDE_COMPOUND_REFERENCE tag is set to NO (default) then doxygen will +# append additional text to a page's title, such as Class Reference. If set to +# YES the compound reference will be hidden. +# The default value is: NO. + +HIDE_COMPOUND_REFERENCE= NO + +# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of +# the files that are included by a file in the documentation of that file. +# The default value is: YES. + +SHOW_INCLUDE_FILES = YES + +# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each +# grouped member an include statement to the documentation, telling the reader +# which file to include in order to use the member. +# The default value is: NO. + +SHOW_GROUPED_MEMB_INC = NO + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include +# files with double quotes in the documentation rather than with sharp brackets. +# The default value is: NO. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the +# documentation for inline members. +# The default value is: YES. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the +# (detailed) documentation of file and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. +# The default value is: YES. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief +# descriptions of file, namespace and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. Note that +# this will also influence the order of the classes in the class list. +# The default value is: NO. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the +# (brief and detailed) documentation of class members so that constructors and +# destructors are listed first. If set to NO the constructors will appear in the +# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS. +# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief +# member documentation. +# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting +# detailed member documentation. +# The default value is: NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy +# of group names into alphabetical order. If set to NO the group names will +# appear in their defined order. +# The default value is: NO. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by +# fully-qualified names, including namespaces. If set to NO, the class list will +# be sorted only by class name, not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the alphabetical +# list. +# The default value is: NO. + +SORT_BY_SCOPE_NAME = NO + +# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper +# type resolution of all parameters of a function it will reject a match between +# the prototype and the implementation of a member function even if there is +# only one candidate or it is obvious which candidate to choose by doing a +# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still +# accept a match between prototype and implementation in such cases. +# The default value is: NO. + +STRICT_PROTO_MATCHING = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or disable (NO) the todo +# list. This list is created by putting \todo commands in the documentation. +# The default value is: YES. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or disable (NO) the test +# list. This list is created by putting \test commands in the documentation. +# The default value is: YES. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or disable (NO) the bug +# list. This list is created by putting \bug commands in the documentation. +# The default value is: YES. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO) +# the deprecated list. This list is created by putting \deprecated commands in +# the documentation. +# The default value is: YES. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional documentation +# sections, marked by \if ... \endif and \cond +# ... \endcond blocks. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the +# initial value of a variable or macro / define can have for it to appear in the +# documentation. If the initializer consists of more lines than specified here +# it will be hidden. Use a value of 0 to hide initializers completely. The +# appearance of the value of individual variables and macros / defines can be +# controlled using \showinitializer or \hideinitializer command in the +# documentation regardless of this setting. +# Minimum value: 0, maximum value: 10000, default value: 30. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at +# the bottom of the documentation of classes and structs. If set to YES, the +# list will mention the files that were used to generate the documentation. +# The default value is: YES. + +SHOW_USED_FILES = YES + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This +# will remove the Files entry from the Quick Index and from the Folder Tree View +# (if specified). +# The default value is: YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces +# page. This will remove the Namespaces entry from the Quick Index and from the +# Folder Tree View (if specified). +# The default value is: YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command command input-file, where command is the value of the +# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided +# by doxygen. Whatever the program writes to standard output is used as the file +# version. For an example see the documentation. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed +# by doxygen. The layout file controls the global structure of the generated +# output files in an output format independent way. To create the layout file +# that represents doxygen's defaults, run doxygen with the -l option. You can +# optionally specify a file name after the option, if omitted DoxygenLayout.xml +# will be used as the name of the layout file. +# +# Note that if you run doxygen from a directory containing a file called +# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE +# tag is left empty. + +LAYOUT_FILE = + +# The CITE_BIB_FILES tag can be used to specify one or more bib files containing +# the reference definitions. This must be a list of .bib files. The .bib +# extension is automatically appended if omitted. This requires the bibtex tool +# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info. +# For LaTeX the style of the bibliography can be controlled using +# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the +# search path. See also \cite for info how to create references. + +CITE_BIB_FILES = + +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated to +# standard output by doxygen. If QUIET is set to YES this implies that the +# messages are off. +# The default value is: NO. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES +# this implies that the warnings are on. +# +# Tip: Turn warnings on while writing the documentation. +# The default value is: YES. + +WARNINGS = YES + +# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate +# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag +# will automatically be disabled. +# The default value is: YES. + +WARN_IF_UNDOCUMENTED = YES + +# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some parameters +# in a documented function, or documenting parameters that don't exist or using +# markup commands wrongly. +# The default value is: YES. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that +# are documented, but have no documentation for their parameters or return +# value. If set to NO, doxygen will only warn about wrong or incomplete +# parameter documentation, but not about the absence of documentation. +# The default value is: NO. + +WARN_NO_PARAMDOC = NO + +# If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when +# a warning is encountered. +# The default value is: NO. + +WARN_AS_ERROR = NO + +# The WARN_FORMAT tag determines the format of the warning messages that doxygen +# can produce. The string should contain the $file, $line, and $text tags, which +# will be replaced by the file and line number from which the warning originated +# and the warning text. Optionally the format may contain $version, which will +# be replaced by the version of the file (if it could be obtained via +# FILE_VERSION_FILTER) +# The default value is: $file:$line: $text. + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning and error +# messages should be written. If left blank the output is written to standard +# error (stderr). + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag is used to specify the files and/or directories that contain +# documented source files. You may enter file names like myfile.cpp or +# directories like /usr/src/myproject. Separate the files or directories with +# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING +# Note: If this tag is empty the current directory is searched. + +INPUT = + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses +# libiconv (or the iconv built into libc) for the transcoding. See the libiconv +# documentation (see: http://www.gnu.org/software/libiconv) for the list of +# possible encodings. +# The default value is: UTF-8. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and +# *.h) to filter out the source-files in the directories. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# read by doxygen. +# +# If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp, +# *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h, +# *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc, +# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08, +# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf and *.qsf. + +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.idl \ + *.ddl \ + *.odl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.cs \ + *.d \ + *.php \ + *.php4 \ + *.php5 \ + *.phtml \ + *.inc \ + *.m \ + *.markdown \ + *.md \ + *.mm \ + *.dox \ + *.py \ + *.pyw \ + *.f90 \ + *.f95 \ + *.f03 \ + *.f08 \ + *.f \ + *.for \ + *.tcl \ + *.vhd \ + *.vhdl \ + *.ucf \ + *.qsf + +# The RECURSIVE tag can be used to specify whether or not subdirectories should +# be searched for input files as well. +# The default value is: NO. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should be +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. +# +# Note that relative paths are relative to the directory from which doxygen is +# run. + +EXCLUDE = bme680.c bme680.h bme680_defs.h + +# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or +# directories that are symbolic links (a Unix file system feature) are excluded +# from the input. +# The default value is: NO. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories use the pattern */test/* + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or directories +# that contain example code fragments that are included (see the \include +# command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and +# *.h) to filter out the source-files in the directories. If left blank all +# files are included. + +EXAMPLE_PATTERNS = * + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude commands +# irrespective of the value of the RECURSIVE tag. +# The default value is: NO. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or directories +# that contain images that are to be included in the documentation (see the +# \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command: +# +# +# +# where is the value of the INPUT_FILTER tag, and is the +# name of an input file. Doxygen will then use the output that the filter +# program writes to standard output. If FILTER_PATTERNS is specified, this tag +# will be ignored. +# +# Note that the filter must not add or remove lines; it is applied before the +# code is scanned, but not when the output code is generated. If lines are added +# or removed, the anchors will not be placed correctly. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# properly processed by doxygen. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: pattern=filter +# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how +# filters are used. If the FILTER_PATTERNS tag is empty or if none of the +# patterns match the file name, INPUT_FILTER is applied. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# properly processed by doxygen. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will also be used to filter the input files that are used for +# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES). +# The default value is: NO. + +FILTER_SOURCE_FILES = NO + +# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file +# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and +# it is also possible to disable source filtering for a specific pattern using +# *.ext= (so without naming a filter). +# This tag requires that the tag FILTER_SOURCE_FILES is set to YES. + +FILTER_SOURCE_PATTERNS = + +# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that +# is part of the input, its contents will be placed on the main page +# (index.html). This can be useful if you have a project on for instance GitHub +# and want to reuse the introduction page also for the doxygen output. + +USE_MDFILE_AS_MAINPAGE = + +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will be +# generated. Documented entities will be cross-referenced with these sources. +# +# Note: To get rid of all source code in the generated output, make sure that +# also VERBATIM_HEADERS is set to NO. +# The default value is: NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body of functions, +# classes and enums directly into the documentation. +# The default value is: NO. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any +# special comment blocks from generated source code fragments. Normal C, C++ and +# Fortran comments will always remain visible. +# The default value is: YES. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES then for each documented +# function all documented functions referencing it will be listed. +# The default value is: NO. + +REFERENCED_BY_RELATION = NO + +# If the REFERENCES_RELATION tag is set to YES then for each documented function +# all documented entities called/used by that function will be listed. +# The default value is: NO. + +REFERENCES_RELATION = NO + +# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set +# to YES then the hyperlinks from functions in REFERENCES_RELATION and +# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will +# link to the documentation. +# The default value is: YES. + +REFERENCES_LINK_SOURCE = YES + +# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the +# source code will show a tooltip with additional information such as prototype, +# brief description and links to the definition and documentation. Since this +# will make the HTML file larger and loading of large files a bit slower, you +# can opt to disable this feature. +# The default value is: YES. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +SOURCE_TOOLTIPS = YES + +# If the USE_HTAGS tag is set to YES then the references to source code will +# point to the HTML generated by the htags(1) tool instead of doxygen built-in +# source browser. The htags tool is part of GNU's global source tagging system +# (see http://www.gnu.org/software/global/global.html). You will need version +# 4.8.6 or higher. +# +# To use it do the following: +# - Install the latest version of global +# - Enable SOURCE_BROWSER and USE_HTAGS in the config file +# - Make sure the INPUT points to the root of the source tree +# - Run doxygen as normal +# +# Doxygen will invoke htags (and that will in turn invoke gtags), so these +# tools must be available from the command line (i.e. in the search path). +# +# The result: instead of the source browser generated by doxygen, the links to +# source code will now point to the output of htags. +# The default value is: NO. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a +# verbatim copy of the header file for each class for which an include is +# specified. Set to NO to disable this. +# See also: Section \class. +# The default value is: YES. + +VERBATIM_HEADERS = YES + +# If the CLANG_ASSISTED_PARSING tag is set to YES then doxygen will use the +# clang parser (see: http://clang.llvm.org/) for more accurate parsing at the +# cost of reduced performance. This can be particularly helpful with template +# rich C++ code for which doxygen's built-in parser lacks the necessary type +# information. +# Note: The availability of this option depends on whether or not doxygen was +# generated with the -Duse-libclang=ON option for CMake. +# The default value is: NO. + +CLANG_ASSISTED_PARSING = NO + +# If clang assisted parsing is enabled you can provide the compiler with command +# line options that you would normally use when invoking the compiler. Note that +# the include paths will already be set by doxygen for the files and directories +# specified with INPUT and INCLUDE_PATH. +# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES. + +CLANG_OPTIONS = + +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all +# compounds will be generated. Enable this if the project contains a lot of +# classes, structs, unions or interfaces. +# The default value is: YES. + +ALPHABETICAL_INDEX = YES + +# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in +# which the alphabetical index list will be split. +# Minimum value: 1, maximum value: 20, default value: 5. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all classes will +# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag +# can be used to specify a prefix (or a list of prefixes) that should be ignored +# while generating the index headers. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output +# The default value is: YES. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a +# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of +# it. +# The default directory is: html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_OUTPUT = docs + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each +# generated HTML page (for example: .htm, .php, .asp). +# The default value is: .html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a user-defined HTML header file for +# each generated HTML page. If the tag is left blank doxygen will generate a +# standard header. +# +# To get valid HTML the header file that includes any scripts and style sheets +# that doxygen needs, which is dependent on the configuration options used (e.g. +# the setting GENERATE_TREEVIEW). It is highly recommended to start with a +# default header using +# doxygen -w html new_header.html new_footer.html new_stylesheet.css +# YourConfigFile +# and then modify the file new_header.html. See also section "Doxygen usage" +# for information on how to generate the default header that doxygen normally +# uses. +# Note: The header is subject to change so you typically have to regenerate the +# default header when upgrading to a newer version of doxygen. For a description +# of the possible markers and block names see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each +# generated HTML page. If the tag is left blank doxygen will generate a standard +# footer. See HTML_HEADER for more information on how to generate a default +# footer and what special commands can be used inside the footer. See also +# section "Doxygen usage" for information on how to generate the default footer +# that doxygen normally uses. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style +# sheet that is used by each HTML page. It can be used to fine-tune the look of +# the HTML output. If left blank doxygen will generate a default style sheet. +# See also section "Doxygen usage" for information on how to generate the style +# sheet that doxygen normally uses. +# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as +# it is more robust and this tag (HTML_STYLESHEET) will in the future become +# obsolete. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_STYLESHEET = + +# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined +# cascading style sheets that are included after the standard style sheets +# created by doxygen. Using this option one can overrule certain style aspects. +# This is preferred over using HTML_STYLESHEET since it does not replace the +# standard style sheet and is therefore more robust against future updates. +# Doxygen will copy the style sheet files to the output directory. +# Note: The order of the extra style sheet files is of importance (e.g. the last +# style sheet in the list overrules the setting of the previous ones in the +# list). For an example see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_STYLESHEET = + +# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or +# other source files which should be copied to the HTML output directory. Note +# that these files will be copied to the base HTML output directory. Use the +# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these +# files. In the HTML_STYLESHEET file, use the file name only. Also note that the +# files will be copied as-is; there are no commands or markers available. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_FILES = + +# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen +# will adjust the colors in the style sheet and background images according to +# this color. Hue is specified as an angle on a colorwheel, see +# http://en.wikipedia.org/wiki/Hue for more information. For instance the value +# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300 +# purple, and 360 is red again. +# Minimum value: 0, maximum value: 359, default value: 220. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_HUE = 220 + +# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors +# in the HTML output. For a value of 0 the output will use grayscales only. A +# value of 255 will produce the most vivid colors. +# Minimum value: 0, maximum value: 255, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_SAT = 100 + +# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the +# luminance component of the colors in the HTML output. Values below 100 +# gradually make the output lighter, whereas values above 100 make the output +# darker. The value divided by 100 is the actual gamma applied, so 80 represents +# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not +# change the gamma. +# Minimum value: 40, maximum value: 240, default value: 80. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_GAMMA = 80 + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting this +# to YES can help to show when doxygen was last run and thus if the +# documentation is up to date. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_TIMESTAMP = NO + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_DYNAMIC_SECTIONS = NO + +# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries +# shown in the various tree structured indices initially; the user can expand +# and collapse entries dynamically later on. Doxygen will expand the tree to +# such a level that at most the specified number of entries are visible (unless +# a fully collapsed tree already exceeds this amount). So setting the number of +# entries 1 will produce a full collapsed tree by default. 0 is a special value +# representing an infinite number of entries and will result in a full expanded +# tree by default. +# Minimum value: 0, maximum value: 9999, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_INDEX_NUM_ENTRIES = 100 + +# If the GENERATE_DOCSET tag is set to YES, additional index files will be +# generated that can be used as input for Apple's Xcode 3 integrated development +# environment (see: http://developer.apple.com/tools/xcode/), introduced with +# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a +# Makefile in the HTML output directory. Running make will produce the docset in +# that directory and running make install will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at +# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html +# for more information. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_DOCSET = NO + +# This tag determines the name of the docset feed. A documentation feed provides +# an umbrella under which multiple documentation sets from a single provider +# (such as a company or product suite) can be grouped. +# The default value is: Doxygen generated docs. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# This tag specifies a string that should uniquely identify the documentation +# set bundle. This should be a reverse domain-name style string, e.g. +# com.mycompany.MyDocSet. Doxygen will append .docset to the name. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify +# the documentation publisher. This should be a reverse domain-name style +# string, e.g. com.mycompany.MyDocSet.documentation. +# The default value is: org.doxygen.Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_ID = org.doxygen.Publisher + +# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher. +# The default value is: Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_NAME = Publisher + +# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three +# additional HTML index files: index.hhp, index.hhc, and index.hhk. The +# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop +# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on +# Windows. +# +# The HTML Help Workshop contains a compiler that can convert all HTML output +# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML +# files are now used as the Windows 98 help format, and will replace the old +# Windows help format (.hlp) on all Windows platforms in the future. Compressed +# HTML files also contain an index, a table of contents, and you can search for +# words in the documentation. The HTML workshop also contains a viewer for +# compressed HTML files. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_HTMLHELP = NO + +# The CHM_FILE tag can be used to specify the file name of the resulting .chm +# file. You can add a path in front of the file if the result should not be +# written to the html output directory. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_FILE = + +# The HHC_LOCATION tag can be used to specify the location (absolute path +# including file name) of the HTML help compiler (hhc.exe). If non-empty, +# doxygen will try to run the HTML help compiler on the generated index.hhp. +# The file has to be specified with full path. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +HHC_LOCATION = + +# The GENERATE_CHI flag controls if a separate .chi index file is generated +# (YES) or that it should be included in the master .chm file (NO). +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +GENERATE_CHI = NO + +# The CHM_INDEX_ENCODING is used to encode HtmlHelp index (hhk), content (hhc) +# and project file content. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_INDEX_ENCODING = + +# The BINARY_TOC flag controls whether a binary table of contents is generated +# (YES) or a normal table of contents (NO) in the .chm file. Furthermore it +# enables the Previous and Next buttons. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members to +# the table of contents of the HTML help documentation and to the tree view. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and +# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that +# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help +# (.qch) of the generated HTML documentation. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify +# the file name of the resulting .qch file. The path specified is relative to +# the HTML output folder. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help +# Project output. For more information please see Qt Help Project / Namespace +# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace). +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt +# Help Project output. For more information please see Qt Help Project / Virtual +# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual- +# folders). +# The default value is: doc. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_VIRTUAL_FOLDER = doc + +# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom +# filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the +# custom filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this +# project's filter section matches. Qt Help Project / Filter Attributes (see: +# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_SECT_FILTER_ATTRS = + +# The QHG_LOCATION tag can be used to specify the location of Qt's +# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the +# generated .qhp file. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be +# generated, together with the HTML files, they form an Eclipse help plugin. To +# install this plugin and make it available under the help contents menu in +# Eclipse, the contents of the directory containing the HTML and XML files needs +# to be copied into the plugins directory of eclipse. The name of the directory +# within the plugins directory should be the same as the ECLIPSE_DOC_ID value. +# After copying Eclipse needs to be restarted before the help appears. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the Eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have this +# name. Each documentation set should have its own identifier. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# If you want full control over the layout of the generated HTML pages it might +# be necessary to disable the index and replace it with your own. The +# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top +# of each HTML page. A value of NO enables the index and the value YES disables +# it. Since the tabs in the index contain the same information as the navigation +# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +DISABLE_INDEX = NO + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. If the tag +# value is set to YES, a side panel will be generated containing a tree-like +# index structure (just like the one that is generated for HTML Help). For this +# to work a browser that supports JavaScript, DHTML, CSS and frames is required +# (i.e. any modern browser). Windows users are probably better off using the +# HTML help feature. Via custom style sheets (see HTML_EXTRA_STYLESHEET) one can +# further fine-tune the look of the index. As an example, the default style +# sheet generated by doxygen has an example that shows how to put an image at +# the root of the tree instead of the PROJECT_NAME. Since the tree basically has +# the same information as the tab index, you could consider setting +# DISABLE_INDEX to YES when enabling this option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_TREEVIEW = NO + +# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that +# doxygen will group on one line in the generated HTML documentation. +# +# Note that a value of 0 will completely suppress the enum values from appearing +# in the overview section. +# Minimum value: 0, maximum value: 20, default value: 4. +# This tag requires that the tag GENERATE_HTML is set to YES. + +ENUM_VALUES_PER_LINE = 4 + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used +# to set the initial width (in pixels) of the frame in which the tree is shown. +# Minimum value: 0, maximum value: 1500, default value: 250. +# This tag requires that the tag GENERATE_HTML is set to YES. + +TREEVIEW_WIDTH = 250 + +# If the EXT_LINKS_IN_WINDOW option is set to YES, doxygen will open links to +# external symbols imported via tag files in a separate window. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +EXT_LINKS_IN_WINDOW = NO + +# Use this tag to change the font size of LaTeX formulas included as images in +# the HTML documentation. When you change the font size after a successful +# doxygen run you need to manually remove any form_*.png images from the HTML +# output directory to force them to be regenerated. +# Minimum value: 8, maximum value: 50, default value: 10. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_FONTSIZE = 10 + +# Use the FORMULA_TRANPARENT tag to determine whether or not the images +# generated for formulas are transparent PNGs. Transparent PNGs are not +# supported properly for IE 6.0, but are supported on all modern browsers. +# +# Note that when changing this option you need to delete any form_*.png files in +# the HTML output directory before the changes have effect. +# The default value is: YES. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_TRANSPARENT = YES + +# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see +# http://www.mathjax.org) which uses client side Javascript for the rendering +# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX +# installed or if you want to formulas look prettier in the HTML output. When +# enabled you may also need to install MathJax separately and configure the path +# to it using the MATHJAX_RELPATH option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +USE_MATHJAX = NO + +# When MathJax is enabled you can set the default output format to be used for +# the MathJax output. See the MathJax site (see: +# http://docs.mathjax.org/en/latest/output.html) for more details. +# Possible values are: HTML-CSS (which is slower, but has the best +# compatibility), NativeMML (i.e. MathML) and SVG. +# The default value is: HTML-CSS. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_FORMAT = HTML-CSS + +# When MathJax is enabled you need to specify the location relative to the HTML +# output directory using the MATHJAX_RELPATH option. The destination directory +# should contain the MathJax.js script. For instance, if the mathjax directory +# is located at the same level as the HTML output directory, then +# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax +# Content Delivery Network so you can quickly see the result without installing +# MathJax. However, it is strongly recommended to install a local copy of +# MathJax from http://www.mathjax.org before deployment. +# The default value is: http://cdn.mathjax.org/mathjax/latest. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest + +# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax +# extension names that should be enabled during MathJax rendering. For example +# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_EXTENSIONS = + +# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces +# of code that will be used on startup of the MathJax code. See the MathJax site +# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an +# example see the documentation. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_CODEFILE = + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box for +# the HTML output. The underlying search engine uses javascript and DHTML and +# should work on any modern browser. Note that when using HTML help +# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) +# there is already a search function so this one should typically be disabled. +# For large projects the javascript based search engine can be slow, then +# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to +# search using the keyboard; to jump to the search box use + S +# (what the is depends on the OS and browser, but it is typically +# , /