Cleanup and prepare for publishing.
This commit is contained in:
parent
d063469e03
commit
6a740d4004
@ -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);
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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 <Adafruit_CC3000.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
|
||||
// delay in ms between calls of available()
|
||||
#define MQTT_CC3000_INTERAVAILDELAY 10
|
||||
|
||||
|
@ -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"
|
||||
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
22
LICENSE
Normal file
22
LICENSE
Normal file
@ -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.
|
||||
|
39
README.md
39
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
|
||||
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
|
||||
|
@ -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 <Adafruit_SleepyDog.h>
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include <SPI.h>
|
||||
@ -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.
|
||||
|
@ -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 <ESP8266WiFi.h>
|
||||
#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.
|
@ -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 <Adafruit_SleepyDog.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#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) {
|
||||
|
9
library.properties
Normal file
9
library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=Adafruit MQTT Library
|
||||
version=0.9.0
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
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=*
|
Loading…
Reference in New Issue
Block a user