diff --git a/examples/AllFunctions/AllFunctions.ino b/examples/AllFunctions/AllFunctions.ino index ff44e08..7447b73 100644 --- a/examples/AllFunctions/AllFunctions.ino +++ b/examples/AllFunctions/AllFunctions.ino @@ -111,6 +111,7 @@ void loop() { // To skip it, call init() instead of restart() DBG("Initializing modem..."); if (!modem.restart()) { + // if (!modem.init()) { DBG("Failed to restart modem, delaying 10s and retrying"); delay(3000); // restart autobaud in case GSM just rebooted @@ -122,10 +123,12 @@ void loop() { String modemInfo = modem.getModemInfo(); DBG("Modem:", modemInfo); +#if TINY_GSM_USE_GPRS // Unlock your SIM card with a PIN if needed if ( GSM_PIN && modem.getSimStatus() != 3 ) { modem.simUnlock(GSM_PIN); } +#endif #if TINY_GSM_USE_WIFI SerialMon.print(F("Setting SSID/password...")); diff --git a/examples/WebClient/WebClient.ino b/examples/WebClient/WebClient.ino index 6bce77d..bf3b843 100644 --- a/examples/WebClient/WebClient.ino +++ b/examples/WebClient/WebClient.ino @@ -116,6 +116,7 @@ void setup() { // To skip it, call init() instead of restart() SerialMon.println("Initializing modem..."); modem.restart(); + // modem.init(); String modemInfo = modem.getModemInfo(); SerialMon.print("Modem: "); diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index d701446..a552c43 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -482,7 +482,7 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE; /* - * Battery functions + * Battery & temperature functions */ uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; @@ -492,12 +492,41 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return false; } - stream.readStringUntil(','); + streamSkipUntil(','); // Skip battery charge status + // Read battery charge level int res = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return res; + } + + uint8_t getBattChargeState() + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + // Read battery charge status + int res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + chargeState = stream.readStringUntil(',').toInt(); + percent = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return true; + } + + + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; + /* * Client related functions */ diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index b36f16b..3cbe153 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -436,7 +436,7 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE; /* - * Battery functions + * Battery & temperature functions */ // Use: float vBatt = modem.getBattVoltage() / 1000.0; @@ -445,10 +445,11 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return 0; } - streamSkipUntil(','); // Skip - streamSkipUntil(','); // Skip - + streamSkipUntil(','); // Skip battery charge status + streamSkipUntil(','); // Skip battery charge level + // return voltage in mV uint16_t res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } @@ -458,12 +459,41 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return false; } - stream.readStringUntil(','); + streamSkipUntil(','); // Skip battery charge status + // Read battery charge level + int res = stream.readStringUntil(',').toInt(); + // Wait for final OK + waitResponse(); + return res; + } + + uint8_t getBattChargeState() + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + // Read battery charge status int res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + chargeState = stream.readStringUntil(',').toInt(); + percent = stream.readStringUntil(',').toInt(); + milliVolts = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return true; + } + + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; + /* * Client related functions */ diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index af8df8f..f736166 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -325,6 +325,17 @@ TINY_GSM_MODEM_MAINTAIN_LISTEN() return TinyGsmIpFromString(getLocalIP()); } + /* + * Battery & temperature functions + */ + + // Use: float vBatt = modem.getBattVoltage() / 1000.0; + uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; + int8_t getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; + uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE; + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) TINY_GSM_ATTR_NOT_AVAILABLE; + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; + /* * Client related functions */ diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index f08b8c8..b2f9673 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -400,12 +400,14 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE; /* - * Battery functions + * Battery & temperature functions */ uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; - int8_t getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; + uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE; + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) TINY_GSM_ATTR_NOT_AVAILABLE; + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; /* * Client related functions diff --git a/src/TinyGsmClientM95.h b/src/TinyGsmClientM95.h index 1f83b22..6fe2885 100644 --- a/src/TinyGsmClientM95.h +++ b/src/TinyGsmClientM95.h @@ -484,7 +484,7 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE; /* - * Battery functions + * Battery & temperature functions */ // Use: float vBatt = modem.getBattVoltage() / 1000.0; @@ -493,10 +493,11 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return 0; } - streamSkipUntil(','); // Skip - streamSkipUntil(','); // Skip - + streamSkipUntil(','); // Skip battery charge status + streamSkipUntil(','); // Skip battery charge level + // return voltage in mV uint16_t res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } @@ -506,12 +507,53 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return false; } - stream.readStringUntil(','); + streamSkipUntil(','); // Skip battery charge status + // Read battery charge level int res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } + uint8_t getBattChargeState() + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + // Read battery charge status + int res = stream.readStringUntil(',').toInt(); + // Wait for final OK + waitResponse(); + return res; + } + + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + chargeState = stream.readStringUntil(',').toInt(); + percent = stream.readStringUntil(',').toInt(); + milliVolts = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return true; + } + + float getTemperature(uint16_t &milliVolts = 0) { + sendAT(GF("+QTEMP")); + if (waitResponse(GF(GSM_NL "+QTEMP:")) != 1) { + return (float)-9999; + } + streamSkipUntil(','); // Skip mode + // Read charge of thermistor + milliVolts = stream.readStringUntil(',').toInt(); + float temp = stream.readStringUntil('\n').toFloat(); + // Wait for final OK + waitResponse(); + return temp; + } + /* * Client related functions */ diff --git a/src/TinyGsmClientMC60.h b/src/TinyGsmClientMC60.h index 4f62903..4f06c97 100644 --- a/src/TinyGsmClientMC60.h +++ b/src/TinyGsmClientMC60.h @@ -503,7 +503,7 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() } /* - * Battery functions + * Battery & temperature functions */ // Use: float vBatt = modem.getBattVoltage() / 1000.0; @@ -512,10 +512,11 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return 0; } - streamSkipUntil(','); // Skip - streamSkipUntil(','); // Skip - + streamSkipUntil(','); // Skip battery charge status + streamSkipUntil(','); // Skip battery charge level + // return voltage in mV uint16_t res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } @@ -525,12 +526,41 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return false; } - stream.readStringUntil(','); + streamSkipUntil(','); // Skip battery charge status + // Read battery charge level + int res = stream.readStringUntil(',').toInt(); + // Wait for final OK + waitResponse(); + return res; + } + + uint8_t getBattChargeState() + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + // Read battery charge status int res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + chargeState = stream.readStringUntil(',').toInt(); + percent = stream.readStringUntil(',').toInt(); + milliVolts = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return true; + } + + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; + /* * Client related functions */ diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 8e6878d..5f8e9ac 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -735,9 +735,9 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return 0; } - streamSkipUntil(','); // Skip - streamSkipUntil(','); // Skip - + streamSkipUntil(','); // Skip battery charge status + streamSkipUntil(','); // Skip battery charge level + // return voltage in mV uint16_t res = stream.readStringUntil(',').toInt(); waitResponse(); return res; @@ -748,12 +748,40 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return false; } - stream.readStringUntil(','); + streamSkipUntil(','); // Skip battery charge status + // Read battery charge level + int res = stream.readStringUntil(',').toInt(); + waitResponse(); + return res; + } + + uint8_t getBattChargeState() + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + // Read battery charge status int res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + chargeState = stream.readStringUntil(',').toInt(); + percent = stream.readStringUntil(',').toInt(); + milliVolts = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return true; + } + + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; + /* * Client related functions */ diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index cd1b9fa..4c80fcb 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -647,7 +647,7 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() } /* - * Battery functions + * Battery & temperature functions */ // Use: float vBatt = modem.getBattVoltage() / 1000.0; @@ -656,10 +656,11 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return 0; } - streamSkipUntil(','); // Skip - streamSkipUntil(','); // Skip - + streamSkipUntil(','); // Skip battery charge status + streamSkipUntil(','); // Skip battery charge level + // return voltage in mV uint16_t res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } @@ -669,12 +670,41 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { return false; } - stream.readStringUntil(','); + streamSkipUntil(','); // Skip battery charge status + // Read battery charge level + int res = stream.readStringUntil(',').toInt(); + // Wait for final OK + waitResponse(); + return res; + } + + uint8_t getBattChargeState() + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + // Read battery charge status int res = stream.readStringUntil(',').toInt(); + // Wait for final OK waitResponse(); return res; } + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + sendAT(GF("+CBC?")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + chargeState = stream.readStringUntil(',').toInt(); + percent = stream.readStringUntil(',').toInt(); + milliVolts = stream.readStringUntil('\n').toInt(); + // Wait for final OK + waitResponse(); + return true; + } + + float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE; + /* * Client related functions */ diff --git a/src/TinyGsmClientSaraR4.h b/src/TinyGsmClientSaraR4.h index cb45109..d06ec78 100644 --- a/src/TinyGsmClientSaraR4.h +++ b/src/TinyGsmClientSaraR4.h @@ -490,7 +490,7 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() } /* - * Battery functions + * Battery & temperature functions */ uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; @@ -501,9 +501,37 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() return 0; } - int res = stream.readStringUntil(',').toInt(); + int8_t res = stream.readStringUntil(',').toInt(); + int8_t percent = res*20; // return is 0-5 + // Wait for final OK waitResponse(); - return res; + return percent; + } + + uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE; + + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + percent = getBattPercent(); + return true; + } + + float getTemperature() { + // First make sure the temperature is set to be in celsius + sendAT(GF("+UTEMP=0")); // Would use 1 for Fahrenheit + if (waitResponse() != 1) { + return (float)-9999; + } + sendAT(GF("+UTEMP?")); + if (waitResponse(GF(GSM_NL "+UTEMP:")) != 1) { + return (float)-9999; + } + streamSkipUntil(','); // Skip units (C/F) + int16_t res = stream.readStringUntil('\n').toInt(); + float temp = -9999; + if (res != 655355) { + temp = ((float)res)/10; + } + return temp; } /* diff --git a/src/TinyGsmClientSequansMonarch.h b/src/TinyGsmClientSequansMonarch.h index 21c2f0a..e48bcde 100644 --- a/src/TinyGsmClientSequansMonarch.h +++ b/src/TinyGsmClientSequansMonarch.h @@ -422,7 +422,6 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() String getLocalIP() { sendAT(GF("+CGPADDR=3")); - if (waitResponse(10000L, GF("+CGPADDR: 3,\"")) != 1) { return ""; } @@ -476,12 +475,28 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* - * Battery functions + * Battery & temperature functions */ uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; - - int getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; + int8_t getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; + uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE; + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) TINY_GSM_ATTR_NOT_AVAILABLE; + + float getTemperature() { + sendAT(GF("+SMDTH")); + if (waitResponse(10000L, GF("+SMDTH: ")) != 1) { + return (float)-9999; + } + String res; + if (waitResponse(1000L, res) != 1) { + return (float)-9999; + } + if (res.indexOf("ERROR") >=0) { + return (float)-9999; + } + return res.toFloat(); + } protected: @@ -569,6 +584,28 @@ protected: return 0; } return len; + + // uint8_t nAttempts = 5; + // bool gotPrompt = false; + // while (nAttempts > 0 && !gotPrompt) { + // sendAT(GF("+SQNSSEND="), mux); + // if (waitResponse(5000, GF(GSM_NL "> ")) == 1) { + // gotPrompt = true; + // } + // nAttempts--; + // delay(50); + // } + // if (gotPrompt) { + // stream.write((uint8_t*)buff, len); + // stream.write((char)0x1A); + // stream.flush(); + // if (waitResponse() != 1) { + // DBG("### no OK after send"); + // return 0; + // } + // return len; + // } + // return 0; } diff --git a/src/TinyGsmClientUBLOX.h b/src/TinyGsmClientUBLOX.h index 53be58d..5c56f9f 100644 --- a/src/TinyGsmClientUBLOX.h +++ b/src/TinyGsmClientUBLOX.h @@ -489,7 +489,7 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() } /* - * Battery functions + * Battery & temperature functions */ uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; @@ -505,6 +505,16 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() return res; } + uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE; + + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) { + percent = getBattPercent(); + return true; + } + + // This would only available for a small number of modules in this group (TOBY-L) + float getTemperature() TINY_GSM_ATTR_NOT_IMPLEMENTED; + /* * Client related functions */ diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index e018353..eaa1d47 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -782,12 +782,25 @@ public: String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE; /* - * Battery functions + * Battery & temperature functions */ + // Use: float vBatt = modem.getBattVoltage() / 1000.0; uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; - int8_t getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; + uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE; + bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) TINY_GSM_ATTR_NOT_AVAILABLE; + + float getTemperature() { + String res = sendATGetString(GF("TP")); + if (res == "") { + return (float)-9999; + } + char buf[5] = {0,}; + res.toCharArray(buf, 5); + int8_t intRes = (int8_t)strtol(buf, 0, 16); // degrees Celsius displayed in 8-bit two's complement format. + return (float)intRes; + } /* * Client related functions