Kazuo Tsubaki
/
ESP-WROOM-02_test
Very simple example using ESP-WROOM-02 (ESP-8266) from LPC1114
Revision 0:e450010334e1, committed 2016-05-13
- Comitter:
- kazz12211
- Date:
- Fri May 13 12:05:34 2016 +0000
- Commit message:
- Use ESP-WROOM-02 (ESP-8266) from LPC1114 using SoftSerial.
Changed in this revision
diff -r 000000000000 -r e450010334e1 ESP8266/ESP8266.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.cpp Fri May 13 12:05:34 2016 +0000 @@ -0,0 +1,140 @@ +#include "ESP8266.h" + +#define LOG_DEBUG(s) if(logger != NULL) { logger->printf("%s\r\n", s); }; + +ESP8266::ESP8266(SoftSerial *s) { + stream = s; +} + +ESP8266::~ESP8266() { +} + +void ESP8266::setLogger(Serial *l) { + logger = l; +} + +bool ESP8266::_ok() { + return (strstr(replybuffer, "OK\r\n") != NULL); +} + +bool ESP8266::nop() { + sendRequest("AT\r\n"); + timeout=20; + getReply(); + return _ok(); +} + +bool ESP8266::reset() { + LOG_DEBUG("Reset & get Firmware"); + sendRequest("AT+RST\r\n"); + timeout=200; + getReply(); + LOG_DEBUG(replybuffer); + return _ok(); +} + +void ESP8266::version() { + LOG_DEBUG("Get Version"); + sendRequest("AT+GMR\r\n"); + timeout=200; + getReply(); + LOG_DEBUG(replybuffer); +} + +// 1=Station, 2=AP, 3=BOTH, default=Station +bool ESP8266::mode(int mode) { + char cmd[255]; + sprintf(cmd, "AT+CWMODE=%d\r\n", mode); + sendRequest(cmd); + timeout=60; + getReply(); + LOG_DEBUG(replybuffer); + return _ok(); +} + +// 0=Single, 1=Multi +bool ESP8266::connectionMode(int connMode) { + char cmd[255]; + sprintf(cmd, "AT+CIPMUX=%d\r\n", connMode); + sendRequest(cmd); + timeout=80; + getReply(); + LOG_DEBUG(replybuffer); + return _ok(); +} + +void ESP8266::connectionStatus() { + LOG_DEBUG("Get Connection Status"); + sendRequest("AT+CIPSTA?\r\n"); + timeout=100; + getReply(); + LOG_DEBUG(replybuffer); +} + +bool ESP8266::config() { + timeout=1000; getcount=600; + wait(1); + + if(!nop()) + return false; + wait(1); + + version(); + wait(3); + + if(!mode(1)) + return false; + wait(2); + + if(!connectionMode(1)) + return false; + wait(2); + + return true; +} + +bool ESP8266::connect(char *ssid, char *password) { + char cmd[256]; + LOG_DEBUG("Connecting"); + strcpy(cmd, "AT+CWJAP=\""); + strcat(cmd, ssid); + strcat(cmd, "\",\""); + strcat(cmd, password); + strcat(cmd, "\"\r\n"); + LOG_DEBUG(cmd); + sendRequest(cmd); + timeout=10000; + getReply(); + + wait(5); + + return _ok(); +} + +void ESP8266::sendRequest(char *req) { + strcpy(cmdbuffer, req); + sendCmd(); +} + +char *ESP8266::readResponse() { + getReply(); + return replybuffer; +} + + +void ESP8266::sendCmd() { + stream->printf("%s", cmdbuffer); +} + +void ESP8266::getReply() { + memset(replybuffer, '\0', sizeof(replybuffer)); + replycount = 0; + t.reset(); t.start(); + while(t.read_ms() < timeout && replycount < getcount) { + if(stream->readable()) { + replybuffer[replycount] = stream->getc(); + replycount++; + } + } +} +
diff -r 000000000000 -r e450010334e1 ESP8266/ESP8266.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.h Fri May 13 12:05:34 2016 +0000 @@ -0,0 +1,66 @@ +/** + * Use ESP-8266 (called ESP-WROOM-02 in Japan) from LPC1114 + * LPC1114 has only 1 Hardware UART so this class uses SoftSerial to + * communicate with ESP-8266. + * I wrote like this class for Arduino then partially ported to mbed. + * + * 2016/05/13 + * By Kazuo Tsubaki + **/ + +#ifndef __ESP8266__ +#define __ESP8266__ + +#include <mbed.h> +#include "SoftSerial.h" + +class ESP8266 { +public: + + // Constructor + ESP8266(SoftSerial *s); + ~ESP8266(); + + // Example for configuring ESP + bool config(); + // Use Serial to logging + void setLogger(Serial *log); + + // Connect to WiFi AP + bool connect(char *ssid, char *password); + // Send command to ESP-8266 (see AT command list of the module) + void sendRequest(char *req); + // Read resultant response of AT command. Needs some waits to read after AT command. + char *readResponse(); + + // AT + bool nop(); + // AT+GMR + void version(); + // AT+RST + bool reset(); + // AT+CWMODE=n n= 1:Station Mode, 2:AP Mode, 3:Station Mode + AP Mode + bool mode(int mode); + // AT+CIPMUX=n n= 0:Single, 1=Multi + bool connectionMode(int connMode); + // AT+CIPSTA? + void connectionStatus(); + +private: + + void sendCmd(); + void getReply(); + bool _ok(); + +private: + + char cmdbuffer[266]; + char replybuffer[1024]; + SoftSerial *stream; + int replycount, timeout, getcount; + Timer t; + Serial *logger; +}; + + +#endif
diff -r 000000000000 -r e450010334e1 SoftSerial.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoftSerial.lib Fri May 13 12:05:34 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Sissors/code/SoftSerial/#a0029614de72
diff -r 000000000000 -r e450010334e1 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri May 13 12:05:34 2016 +0000 @@ -0,0 +1,37 @@ +#include "mbed.h" +#include "SoftSerial.h" +#include "ESP8266.h" + +Serial *logger; +SoftSerial *ser; +ESP8266 *esp; + +int main() { + logger = new Serial(USBTX, USBRX); + ser = new SoftSerial(dp18, dp17); + + ser->baud(9600); + logger->baud(9600); + + esp = new ESP8266(ser); + esp->setLogger(logger); + + if(!esp->config()) { + logger->printf("Configuration failed.\r\n"); + return -1; + } + + if(!esp->connect("ArtesWare HQ Main", "kt621221")) { + logger->printf("Connection failed.\r\n"); + return -1; + } + + esp->connectionStatus(); + + delete esp; + delete logger; + delete ser; + + return 0; + +}
diff -r 000000000000 -r e450010334e1 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri May 13 12:05:34 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e \ No newline at end of file