Browse Source

Update API and examples

v_master
Volodymyr Shymanskyy 8 years ago
parent
commit
f11060e885
9 changed files with 203 additions and 138 deletions
  1. +71
    -41
      TinyGsmClient.h
  2. +7
    -7
      examples/BlynkClient/BlynkClient.ino
  3. +19
    -11
      examples/FileDownload/FileDownload.ino
  4. +17
    -10
      examples/MqttClient/MqttClient.ino
  5. +17
    -9
      examples/WebClient/WebClient.ino
  6. +8
    -4
      keywords.txt
  7. +21
    -21
      tools/AT_Debug/AT_Debug.ino
  8. +15
    -15
      tools/FactoryReset/FactoryReset.ino
  9. +28
    -20
      tools/SimpleTest/SimpleTest.ino

+ 71
- 41
TinyGsmClient.h View File

@ -61,8 +61,7 @@ enum RegStatus {
};
class TinyGsmClient
: public Client
class TinyGsm
{
typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
@ -83,17 +82,40 @@ class TinyGsmClient
#endif
public:
TinyGsmClient(Stream& stream, uint8_t mux = 1)
TinyGsm(Stream& stream)
: stream(stream)
, mux(mux)
, sock_available(0)
, sock_connected(false)
{}
public:
class GsmClient : public Client
{
friend class TinyGsm;
typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
public:
GsmClient() {
init(NULL, -1);
}
GsmClient(TinyGsm& at, uint8_t mux = 1) {
init(&at, mux);
}
bool init(TinyGsm* at, uint8_t mux = 1) {
this->at = at;
this->mux = mux;
at->sockets[mux] = this;
sock_available = 0;
sock_connected = false;
return true;
}
public:
virtual int connect(const char *host, uint16_t port) {
return modemConnect(host, port);
rx.clear();
sock_connected = at->modemConnect(host, port, mux);
return sock_connected;
}
virtual int connect(IPAddress ip, uint16_t port) {
@ -105,18 +127,18 @@ public:
host += ip[2];
host += ".";
host += ip[3];
return modemConnect(host.c_str(), port); //TODO: c_str may be missing
return connect(host.c_str(), port);
}
virtual void stop() {
sendAT(GF("+CIPCLOSE="), mux);
at->sendAT(GF("+CIPCLOSE="), mux);
sock_connected = false;
waitResponse();
at->waitResponse();
}
virtual size_t write(const uint8_t *buf, size_t size) {
maintain();
return modemSend(buf, size);
at->maintain();
return at->modemSend(buf, size, mux);
}
virtual size_t write(uint8_t c) {
@ -124,12 +146,12 @@ public:
}
virtual int available() {
maintain();
at->maintain();
return rx.size() + sock_available;
}
virtual int read(uint8_t *buf, size_t size) {
maintain();
at->maintain();
size_t cnt = 0;
while (cnt < size) {
size_t chunk = min(size-cnt, rx.size());
@ -140,9 +162,9 @@ public:
continue;
}
// TODO: Read directly into user buffer?
maintain();
at->maintain();
if (sock_available > 0) {
modemRead(rx.free());
at->modemRead(rx.free(), mux);
} else {
break;
}
@ -159,13 +181,20 @@ public:
}
virtual int peek() { return -1; } //TODO
virtual void flush() { stream.flush(); }
virtual void flush() { at->stream.flush(); }
virtual uint8_t connected() {
maintain();
at->maintain();
return sock_connected;
}
virtual operator bool() { return connected(); }
private:
TinyGsm* at;
uint8_t mux;
uint16_t sock_available;
bool sock_connected;
RxFifo rx;
};
public:
@ -180,7 +209,7 @@ public:
if (waitResponse() != 1) {
return false;
}
getSimStatus();
return true;
}
@ -297,13 +326,13 @@ public:
default: return SIM_ERROR;
}
}
return 0;
return SIM_ERROR;
}
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG: 0,")) != 1) {
return 0;
return REG_UNKNOWN;
}
int status = stream.readStringUntil('\n').toInt();
waitResponse();
@ -335,8 +364,8 @@ public:
/*
* GPRS functions
*/
bool networkConnect(const char* apn, const char* user, const char* pwd) {
networkDisconnect();
bool gprsConnect(const char* apn, const char* user, const char* pwd) {
gprsDisconnect();
// AT+CGATT?
// AT+CGATT=1
@ -381,7 +410,7 @@ public:
return true;
}
bool networkDisconnect() {
bool gprsDisconnect() {
sendAT(GF("+CIPSHUT"));
return waitResponse(60000L) == 1;
}
@ -411,13 +440,16 @@ public:
*/
private:
int modemConnect(const char* host, uint16_t port) {
int modemConnect(const char* host, uint16_t port, uint8_t mux) {
sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port);
sock_connected = (1 == waitResponse(75000L, GF("CONNECT OK" GSM_NL), GF("CONNECT FAIL" GSM_NL), GF("ALREADY CONNECT" GSM_NL)));
return sock_connected;
int rsp = waitResponse(75000L,
GF("CONNECT OK" GSM_NL),
GF("CONNECT FAIL" GSM_NL),
GF("ALREADY CONNECT" GSM_NL));
return (1 == rsp);
}
int modemSend(const void* buff, size_t len) {
int modemSend(const void* buff, size_t len, uint8_t mux) {
sendAT(GF("+CIPSEND="), mux, ',', len);
if (waitResponse(GF(">")) != 1) {
return -1;
@ -431,7 +463,7 @@ private:
return data.toInt();
}
size_t modemRead(size_t size) {
size_t modemRead(size_t size, uint8_t mux) {
#ifdef GSM_USE_HEX
sendAT(GF("+CIPRXGET=3,"), mux, ',', size);
if (waitResponse(GF("+CIPRXGET: 3,")) != 1) {
@ -445,7 +477,7 @@ private:
#endif
stream.readStringUntil(','); // Skip mux
size_t len = stream.readStringUntil(',').toInt();
sock_available = stream.readStringUntil('\n').toInt();
sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
for (size_t i=0; i<len; i++) {
#ifdef GSM_USE_HEX
@ -458,13 +490,13 @@ private:
while (stream.available() < 1) { delay(1); }
char c = stream.read();
#endif
rx.put(c);
sockets[mux]->rx.put(c);
}
waitResponse();
return len;
}
size_t modemGetAvailable() {
size_t modemGetAvailable(uint8_t mux) {
sendAT(GF("+CIPRXGET=4,"), mux);
size_t result = 0;
for (byte i = 0; i < 2; i++) {
@ -479,12 +511,12 @@ private:
}
}
if (!result) {
sock_connected = modemGetConnected();
sockets[mux]->sock_connected = modemGetConnected(mux);
}
return result;
}
bool modemGetConnected() {
bool modemGetConnected(uint8_t mux) {
sendAT(GF("+CIPSTATUS="), mux);
int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
waitResponse();
@ -544,7 +576,7 @@ private:
gotNewData = true;
data = "";
} else if (data.indexOf(GF(GSM_NL "1, CLOSED" GSM_NL)) >= 0) { //TODO: use mux
sock_connected = false;
sockets[1]->sock_connected = false;
data = "";
}
}
@ -557,7 +589,7 @@ finish:
data = "";
}
if (gotNewData) {
sock_available = modemGetAvailable();
sockets[1]->sock_available = modemGetAvailable(1);
}
return index;
}
@ -578,11 +610,9 @@ finish:
private:
Stream& stream;
const uint8_t mux;
RxFifo rx;
uint16_t sock_available;
bool sock_connected;
GsmClient* sockets[5];
};
typedef TinyGsm::GsmClient TinyGsmClient;
#endif

+ 7
- 7
examples/BlynkClient/BlynkClient.ino View File

@ -42,13 +42,14 @@ char user[] = "";
char pass[] = "";
// Hardware Serial on Mega, Leonardo, Micro
#define GsmSerial Serial1
#define SerialAT Serial1
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial GsmSerial(2, 3); // RX, TX
//SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsmClient gsm(GsmSerial);
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
void setup()
{
@ -57,15 +58,14 @@ void setup()
delay(10);
// Set GSM module baud rate
GsmSerial.begin(115200);
SerialAT.begin(115200);
delay(3000);
// Restart takes quite some time
// You can skip it in many cases
Serial.println("Restarting modem...");
gsm.restart();
modem.restart();
Blynk.begin(auth, gsm, apn, user, pass);
Blynk.begin(auth, modem, apn, user, pass);
}
void loop()


+ 19
- 11
examples/FileDownload/FileDownload.ino View File

@ -19,13 +19,14 @@ char user[] = "";
char pass[] = "";
// Use Hardware Serial on Mega, Leonardo, Micro
#define GsmSerial Serial1
#define SerialAT Serial1
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial GsmSerial(2, 3); // RX, TX
//SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsmClient client(GsmSerial);
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
char server[] = "cdn.rawgit.com";
char resource[] = "/vshymanskyy/tinygsm/master/extras/test_10k.hex";
@ -38,31 +39,38 @@ void setup() {
delay(10);
// Set GSM module baud rate
GsmSerial.begin(115200);
SerialAT.begin(115200);
delay(3000);
// Restart takes quite some time
// You can skip it in many cases
Serial.println("Restarting modem...");
client.restart();
modem.restart();
}
void printPercent(uint32_t readLength, uint32_t contentLength) {
// If we know the total length
if (contentLength != -1) {
Serial.print(F("\r "));
Serial.print("\r ");
Serial.print((100.0 * readLength) / contentLength);
Serial.println('%');
Serial.print('%');
} else {
Serial.println(readLength);
}
}
void loop() {
Serial.print("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(apn);
if (!client.networkConnect(apn, user, pass)) {
Serial.println(" failed");
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println(" fail");
delay(10000);
return;
}
@ -134,7 +142,7 @@ void loop() {
client.stop();
Serial.println("Server disconnected");
client.networkDisconnect();
modem.gprsDisconnect();
Serial.println("GPRS disconnected");
Serial.println();


+ 17
- 10
examples/MqttClient/MqttClient.ino View File

@ -35,14 +35,15 @@ char user[] = "";
char pass[] = "";
// Use Hardware Serial on Mega, Leonardo, Micro
#define GsmSerial Serial1
#define SerialAT Serial1
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial GsmSerial(2, 3); // RX, TX
//SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsmClient gsm(GsmSerial);
PubSubClient mqtt(gsm);
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
const char* broker = "test.mosquitto.org";
@ -63,18 +64,24 @@ void setup() {
delay(10);
// Set GSM module baud rate
GsmSerial.begin(115200);
SerialAT.begin(115200);
delay(3000);
// Restart takes quite some time
// You can skip it in many cases
Serial.println("Restarting modem...");
gsm.restart();
modem.restart();
Serial.print("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println(" fail");
while (true);
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(apn);
if (!gsm.networkConnect(apn, user, pass)) {
Serial.println(" failed");
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println(" fail");
while (true);
}
Serial.println(" OK");
@ -88,7 +95,7 @@ boolean mqttConnect() {
Serial.print("Connecting to ");
Serial.print(broker);
if (!mqtt.connect("GsmClientTest")) {
Serial.println(" failed");
Serial.println(" fail");
return false;
}
Serial.println(" OK");


+ 17
- 9
examples/WebClient/WebClient.ino View File

@ -17,13 +17,14 @@ char user[] = "";
char pass[] = "";
// Use Hardware Serial on Mega, Leonardo, Micro
#define GsmSerial Serial1
#define SerialAT Serial1
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial GsmSerial(2, 3); // RX, TX
//SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsmClient client(GsmSerial);
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
char server[] = "cdn.rawgit.com";
char resource[] = "/vshymanskyy/tinygsm/master/extras/logo.txt";
@ -34,20 +35,27 @@ void setup() {
delay(10);
// Set GSM module baud rate
GsmSerial.begin(115200);
SerialAT.begin(115200);
delay(3000);
// Restart takes quite some time
// You can skip it in many cases
Serial.println("Restarting modem...");
client.restart();
modem.restart();
}
void loop() {
Serial.print("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(apn);
if (!client.networkConnect(apn, user, pass)) {
Serial.println(" failed");
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println(" fail");
delay(10000);
return;
}
@ -81,7 +89,7 @@ void loop() {
client.stop();
Serial.println("Server disconnected");
client.networkDisconnect();
modem.gprsDisconnect();
Serial.println("GPRS disconnected");
// Do nothing forevermore


+ 8
- 4
keywords.txt View File

@ -1,19 +1,23 @@
#######################################
# Data types (KEYWORD1)
#######################################
TinyGsm KEYWORD1
TinyGsmClient KEYWORD1
SerialAT KEYWORD1
SerialMon KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
restart KEYWORD2
networkConnect KEYWORD2
networkDisconnect KEYWORD2
waitForNetwork KEYWORD2
getSimStatus KEYWORD2
gprsConnect KEYWORD2
gprsDisconnect KEYWORD2
factoryReset KEYWORD2
GsmSerial KEYWORD3
#######################################
# Literals (LITERAL1)
#######################################

+ 21
- 21
tools/AT_Debug/AT_Debug.ino View File

@ -9,7 +9,7 @@
**************************************************************/
// Set serial for debug console (to the Serial Monitor, speed 115200)
#define SerialMonitor Serial
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
@ -20,11 +20,11 @@
//SoftwareSerial SerialAT(2, 3); // RX, TX
#include <TinyGsmClient.h>
TinyGsmClient gsm(SerialAT);
TinyGsm modem(SerialAT);
void setup() {
// Set console baud rate
SerialMonitor.begin(115200);
SerialMon.begin(115200);
delay(5000);
}
@ -33,43 +33,43 @@ void loop() {
uint32_t rate = 0;
uint32_t rates[] = { 115200, 9600, 57600, 19200, 74400, 74880 };
SerialMonitor.println("Autodetecting baud rate");
SerialMon.println("Autodetecting baud rate");
for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
SerialMonitor.print(String("Trying baud rate ") + rates[i] + "... ");
SerialMon.print(String("Trying baud rate ") + rates[i] + "... ");
SerialAT.begin(rates[i]);
delay(10);
if (gsm.autoBaud(3000)) {
if (modem.autoBaud(2000)) {
rate = rates[i];
SerialMonitor.println(F("OK"));
SerialMon.println(F("OK"));
break;
} else {
SerialMonitor.println(F("fail"));
SerialMon.println(F("fail"));
}
}
if (!rate) {
SerialMonitor.println(F("***********************************************************"));
SerialMonitor.println(F(" Module does not respond!"));
SerialMonitor.println(F(" Check your Serial wiring"));
SerialMonitor.println(F(" Check the module is correctly powered and turned on"));
SerialMonitor.println(F("***********************************************************"));
SerialMon.println(F("***********************************************************"));
SerialMon.println(F(" Module does not respond!"));
SerialMon.println(F(" Check your Serial wiring"));
SerialMon.println(F(" Check the module is correctly powered and turned on"));
SerialMon.println(F("***********************************************************"));
delay(30000L);
return;
}
// Access AT commands from Serial Monitor
SerialMonitor.println(F("***********************************************************"));
SerialMonitor.println(F(" You can now send AT commands"));
SerialMonitor.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
SerialMonitor.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
SerialMonitor.println(F("***********************************************************"));
SerialMon.println(F("***********************************************************"));
SerialMon.println(F(" You can now send AT commands"));
SerialMon.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
SerialMon.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
SerialMon.println(F("***********************************************************"));
while(true) {
if (SerialAT.available()) {
SerialMonitor.write(SerialAT.read());
SerialMon.write(SerialAT.read());
}
if (SerialMonitor.available()) {
SerialAT.write(SerialMonitor.read());
if (SerialMon.available()) {
SerialAT.write(SerialMon.read());
}
delay(0);
}


+ 15
- 15
tools/FactoryReset/FactoryReset.ino View File

@ -10,7 +10,7 @@
**************************************************************/
// Set serial for debug console (to the Serial Monitor, speed 115200)
#define SerialMonitor Serial
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
@ -22,33 +22,33 @@
#include <TinyGsmClient.h>
#include <StreamDebugger.h>
StreamDebugger DebugAT(SerialAT, SerialMonitor);
TinyGsmClient gsm(DebugAT);
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
void setup() {
// Set console baud rate
SerialMonitor.begin(115200);
SerialMon.begin(115200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(115200);
delay(3000);
if (!gsm.begin()) {
SerialMonitor.println(F("***********************************************************"));
SerialMonitor.println(F(" Cannot initialize module!"));
SerialMonitor.println(F(" Use File -> Examples -> TinyGSM -> tools -> AT_Debug"));
SerialMonitor.println(F(" to find correct configuration"));
SerialMonitor.println(F("***********************************************************"));
if (!modem.begin()) {
SerialMon.println(F("***********************************************************"));
SerialMon.println(F(" Cannot initialize module!"));
SerialMon.println(F(" Use File -> Examples -> TinyGSM -> tools -> AT_Debug"));
SerialMon.println(F(" to find correct configuration"));
SerialMon.println(F("***********************************************************"));
return;
}
bool ret = gsm.factoryDefault();
bool ret = modem.factoryDefault();
SerialMonitor.println(F("***********************************************************"));
SerialMonitor.print (F(" Return settings to Factory Defaults: "));
SerialMonitor.println((ret) ? "OK" : "FAIL");
SerialMonitor.println(F("***********************************************************"));
SerialMon.println(F("***********************************************************"));
SerialMon.print (F(" Return settings to Factory Defaults: "));
SerialMon.println((ret) ? "OK" : "FAIL");
SerialMon.println(F("***********************************************************"));
}
void loop() {


+ 28
- 20
tools/SimpleTest/SimpleTest.ino View File

@ -12,15 +12,12 @@
// Increase buffer fo see less commands
#define GSM_RX_BUFFER 256
#include <TinyGsmClient.h>
#include <StreamDebugger.h>
char apn[] = "YourAPN";
char user[] = "";
char pass[] = "";
// Set serial for debug console (to the Serial Monitor, speed 115200)
#define SerialMonitor Serial
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
@ -30,15 +27,19 @@ char pass[] = "";
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // RX, TX
StreamDebugger DebugAT(SerialAT, SerialMonitor);
TinyGsmClient client(DebugAT);
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
#include <TinyGsmClient.h>
TinyGsm modem(debugger);
TinyGsmClient client(modem);
char server[] = "cdn.rawgit.com";
char resource[] = "/vshymanskyy/tinygsm/master/extras/test_simple.txt";
void setup() {
// Set console baud rate
SerialMonitor.begin(115200);
SerialMon.begin(115200);
delay(10);
// Set GSM module baud rate
@ -47,12 +48,19 @@ void setup() {
// Restart takes quite some time
// You can skip it in many cases
SerialMonitor.println("Restarting modem...");
client.restart();
SerialMon.println("Restarting modem...");
modem.restart();
SerialMon.println("Waiting for network... ");
if (modem.waitForNetwork()) {
SerialMon.println("OK");
} else {
SerialMon.println("fail");
}
}
void loop() {
if (!client.networkConnect(apn, user, pass)) {
if (!modem.networkConnect(apn, user, pass)) {
delay(10000);
return;
}
@ -76,7 +84,7 @@ void loop() {
while (client.connected() && millis() - timeout < 10000L) {
while (client.available()) {
char c = client.read();
//SerialMonitor.print(c);
//SerialMon.print(c);
bytesReceived += 1;
timeout = millis();
}
@ -84,16 +92,16 @@ void loop() {
client.stop();
client.networkDisconnect();
modem.networkDisconnect();
SerialMonitor.println();
SerialMonitor.println("************************");
SerialMonitor.print (" Received: ");
SerialMonitor.print(bytesReceived);
SerialMonitor.println("bytes");
SerialMonitor.print (" Test: ");
SerialMonitor.println((bytesReceived == 1000) ? "PASSED" : "FAIL");
SerialMonitor.println("************************");
SerialMon.println();
SerialMon.println("************************");
SerialMon.print (" Received: ");
SerialMon.print(bytesReceived);
SerialMon.println("bytes");
SerialMon.print (" Test: ");
SerialMon.println((bytesReceived == 1000) ? "PASSED" : "FAIL");
SerialMon.println("************************");
// Do nothing forevermore
while (true) {


Loading…
Cancel
Save