This commit is contained in:
Volodymyr Shymanskyy
2018-06-11 00:56:41 +03:00
10 changed files with 826 additions and 340 deletions

View File

@@ -24,24 +24,51 @@ static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
static unsigned TINY_GSM_TCP_KEEP_ALIVE = 120;
class TinyGsm
// <stat> status of ESP8266 station interface
// 2 : ESP8266 station connected to an AP and has obtained IP
// 3 : ESP8266 station created a TCP or UDP transmission
// 4 : the TCP or UDP transmission of ESP8266 station disconnected
// 5 : ESP8266 station did NOT connect to an AP
enum RegStatus {
REG_OK_IP = 2,
REG_OK_TCP = 3,
REG_UNREGISTERED = 4,
REG_DENIED = 5,
REG_UNKNOWN = 6,
};
//============================================================================//
//============================================================================//
// Declaration of the TinyGsmESP8266 Class
//============================================================================//
//============================================================================//
class TinyGsmESP8266
{
//============================================================================//
//============================================================================//
// The ESP8266 Internal Client Class
//============================================================================//
//============================================================================//
public:
class GsmClient : public Client
{
friend class TinyGsm;
friend class TinyGsmESP8266;
typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
public:
GsmClient() {}
GsmClient(TinyGsm& modem, uint8_t mux = 1) {
GsmClient(TinyGsmESP8266& modem, uint8_t mux = 1) {
init(&modem, mux);
}
bool init(TinyGsm* modem, uint8_t mux = 1) {
bool init(TinyGsmESP8266* modem, uint8_t mux = 1) {
this->at = modem;
this->mux = mux;
sock_connected = false;
@@ -149,18 +176,25 @@ public:
String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
private:
TinyGsm* at;
uint8_t mux;
bool sock_connected;
RxFifo rx;
TinyGsmESP8266* at;
uint8_t mux;
bool sock_connected;
RxFifo rx;
};
//============================================================================//
//============================================================================//
// The Secure ESP8266 Client Class
//============================================================================//
//============================================================================//
class GsmClientSecure : public GsmClient
{
public:
GsmClientSecure() {}
GsmClientSecure(TinyGsm& modem, uint8_t mux = 1)
GsmClientSecure(TinyGsmESP8266& modem, uint8_t mux = 1)
: GsmClient(modem, mux)
{}
@@ -174,9 +208,20 @@ public:
}
};
//============================================================================//
//============================================================================//
// The ESP8266 Modem Functions
//============================================================================//
//============================================================================//
public:
TinyGsm(Stream& stream)
#ifdef GSM_DEFAULT_STREAM
TinyGsmESP8266(Stream& stream = GSM_DEFAULT_STREAM)
#else
TinyGsmESP8266(Stream& stream)
#endif
: stream(stream)
{
memset(sockets, 0, sizeof(sockets));
@@ -197,6 +242,14 @@ public:
if (waitResponse() != 1) {
return false;
}
sendAT(GF("+CIPMUX=1")); // Enable Multiple Connections
if (waitResponse() != 1) {
return false;
}
sendAT(GF("+CWMODE_CUR=1")); // Put into "station" mode
if (waitResponse() != 1) {
return false;
}
return true;
}
@@ -208,8 +261,8 @@ public:
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) {
delay(100);
return true;
delay(100);
return true;
}
delay(100);
}
@@ -260,6 +313,23 @@ public:
return init();
}
bool poweroff() TINY_GSM_ATTR_NOT_IMPLEMENTED;
bool radioOff() TINY_GSM_ATTR_NOT_IMPLEMENTED;
bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
/*
* SIM card functions
*/
RegStatus getRegistrationStatus() {
sendAT(GF("+CIPSTATUS"));
if (waitResponse(3000, GF("STATUS:")) != 1) return REG_UNKNOWN;
int status = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5"));
waitResponse(); // Returns an OK after the status
return (RegStatus)status;
}
/*
* Generic network functions
@@ -280,21 +350,9 @@ public:
return res2;
}
bool isNetworkConnected() {
sendAT(GF("+CIPSTATUS"));
int res1 = waitResponse(3000, GF("STATUS:"));
int res2 = 0;
if (res1 == 1) {
res2 = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5"));
}
// <stat> status of ESP8266 station interface
// 2 : ESP8266 station connected to an AP and has obtained IP
// 3 : ESP8266 station created a TCP or UDP transmission
// 4 : the TCP or UDP transmission of ESP8266 station disconnected (but AP is connected)
// 5 : ESP8266 station did NOT connect to an AP
waitResponse(); // Returns an OK after the status
if (res2 == 2 || res2 == 3 || res2 == 4) return true;
else return false;
bool isNetworkConnected() {
RegStatus s = getRegistrationStatus();
return (s == REG_OK_IP || s == REG_OK_TCP);
}
bool waitForNetwork(unsigned long timeout = 60000L) {
@@ -303,7 +361,7 @@ public:
int res1 = waitResponse(3000, GF("busy p..."), GF("STATUS:"));
if (res1 == 2) {
int res2 = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5"));
if (res2 == 2 || res2 == 3 || res2 == 4) {
if (res2 == 2 || res2 == 3) {
waitResponse();
return true;
}
@@ -317,17 +375,6 @@ public:
* WiFi functions
*/
bool networkConnect(const char* ssid, const char* pwd) {
sendAT(GF("+CIPMUX=1"));
if (waitResponse() != 1) {
return false;
}
sendAT(GF("+CWMODE_CUR=1"));
if (waitResponse() != 1) {
return false;
}
sendAT(GF("+CWJAP_CUR=\""), ssid, GF("\",\""), pwd, GF("\""));
if (waitResponse(30000L, GFP(GSM_OK), GF(GSM_NL "FAIL" GSM_NL)) != 1) {
return false;
@@ -358,6 +405,28 @@ public:
return TinyGsmIpFromString(getLocalIP());
}
/*
* GPRS functions
*/
/*
* Messaging functions
*/
/*
* Location functions
*/
String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE;
/*
* Battery functions
*/
uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE;
int getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE;
protected:
bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false) {
@@ -389,21 +458,8 @@ protected:
}
bool modemGetConnected(uint8_t mux) {
// TODO: re-check this
sendAT(GF("+CIPSTATUS="), mux);
int res1 = waitResponse(3000, GF("STATUS:"));
int res2;
if (res1 == 1) {
res2 = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5"));
}
// <stat> status of ESP8266 station interface
// 2 : ESP8266 station connected to an AP and has obtained IP
// 3 : ESP8266 station created a TCP or UDP transmission
// 4 : the TCP or UDP transmission of ESP8266 station disconnected (but AP is connected)
// 5 : ESP8266 station did NOT connect to an AP
waitResponse(); // Returns an OK after the status
if (res2 == 2 || res2 == 3 || res2 == 4) return true;
else return false;
RegStatus s = getRegistrationStatus();
return (s == REG_OK_IP || s == REG_OK_TCP);
}
public:
@@ -421,9 +477,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;
}
@@ -510,6 +570,7 @@ finish:
}
data = "";
}
//DBG('<', index, '>');
return index;
}