Skip to content

Commit

Permalink
Attempt v2 at recovering from modem crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
ShonP40 committed Sep 29, 2023
1 parent ad94aae commit a8e9e7d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ extern TinyGsmClient client;
extern PubSubClient mqtt;

// Export functions
void rebootDevice();
void packageAndSendMQTT(String value, String topic);
const char* stringToChar(String value);
int stringToInt(String value);
void setCPUFrequency(int frequency);

// Extern configuration variables
extern String apn, apnusername, apnpassword, simpin, timezone, dst, apssid, appassword, appowersaving, mqttaddress, mqttport, mqttclientname, mqttusername, mqttpassword, locationgnssmode, locationdpo, sensorsenable, sensorsenablebme280, sensorbme280i2caddress, mqttsensorsbme280temperature, mqttsensorsbme280pressure, mqttsensorsbme280humidity, sensorsenabletsl2561, sensortsl2561i2caddress, sensortsl2561gain, mqttsensorstsl2561lux, sensorsenablepir, sensorpirpin, mqttsensorspir, sensorsenablecputemp, mqttsensorscputemp, sensorsenablecpufreq, mqttsensorscpufreq, sensorsenablefreeram, mqttsensorsfreeram, sensorsenablehall, mqttsensorshall, mqttmodeminfo, mqttmodemccid, mqttmodemimsi, mqttmodemoperator, mqttmodemsignalquality, mqttmodempublicip, mqttbatterypercentage, mqttbatteryvoltage, mqttbatterystatus, mqttlocationtype, mqttlocationlatitude, mqttlocationlongitude, mqttlocationspeed, mqttlocationaltitude, mqttlocationaccuracy, ftpenable, ftpuser, ftppass, bluetoothenable, bluetoothname, bluetoothobdidentifier, bluetoothobdmqttobdpayload, bluetoothobdmqttobddebug, bluetoothobdmqttengineload, bluetoothobdmqttenginecoolanttemp, bluetoothobdmqttshorttermfueltrimbank1, bluetoothobdmqttlongtermfueltrimbank1, bluetoothobdmqttshorttermfueltrimbank2, bluetoothobdmqttlongtermfueltrimbank2, bluetoothobdmqttfuelpressure, bluetoothobdmqttmanifoldpressure, bluetoothobdmqttrpm, bluetoothobdmqttkph, bluetoothobdmqttmph, bluetoothobdmqtttimingadvance, bluetoothobdmqttintakeairtemp, bluetoothobdmqttmafrate, bluetoothobdmqttthrottle, bluetoothobdmqttauxinputstatus, bluetoothobdmqttruntime, bluetoothobdmqttdisttravelwithmil, bluetoothobdmqttfuelrailpressure, bluetoothobdmqttfuelrailguagepressure, bluetoothobdmqttcommandedegr, bluetoothobdmqttegrerror, bluetoothobdmqttcommandedevappurge, bluetoothobdmqttfuellevel, bluetoothobdmqttwarmupssincecodescleared, bluetoothobdmqttdistsincecodescleared, bluetoothobdmqttevapsysvappressure, bluetoothobdmqttabsbaropressure, bluetoothobdmqttcattempb1s1, bluetoothobdmqttcattempb2s1, bluetoothobdmqttcattempb1s2, bluetoothobdmqttcattempb2s2, bluetoothobdmqttctrlmodvoltage, bluetoothobdmqttabsload, bluetoothobdmqttcommandedairfuelratio, bluetoothobdmqttrelativethrottle, bluetoothobdmqttambientairtemp, bluetoothobdmqttabsthrottleposb, bluetoothobdmqttabsthrottleposc, bluetoothobdmqttabsthrottleposd, bluetoothobdmqttabsthrottlepose, bluetoothobdmqttabsthrottleposf, bluetoothobdmqttcommandedthrottleactuator, bluetoothobdmqtttimerunwithmil, bluetoothobdmqtttimesincecodescleared, bluetoothobdmqttmaxmafrate, bluetoothobdmqttethanolPercent, bluetoothobdmqttabsevapsysvappressure, bluetoothobdmqttevapsysvappressure2, bluetoothobdmqttabsfuelrailpressure, bluetoothobdmqttrelativepedalpos, bluetoothobdmqtthybridbatlife, bluetoothobdmqttoiltemp, bluetoothobdmqttfuelinjecttiming, bluetoothobdmqttfuelrate, bluetoothobdmqttdemandedtorque, bluetoothobdmqtttorque, bluetoothobdmqttreferencetorque, bluetoothobdmqttbatteryvoltage, bluetoothobdmqttvin, mqttuptime, mqttversion, dynamicfrequency, lowpowermodeonbattery;
extern String apn, apnusername, apnpassword, simpin, timezone, dst, apssid, appassword, appowersaving, mqttaddress, mqttport, mqttclientname, mqttusername, mqttpassword, locationgnssmode, locationdpo, sensorsenable, sensorsenablebme280, sensorbme280i2caddress, mqttsensorsbme280temperature, mqttsensorsbme280pressure, mqttsensorsbme280humidity, sensorsenabletsl2561, sensortsl2561i2caddress, sensortsl2561gain, mqttsensorstsl2561lux, sensorsenablepir, sensorpirpin, mqttsensorspir, sensorsenablecputemp, mqttsensorscputemp, sensorsenablecpufreq, mqttsensorscpufreq, sensorsenablefreeram, mqttsensorsfreeram, sensorsenablehall, mqttsensorshall, mqttmodeminfo, mqttmodemccid, mqttmodemimsi, mqttmodemoperator, mqttmodemsignalquality, mqttmodempublicip, mqttbatterypercentage, mqttbatteryvoltage, mqttbatterystatus, mqttlocationtype, mqttlocationlatitude, mqttlocationlongitude, mqttlocationspeed, mqttlocationaltitude, mqttlocationaccuracy, ftpenable, ftpuser, ftppass, bluetoothenable, bluetoothname, bluetoothobdidentifier, bluetoothobdmqttobdpayload, bluetoothobdmqttobddebug, bluetoothobdmqttengineload, bluetoothobdmqttenginecoolanttemp, bluetoothobdmqttshorttermfueltrimbank1, bluetoothobdmqttlongtermfueltrimbank1, bluetoothobdmqttshorttermfueltrimbank2, bluetoothobdmqttlongtermfueltrimbank2, bluetoothobdmqttfuelpressure, bluetoothobdmqttmanifoldpressure, bluetoothobdmqttrpm, bluetoothobdmqttkph, bluetoothobdmqttmph, bluetoothobdmqtttimingadvance, bluetoothobdmqttintakeairtemp, bluetoothobdmqttmafrate, bluetoothobdmqttthrottle, bluetoothobdmqttauxinputstatus, bluetoothobdmqttruntime, bluetoothobdmqttdisttravelwithmil, bluetoothobdmqttfuelrailpressure, bluetoothobdmqttfuelrailguagepressure, bluetoothobdmqttcommandedegr, bluetoothobdmqttegrerror, bluetoothobdmqttcommandedevappurge, bluetoothobdmqttfuellevel, bluetoothobdmqttwarmupssincecodescleared, bluetoothobdmqttdistsincecodescleared, bluetoothobdmqttevapsysvappressure, bluetoothobdmqttabsbaropressure, bluetoothobdmqttcattempb1s1, bluetoothobdmqttcattempb2s1, bluetoothobdmqttcattempb1s2, bluetoothobdmqttcattempb2s2, bluetoothobdmqttctrlmodvoltage, bluetoothobdmqttabsload, bluetoothobdmqttcommandedairfuelratio, bluetoothobdmqttrelativethrottle, bluetoothobdmqttambientairtemp, bluetoothobdmqttabsthrottleposb, bluetoothobdmqttabsthrottleposc, bluetoothobdmqttabsthrottleposd, bluetoothobdmqttabsthrottlepose, bluetoothobdmqttabsthrottleposf, bluetoothobdmqttcommandedthrottleactuator, bluetoothobdmqtttimerunwithmil, bluetoothobdmqtttimesincecodescleared, bluetoothobdmqttmaxmafrate, bluetoothobdmqttethanolPercent, bluetoothobdmqttabsevapsysvappressure, bluetoothobdmqttevapsysvappressure2, bluetoothobdmqttabsfuelrailpressure, bluetoothobdmqttrelativepedalpos, bluetoothobdmqtthybridbatlife, bluetoothobdmqttoiltemp, bluetoothobdmqttfuelinjecttiming, bluetoothobdmqttfuelrate, bluetoothobdmqttdemandedtorque, bluetoothobdmqtttorque, bluetoothobdmqttreferencetorque, bluetoothobdmqttbatteryvoltage, bluetoothobdmqttvin, mqttuptime, mqttversion, dynamicfrequency, lowpowermodeonbattery;
extern int connectionRetries;
47 changes: 32 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ AsyncWebServer server(80);

