IT WORKS!
This commit is contained in:
		@@ -90,7 +90,7 @@ public:
 | 
				
			|||||||
    return sock_connected;
 | 
					    return sock_connected;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  virtual void stop() {
 | 
					  virtual void stop() {  // Not supported
 | 
				
			||||||
    sock_connected = false;
 | 
					    sock_connected = false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -106,38 +106,16 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  virtual int available() {
 | 
					  virtual int available() {
 | 
				
			||||||
    TINY_GSM_YIELD();
 | 
					    TINY_GSM_YIELD();
 | 
				
			||||||
    if (!rx.size()) {
 | 
					    return at->stream.available();
 | 
				
			||||||
      at->maintain();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return rx.size();
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  virtual int read(uint8_t *buf, size_t size) {
 | 
					  virtual int read(uint8_t *buf, size_t size) {
 | 
				
			||||||
    TINY_GSM_YIELD();
 | 
					    return available();
 | 
				
			||||||
    size_t cnt = 0;
 | 
					 | 
				
			||||||
    while (cnt < size) {
 | 
					 | 
				
			||||||
      size_t chunk = TinyGsmMin(size-cnt, rx.size());
 | 
					 | 
				
			||||||
      if (chunk > 0) {
 | 
					 | 
				
			||||||
        rx.get(buf, chunk);
 | 
					 | 
				
			||||||
        buf += chunk;
 | 
					 | 
				
			||||||
        cnt += chunk;
 | 
					 | 
				
			||||||
        continue;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      // TODO: Read directly into user buffer?
 | 
					 | 
				
			||||||
      if (!rx.size()) {
 | 
					 | 
				
			||||||
        at->maintain();
 | 
					 | 
				
			||||||
        //break;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return cnt;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  virtual int read() {
 | 
					  virtual int read() {
 | 
				
			||||||
    uint8_t c;
 | 
					    TINY_GSM_YIELD();
 | 
				
			||||||
    if (read(&c, 1) == 1) {
 | 
					    return at->stream.read();
 | 
				
			||||||
      return c;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return -1;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  virtual int peek() { return at->stream.peek(); }
 | 
					  virtual int peek() { return at->stream.peek(); }
 | 
				
			||||||
@@ -167,7 +145,15 @@ public:
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool init() {
 | 
					  bool init() {
 | 
				
			||||||
    factoryDefault();
 | 
					    guardTime = 1100;
 | 
				
			||||||
 | 
					    commandMode();
 | 
				
			||||||
 | 
					    sendAT(GF("AP0"));  // Put in transparent mode
 | 
				
			||||||
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					    sendAT(GF("GTFA")); // shorten the guard time to 250ms
 | 
				
			||||||
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					    writeChanges();
 | 
				
			||||||
 | 
					    exitCommand();
 | 
				
			||||||
 | 
					    guardTime = 300;
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -222,7 +208,7 @@ public:
 | 
				
			|||||||
  String getSimCCID() {
 | 
					  String getSimCCID() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("S#"));
 | 
					    sendAT(GF("S#"));
 | 
				
			||||||
    String res = streamReadUntil('\r');
 | 
					    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
    return res;
 | 
					    return res;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -230,7 +216,7 @@ public:
 | 
				
			|||||||
  String getIMEI() {
 | 
					  String getIMEI() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("IM"));
 | 
					    sendAT(GF("IM"));
 | 
				
			||||||
    String res = streamReadUntil('\r');
 | 
					    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
    return res;
 | 
					    return res;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -238,7 +224,7 @@ public:
 | 
				
			|||||||
  int getSignalQuality() {
 | 
					  int getSignalQuality() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("DB"));
 | 
					    sendAT(GF("DB"));
 | 
				
			||||||
    char buf[4] = { 0, };
 | 
					    char buf[4] = { 0, };  // Does not send an OK, just the result
 | 
				
			||||||
    buf[0] = streamRead();
 | 
					    buf[0] = streamRead();
 | 
				
			||||||
    buf[1] = streamRead();
 | 
					    buf[1] = streamRead();
 | 
				
			||||||
    buf[2] = streamRead();
 | 
					    buf[2] = streamRead();
 | 
				
			||||||
@@ -255,7 +241,7 @@ public:
 | 
				
			|||||||
  RegStatus getRegistrationStatus() {
 | 
					  RegStatus getRegistrationStatus() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("AI"));
 | 
					    sendAT(GF("AI"));
 | 
				
			||||||
    String res = streamReadUntil('\r');
 | 
					    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(res == GF("0x00"))
 | 
					    if(res == GF("0x00"))
 | 
				
			||||||
@@ -277,7 +263,7 @@ public:
 | 
				
			|||||||
  String getOperator() {
 | 
					  String getOperator() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("MN"));
 | 
					    sendAT(GF("MN"));
 | 
				
			||||||
    String res = streamReadUntil('\r');
 | 
					    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
    return res;
 | 
					    return res;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -287,10 +273,9 @@ public:
 | 
				
			|||||||
    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
					    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
				
			||||||
      commandMode();
 | 
					      commandMode();
 | 
				
			||||||
      sendAT(GF("AI"));
 | 
					      sendAT(GF("AI"));
 | 
				
			||||||
      waitResponse();
 | 
					      String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
      String res = streamReadUntil('\r');
 | 
					 | 
				
			||||||
      exitCommand();
 | 
					      exitCommand();
 | 
				
			||||||
      if (res == 0) {
 | 
					      if (res == GF("0")) {
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      delay(1000);
 | 
					      delay(1000);
 | 
				
			||||||
@@ -309,6 +294,8 @@ public:
 | 
				
			|||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    sendAT(GF("IP"), 1);  // Put in TCP mode
 | 
					    sendAT(GF("IP"), 1);  // Put in TCP mode
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					    sendAT(GF("EE"), 2);  // Set security to WPA2
 | 
				
			||||||
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sendAT(GF("ID"), ssid);
 | 
					    sendAT(GF("ID"), ssid);
 | 
				
			||||||
    if (waitResponse() != 1) {
 | 
					    if (waitResponse() != 1) {
 | 
				
			||||||
@@ -338,20 +325,15 @@ public:
 | 
				
			|||||||
   * GPRS functions
 | 
					   * GPRS functions
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  bool gprsConnect(const char* apn, const char* user = "", const char* pw = "") {
 | 
					  bool gprsConnect(const char* apn, const char* user = "", const char* pw = "") {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    sendAT(GF("AP"), 0);  // Put in transparent mode
 | 
					    sendAT(GF("AP"), 0);  // Put in transparent mode
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    sendAT(GF("IP"), 1);  // Put in TCP mode
 | 
					    sendAT(GF("IP"), 1);  // Put in TCP mode
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    sendAT(GF("AN"), apn);  // Set the APN
 | 
					    sendAT(GF("AN"), apn);  // Set the APN
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    writeChanges();
 | 
					    writeChanges();
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -371,20 +353,18 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  bool sendSMS(const String& number, const String& text) {
 | 
					  bool sendSMS(const String& number, const String& text) {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("AP"), 0);
 | 
					    sendAT(GF("AP"), 0);  // Put in transparent mode
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    sendAT(GF("IP"), 2);
 | 
					    sendAT(GF("IP"), 2);  // Put in text messaging mode
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    sendAT(GF("PH"), number);
 | 
					    sendAT(GF("PH"), number);  // Set the phone number
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    sendAT(GF("TD D"));
 | 
					    sendAT(GF("TDD"));  // Set the text delimiter to the standard 0x0D (carriabe return)
 | 
				
			||||||
    waitResponse();
 | 
					 | 
				
			||||||
    sendAT(GF("TD D"));
 | 
					 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    writeChanges();
 | 
					    writeChanges();
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
    stream.print(text);
 | 
					    stream.print(text);
 | 
				
			||||||
    stream.write((char)0x0D);
 | 
					    stream.write((char)0x0D);  // close off with the carriage return
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -394,7 +374,7 @@ public:
 | 
				
			|||||||
    streamWrite("AT", cmd..., GSM_NL);
 | 
					    streamWrite("AT", cmd..., GSM_NL);
 | 
				
			||||||
    stream.flush();
 | 
					    stream.flush();
 | 
				
			||||||
    TINY_GSM_YIELD();
 | 
					    TINY_GSM_YIELD();
 | 
				
			||||||
    DBG("\r\n", ">>> AT ", cmd..., "\r\n");
 | 
					    DBG(">>> AT ", cmd..., "\r\n");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // TODO: Optimize this!
 | 
					  // TODO: Optimize this!
 | 
				
			||||||
@@ -447,9 +427,9 @@ public:
 | 
				
			|||||||
    else {
 | 
					    else {
 | 
				
			||||||
      data.trim();
 | 
					      data.trim();
 | 
				
			||||||
      data.replace(GSM_NL GSM_NL, GSM_NL);
 | 
					      data.replace(GSM_NL GSM_NL, GSM_NL);
 | 
				
			||||||
      data.replace(GSM_NL, "\r\n" "    ");
 | 
					      data.replace(GSM_NL, "\r\n    ");
 | 
				
			||||||
      if (data.length()) {
 | 
					      if (data.length()) {
 | 
				
			||||||
        DBG("\r\n", "<<< ", data);
 | 
					        DBG("<<< ", data, "\r\n");
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // if (gotData) {
 | 
					    // if (gotData) {
 | 
				
			||||||
@@ -532,17 +512,17 @@ private:
 | 
				
			|||||||
  String streamReadUntil(char c) {
 | 
					  String streamReadUntil(char c) {
 | 
				
			||||||
    String return_string = stream.readStringUntil(c);
 | 
					    String return_string = stream.readStringUntil(c);
 | 
				
			||||||
    return_string.trim();
 | 
					    return_string.trim();
 | 
				
			||||||
    if (String(c) == GSM_NL || String(c) == "\n"){
 | 
					    if (String(c) == GSM_NL){
 | 
				
			||||||
      DBG(return_string, c, "    ");
 | 
					      DBG(return_string, "\r\n");
 | 
				
			||||||
    } else DBG(return_string, c);
 | 
					    } else DBG(return_string, c);
 | 
				
			||||||
    return return_string;
 | 
					    return return_string;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool commandMode(void){
 | 
					  bool commandMode(void){
 | 
				
			||||||
    delay(1000);  // cannot send anything for 1 second before entering command mode
 | 
					    delay(guardTime);  // cannot send anything for 1 second before entering command mode
 | 
				
			||||||
    streamWrite(GF("+++"));  // enter command mode
 | 
					    streamWrite(GF("+++"));  // enter command mode
 | 
				
			||||||
    DBG("\r\n+++\r\n");
 | 
					    DBG("\r\n+++\r\n");
 | 
				
			||||||
    waitResponse(1100);
 | 
					    waitResponse(guardTime);
 | 
				
			||||||
    return 1 == waitResponse(1100);  // wait another second for an "OK\r"
 | 
					    return 1 == waitResponse(1100);  // wait another second for an "OK\r"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -559,6 +539,7 @@ private:
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
 | 
					  int           guardTime;
 | 
				
			||||||
  Stream&       stream;
 | 
					  Stream&       stream;
 | 
				
			||||||
  GsmClient*    sockets[1];
 | 
					  GsmClient*    sockets[1];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user