diff --git a/ESP8266/ESP8266.cpp b/ESP8266/ESP8266.cpp index 3f94dc2..5e31998 100644 --- a/ESP8266/ESP8266.cpp +++ b/ESP8266/ESP8266.cpp @@ -24,6 +24,19 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug) _parser.debugOn(debug); } +int ESP8266::get_firmware_version() +{ + _parser.send("AT+GMR"); + int version; + if(_parser.recv("SDK version:%d", &version) && _parser.recv("OK")) { + return version; + } else { + // Older firmware versions do not prefix the version with "SDK version: " + return -1; + } + +} + bool ESP8266::startup(int mode) { //only 3 valid modes @@ -31,8 +44,7 @@ bool ESP8266::startup(int mode) return false; } - bool success = reset() - && _parser.send("AT+CWMODE_CUR=%d", mode) + bool success = _parser.send("AT+CWMODE_CUR=%d", mode) && _parser.recv("OK") && _parser.send("AT+CIPMUX=1") && _parser.recv("OK"); diff --git a/ESP8266/ESP8266.h b/ESP8266/ESP8266.h index 3042274..2164526 100644 --- a/ESP8266/ESP8266.h +++ b/ESP8266/ESP8266.h @@ -27,6 +27,13 @@ class ESP8266 public: ESP8266(PinName tx, PinName rx, bool debug=false); + /** + * Check firmware version of ESP8266 + * + * @return integer firmware version or -1 if firmware query command gives outdated response + */ + int get_firmware_version(void); + /** * Startup the ESP8266 * diff --git a/ESP8266Interface.cpp b/ESP8266Interface.cpp index 56c1ae1..65f1bd8 100644 --- a/ESP8266Interface.cpp +++ b/ESP8266Interface.cpp @@ -16,6 +16,7 @@ #include #include "ESP8266Interface.h" +#include "mbed_debug.h" // Various timeouts for different ESP8266 operations #define ESP8266_CONNECT_TIMEOUT 15000 @@ -23,6 +24,9 @@ #define ESP8266_RECV_TIMEOUT 0 #define ESP8266_MISC_TIMEOUT 500 +// Firmware version +#define ESP8266_VERSION 2 + // ESP8266Interface implementation ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) : _esp(tx, rx, debug) @@ -47,6 +51,20 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security int ESP8266Interface::connect() { _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); + + if (!_esp.reset()) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + _esp.setTimeout(ESP8266_MISC_TIMEOUT); + + if (_esp.get_firmware_version() != ESP8266_VERSION) { + debug("ESP8266: ERROR: Firmware incompatible with this driver.\ + \r\nUpdate to v%d - https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update\r\n",ESP8266_VERSION); + return NSAPI_ERROR_DEVICE_ERROR; + } + + _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); if (!_esp.startup(3)) { return NSAPI_ERROR_DEVICE_ERROR;