// Configuration variables
String apn, apnusername, apnpassword, simpin, timezone, dst, apssid, appassword, appowersaving, mqttaddress, mqttport, mqttclientname, mqttusername, mqttpassword, locationgnssmode, locationdpo, sensorsenable, sensorsenablebme280, sensorbme280i2caddress, mqttsensorsbme280temperature, mqttsensorsbme280pressure, mqttsensorsbme280humidity, sensorsenabletsl2561, sensortsl2561i2caddress, sensortsl2561gain, mqttsensorstsl2561lux, sensorsenablepir, sensorpirpin, mqttsensorspir, sensorsenablecputemp, mqttsensorscputemp, sensorsenablecpufreq, mqttsensorscpufreq, sensorsenablefreeram, mqttsensorsfreeram, sensorsenablehall, mqttsensorshall, mqttmodeminfo, mqttmodemccid, mqttmodemimsi, mqttmodemoperator, mqttmodemsignalquality, mqttmodempublicip, mqttbatterypercentage, mqttbatteryvoltage, mqttbatterystatus, mqttlocationtype, mqttlocationlatitude, mqttlocationlongitude, mqttlocationspeed, mqttlocationaltitude, mqttlocationaccuracy, ftpenable, ftpuser, ftppass, bluetoothenable, bluetoothname, bluetoothobdidentifier, bluetoothobdmqttobdpayload, bluetoothobdmqttobddebug, bluetoothobdmqttengineload, bluetoothobdmqttenginecoolanttemp, bluetoothobdmqttshorttermfueltrimbank1, bluetoothobdmqttlongtermfueltrimbank1, bluetoothobdmqttshorttermfueltrimbank2, bluetoothobdmqttlongtermfueltrimbank2, bluetoothobdmqttfuelpressure, bluetoothobdmqttmanifoldpressure, bluetoothobdmqttrpm, bluetoothobdmqttkph, bluetoothobdmqttmph, bluetoothobdmqtttimingadvance, bluetoothobdmqttintakeairtemp, bluetoothobdmqttmafrate, bluetoothobdmqttthrottle, bluetoothobdmqttauxinputstatus, bluetoothobdmqttruntime, bluetoothobdmqttdisttravelwithmil, bluetoothobdmqttfuelrailpressure, bluetoothobdmqttfuelrailguagepressure, bluetoothobdmqttcommandedegr, bluetoothobdmqttegrerror, bluetoothobdmqttcommandedevappurge, bluetoothobdmqttfuellevel, bluetoothobdmqttwarmupssincecodescleared, bluetoothobdmqttdistsincecodescleared, bluetoothobdmqttevapsysvappressure, bluetoothobdmqttabsbaropressure, bluetoothobdmqttcattempb1s1, bluetoothobdmqttcattempb2s1, bluetoothobdmqttcattempb1s2, bluetoothobdmqttcattempb2s2, bluetoothobdmqttctrlmodvoltage, bluetoothobdmqttabsload, bluetoothobdmqttcommandedairfuelratio, bluetoothobdmqttrelativethrottle, bluetoothobdmqttambientairtemp, bluetoothobdmqttabsthrottleposb, bluetoothobdmqttabsthrottleposc, bluetoothobdmqttabsthrottleposd, bluetoothobdmqttabsthrottlepose, bluetoothobdmqttabsthrottleposf, bluetoothobdmqttcommandedthrottleactuator, bluetoothobdmqtttimerunwithmil, bluetoothobdmqtttimesincecodescleared, bluetoothobdmqttmaxmafrate, bluetoothobdmqttethanolPercent, bluetoothobdmqttabsevapsysvappressure, bluetoothobdmqttevapsysvappressure2, bluetoothobdmqttabsfuelrailpressure, bluetoothobdmqttrelativepedalpos, bluetoothobdmqtthybridbatlife, bluetoothobdmqttoiltemp, bluetoothobdmqttfuelinjecttiming, bluetoothobdmqttfuelrate, bluetoothobdmqttdemandedtorque, bluetoothobdmqtttorque, bluetoothobdmqttreferencetorque, bluetoothobdmqttbatteryvoltage, bluetoothobdmqttvin, mqttuptime, mqttversion, dynamicfrequency, lowpowermodeonbattery;
int connectionRetries = 0;

