Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traffic counter in GsmClient #800

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion src/TinyGsmClientSIM7000SSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "TinyGsmTime.tpp"
#include "TinyGsmNTP.tpp"
#include "TinyGsmBattery.tpp"
#include "TinyGsmMqtt.tpp"

class TinyGsmSim7000SSL
: public TinyGsmSim70xx<TinyGsmSim7000SSL>,
Expand All @@ -32,7 +33,8 @@ class TinyGsmSim7000SSL
public TinyGsmGSMLocation<TinyGsmSim7000SSL>,
public TinyGsmTime<TinyGsmSim7000SSL>,
public TinyGsmNTP<TinyGsmSim7000SSL>,
public TinyGsmBattery<TinyGsmSim7000SSL> {
public TinyGsmBattery<TinyGsmSim7000SSL>,
public TinyGsmMqtt<TinyGsmSim7000SSL> {
friend class TinyGsmSim70xx<TinyGsmSim7000SSL>;
friend class TinyGsmTCP<TinyGsmSim7000SSL, TINY_GSM_MUX_COUNT>;
friend class TinyGsmSSL<TinyGsmSim7000SSL, TINY_GSM_MUX_COUNT>;
Expand All @@ -44,6 +46,7 @@ class TinyGsmSim7000SSL
friend class TinyGsmNTP<TinyGsmSim7000SSL>;
friend class TinyGsmTime<TinyGsmSim7000SSL>;
friend class TinyGsmBattery<TinyGsmSim7000SSL>;
friend class TinyGsmMqtt<TinyGsmSim7000SSL>;

/*
* Inner Client
Expand Down Expand Up @@ -356,6 +359,92 @@ class TinyGsmSim7000SSL
*/
// No functions of this type supported


/*
* MQTT functions
*/

bool mqttSetBrokerImpl(String host, uint16_t port) {
sendAT(GF("+SMCONF=\"URL\",\""), host, GF("\",\""), port, GF("\""));
int ret = waitResponse();
return (ret == 1);
}

bool mqttSetClientIdImpl(String id) {
sendAT(GF("+SMCONF=\"CLIENTID\",\""), id, GF("\""));
int ret = waitResponse();
return (ret == 1);
}

bool mqttSetKeepAliveImpl(uint16_t timeout) {
sendAT(GF("+SMCONF=\"KEEPTIME\",\""), timeout, GF("\""));
int ret = waitResponse();
return (ret == 1);
}

bool mqttConnectImpl() {
sendAT(GF("+SMCONN"));
int ret = waitResponse(60000);
connected = (ret == 1);
return connected;
}

void mqttDisconnectImpl() {
sendAT(GF("+SMDISC"));
waitResponse();
connected = false;
}

bool connected;
bool mqttIsConnectedImpl() {
//return connected;
bool ret = false;
sendAT(GF("+SMSTATE?"));
int res = waitResponse(3000, GF("+SMSTATE:"), GFP(GSM_OK),
GFP(GSM_ERROR));
// if we get the +SMSTATE: response, read the status
if (res == 1) {
int status = streamGetIntBefore('\n');
ret = (status == 1);
waitResponse(); //OK
}
connected = ret;
return ret;
}

bool mqttPublishImpl(const char* topic, const uint8_t * payload, unsigned int plength) {
// send data on prompt
int qos = 0;
int retain = 0;
sendAT(GF("+SMPUB=\""), topic, "\",\"", plength, "\",", qos, ",", retain);
if (waitResponse(GF(">")) != 1) { return false; }

stream.write(payload, plength);
stream.flush();

// after posting data, module responds with:
// OK
if (waitResponse(GF("OK")) != 1) { return false; }

return true;
}

bool mqttSubscribeImpl(const char* topic, uint8_t qos) {
sendAT(GF("+SMSUB=\""), topic, "\",", qos);
if (waitResponse(GF("OK")) != 1) { return false; }
return true;
}
bool mqttUnsubscribeImpl(const char* topic) {
sendAT(GF("+SMUNSUB=\""), topic, "\"");
if (waitResponse(GF("OK")) != 1) { return false; }
return true;
}

void mqttLoopImpl() {
mqttIsConnectedImpl();
}


/*
* Client related functions
*/
Expand Down Expand Up @@ -688,6 +777,15 @@ class TinyGsmSim7000SSL
DBG("### Unexpected module reset!");
init();
return true;
} else if (data.endsWith(GF("+SMSUB:"))) {
streamSkipUntil('\"');
String topic = stream.readStringUntil('\"');
streamSkipUntil('\"');
String message = stream.readStringUntil('\"');

if (mqtt_callback) {
mqtt_callback((char *)topic.c_str(), (uint8_t *) message.c_str(), message.length());
}
}
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/TinyGsmClientXBee.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class TinyGsmXBee : public TinyGsmModem<TinyGsmXBee>,

size_t write(const uint8_t* buf, size_t size) override {
TINY_GSM_YIELD();
#ifdef TINY_GSM_DEBUG_TRAFFIC
tx_count += size;
#endif
return at->modemSend(buf, size, mux);
}

Expand Down
105 changes: 105 additions & 0 deletions src/TinyGsmMqtt.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* @file TinyGsmMqtt.tpp
* @author Matteo Murtas
* @license TODO
* @copyright Copyright (c) 2024 Matteo Murtas
* @date Jul 2024
*/

#ifndef SRC_TINYGSMMQTT_H_
#define SRC_TINYGSMMQTT_H_

#include "TinyGsmCommon.h"

#define TINY_GSM_MODEM_HAS_MQTT


#if defined(ESP8266) || defined(ESP32)
#include <functional>
#define MQTT_CALLBACK(f) std::function<void(char*, uint8_t*, unsigned int)> f
#else
#define MQTT_CALLBACK(f) void (*f)(char*, uint8_t*, unsigned int)
#endif

template <class modemType>
class TinyGsmMqtt {
/* =========================================== */
/* =========================================== */
/*
* Define the interface
*/
public:
/*
* MQTT functions
*/


bool mqttSetBroker(String host, uint16_t port) {
mqtt_host = host;
mqtt_port = port;
return thisModem().mqttSetBrokerImpl(host, port);
}

bool mqttSetClientId(String id) {
mqtt_client_id = id;
return thisModem().mqttSetClientIdImpl(id);
}

bool mqttSetKeepAlive(uint16_t timeout) {
return thisModem().mqttSetKeepAliveImpl(timeout);
}

bool mqttConnect() {
return thisModem().mqttConnectImpl();
}

void mqttDisconnect() {
thisModem().mqttDisconnectImpl();
}

bool mqttIsConnected() {
return thisModem().mqttIsConnectedImpl();
}

bool mqttPublish(const char* topic, const uint8_t * payload, unsigned int plength) {
return thisModem().mqttPublishImpl(topic, payload, plength);
}
bool mqttSubscribe(const char* topic, uint8_t qos) {
return thisModem().mqttSubscribeImpl(topic, qos);
}
bool mqttUnsubscribe(const char* topic) {
return thisModem().mqttUnsubscribeImpl(topic);
}

void mqttLoop() {
thisModem().mqttLoopImpl();
}

void mqttSetCallback(MQTT_CALLBACK(cb)) {
mqtt_callback = cb;
}

/*
* CRTP Helper
*/
protected:
inline const modemType& thisModem() const {
return static_cast<const modemType&>(*this);
}
inline modemType& thisModem() {
return static_cast<modemType&>(*this);
}
~TinyGsmMqtt() {}

/* =========================================== */
/* =========================================== */
/*
* Define the default function implementations
*/
String mqtt_host;
uint16_t mqtt_port;
String mqtt_client_id;
MQTT_CALLBACK(mqtt_callback);
};

#endif // SRC_TINYGSMTEMPERATURE_H_
33 changes: 32 additions & 1 deletion src/TinyGsmTCP.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class TinyGsmTCP {
size_t write(const uint8_t* buf, size_t size) override {
TINY_GSM_YIELD();
at->maintain();
#ifdef TINY_GSM_DEBUG_TRAFFIC
tx_count += (uint32_t) size;
#endif
return at->modemSend(buf, size, mux);
}

Expand Down Expand Up @@ -198,6 +201,9 @@ class TinyGsmTCP {
int n = at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available),
mux);
if (n == 0) break;
#ifdef TINY_GSM_DEBUG_TRAFFIC
rx_count += (uint32_t) n;
#endif
} else {
break;
}
Expand Down Expand Up @@ -230,6 +236,9 @@ class TinyGsmTCP {
int n = at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available),
mux);
if (n == 0) break;
#ifdef TINY_GSM_DEBUG_TRAFFIC
rx_count += (uint32_t) n;
#endif
} else {
break;
}
Expand Down Expand Up @@ -284,6 +293,20 @@ class TinyGsmTCP {

String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;

#ifdef TINY_GSM_DEBUG_TRAFFIC
/*
* Debug Traffic
*/

uint32_t getSentBytes() {
return tx_count;
}

uint32_t getReceivedBytes() {
return rx_count;
}
#endif

protected:
// Read and dump anything remaining in the modem's internal buffer.
// Using this in the client stop() function.
Expand All @@ -298,7 +321,10 @@ class TinyGsmTCP {
uint32_t startMillis = millis();
while (sock_available > 0 && (millis() - startMillis < maxWaitMs)) {
rx.clear();
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
size_t rcv = at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
#ifdef TINY_GSM_DEBUG_TRAFFIC
rx_count += (uint32_t) rcv;
#endif
}
rx.clear();
at->streamClear();
Expand All @@ -319,6 +345,11 @@ class TinyGsmTCP {
bool sock_connected;
bool got_data;
RxFifo rx;

#ifdef TINY_GSM_DEBUG_TRAFFIC
uint32_t rx_count;
uint32_t tx_count;
#endif
};

/* =========================================== */
Expand Down