Merge pull request #4 from EnviroDIY/sigQual2
Returning signal quality from ESP8266, XBee, tweeks for LTE-C1
This commit is contained in:
		@@ -225,7 +225,7 @@ public:
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /*
 | 
					  /*
 | 
				
			||||||
   * SIM card & Networ Operator functions
 | 
					   * SIM card & Network Operator functions
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool simUnlock(const char *pin) {
 | 
					  bool simUnlock(const char *pin) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -203,13 +203,29 @@ public:
 | 
				
			|||||||
    return autoBaud();
 | 
					    return autoBaud();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /*
 | 
				
			||||||
 | 
					   * SIM card & Network Operator functions
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  int getSignalQuality() {
 | 
				
			||||||
 | 
					    sendAT(GF("+CWLAP=\""), _ssid, GF("\""));
 | 
				
			||||||
 | 
					    DBG(GSM_NL, "<<< ");
 | 
				
			||||||
 | 
					    streamSkipUntil(':');
 | 
				
			||||||
 | 
					    streamSkipUntil(',');
 | 
				
			||||||
 | 
					    streamSkipUntil(',');
 | 
				
			||||||
 | 
					    streamSkipUntil(',');
 | 
				
			||||||
 | 
					    String res2 = streamReadUntil(',');
 | 
				
			||||||
 | 
					    streamSkipUntil(')');
 | 
				
			||||||
 | 
					    waitResponse();
 | 
				
			||||||
 | 
					    return res2.toInt();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool waitForNetwork(unsigned long timeout = 60000L) {
 | 
					  bool waitForNetwork(unsigned long timeout = 60000L) {
 | 
				
			||||||
    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
					    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
				
			||||||
      sendAT(GF("+CIPSTATUS"));
 | 
					      sendAT(GF("+CIPSTATUS"));
 | 
				
			||||||
      String res1 = stream.readStringUntil(':');
 | 
					      DBG(GSM_NL, "<<< ");
 | 
				
			||||||
      DBG(GSM_NL, res1, ':');
 | 
					      String res1 = streamReadUntil(':');
 | 
				
			||||||
      String res2 = stream.readStringUntil(*GSM_NL);
 | 
					      String res2 = streamReadUntil(*GSM_NL);
 | 
				
			||||||
      DBG(res2);
 | 
					 | 
				
			||||||
      waitResponse();
 | 
					      waitResponse();
 | 
				
			||||||
      if (res2 == GF("2") || res2 == GF("3") || res2 == GF("4")) {
 | 
					      if (res2 == GF("2") || res2 == GF("3") || res2 == GF("4")) {
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
@@ -224,6 +240,8 @@ public:
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  bool networkConnect(const char* ssid, const char* pwd) {
 | 
					  bool networkConnect(const char* ssid, const char* pwd) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _ssid = ssid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sendAT(GF("+CIPMUX=1"));
 | 
					    sendAT(GF("+CIPMUX=1"));
 | 
				
			||||||
    if (waitResponse() != 1) {
 | 
					    if (waitResponse() != 1) {
 | 
				
			||||||
      return false;
 | 
					      return false;
 | 
				
			||||||
@@ -264,7 +282,7 @@ public:
 | 
				
			|||||||
    streamWrite("AT", cmd..., GSM_NL);
 | 
					    streamWrite("AT", cmd..., GSM_NL);
 | 
				
			||||||
    stream.flush();
 | 
					    stream.flush();
 | 
				
			||||||
    TINY_GSM_YIELD();
 | 
					    TINY_GSM_YIELD();
 | 
				
			||||||
    DBG(GSM_NL, ">>> AT:", cmd...);
 | 
					    DBG(GSM_NL, ">>> AT", cmd...);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // TODO: Optimize this!
 | 
					  // TODO: Optimize this!
 | 
				
			||||||
@@ -415,9 +433,30 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  int streamRead() { return stream.read(); }
 | 
					  int streamRead() { return stream.read(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  String streamReadUntil(char c) {
 | 
				
			||||||
 | 
					    String return_string = stream.readStringUntil(c);
 | 
				
			||||||
 | 
					    return_string.trim();
 | 
				
			||||||
 | 
					    if (String(c) == GSM_NL || String(c) == "\n"){
 | 
				
			||||||
 | 
					      DBG(return_string, c, "    ");
 | 
				
			||||||
 | 
					    } else DBG(return_string, c);
 | 
				
			||||||
 | 
					    return return_string;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  bool streamSkipUntil(char c) {
 | 
				
			||||||
 | 
					    String skipped = stream.readStringUntil(c);
 | 
				
			||||||
 | 
					    skipped.trim();
 | 
				
			||||||
 | 
					    if (skipped.length()) {
 | 
				
			||||||
 | 
					      if (String(c) == GSM_NL || String(c) == "\n"){
 | 
				
			||||||
 | 
					        DBG(skipped, c, "    ");
 | 
				
			||||||
 | 
					      } else DBG(skipped, c);
 | 
				
			||||||
 | 
					      return true;
 | 
				
			||||||
 | 
					    } else return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
  Stream&       stream;
 | 
					  Stream&       stream;
 | 
				
			||||||
  GsmClient*    sockets[5];
 | 
					  GsmClient*    sockets[5];
 | 
				
			||||||
 | 
					  const char*   _ssid;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef TinyGsm::GsmClient TinyGsmClient;
 | 
					typedef TinyGsm::GsmClient TinyGsmClient;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -234,7 +234,7 @@ public:
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /*
 | 
					  /*
 | 
				
			||||||
   * SIM card & Networ Operator functions
 | 
					   * SIM card & Network Operator functions
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool simUnlock(const char *pin) {
 | 
					  bool simUnlock(const char *pin) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -241,7 +241,7 @@ public:
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /*
 | 
					  /*
 | 
				
			||||||
   * SIM card & Networ Operator functions
 | 
					   * SIM card & Network Operator functions
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool simUnlock(const char *pin) {
 | 
					  bool simUnlock(const char *pin) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,6 +27,11 @@ enum SimStatus {
 | 
				
			|||||||
  SIM_LOCKED = 2,
 | 
					  SIM_LOCKED = 2,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					enum XBeeType {
 | 
				
			||||||
 | 
					  S6B    = 0,
 | 
				
			||||||
 | 
					  LTEC1 = 1,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum RegStatus {
 | 
					enum RegStatus {
 | 
				
			||||||
  REG_UNREGISTERED = 0,
 | 
					  REG_UNREGISTERED = 0,
 | 
				
			||||||
  REG_SEARCHING    = 2,
 | 
					  REG_SEARCHING    = 2,
 | 
				
			||||||
@@ -160,7 +165,14 @@ public:
 | 
				
			|||||||
    sendAT(GF("GT64")); // shorten the guard time to 100ms
 | 
					    sendAT(GF("GT64")); // shorten the guard time to 100ms
 | 
				
			||||||
    waitResponse();
 | 
					    waitResponse();
 | 
				
			||||||
    writeChanges();
 | 
					    writeChanges();
 | 
				
			||||||
 | 
					    sendAT(GF("HS"));  // Get the "Hardware Series"; 0x601 for S6B (Wifi)
 | 
				
			||||||
 | 
					    // wait for the response
 | 
				
			||||||
 | 
					    unsigned long startMillis = millis();
 | 
				
			||||||
 | 
					    while (!stream.available() && millis() - startMillis < 1000) {};
 | 
				
			||||||
 | 
					    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
 | 
					    if (res == "601") beeType = S6B;
 | 
				
			||||||
 | 
					    else beeType = LTEC1;
 | 
				
			||||||
    guardTime = 125;
 | 
					    guardTime = 125;
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -212,7 +224,7 @@ public:
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /*
 | 
					  /*
 | 
				
			||||||
   * SIM card & Networ Operator functions
 | 
					   * SIM card & Network Operator functions
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool simUnlock(const char *pin) {  // Not supported
 | 
					  bool simUnlock(const char *pin) {  // Not supported
 | 
				
			||||||
@@ -243,18 +255,19 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  int getSignalQuality() {
 | 
					  int getSignalQuality() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("DB"));
 | 
					    if (beeType == S6B) sendAT(GF("LM"));  // ask for the "link margin" - the dB above sensitivity
 | 
				
			||||||
 | 
					    else sendAT(GF("DB"));  // ask for the cell strenght in dBm
 | 
				
			||||||
    // wait for the response
 | 
					    // wait for the response
 | 
				
			||||||
    unsigned long startMillis = millis();
 | 
					    unsigned long startMillis = millis();
 | 
				
			||||||
    while (!stream.available() && millis() - startMillis < 1000) {};
 | 
					    while (!stream.available() && millis() - startMillis < 1000) {};
 | 
				
			||||||
    char buf[4] = { 0, };  // Does not send an OK, just the result
 | 
					    char buf[2] = {0};  // Set up buffer for response
 | 
				
			||||||
    buf[0] = streamRead();
 | 
					    buf[0] = streamRead();
 | 
				
			||||||
    buf[1] = streamRead();
 | 
					    buf[1] = streamRead();
 | 
				
			||||||
    buf[2] = streamRead();
 | 
					    DBG(buf[0], buf[1], "\n");
 | 
				
			||||||
    buf[3] = streamRead();
 | 
					 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
    int intr = strtol(buf, 0, 16);
 | 
					    int intr = strtol(buf, 0, 16);
 | 
				
			||||||
    return intr;
 | 
					    if (beeType == S6B) return -93 + intr;  // the maximum sensitivity is -93dBm
 | 
				
			||||||
 | 
					    else return -1*intr; // need to convert to negative number
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  SimStatus getSimStatus(unsigned long timeout = 10000L) {
 | 
					  SimStatus getSimStatus(unsigned long timeout = 10000L) {
 | 
				
			||||||
@@ -263,24 +276,25 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  RegStatus getRegistrationStatus() {
 | 
					  RegStatus getRegistrationStatus() {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("AI"));
 | 
					    if (beeType == S6B) sendAT(GF("AI"));
 | 
				
			||||||
 | 
					    else sendAT(GF("CI"));
 | 
				
			||||||
    // wait for the response
 | 
					    // wait for the response
 | 
				
			||||||
    unsigned long startMillis = millis();
 | 
					    unsigned long startMillis = millis();
 | 
				
			||||||
    while (!stream.available() && millis() - startMillis < 1000) {};
 | 
					    while (!stream.available() && millis() - startMillis < 1000) {};
 | 
				
			||||||
    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
					    String res = streamReadUntil('\r');  // Does not send an OK, just the result
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(res == GF("0x00"))
 | 
					    if(res == GF("0"))
 | 
				
			||||||
      return REG_OK_HOME;
 | 
					      return REG_OK_HOME;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else if(res == GF("0x13") || res == GF("0x2A"))
 | 
					    else if(res == GF("13") || res == GF("2A"))
 | 
				
			||||||
      return REG_UNREGISTERED;
 | 
					      return REG_UNREGISTERED;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else if(res == GF("0xFF") || res == GF("0x22") || res == GF("0x23") ||
 | 
					    else if(res == GF("FF") || res == GF("22") || res == GF("23") ||
 | 
				
			||||||
            res == GF("0x40") || res == GF("0x41") || res == GF("0x42"))
 | 
					            res == GF("40") || res == GF("41") || res == GF("42"))
 | 
				
			||||||
      return REG_SEARCHING;
 | 
					      return REG_SEARCHING;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else if(res == GF("0x24"))
 | 
					    else if(res == GF("24"))
 | 
				
			||||||
      return REG_DENIED;
 | 
					      return REG_DENIED;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else return REG_UNKNOWN;
 | 
					    else return REG_UNKNOWN;
 | 
				
			||||||
@@ -301,7 +315,8 @@ public:
 | 
				
			|||||||
  bool waitForNetwork(unsigned long timeout = 60000L) {
 | 
					  bool waitForNetwork(unsigned long timeout = 60000L) {
 | 
				
			||||||
    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
					    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
				
			||||||
      commandMode();
 | 
					      commandMode();
 | 
				
			||||||
      sendAT(GF("AI"));
 | 
					      if (beeType == S6B) sendAT(GF("AI"));
 | 
				
			||||||
 | 
					      else sendAT(GF("CI"));
 | 
				
			||||||
      // wait for the response
 | 
					      // wait for the response
 | 
				
			||||||
      unsigned long startMillis = millis();
 | 
					      unsigned long startMillis = millis();
 | 
				
			||||||
      while (!stream.available() && millis() - startMillis < 1000) {};
 | 
					      while (!stream.available() && millis() - startMillis < 1000) {};
 | 
				
			||||||
@@ -510,7 +525,8 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  bool modemGetConnected(uint8_t mux = 1) {
 | 
					  bool modemGetConnected(uint8_t mux = 1) {
 | 
				
			||||||
    commandMode();
 | 
					    commandMode();
 | 
				
			||||||
    sendAT(GF("AI"));
 | 
					    if (beeType == S6B) sendAT(GF("AI"));
 | 
				
			||||||
 | 
					    else sendAT(GF("CI"));
 | 
				
			||||||
    int res = waitResponse(GF("0"));
 | 
					    int res = waitResponse(GF("0"));
 | 
				
			||||||
    exitCommand();
 | 
					    exitCommand();
 | 
				
			||||||
    return 1 == res;
 | 
					    return 1 == res;
 | 
				
			||||||
@@ -528,12 +544,7 @@ private:
 | 
				
			|||||||
    streamWrite(tail...);
 | 
					    streamWrite(tail...);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  int streamRead() {
 | 
					  int streamRead() { return stream.read(); }
 | 
				
			||||||
    TINY_GSM_YIELD();
 | 
					 | 
				
			||||||
    int c = stream.read();
 | 
					 | 
				
			||||||
    DBG((char)c);
 | 
					 | 
				
			||||||
    return c;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  String streamReadUntil(char c) {
 | 
					  String streamReadUntil(char c) {
 | 
				
			||||||
    TINY_GSM_YIELD();
 | 
					    TINY_GSM_YIELD();
 | 
				
			||||||
@@ -571,6 +582,7 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
  int           guardTime;
 | 
					  int           guardTime;
 | 
				
			||||||
 | 
					  XBeeType      beeType;
 | 
				
			||||||
  Stream&       stream;
 | 
					  Stream&       stream;
 | 
				
			||||||
  GsmClient*    sockets[1];
 | 
					  GsmClient*    sockets[1];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user