// Search for parameter in HTTP POST request
const String configIDs[] = {"apn", "apnusername", "apnpassword", "simpin", "timezone", "dst", "apssid", "appassword", "appowersaving", "mqttaddress", "mqttport", "mqttclientname", "mqttusername", "mqttpassword", "locationgnssmode", "locationdpo", "sensorsenable", "sensorsenablebme280", "sensorbme280i2caddress", "mqttsensorsbme280temperature", "mqttsensorsbme280pressure", "mqttsensorsbme280humidity", "sensorsenabletsl2561", "sensortsl2561i2caddress", "sensortsl2561gain", "mqttsensorstsl2561lux", "sensorsenablepir", "sensorpirpin", "mqttsensorspir", "sensorsenablecputemp", "mqttsensorscputemp", "sensorsenablecpufreq", "mqttsensorscpufreq", "sensorsenablefreeram", "mqttsensorsfreeram", "sensorsenablehall", "mqttsensorshall", "mqttmodeminfo", "mqttmodemccid", "mqttmodemimsi", "mqttmodemoperator", "mqttmodemsignalquality", "mqttmodempublicip", "mqttbatterypercentage", "mqttbatteryvoltage", "mqttbatterystatus", "mqttlocationtype", "mqttlocationlatitude", "mqttlocationlongitude", "mqttlocationspeed", "mqttlocationaltitude", "mqttlocationaccuracy", "ftpenable", "ftpuser", "ftppass", "bluetoothenable", "bluetoothname", "bluetoothobdidentifier", "bluetoothobdmqttobdpayload", "bluetoothobdmqttobddebug", "bluetoothobdmqttengineload", "bluetoothobdmqttenginecoolanttemp", "bluetoothobdmqttshorttermfueltrimbank1", "bluetoothobdmqttlongtermfueltrimbank1", "bluetoothobdmqttshorttermfueltrimbank2", "bluetoothobdmqttlongtermfueltrimbank2", "bluetoothobdmqttfuelpressure", "bluetoothobdmqttmanifoldpressure", "bluetoothobdmqttrpm", "bluetoothobdmqttkph", "bluetoothobdmqttmph", "bluetoothobdmqtttimingadvance", "bluetoothobdmqttintakeairtemp", "bluetoothobdmqttmafrate", "bluetoothobdmqttthrottle", "bluetoothobdmqttauxinputstatus", "bluetoothobdmqttruntime", "bluetoothobdmqttdisttravelwithmil", "bluetoothobdmqttfuelrailpressure", "bluetoothobdmqttfuelrailguagepressure", "bluetoothobdmqttcommandedegr", "bluetoothobdmqttegrerror", "bluetoothobdmqttcommandedevappurge", "bluetoothobdmqttfuellevel", "bluetoothobdmqttwarmupssincecodescleared", "bluetoothobdmqttdistsincecodescleared", "bluetoothobdmqttevapsysvappressure", "bluetoothobdmqttabsbaropressure", "bluetoothobdmqttcattempb1s1", "bluetoothobdmqttcattempb2s1", "bluetoothobdmqttcattempb1s2", "bluetoothobdmqttcattempb2s2", "bluetoothobdmqttctrlmodvoltage", "bluetoothobdmqttabsload", "bluetoothobdmqttcommandedairfuelratio", "bluetoothobdmqttrelativethrottle", "bluetoothobdmqttambientairtemp", "bluetoothobdmqttabsthrottleposb", "bluetoothobdmqttabsthrottleposc", "bluetoothobdmqttabsthrottleposd", "bluetoothobdmqttabsthrottlepose", "bluetoothobdmqttabsthrottleposf", "bluetoothobdmqttcommandedthrottleactuator", "bluetoothobdmqtttimerunwithmil", "bluetoothobdmqtttimesincecodescleared", "bluetoothobdmqttmaxmafrate", "bluetoothobdmqttethanolPercent", "bluetoothobdmqttabsevapsysvappressure", "bluetoothobdmqttevapsysvappressure2", "bluetoothobdmqttabsfuelrailpressure", "bluetoothobdmqttrelativepedalpos", "bluetoothobdmqtthybridbatlife", "bluetoothobdmqttoiltemp", "bluetoothobdmqttfuelinjecttiming", "bluetoothobdmqttfuelrate", "bluetoothobdmqttdemandedtorque", "bluetoothobdmqtttorque", "bluetoothobdmqttreferencetorque", "bluetoothobdmqttbatteryvoltage", "bluetoothobdmqttvin", "mqttuptime", "mqttversion", "dynamicfrequency", "lowpowermodeonbattery"};
Expand Down Expand Up @@ -352,6 +353,20 @@ TinyGsm modem(SerialAT);

