diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 93b63c6..039d210 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -1,3 +1,24 @@ +// The MIT License (MIT) +// +// Copyright (c) 2015 Adafruit Industries +// +// 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. #include "Adafruit_MQTT.h" @@ -342,6 +363,18 @@ bool Adafruit_MQTT_Publish::publish(int32_t i) { return mqtt->publish(topic, payload, qos); } +bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) { + char payload[40]; // Need to technically hold float max, 39 digits and minus sign. + #if defined(ARDUINO_ARCH_AVR) + // Use avrlibc dtostre function on AVR platforms. + dtostrf(f, 0, precision, payload); + #else + // Otherwise fall back to snprintf on other platforms. + snprintf(payload, sizeof(payload)-1, "%f", f); + #endif + return mqtt->publish(topic, payload, qos); +} + bool Adafruit_MQTT_Publish::publish(uint32_t i) { char payload[18]; itoa(i, payload, 10); diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index bea6924..ba4b39f 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -1,3 +1,24 @@ +// The MIT License (MIT) +// +// Copyright (c) 2015 Adafruit Industries +// +// 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. #ifndef _ADAFRUIT_MQTT_H_ #define _ADAFRUIT_MQTT_H_ @@ -148,7 +169,8 @@ class Adafruit_MQTT_Publish { Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0); bool publish(char *s); - //bool publish(double f); + bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal. + // This might be ignored and a higher precision value sent. bool publish(int32_t i); bool publish(uint32_t i); diff --git a/Adafruit_MQTT_CC3000.h b/Adafruit_MQTT_CC3000.h index f75cad8..47ea841 100644 --- a/Adafruit_MQTT_CC3000.h +++ b/Adafruit_MQTT_CC3000.h @@ -1,3 +1,24 @@ +// The MIT License (MIT) +// +// Copyright (c) 2015 Adafruit Industries +// +// 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. #ifndef _ADAFRUIT_MQTT_CC3000_H_ #define _ADAFRUIT_MQTT_CC3000_H_ @@ -5,6 +26,7 @@ #include #include "Adafruit_MQTT.h" + // delay in ms between calls of available() #define MQTT_CC3000_INTERAVAILDELAY 10 diff --git a/Adafruit_MQTT_Client.cpp b/Adafruit_MQTT_Client.cpp index c956876..d91e04b 100644 --- a/Adafruit_MQTT_Client.cpp +++ b/Adafruit_MQTT_Client.cpp @@ -1,3 +1,24 @@ +// The MIT License (MIT) +// +// Copyright (c) 2015 Adafruit Industries +// +// 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. #include "Adafruit_MQTT_Client.h" diff --git a/Adafruit_MQTT_Client.h b/Adafruit_MQTT_Client.h index 7d9e4b5..1bce8e4 100644 --- a/Adafruit_MQTT_Client.h +++ b/Adafruit_MQTT_Client.h @@ -1,16 +1,38 @@ +// The MIT License (MIT) +// +// Copyright (c) 2015 Adafruit Industries +// +// 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. #ifndef _ADAFRUIT_MQTT_CLIENT_H_ #define _ADAFRUIT_MQTT_CLIENT_H_ #include "Client.h" #include "Adafruit_MQTT.h" + // How long to delay waiting for new data to be available in readPacket. #define MQTT_CLIENT_READINTERVAL_MS 10 + // MQTT client implementation for a generic Arduino Client interface. Can work // with almost all Arduino network hardware like ethernet shield, wifi shield, // and even other platforms like ESP8266. - class Adafruit_MQTT_Client : public Adafruit_MQTT { public: Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port, diff --git a/Adafruit_MQTT_FONA.h b/Adafruit_MQTT_FONA.h index afdb71a..631a93e 100644 --- a/Adafruit_MQTT_FONA.h +++ b/Adafruit_MQTT_FONA.h @@ -1,3 +1,24 @@ +// The MIT License (MIT) +// +// Copyright (c) 2015 Adafruit Industries +// +// 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. #ifndef _ADAFRUIT_MQTT_FONA_H_ #define _ADAFRUIT_MQTT_FONA_H_ @@ -7,6 +28,11 @@ #define MQTT_FONA_INTERAVAILDELAY 100 #define MQTT_FONA_QUERYDELAY 500 + +// FONA-specific version of the Adafruit_MQTT class. +// Note that this is defined as a header-only class to prevent issues with using +// the library on non-FONA platforms (since Arduino will include all .cpp files +// in the compilation of the library). class Adafruit_MQTT_FONA : public Adafruit_MQTT { public: Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port, @@ -109,4 +135,5 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT { Adafruit_FONA *fona; }; + #endif diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..04ebb92 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Adafruit Industries + +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/README.md b/README.md index 6cc012e..1807c8c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,32 @@ -# Adafruit_MQTT_Library -Arduino library for MQTT support +# Adafruit MQTT Library -ToDo: - * QoS > 0 for publication - * Subscription callbacks - * example with way more timeout/reconnection protection - * Wills - * remove watchdog \ No newline at end of file +Arduino library for MQTT support, including access to Adafruit IO. Works with +the Adafruit CC3000, FONA, ESP8266 Arduino platforms, and anything that supports +Arduino's Client interface (like Ethernet shield). + +See included examples for how to use the library to access an MQTT service to +publish and subscribe to feeds. Note that this does not support the full MQTT +spec but is intended to support enough for QoS 0 and 1 publishing. + +Depends on the following other libraries depending on the target platform: + + - [Adafruit SleepyDog](https://github.com/adafruit/Adafruit_SleepyDog), watchdog + library used by FONA and CC3000 code for reliability. + + - [Adafruit CC3000](https://github.com/adafruit/Adafruit_CC3000_Library), required + for the CC3000 hardware. + + - [Adafruit FONA](https://github.com/adafruit/Adafruit_FONA_Library), required for + the FONA hardware. + +Future todos: + + - QoS > 0 for publication + + - Subscription callbacks + + - example with way more timeout/reconnection protection + + - Wills + + - remove watchdog diff --git a/examples/mqtt_cc3k/mqtt_cc3k.ino b/examples/mqtt_cc3k/mqtt_cc3k.ino index 4ef3c6d..d1f1da8 100644 --- a/examples/mqtt_cc3k/mqtt_cc3k.ino +++ b/examples/mqtt_cc3k/mqtt_cc3k.ino @@ -1,3 +1,16 @@ +/*************************************************** + Adafruit MQTT Library CC3000 Example + + Designed specifically to work with the Adafruit WiFi products: + ----> https://www.adafruit.com/products/1469 + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + MIT license, all text above must be included in any redistribution + ****************************************************/ #include #include #include @@ -13,11 +26,6 @@ // Use hardware SPI for the remaining pins // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 -// Alternate config: -//#define ADAFRUIT_CC3000_IRQ 2 -//#define ADAFRUIT_CC3000_VBAT 8 -//#define ADAFRUIT_CC3000_CS 10 - /************************* WiFi Access Point *********************************/ #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! @@ -32,11 +40,6 @@ #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." #define AIO_KEY "...your AIO key..."; -/* Debug server configuration (not normally used): -#define AIO_SERVER "apt.adafruit.com" -#define AIO_SERVERPORT 1883 -*/ - /************ Global State (you don't need to change this!) ******************/ // Setup the main CC3000 class, just like a normal CC3000 sketch. diff --git a/examples/AIO_ESP8266/AIO_ESP8266.ino b/examples/mqtt_esp8266/mqtt_esp8266.ino similarity index 86% rename from examples/AIO_ESP8266/AIO_ESP8266.ino rename to examples/mqtt_esp8266/mqtt_esp8266.ino index bd7c3c6..38bf909 100644 --- a/examples/AIO_ESP8266/AIO_ESP8266.ino +++ b/examples/mqtt_esp8266/mqtt_esp8266.ino @@ -1,6 +1,19 @@ -// Adafruit IO MQTT Example for ESP8266. -// Must be using ESP8266 Arduino from: -// https://github.com/esp8266/Arduino +/*************************************************** + Adafruit MQTT Library ESP8266 Example + + Must use ESP8266 Arduino from: + https://github.com/esp8266/Arduino + + Works great with Adafruit's Huzzah ESP board: + ----> https://www.adafruit.com/product/2471 + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Tony DiCola for Adafruit Industries. + MIT license, all text above must be included in any redistribution + ****************************************************/ #include #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" @@ -17,11 +30,6 @@ #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." #define AIO_KEY "...your AIO key..."; -/* Debug server configuration (not normally used): -#define AIO_SERVER "apt.adafruit.com" -#define AIO_SERVERPORT 1883 -*/ - /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. diff --git a/examples/mqtt_fona/mqtt_fona.ino b/examples/mqtt_fona/mqtt_fona.ino index 3ac1d35..f47b323 100644 --- a/examples/mqtt_fona/mqtt_fona.ino +++ b/examples/mqtt_fona/mqtt_fona.ino @@ -1,3 +1,22 @@ +/*************************************************** + Adafruit MQTT Library FONA Example + + Designed specifically to work with the Adafruit FONA + ----> http://www.adafruit.com/products/1946 + ----> http://www.adafruit.com/products/1963 + ----> http://www.adafruit.com/products/2468 + ----> http://www.adafruit.com/products/2542 + + These cellular modules use TTL Serial to communicate, 2 pins are + required to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + MIT license, all text above must be included in any redistribution + ****************************************************/ #include #include #include "Adafruit_FONA.h" @@ -97,7 +116,6 @@ void loop() { // check if we're still connected if (!fona.TCPconnected() || (txfailures >= MAXTXFAILURES)) { - ////////////////////////////// Serial.println(F("Connecting to MQTT...")); int8_t ret, retries = 5; while (retries && (ret = mqtt.connect()) != 0) { diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..053cd1e --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=Adafruit MQTT Library +version=0.9.0 +author=Adafruit +maintainer=Adafruit +sentence=MQTT library that supports the CC3000, FONA, ESP8266, and generic Arduino Client hardware. +paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics. +category=Communication +url=https://github.com/adafruit/Adafruit_MQTT_Library +architectures=*