Clean up
This commit is contained in:
BIN
extras/doc/Quectel_M95_GSM_Specification_V3.1.pdf
Normal file
BIN
extras/doc/Quectel_M95_GSM_Specification_V3.1.pdf
Normal file
Binary file not shown.
BIN
extras/doc/Quectel_M95_at_commands_manual_v1.3.pdf
Normal file
BIN
extras/doc/Quectel_M95_at_commands_manual_v1.3.pdf
Normal file
Binary file not shown.
@@ -48,20 +48,20 @@
|
||||
#define TINY_GSM_MODEM_HAS_GPRS
|
||||
#include <TinyGsmClientA6.h>
|
||||
typedef TinyGsmA6 TinyGsm;
|
||||
typedef TinyGsm::GsmClient TinyGsmClient;
|
||||
typedef TinyGsmA6::GsmClient TinyGsmClient;
|
||||
|
||||
#elif defined(TINY_GSM_MODEM_M590)
|
||||
#define TINY_GSM_MODEM_HAS_GPRS
|
||||
#include <TinyGsmClientM590.h>
|
||||
typedef TinyGsmM590 TinyGsm;
|
||||
typedef TinyGsm::GsmClient TinyGsmClient;
|
||||
typedef TinyGsmM590::GsmClient TinyGsmClient;
|
||||
|
||||
#elif defined(TINY_GSM_MODEM_ESP8266)
|
||||
#define TINY_GSM_MODEM_HAS_WIFI
|
||||
#include <TinyGsmClientESP8266.h>
|
||||
typedef TinyGsmESP8266 TinyGsm;
|
||||
typedef TinyGsm::GsmClient TinyGsmClient;
|
||||
typedef TinyGsm::GsmClientSecure TinyGsmClientSecure;
|
||||
typedef TinyGsmESP8266::GsmClient TinyGsmClient;
|
||||
typedef TinyGsmESP8266::GsmClientSecure TinyGsmClientSecure;
|
||||
|
||||
#elif defined(TINY_GSM_MODEM_XBEE)
|
||||
#define TINY_GSM_MODEM_HAS_GPRS
|
||||
@@ -74,7 +74,8 @@
|
||||
#elif defined(TINY_GSM_MODEM_M95)
|
||||
#define TINY_GSM_MODEM_HAS_GPRS
|
||||
#include <TinyGsmClientM95.h>
|
||||
typedef TinyGsm::GsmClient TinyGsmClient;
|
||||
typedef TinyGsmM95 TinyGsm;
|
||||
typedef TinyGsmM95::GsmClient TinyGsmClient;
|
||||
|
||||
#else
|
||||
#error "Please define GSM modem model"
|
||||
|
||||
@@ -38,21 +38,15 @@ enum RegStatus {
|
||||
REG_UNKNOWN = 4,
|
||||
};
|
||||
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// Declaration of the TinyGsmA6 Class
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
|
||||
|
||||
class TinyGsmA6
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The A6 Client Class
|
||||
@@ -60,6 +54,8 @@ public:
|
||||
//============================================================================//
|
||||
|
||||
|
||||
public:
|
||||
|
||||
class GsmClient : public Client
|
||||
{
|
||||
friend class TinyGsmA6;
|
||||
@@ -587,7 +583,7 @@ protected:
|
||||
}
|
||||
|
||||
bool modemGetConnected(uint8_t mux) {
|
||||
sendAT(GF("+CIPSTATUS")); //TODO mux?
|
||||
sendAT(GF("+CIPSTATUS")); // TODO mux?
|
||||
int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
|
||||
waitResponse();
|
||||
return 1 == res;
|
||||
|
||||
@@ -9,14 +9,14 @@
|
||||
#ifndef TinyGsmClientBG96_h
|
||||
#define TinyGsmClientBG96_h
|
||||
|
||||
//#define TINY_GSM_DEBUG Serial
|
||||
//#define TINY_GSM_USE_HEX
|
||||
// #define TINY_GSM_DEBUG Serial
|
||||
// #define TINY_GSM_USE_HEX
|
||||
|
||||
#if !defined(TINY_GSM_RX_BUFFER)
|
||||
#define TINY_GSM_RX_BUFFER 64
|
||||
#endif
|
||||
|
||||
#define TINY_GSM_MUX_COUNT 5
|
||||
#define TINY_GSM_MUX_COUNT 12
|
||||
|
||||
#include <TinyGsmCommon.h>
|
||||
|
||||
@@ -39,10 +39,22 @@ enum RegStatus {
|
||||
REG_UNKNOWN = 4,
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// Declaration of the TinyGsmBG96 Class
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
class TinyGsmBG96
|
||||
{
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The BG96 Client Class
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
|
||||
public:
|
||||
|
||||
class GsmClient : public Client
|
||||
@@ -173,6 +185,13 @@ private:
|
||||
RxFifo rx;
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The BG96 Secure Client
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
|
||||
class GsmClientSecure : public GsmClient
|
||||
{
|
||||
public:
|
||||
@@ -192,9 +211,19 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The BG96 Modem Functions
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
public:
|
||||
|
||||
#ifdef GSM_DEFAULT_STREAM
|
||||
TinyGsmBG96(Stream& stream = GSM_DEFAULT_STREAM)
|
||||
#else
|
||||
TinyGsmBG96(Stream& stream)
|
||||
#endif
|
||||
: stream(stream)
|
||||
{
|
||||
memset(sockets, 0, sizeof(sockets));
|
||||
@@ -353,17 +382,6 @@ public:
|
||||
return SIM_ERROR;
|
||||
}
|
||||
|
||||
RegStatus getRegistrationStatus() {
|
||||
sendAT(GF("+CREG?"));
|
||||
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
|
||||
return REG_UNKNOWN;
|
||||
}
|
||||
streamSkipUntil(','); // Skip format (0)
|
||||
int status = stream.readStringUntil('\n').toInt();
|
||||
waitResponse();
|
||||
return (RegStatus)status;
|
||||
}
|
||||
|
||||
String getOperator() {
|
||||
sendAT(GF("+COPS?"));
|
||||
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
|
||||
@@ -379,6 +397,17 @@ public:
|
||||
* Generic network functions
|
||||
*/
|
||||
|
||||
RegStatus getRegistrationStatus() {
|
||||
sendAT(GF("+CREG?"));
|
||||
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
|
||||
return REG_UNKNOWN;
|
||||
}
|
||||
streamSkipUntil(','); // Skip format (0)
|
||||
int status = stream.readStringUntil('\n').toInt();
|
||||
waitResponse();
|
||||
return (RegStatus)status;
|
||||
}
|
||||
|
||||
int getSignalQuality() {
|
||||
sendAT(GF("+CSQ"));
|
||||
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
|
||||
@@ -404,6 +433,27 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
String getLocalIP() {
|
||||
sendAT(GF("+CGPADDR=1"));
|
||||
if (waitResponse(10000L, GF(GSM_NL "+CGPADDR:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
streamSkipUntil(',');
|
||||
String res = stream.readStringUntil('\n');
|
||||
if (waitResponse() != 1) {
|
||||
return "";
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
IPAddress localIP() {
|
||||
return TinyGsmIpFromString(getLocalIP());
|
||||
}
|
||||
|
||||
/*
|
||||
* WiFi functions
|
||||
*/
|
||||
|
||||
/*
|
||||
* GPRS functions
|
||||
*/
|
||||
@@ -450,53 +500,6 @@ public:
|
||||
return localIP() != 0;
|
||||
}
|
||||
|
||||
String getLocalIP() {
|
||||
sendAT(GF("+CGPADDR=1"));
|
||||
if (waitResponse(10000L, GF(GSM_NL "+CGPADDR:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
streamSkipUntil(',');
|
||||
String res = stream.readStringUntil('\n');
|
||||
if (waitResponse() != 1) {
|
||||
return "";
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
IPAddress localIP() {
|
||||
return TinyGsmIpFromString(getLocalIP());
|
||||
}
|
||||
|
||||
/*
|
||||
* Phone Call functions
|
||||
*/
|
||||
|
||||
bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
|
||||
bool callAnswer() {
|
||||
sendAT(GF("A"));
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
// Returns true on pick-up, false on error/busy
|
||||
bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool callHangup() {
|
||||
sendAT(GF("H"));
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
// 0-9,*,#,A,B,C,D
|
||||
bool dtmfSend(char cmd, int duration_ms = 100) { // TODO: check
|
||||
duration_ms = constrain(duration_ms, 100, 1000);
|
||||
|
||||
sendAT(GF("+VTD="), duration_ms / 100); // VTD accepts in 1/10 of a second
|
||||
waitResponse();
|
||||
|
||||
sendAT(GF("+VTS="), cmd);
|
||||
return waitResponse(10000L) == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Messaging functions
|
||||
*/
|
||||
@@ -660,9 +663,13 @@ public:
|
||||
streamWrite(tail...);
|
||||
}
|
||||
|
||||
bool streamSkipUntil(char c) { //TODO: timeout
|
||||
while (true) {
|
||||
while (!stream.available()) { TINY_GSM_YIELD(); }
|
||||
bool streamSkipUntil(char c) {
|
||||
const unsigned long timeout = 1000L;
|
||||
unsigned long startMillis = millis();
|
||||
while (millis() - startMillis < timeout) {
|
||||
while (millis() - startMillis < timeout && !stream.available()) {
|
||||
TINY_GSM_YIELD();
|
||||
}
|
||||
if (stream.read() == c)
|
||||
return true;
|
||||
}
|
||||
@@ -674,7 +681,7 @@ public:
|
||||
streamWrite("AT", cmd..., GSM_NL);
|
||||
stream.flush();
|
||||
TINY_GSM_YIELD();
|
||||
//DBG("### AT:", cmd...);
|
||||
DBG("### AT:", cmd...);
|
||||
}
|
||||
|
||||
// TODO: Optimize this!
|
||||
@@ -682,12 +689,12 @@ public:
|
||||
GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
/*String r1s(r1); r1s.trim();
|
||||
String r1s(r1); r1s.trim();
|
||||
String r2s(r2); r2s.trim();
|
||||
String r3s(r3); r3s.trim();
|
||||
String r4s(r4); r4s.trim();
|
||||
String r5s(r5); r5s.trim();
|
||||
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
|
||||
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);
|
||||
data.reserve(64);
|
||||
int index = 0;
|
||||
unsigned long startMillis = millis();
|
||||
@@ -743,6 +750,7 @@ finish:
|
||||
}
|
||||
data = "";
|
||||
}
|
||||
DBG('<', index, '>');
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
@@ -313,7 +313,6 @@ public:
|
||||
|
||||
bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
|
||||
/*
|
||||
* SIM card functions
|
||||
*/
|
||||
|
||||
@@ -54,6 +54,7 @@ class TinyGsmM590
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
|
||||
public:
|
||||
|
||||
class GsmClient : public Client
|
||||
|
||||
@@ -9,13 +9,15 @@
|
||||
#ifndef TinyGsmClientM95_h
|
||||
#define TinyGsmClientM95_h
|
||||
|
||||
#define TINY_GSM_DEBUG Serial
|
||||
#define TINY_GSM_USE_HEX
|
||||
// #define TINY_GSM_DEBUG Serial
|
||||
// #define TINY_GSM_USE_HEX
|
||||
|
||||
#if !defined(TINY_GSM_RX_BUFFER)
|
||||
#define TINY_GSM_RX_BUFFER 64
|
||||
#endif
|
||||
|
||||
#define TINY_GSM_MUX_COUNT 6
|
||||
|
||||
#include <TinyGsmCommon.h>
|
||||
|
||||
#define GSM_NL "\r\n"
|
||||
@@ -37,35 +39,51 @@ enum RegStatus {
|
||||
REG_UNKNOWN = 4,
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// Declaration of the TinyGsmM95 Class
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
class TinyGsmM95
|
||||
{
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The M95 Client Class
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
class TinyGsm {
|
||||
|
||||
public:
|
||||
TinyGsm(Stream& stream) : stream(stream) {}
|
||||
|
||||
class GsmClient : public Client {
|
||||
friend class TinyGsm;
|
||||
class GsmClient : public Client
|
||||
{
|
||||
friend class TinyGsmM95;
|
||||
typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
|
||||
|
||||
public:
|
||||
public:
|
||||
GsmClient() {}
|
||||
|
||||
GsmClient(TinyGsm& modem, uint8_t mux = 1) {
|
||||
GsmClient(TinyGsmM95& modem, uint8_t mux = 1) {
|
||||
init(&modem, mux);
|
||||
}
|
||||
|
||||
bool init(TinyGsm* modem, uint8_t mux = 1) {
|
||||
bool init(TinyGsmM95* modem, uint8_t mux = 1) {
|
||||
this->at = modem;
|
||||
this->mux = mux;
|
||||
sock_available = 0;
|
||||
sock_connected = false;
|
||||
got_data = false;
|
||||
|
||||
at->sockets[mux] = this;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
virtual int connect(const char *host, uint16_t port) {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
sock_connected = at->modemConnect(host, port, mux);
|
||||
@@ -86,9 +104,10 @@ public:
|
||||
|
||||
virtual void stop() {
|
||||
TINY_GSM_YIELD();
|
||||
at->sendAT(GF("+QICLOSE"));
|
||||
at->sendAT(GF("+QICLOSE="), mux);
|
||||
sock_connected = false;
|
||||
at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
|
||||
rx.clear();
|
||||
}
|
||||
|
||||
virtual size_t write(const uint8_t *buf, size_t size) {
|
||||
@@ -151,21 +170,55 @@ public:
|
||||
}
|
||||
virtual operator bool() { return connected(); }
|
||||
|
||||
private:
|
||||
TinyGsm* at;
|
||||
/*
|
||||
* Extended API
|
||||
*/
|
||||
|
||||
String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
private:
|
||||
TinyGsmM95* at;
|
||||
uint8_t mux;
|
||||
uint16_t sock_available;
|
||||
bool sock_connected;
|
||||
bool got_data;
|
||||
RxFifo rx;
|
||||
};
|
||||
};
|
||||
|
||||
// Basic functions
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The M95 Has No Secure Client
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The M95 Modem Functions
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
|
||||
public:
|
||||
|
||||
#ifdef GSM_DEFAULT_STREAM
|
||||
TinyGsmM95(Stream& stream = GSM_DEFAULT_STREAM)
|
||||
#else
|
||||
TinyGsmM95(Stream& stream)
|
||||
#endif
|
||||
: stream(stream)
|
||||
{
|
||||
memset(sockets, 0, sizeof(sockets));
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic functions
|
||||
*/
|
||||
bool begin() {
|
||||
return init();
|
||||
}
|
||||
|
||||
bool init() {
|
||||
if (!autoBaud()) {
|
||||
if (!testAT()) {
|
||||
return false;
|
||||
}
|
||||
sendAT(GF("&FZE0")); // Factory + Reset + Echo Off
|
||||
@@ -176,7 +229,11 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool autoBaud(unsigned long timeout = 10000L) {
|
||||
void setBaud(unsigned long baud) {
|
||||
sendAT(GF("+IPR="), baud);
|
||||
}
|
||||
|
||||
bool testAT(unsigned long timeout = 10000L) {
|
||||
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
||||
sendAT(GF(""));
|
||||
if (waitResponse(200) == 1) {
|
||||
@@ -189,6 +246,13 @@ public:
|
||||
}
|
||||
|
||||
void maintain() {
|
||||
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
|
||||
GsmClient* sock = sockets[mux];
|
||||
if (sock && sock->got_data) {
|
||||
sock->got_data = false;
|
||||
sock->sock_available = modemGetAvailable(mux);
|
||||
}
|
||||
}
|
||||
while (stream.available()) {
|
||||
waitResponse(10, NULL, NULL);
|
||||
}
|
||||
@@ -209,10 +273,28 @@ public:
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
// Power functions
|
||||
String getModemInfo() {
|
||||
sendAT(GF("I"));
|
||||
String res;
|
||||
if (waitResponse(1000L, res) != 1) {
|
||||
return "";
|
||||
}
|
||||
res.replace(GSM_NL "OK" GSM_NL, "");
|
||||
res.replace(GSM_NL, " ");
|
||||
res.trim();
|
||||
return res;
|
||||
}
|
||||
|
||||
bool hasSSL() {
|
||||
return false; // TODO: For now
|
||||
}
|
||||
|
||||
/*
|
||||
* Power functions
|
||||
*/
|
||||
|
||||
bool restart() {
|
||||
if (!autoBaud()) {
|
||||
if (!testAT()) {
|
||||
return false;
|
||||
}
|
||||
sendAT(GF("+CFUN=0"));
|
||||
@@ -226,7 +308,23 @@ public:
|
||||
return init();
|
||||
}
|
||||
|
||||
// SIM card & Networ Operator functions
|
||||
bool poweroff() {
|
||||
sendAT(GF("+QPOWD"));
|
||||
return waitResponse(GF("POWERED DOWN")) == 1; // TODO
|
||||
}
|
||||
|
||||
bool radioOff() {
|
||||
sendAT(GF("+CFUN=0"));
|
||||
if (waitResponse(10000L) != 1) {
|
||||
return false;
|
||||
}
|
||||
delay(3000);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* SIM card functions
|
||||
*/
|
||||
|
||||
bool simUnlock(const char *pin) {
|
||||
sendAT(GF("+CPIN=\""), pin, GF("\""));
|
||||
@@ -255,27 +353,6 @@ public:
|
||||
return res;
|
||||
}
|
||||
|
||||
int getSignalQuality() {
|
||||
sendAT(GF("+CSQ"));
|
||||
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
|
||||
return 99;
|
||||
}
|
||||
int res = stream.readStringUntil(',').toInt();
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
|
||||
String getClock() {
|
||||
sendAT(GF("+QLTS"));
|
||||
if (waitResponse(GF(GSM_NL "+QLTS:")) != 1) {
|
||||
// Serial.println( F("No response from AT+QLTS") );
|
||||
return "";
|
||||
}
|
||||
String res = stream.readStringUntil('\n');
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
|
||||
SimStatus getSimStatus(unsigned long timeout = 10000L) {
|
||||
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
||||
sendAT(GF("+CPIN?"));
|
||||
@@ -295,17 +372,6 @@ public:
|
||||
return SIM_ERROR;
|
||||
}
|
||||
|
||||
RegStatus getRegistrationStatus() {
|
||||
sendAT(GF("+CREG?"));
|
||||
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
|
||||
return REG_UNKNOWN;
|
||||
}
|
||||
streamSkipUntil(','); // Skip format (0)
|
||||
int status = stream.readStringUntil('\n').toInt();
|
||||
waitResponse();
|
||||
return (RegStatus)status;
|
||||
}
|
||||
|
||||
String getOperator() {
|
||||
sendAT(GF("+COPS?"));
|
||||
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
|
||||
@@ -317,15 +383,42 @@ public:
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic network functions
|
||||
*/
|
||||
|
||||
RegStatus getRegistrationStatus() {
|
||||
sendAT(GF("+CREG?"));
|
||||
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
|
||||
return REG_UNKNOWN;
|
||||
}
|
||||
streamSkipUntil(','); // Skip format (0)
|
||||
int status = stream.readStringUntil('\n').toInt();
|
||||
waitResponse();
|
||||
return (RegStatus)status;
|
||||
}
|
||||
|
||||
int getSignalQuality() {
|
||||
sendAT(GF("+CSQ"));
|
||||
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
|
||||
return 99;
|
||||
}
|
||||
int res = stream.readStringUntil(',').toInt();
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
|
||||
bool isNetworkConnected() {
|
||||
RegStatus s = getRegistrationStatus();
|
||||
return (s == REG_OK_HOME || s == REG_OK_ROAMING);
|
||||
}
|
||||
|
||||
bool waitForNetwork(unsigned long timeout = 60000L) {
|
||||
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
||||
RegStatus s = getRegistrationStatus();
|
||||
if (s == REG_OK_HOME || s == REG_OK_ROAMING) {
|
||||
if (isNetworkConnected()) {
|
||||
return true;
|
||||
} else if (s == REG_UNREGISTERED) {
|
||||
return false;
|
||||
}
|
||||
delay(1000);
|
||||
delay(250);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -339,18 +432,47 @@ public:
|
||||
waitResponse();
|
||||
}
|
||||
|
||||
// GPRS functions
|
||||
bool gprsConnect(const char* apn, const char* user, const char* pwd) {
|
||||
String getLocalIP() {
|
||||
sendAT(GF("+CGPADDR=1"));
|
||||
if (waitResponse(10000L, GF(GSM_NL "+CGPADDR:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
streamSkipUntil(',');
|
||||
String res = stream.readStringUntil('\n');
|
||||
if (waitResponse() != 1) {
|
||||
return "";
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
IPAddress localIP() {
|
||||
return TinyGsmIpFromString(getLocalIP());
|
||||
}
|
||||
|
||||
/*
|
||||
* WiFi functions
|
||||
*/
|
||||
|
||||
/*
|
||||
* GPRS functions
|
||||
*/
|
||||
bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) {
|
||||
gprsDisconnect();
|
||||
|
||||
sendAT(GF("+QIFGCNT=0"));
|
||||
waitResponse();
|
||||
sendAT(GF("+QIFGCNT=0")); // Set the forground context
|
||||
if (waitResponse() != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sendAT(GF("+QICSGP=1,\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\""));
|
||||
waitResponse();
|
||||
if (waitResponse() != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sendAT(GF("+QIREGAPP"));
|
||||
waitResponse();
|
||||
if (waitResponse() != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sendAT(GF("+QIACT"));
|
||||
waitResponse(10000L);
|
||||
@@ -363,29 +485,24 @@ public:
|
||||
return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1;
|
||||
}
|
||||
|
||||
// Phone Call functions
|
||||
bool isGprsConnected() {
|
||||
sendAT(GF("+CGATT?"));
|
||||
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
|
||||
return false;
|
||||
}
|
||||
int res = stream.readStringUntil('\n').toInt();
|
||||
waitResponse();
|
||||
if (res != 1)
|
||||
return false;
|
||||
|
||||
bool setGsmBusy(bool busy = true) {
|
||||
sendAT(GF("+GSMBUSY="), busy ? 1 : 0);
|
||||
return waitResponse() == 1;
|
||||
return localIP() != 0;
|
||||
}
|
||||
|
||||
bool callAnswer() {
|
||||
sendAT(GF("A"));
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
/*
|
||||
* Messaging functions
|
||||
*/
|
||||
|
||||
bool callNumber(const String& number) {
|
||||
sendAT(GF("D"), number);
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool callHangup(const String& number) {
|
||||
sendAT(GF("H"), number);
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
// Messaging functions
|
||||
String sendUSSD(const String& code) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool sendSMS(const String& number, const String& text) {
|
||||
sendAT(GF("+CMGF=1"));
|
||||
@@ -396,6 +513,7 @@ public:
|
||||
}
|
||||
stream.print(text);
|
||||
stream.write((char)0x1A);
|
||||
stream.flush();
|
||||
return waitResponse(60000L) == 1;
|
||||
}
|
||||
|
||||
@@ -422,6 +540,7 @@ public:
|
||||
stream.print(c, HEX);
|
||||
}
|
||||
stream.write((char)0x1A);
|
||||
stream.flush();
|
||||
return waitResponse(60000L) == 1;
|
||||
}
|
||||
|
||||
@@ -438,7 +557,10 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
// Location functions
|
||||
|
||||
/*
|
||||
* Location functions
|
||||
*/
|
||||
void getLocation() {}
|
||||
|
||||
String getGsmLocation() {
|
||||
@@ -452,37 +574,45 @@ public:
|
||||
return res;
|
||||
}
|
||||
|
||||
// Battery functions
|
||||
/*
|
||||
* Battery functions
|
||||
*/
|
||||
|
||||
// HTTP functions
|
||||
void httpGet( const char *url ) {
|
||||
int len = strlen( url );
|
||||
sendAT( GF("+QHTTPURL="), len);
|
||||
delay( 500 );
|
||||
streamWrite(url, GSM_NL);
|
||||
stream.flush();
|
||||
waitResponse(5000L, GF("OK"));
|
||||
// Use: float vBatt = modem.getBattVoltage() / 1000.0;
|
||||
uint16_t getBattVoltage() {
|
||||
sendAT(GF("+CBC"));
|
||||
if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
streamSkipUntil(','); // Skip
|
||||
streamSkipUntil(','); // Skip
|
||||
|
||||
sendAT( GF("+QHTTPGET"));
|
||||
waitResponse(30000L);
|
||||
uint16_t res = stream.readStringUntil(',').toInt();
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
|
||||
String data;
|
||||
sendAT( GF("+QHTTPREAD"));
|
||||
waitResponse(30000L, data);
|
||||
int getBattPercent() {
|
||||
sendAT(GF("+CBC"));
|
||||
if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
|
||||
return false;
|
||||
}
|
||||
stream.readStringUntil(',');
|
||||
int res = stream.readStringUntil(',').toInt();
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
|
||||
protected:
|
||||
Stream& stream;
|
||||
GsmClient* sockets[5];
|
||||
|
||||
int modemConnect(const char* host, uint16_t port, uint8_t mux) {
|
||||
bool modemConnect(const char* host, uint16_t port, uint8_t mux) {
|
||||
sendAT(GF("+QIOPEN="), GF("\"TCP"), GF("\",\""), host, GF("\","), port);
|
||||
int rsp = waitResponse(75000L,
|
||||
GF("CONNECT OK" GSM_NL),
|
||||
GF("CONNECT FAIL" GSM_NL),
|
||||
GF("ALREADY CONNECT" GSM_NL));
|
||||
if ( rsp != 1 ) {
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
return (1 == rsp);
|
||||
}
|
||||
@@ -491,13 +621,14 @@ protected:
|
||||
sendAT(GF("+QIHEAD=1"));
|
||||
waitResponse(5000L);
|
||||
|
||||
sendAT(GF("+QISEND="),len);
|
||||
sendAT(GF("+QISEND="), mux, ',', len);
|
||||
if (waitResponse(GF(">")) != 1) {
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
stream.write((uint8_t*)buff, len);
|
||||
stream.flush();
|
||||
if (waitResponse(GF(GSM_NL "SEND OK")) != 1) {
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool allAcknowledged = false;
|
||||
@@ -522,46 +653,30 @@ protected:
|
||||
}
|
||||
|
||||
size_t modemRead(size_t size, uint8_t mux) {
|
||||
#ifdef TINY_GSM_USE_HEX
|
||||
sendAT(GF("+CIPRXGET=3,"), mux, ',', size);
|
||||
if (waitResponse(GF("+CIPRXGET:")) != 1) {
|
||||
sendAT(GF("+QIRD="), mux, ',', size);
|
||||
if (waitResponse(GF("+QIRD:")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
sendAT(GF("+CIPRXGET=2,"), mux, ',', size);
|
||||
if (waitResponse(GF("+CIPRXGET:")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
streamSkipUntil(','); // Skip mode 2/3
|
||||
streamSkipUntil(','); // Skip mux
|
||||
size_t len = stream.readStringUntil(',').toInt();
|
||||
sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
|
||||
size_t len = stream.readStringUntil('\n').toInt();
|
||||
|
||||
for (size_t i=0; i<len; i++) {
|
||||
#ifdef TINY_GSM_USE_HEX
|
||||
while (stream.available() < 2) {}
|
||||
char buf[4] = { 0, };
|
||||
buf[0] = stream.read();
|
||||
buf[1] = stream.read();
|
||||
char c = strtol(buf, NULL, 16);
|
||||
#else
|
||||
while (!stream.available()) {}
|
||||
while (!stream.available()) { TINY_GSM_YIELD(); }
|
||||
char c = stream.read();
|
||||
#endif
|
||||
sockets[mux]->rx.put(c);
|
||||
}
|
||||
waitResponse();
|
||||
DBG("### READ:", mux, ",", len);
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t modemGetAvailable(uint8_t mux) {
|
||||
sendAT(GF("+CIPRXGET=4,"), mux);
|
||||
sendAT(GF("+QIRD="), mux, GF(",0"));
|
||||
size_t result = 0;
|
||||
if (waitResponse(GF("+CIPRXGET:")) == 1) {
|
||||
streamSkipUntil(','); // Skip mode 4
|
||||
streamSkipUntil(','); // Skip mux
|
||||
if (waitResponse(GF("+QIRD:")) == 1) {
|
||||
streamSkipUntil(','); // Skip total received
|
||||
streamSkipUntil(','); // Skip have read
|
||||
result = stream.readStringUntil('\n').toInt();
|
||||
DBG("### STILL:", mux, "has", result);
|
||||
waitResponse();
|
||||
}
|
||||
if (!result) {
|
||||
@@ -571,13 +686,29 @@ protected:
|
||||
}
|
||||
|
||||
bool modemGetConnected(uint8_t mux) {
|
||||
sendAT(GF("+CIPSTATUS="), mux);
|
||||
int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
|
||||
sendAT(GF("+QISTATE=1,"), mux);
|
||||
//+QISTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1"
|
||||
|
||||
if (waitResponse(GF("+QISTATE:")))
|
||||
return false;
|
||||
|
||||
streamSkipUntil(','); // Skip mux
|
||||
streamSkipUntil(','); // Skip socket type
|
||||
streamSkipUntil(','); // Skip remote ip
|
||||
streamSkipUntil(','); // Skip remote port
|
||||
streamSkipUntil(','); // Skip local port
|
||||
int res = stream.readStringUntil(',').toInt(); // socket state
|
||||
|
||||
waitResponse();
|
||||
return 1 == res;
|
||||
|
||||
// 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing
|
||||
return 2 == res;
|
||||
}
|
||||
|
||||
// Utilities
|
||||
public:
|
||||
|
||||
/* Utilities */
|
||||
|
||||
template<typename T>
|
||||
void streamWrite(T last) {
|
||||
stream.print(last);
|
||||
@@ -589,23 +720,16 @@ protected:
|
||||
streamWrite(tail...);
|
||||
}
|
||||
|
||||
int streamRead() { return stream.read(); }
|
||||
|
||||
bool streamSkipUntil(char c, int timeoutInMs=1000) {
|
||||
unsigned long startTime = millis();
|
||||
unsigned long t = millis();
|
||||
while ( t < (startTime + timeoutInMs) ) {
|
||||
while (!stream.available() && (t < (startTime + timeoutInMs)) ) {
|
||||
t = millis();
|
||||
delay( 250 );
|
||||
bool streamSkipUntil(char c) {
|
||||
const unsigned long timeout = 1000L;
|
||||
unsigned long startMillis = millis();
|
||||
while (millis() - startMillis < timeout) {
|
||||
while (millis() - startMillis < timeout && !stream.available()) {
|
||||
TINY_GSM_YIELD();
|
||||
}
|
||||
if ( stream.available() ) {
|
||||
if (stream.read() == c) {
|
||||
if (stream.read() == c)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
t = millis();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -620,66 +744,62 @@ protected:
|
||||
// TODO: Optimize this!
|
||||
uint8_t waitResponse(uint32_t timeout, String& data,
|
||||
GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) {
|
||||
/* String r1s(r1); r1s.trim();
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
String r1s(r1); r1s.trim();
|
||||
String r2s(r2); r2s.trim();
|
||||
String r3s(r3); r3s.trim();
|
||||
String r4s(r4); r4s.trim();
|
||||
String r5s(r5); r5s.trim();
|
||||
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s); */
|
||||
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);
|
||||
data.reserve(64);
|
||||
bool gotData = false;
|
||||
int mux = -1;
|
||||
int index = 0;
|
||||
unsigned long startMillis = millis();
|
||||
do {
|
||||
TINY_GSM_YIELD();
|
||||
while (stream.available() > 0) {
|
||||
int a = streamRead();
|
||||
int a = stream.read();
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
Serial.print((char)a);
|
||||
data += (char)a;
|
||||
if (r1 && data.endsWith(r1)) {
|
||||
index = 1;
|
||||
DBG("<<< Got1 '", String(r1), "'");
|
||||
goto finish;
|
||||
} else if (r2 && data.endsWith(r2)) {
|
||||
index = 2;
|
||||
DBG("<<< Got2 '", String(r2), "'");
|
||||
goto finish;
|
||||
} else if (r3 && data.endsWith(r3)) {
|
||||
index = 3;
|
||||
DBG("<<< Got3 '", String(r3), "'");
|
||||
goto finish;
|
||||
} else if (r4 && data.endsWith(r4)) {
|
||||
index = 4;
|
||||
DBG("<<< Got4 '", String(r4), "'");
|
||||
goto finish;
|
||||
} else if (r5 && data.endsWith(r5)) {
|
||||
index = 5;
|
||||
DBG("<<< Got5 '", String(r5), "'");
|
||||
goto finish;
|
||||
} else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) {
|
||||
String mode = stream.readStringUntil(',');
|
||||
if (mode.toInt() == 1) {
|
||||
mux = stream.readStringUntil('\n').toInt();
|
||||
gotData = true;
|
||||
data = "";
|
||||
} else {
|
||||
data += mode;
|
||||
} else if (data.endsWith(GF(GSM_NL "+QIURC:"))) {
|
||||
stream.readStringUntil('\"');
|
||||
String urc = stream.readStringUntil('\"');
|
||||
stream.readStringUntil(',');
|
||||
if (urc == "recv") {
|
||||
int mux = stream.readStringUntil('\n').toInt();
|
||||
DBG("### URC RECV:", mux);
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->got_data = true;
|
||||
}
|
||||
} else if (data.endsWith(GF("CLOSED" GSM_NL))) {
|
||||
int nl = data.lastIndexOf(GSM_NL, data.length()-8);
|
||||
int coma = data.indexOf(',', nl+2);
|
||||
mux = data.substring(nl+2, coma).toInt();
|
||||
if (mux) {
|
||||
} else if (urc == "closed") {
|
||||
int mux = stream.readStringUntil('\n').toInt();
|
||||
DBG("### URC CLOSE:", mux);
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->sock_connected = false;
|
||||
data = "";
|
||||
}
|
||||
} else {
|
||||
stream.readStringUntil('\n');
|
||||
}
|
||||
data = "";
|
||||
}
|
||||
}
|
||||
} while (millis() - startMillis < timeout);
|
||||
finish:
|
||||
finish:
|
||||
if (!index) {
|
||||
data.trim();
|
||||
if (data.length()) {
|
||||
@@ -687,71 +807,29 @@ protected:
|
||||
}
|
||||
data = "";
|
||||
}
|
||||
if (gotData) {
|
||||
sockets[mux]->sock_available = modemGetAvailable(mux);
|
||||
}
|
||||
DBG('<', index, '>');
|
||||
return index;
|
||||
}
|
||||
|
||||
uint8_t waitResponse(uint32_t timeout,
|
||||
GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) {
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
String data;
|
||||
return waitResponse(timeout, data, r1, r2, r3, r4, r5);
|
||||
}
|
||||
|
||||
uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) {
|
||||
GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
return waitResponse(1000, r1, r2, r3, r4, r5);
|
||||
}
|
||||
|
||||
void printResponse(uint32_t timeout) {
|
||||
unsigned long startMillis = millis();
|
||||
do {
|
||||
TINY_GSM_YIELD();
|
||||
while (stream.available() > 0) {
|
||||
int a = streamRead();
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
Serial.print((char)a);
|
||||
}
|
||||
} while (millis() - startMillis < timeout);
|
||||
}
|
||||
public:
|
||||
Stream& stream;
|
||||
|
||||
uint16_t waitForHTTPResponse(uint32_t timeout, char * buffer, uint8_t bufferSize) {
|
||||
unsigned long startMillis = millis();
|
||||
uint8_t bufferIndex = 0;
|
||||
uint16_t httpCode = 0;
|
||||
do {
|
||||
TINY_GSM_YIELD();
|
||||
while (stream.available() > 0) {
|
||||
int a = streamRead();
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
Serial.print((char)a);
|
||||
if (bufferIndex < bufferSize-1) { // leave room to null-terminate
|
||||
buffer[bufferIndex] = (char)a;
|
||||
bufferIndex++;
|
||||
}
|
||||
if ((char)a == '\n') {
|
||||
// null-terminate buffer!
|
||||
buffer[bufferIndex] = '\0';
|
||||
if (strstr((const char *)buffer, "CLOSED") != NULL) {
|
||||
return httpCode;
|
||||
}
|
||||
if(httpCode == 0 && strstr((const char *)buffer, "HTTP/1.") != NULL) {
|
||||
strtok((char *)buffer, " "); /** Parse out HTTP/1.x */
|
||||
|
||||
char * codeString = strtok(NULL, " ");
|
||||
httpCode = atoi((const char *)codeString);
|
||||
}
|
||||
bufferIndex = 0;
|
||||
}
|
||||
}
|
||||
} while (millis() - startMillis < timeout);
|
||||
|
||||
return httpCode;
|
||||
}
|
||||
protected:
|
||||
GsmClient* sockets[TINY_GSM_MUX_COUNT];
|
||||
};
|
||||
|
||||
typedef TinyGsm::GsmClient TinyGsmClient;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -39,7 +39,6 @@ enum RegStatus {
|
||||
REG_UNKNOWN = 4,
|
||||
};
|
||||
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// Declaration of the TinyGsmU201 Class
|
||||
@@ -212,7 +211,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// The U201 Modem Functions
|
||||
@@ -521,19 +519,6 @@ public:
|
||||
|
||||
return localIP() != 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Phone Call functions
|
||||
*/
|
||||
|
||||
bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
|
||||
bool callAnswer() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool callHangup() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
/*
|
||||
* Messaging functions
|
||||
*/
|
||||
@@ -721,7 +706,7 @@ public:
|
||||
TINY_GSM_YIELD();
|
||||
while (stream.available() > 0) {
|
||||
int a = stream.read();
|
||||
if (a < 0) continue;
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
data += (char)a;
|
||||
if (r1 && data.endsWith(r1)) {
|
||||
index = 1;
|
||||
|
||||
@@ -44,7 +44,6 @@ enum XBeeType {
|
||||
XBEE3_LTENB = 3, // Digi XBee3™ Cellular NB-IoT -- HS unknown to SRGD
|
||||
};
|
||||
|
||||
|
||||
//============================================================================//
|
||||
//============================================================================//
|
||||
// Declaration of the TinyGsmXBee Class
|
||||
@@ -665,8 +664,6 @@ fail:
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
bool modemConnect(const char* host, uint16_t port, uint8_t mux = 0, bool ssl = false) {
|
||||
String strIP; strIP.reserve(16);
|
||||
unsigned long startMillis = millis();
|
||||
|
||||
Reference in New Issue
Block a user