TinyGsmClient client(modem, 0);

void rebootDevice() {
#if DEBUG
SerialMon.println("Rebooting device...");
#endif

digitalWrite(MODEM_PWRKEY, HIGH);
delay(300); // Needed delay
digitalWrite(MODEM_PWRKEY, LOW);
delay(300);

modem.restart();
ESP.restart();
}

// Prepare MQTT
PubSubClient mqtt(client);

Expand All @@ -362,11 +377,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
String newPayload = String((char *)payload);
if (newTopic == mqttclientname + (String)"/commands") {
if (newPayload == "restart") {
#if DEBUG
SerialMon.println("MQTT: Restarting device...");
#endif
modem.restart();
ESP.restart();
rebootDevice();
}
}
}
Expand Down Expand Up @@ -478,17 +489,23 @@ void setup() {
void initMQTT() {
if (initialized) {
if (!mqtt.connected()) {
#if DEBUG
SerialMon.println("MQTT not connected");
#endif
#if DEBUG
SerialMon.println("MQTT not connected");
#endif

mqtt.connect(stringToChar(mqttclientname), stringToChar(mqttusername), stringToChar(mqttpassword));
}
else {
#if DEBUG
SerialMon.println("MQTT connected");
#endif
mqtt.subscribe(stringToChar(mqttclientname + (String)"/commands"));
connectionRetries++;
if (connectionRetries > 100) {
rebootDevice();
}

mqtt.connect(stringToChar(mqttclientname), stringToChar(mqttusername), stringToChar(mqttpassword));
} else {
#if DEBUG
SerialMon.println("MQTT connected");
#endif
connectionRetries = 0;

mqtt.subscribe(stringToChar(mqttclientname + (String)"/commands"));
}
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

bool initialized = false;

int connectionRetries = 0;

bool timeSet = false;

String publicIP;
Expand All @@ -28,11 +26,7 @@ void initModem() {
connectionRetries++;

if (connectionRetries > 100) {
#if DEBUG
SerialMon.println("Connection Error: Restarting device...");
#endif
modem.restart();
ESP.restart();
rebootDevice();
}
return;
} else {
Expand Down Expand Up @@ -126,11 +120,7 @@ void initNetwork() {
connectionRetries++;

if (connectionRetries > 100) {
#if DEBUG
SerialMon.println("Connection Error: Restarting device...");
#endif
modem.restart();
ESP.restart();
rebootDevice();
}
}
}
Expand Down

0 comments on commit a8e9e7d

Please sign in to comment.