Skip to content

Commit f6f7c3a

Browse files
authored
Merge pull request #23 from sarahmarshy/firmware-version-error
Ensures ES8266 firmware version up to date
2 parents abd4594 + 530ab25 commit f6f7c3a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

ESP8266/ESP8266.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
2424
_parser.debugOn(debug);
2525
}
2626

27+
int ESP8266::get_firmware_version()
28+
{
29+
_parser.send("AT+GMR");
30+
int version;
31+
if(_parser.recv("SDK version:%d", &version) && _parser.recv("OK")) {
32+
return version;
33+
} else {
34+
// Older firmware versions do not prefix the version with "SDK version: "
35+
return -1;
36+
}
37+
38+
}
39+
2740
bool ESP8266::startup(int mode)
2841
{
2942
//only 3 valid modes
3043
if(mode < 1 || mode > 3) {
3144
return false;
3245
}
3346

34-
bool success = reset()
35-
&& _parser.send("AT+CWMODE_CUR=%d", mode)
47+
bool success = _parser.send("AT+CWMODE_CUR=%d", mode)
3648
&& _parser.recv("OK")
3749
&& _parser.send("AT+CIPMUX=1")
3850
&& _parser.recv("OK");

ESP8266/ESP8266.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class ESP8266
2727
public:
2828
ESP8266(PinName tx, PinName rx, bool debug=false);
2929

30+
/**
31+
* Check firmware version of ESP8266
32+
*
33+
* @return integer firmware version or -1 if firmware query command gives outdated response
34+
*/
35+
int get_firmware_version(void);
36+
3037
/**
3138
* Startup the ESP8266
3239
*

ESP8266Interface.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
#include <string.h>
1818
#include "ESP8266Interface.h"
19+
#include "mbed_debug.h"
1920

2021
// Various timeouts for different ESP8266 operations
2122
#define ESP8266_CONNECT_TIMEOUT 15000
2223
#define ESP8266_SEND_TIMEOUT 500
2324
#define ESP8266_RECV_TIMEOUT 0
2425
#define ESP8266_MISC_TIMEOUT 500
2526

27+
// Firmware version
28+
#define ESP8266_VERSION 2
29+
2630
// ESP8266Interface implementation
2731
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
2832
: _esp(tx, rx, debug)
@@ -47,6 +51,20 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
4751
int ESP8266Interface::connect()
4852
{
4953
_esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
54+
55+
if (!_esp.reset()) {
56+
return NSAPI_ERROR_DEVICE_ERROR;
57+
}
58+
59+
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
60+
61+
if (_esp.get_firmware_version() != ESP8266_VERSION) {
62+
debug("ESP8266: ERROR: Firmware incompatible with this driver.\
63+
\r\nUpdate to v%d - https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update\r\n",ESP8266_VERSION);
64+
return NSAPI_ERROR_DEVICE_ERROR;
65+
}
66+
67+
_esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
5068

5169
if (!_esp.startup(3)) {
5270
return NSAPI_ERROR_DEVICE_ERROR;

0 commit comments

Comments
 (0)