2015-06-11 00:38:34 +02:00
|
|
|
// 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.
|
2015-06-10 06:22:36 +02:00
|
|
|
#ifndef _ADAFRUIT_MQTT_FONA_H_
|
|
|
|
#define _ADAFRUIT_MQTT_FONA_H_
|
|
|
|
|
|
|
|
#include <Adafruit_FONA.h>
|
|
|
|
#include "Adafruit_MQTT.h"
|
|
|
|
|
|
|
|
#define MQTT_FONA_INTERAVAILDELAY 100
|
|
|
|
#define MQTT_FONA_QUERYDELAY 500
|
|
|
|
|
2015-06-11 00:38:34 +02:00
|
|
|
|
|
|
|
// 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).
|
2015-06-10 06:22:36 +02:00
|
|
|
class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
|
|
|
public:
|
2015-10-22 16:09:16 +02:00
|
|
|
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
|
|
|
const char *cid, const char *user, const char *pass):
|
|
|
|
Adafruit_MQTT(server, port, cid, user, pass),
|
|
|
|
fona(f)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
|
|
|
const char *user, const char *pass):
|
|
|
|
Adafruit_MQTT(server, port, user, pass),
|
2015-06-10 06:22:36 +02:00
|
|
|
fona(f)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool connectServer() {
|
|
|
|
char server[40];
|
|
|
|
strncpy_P(server, servername, 40);
|
2015-12-10 20:46:40 +01:00
|
|
|
#ifdef ADAFRUIT_SLEEPYDOG_H
|
2015-06-10 06:22:36 +02:00
|
|
|
Watchdog.reset();
|
2015-12-10 20:46:40 +01:00
|
|
|
#endif
|
2015-06-10 06:22:36 +02:00
|
|
|
|
|
|
|
// connect to server
|
|
|
|
DEBUG_PRINTLN(F("Connecting to TCP"));
|
|
|
|
return fona->TCPconnect(server, portnum);
|
|
|
|
}
|
|
|
|
|
2015-10-05 20:18:49 +02:00
|
|
|
bool disconnectServer() {
|
2015-06-10 06:22:36 +02:00
|
|
|
return fona->TCPclose();
|
|
|
|
}
|
|
|
|
|
2015-07-05 23:29:13 +02:00
|
|
|
bool connected() {
|
|
|
|
// Return true if connected, false if not connected.
|
|
|
|
return fona->TCPconnected();
|
|
|
|
}
|
|
|
|
|
2016-07-06 23:20:02 +02:00
|
|
|
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
|
2015-06-10 06:22:36 +02:00
|
|
|
uint8_t *buffp = buffer;
|
2016-06-08 04:39:07 +02:00
|
|
|
DEBUG_PRINTLN(F("Reading data.."));
|
2015-06-10 06:22:36 +02:00
|
|
|
|
|
|
|
if (!fona->TCPconnected()) return 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* Read data until either the connection is closed, or the idle timeout is reached. */
|
|
|
|
uint16_t len = 0;
|
|
|
|
int16_t t = timeout;
|
|
|
|
uint16_t avail;
|
|
|
|
|
|
|
|
while (fona->TCPconnected() && (timeout >= 0)) {
|
2016-06-08 04:39:07 +02:00
|
|
|
//DEBUG_PRINT('.');
|
2015-06-10 06:22:36 +02:00
|
|
|
while (avail = fona->TCPavailable()) {
|
2016-06-08 04:39:07 +02:00
|
|
|
//DEBUG_PRINT('!');
|
2015-07-05 23:29:13 +02:00
|
|
|
|
2015-06-10 06:22:36 +02:00
|
|
|
if (len + avail > maxlen) {
|
2015-06-10 06:30:52 +02:00
|
|
|
avail = maxlen - len;
|
|
|
|
if (avail == 0) return len;
|
2015-06-10 06:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to read the data into the end of the pointer
|
|
|
|
if (! fona->TCPread(buffp, avail)) return len;
|
2015-07-05 23:29:13 +02:00
|
|
|
|
2015-06-10 06:22:36 +02:00
|
|
|
// read it! advance pointer
|
|
|
|
buffp += avail;
|
|
|
|
len += avail;
|
|
|
|
timeout = t; // reset the timeout
|
|
|
|
|
|
|
|
//DEBUG_PRINTLN((uint8_t)c, HEX);
|
|
|
|
|
|
|
|
if (len == maxlen) { // we read all we want, bail
|
2016-06-08 04:39:07 +02:00
|
|
|
DEBUG_PRINT(F("Read:\t"));
|
2015-06-10 06:22:36 +02:00
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
2016-06-08 04:17:05 +02:00
|
|
|
return len;
|
2015-06-10 06:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-10 20:46:40 +01:00
|
|
|
#ifdef ADAFRUIT_SLEEPYDOG_H
|
2015-06-10 06:22:36 +02:00
|
|
|
Watchdog.reset();
|
2015-12-10 20:46:40 +01:00
|
|
|
#endif
|
2015-06-10 06:22:36 +02:00
|
|
|
timeout -= MQTT_FONA_INTERAVAILDELAY;
|
|
|
|
timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the FONA for avail()
|
|
|
|
delay(MQTT_FONA_INTERAVAILDELAY);
|
|
|
|
}
|
2016-06-08 04:17:05 +02:00
|
|
|
|
2015-06-10 06:22:36 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-07-07 00:32:21 +02:00
|
|
|
bool sendPacket(uint8_t *buffer, uint16_t len) {
|
2015-06-10 06:22:36 +02:00
|
|
|
DEBUG_PRINTLN(F("Writing packet"));
|
|
|
|
if (fona->TCPconnected()) {
|
|
|
|
boolean ret = fona->TCPsend((char *)buffer, len);
|
|
|
|
//DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
|
|
|
if (!ret) {
|
2016-07-07 00:34:56 +02:00
|
|
|
DEBUG_PRINTLN("Failed to send packet.");
|
2015-06-10 06:22:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_PRINTLN(F("Connection failed!"));
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-05 23:29:13 +02:00
|
|
|
return true;
|
2015-06-10 06:22:36 +02:00
|
|
|
}
|
2015-07-05 23:29:13 +02:00
|
|
|
|
2015-06-10 06:22:36 +02:00
|
|
|
private:
|
|
|
|
uint32_t serverip;
|
|
|
|
Adafruit_FONA *fona;
|
|
|
|
};
|
|
|
|
|
2015-06-11 00:38:34 +02:00
|
|
|
|
2015-06-10 06:22:36 +02:00
|
|
|
#endif
|