Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset wifi coprocessor on disconnect #590

Closed
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,9 +2527,15 @@ void Wippersnapper::pingBroker() {
// ping within keepalive-10% to keep connection open
if (millis() > (_prv_ping + (WS_KEEPALIVE_INTERVAL_MS -
(WS_KEEPALIVE_INTERVAL_MS * 0.10)))) {
WS_DEBUG_PRINTLN("PING!");
WS_DEBUG_PRINT("PING!");
// TODO: Add back, is crashing currently
tyeth marked this conversation as resolved.
Show resolved Hide resolved
WS._mqtt->ping();
if (WS._mqtt->ping()) {
WS_DEBUG_PRINTLN("SUCCESS!");
} else {
WS_DEBUG_PRINTLN("FAILURE! Running network FSM...");
tyeth marked this conversation as resolved.
Show resolved Hide resolved
WS._mqtt->disconnect();
runNetFSM();
}
_prv_ping = millis();
}
// blink status LED every STATUS_LED_KAT_BLINK_TIME millis
Expand Down
110 changes: 94 additions & 16 deletions src/network_interfaces/Wippersnapper_AIRLIFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "Wippersnapper.h"

#define NINAFWVER \
"1.6.0" /*!< min. nina-fw version compatible with this library. */
"1.7.7" /*!< min. nina-fw version compatible with this library. */

#define SPIWIFI SPI /*!< Instance of SPI interface used by an AirLift. */

Expand All @@ -47,17 +47,22 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
*/
/**************************************************************************/
Wippersnapper_AIRLIFT() : Wippersnapper() {
_ssPin = 10;
_ackPin = 7;
_rstPin = 5;
_ssPin = SPIWIFI_SS; // 10;
_ackPin = SPIWIFI_ACK; // 7;
_rstPin = SPIWIFI_RESET; // 5; // should be 7 on PyPortals
#ifdef ESP32_GPIO0
_gpio0Pin = ESP32_GPIO0;
#else
_gpio0Pin = -1;
#endif
_wifi = &SPIWIFI;
_ssid = 0;
_pass = 0;
_mqtt_client = new WiFiSSLClient;

// setup ESP32 co-processor pins during init.
WiFi.setPins(_ssPin, _ackPin, _rstPin, _gpio0Pin, _wifi);
delay(1000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know why it's there, but for maintainers, please add a comment explaining the utility of this "magic" delay() call

}

/**************************************************************************/
Expand Down Expand Up @@ -174,9 +179,35 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
/********************************************************/
bool firmwareCheck() {
_fv = WiFi.firmwareVersion();
if (_fv < NINAFWVER)
return false;
return true;
return compareVersions(_fv, NINAFWVER);
}

/********************************************************/
/*!
@brief Compares two version strings.
@param currentVersion
Current version string.
@param requiredVersion
Required version string.
@returns True if the current version is greater than or
equal to the required version, False otherwise.
*/
/********************************************************/
bool compareVersions(const String &currentVersion,
const String &requiredVersion) {
int curMajor, curMinor, curPatch;
int reqMajor, reqMinor, reqPatch;
int per_major, per_minor, per_patch;
tyeth marked this conversation as resolved.
Show resolved Hide resolved

sscanf(currentVersion.c_str(), "%d.%d.%d", &curMajor, &curMinor, &curPatch);
sscanf(requiredVersion.c_str(), "%d.%d.%d", &reqMajor, &reqMinor,
&reqPatch);

if (curMajor != reqMajor)
return curMajor > reqMajor;
if (curMinor != reqMinor)
return curMinor > reqMinor;
return curPatch >= reqPatch;
}

/********************************************************/
Expand Down Expand Up @@ -249,25 +280,72 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
/**************************************************************************/
void _connect() {
if (strlen(_ssid) == 0) {
_status = WS_SSID_INVALID;
_status = WS_SSID_INVALID; // possibly unneccesary as already checking
// elsewhere
} else {

// validate co-processor is physically connected connection
if (WiFi.status() == WL_NO_MODULE) {
WS_DEBUG_PRINT("No ESP32 module detected!");
return;
// disconnect from possible previous connection
_disconnect();
delay(100);
WiFi.end();
_wifi->end();
delay(100);
_wifi->begin();
feedWDT();
WS_DEBUG_PRINT("Reset Pin: ");
tyeth marked this conversation as resolved.
Show resolved Hide resolved
WS_DEBUG_PRINTLN(_rstPin);
// reset the esp32 if possible
if (_rstPin != -1) {
WS_DEBUG_PRINTLN("Resetting ESP32...");
WS_PRINTER.flush();
// Chip select for esp32
pinMode(_ssPin, OUTPUT);
digitalWrite(_ssPin, HIGH); // Do we need to set SS low again?
if (_gpio0Pin != -1) {
pinMode(_gpio0Pin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
}
pinMode(_rstPin, OUTPUT);
digitalWrite(_rstPin, LOW);
delay(50);
digitalWrite(_rstPin, HIGH);
delay(10);
if (_gpio0Pin != -1) {
pinMode(_gpio0Pin, INPUT);
}
// wait for the ESP32 to boot
delay(2000);
tyeth marked this conversation as resolved.
Show resolved Hide resolved
}
feedWDT();
WS_DEBUG_PRINT("ESP32 booted, version: ");
WS_PRINTER.flush();
WS_DEBUG_PRINTLN(WiFi.firmwareVersion());
WS_PRINTER.flush();
feedWDT();

// // validate co-processor is physically connected connection
tyeth marked this conversation as resolved.
Show resolved Hide resolved
// if (WiFi.status() == WL_NO_MODULE) {
// WS_DEBUG_PRINT("No ESP32 module detected!");
// WS_DEBUG_PRINT("Current Module Status:");
// WS_DEBUG_PRINTLN(WiFi.status());
// return;
// }

// validate co-processor's firmware version
if (!firmwareCheck())
WS_DEBUG_PRINTLN("Please upgrade the firmware on the ESP module to the "
"latest version.");

// disconnect from possible previous connection
_disconnect();

WS_DEBUG_PRINT("Connecting to ");
WS_DEBUG_PRINTLN(_ssid);
WS_PRINTER.flush();
feedWDT();
WiFi.begin(_ssid, _pass);
_status = WS_NET_DISCONNECTED;
feedWDT();
tyeth marked this conversation as resolved.
Show resolved Hide resolved
delay(5000);
feedWDT();
delay(5000);
feedWDT();
}
}

Expand Down
Loading
Loading