A library to operate the ESP8266
Revision 0:011fdf85bfc8, committed 2017-05-23
- Comitter:
- epgmdm
- Date:
- Tue May 23 10:26:19 2017 +0000
- Commit message:
- A working version
Changed in this revision
diff -r 000000000000 -r 011fdf85bfc8 ESP8266_mdm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266_mdm.cpp Tue May 23 10:26:19 2017 +0000 @@ -0,0 +1,228 @@ +/** + *@section DESCRIPTION + * ESP8266_mdm Library + * This sets up the ESP8266 as a server or client. + * It detect \r\n sequence from the ESP8266 to chunk the response into lines. This then drives a state machine. + *@section LICENSE + * Copyright (c) 2016, Malcolm McCulloch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * @file "ESP8266_mdm.cpp" + */ +#include "ESP8266_mdm.h" + +// ********************* +// * Protected variables: * +// ********************* +char* ESP8266_mdm::startCmnds[10]= { + "AT", + "AT", + "AT", + "AT+RST", + "AT+CWMODE=1", + "AT+CWJAP=\"epgmdm\",\"3F7540BC59\"", + "AT+CIFSR", + "AT+CIPMUX=1", + "AT+CIPSERVER=1,8080", + "AT+CIPSEND=0," +}; +char* ESP8266_mdm::startResp[10]= { + "OK", + "OK", + "OK", + "OK", + "WIFI GOT IP", + "OK", + "OK", + "+CIFSR:STAIP", + "OK", + "+IPD" +}; + +int ESP8266_mdm::startMaxState=7; + +// ********************* +// * Public functions: * +// ********************* + +ESP8266_mdm::ESP8266_mdm(RawSerial *esp, int server, RawSerial *pc, PinName rstPn):esp(esp),server(server),pc(pc),rstPn(rstPn) +{ + reset = new DigitalOut(rstPn,1); + esp->baud(115200); + esp->attach(callback(this, &ESP8266_mdm::dev_recv)); + reset->write(0); + // Init variables + buffer=(char *)calloc(BUFF_SIZE,1); + bufferPnt=0; + ready=0; + state=0; + // Create hardware + last = new Timer(); + + + //esp->attach(this,&ESP8266_mdm::dev_recv, Serial::RxIrq); + // + // Setting up interrupts + // + INFO("Reseting"); + wait(1.5); + reset->write(1); + pc->printf("Start up\n\r"); + wait(2.0); + esp->printf("AT\r\n"); + + INFO("Starting up v5"); + + // Startup + command = startCmnds; + responseRequired = startResp; + maxState = startMaxState; + timeOut=11; + DBG("Start Do Loop"); + doLoop(0); + DBG ("DONE"); + while(1); + +} + +// ********************* +// * Protected functions: * +// ********************* + +/** +* Device has received a byte +*/ +void ESP8266_mdm::dev_recv() +{ + char c; + if(bufferPnt==0) { + memset(buffer,0,BUFF_SIZE); + } + while(esp->readable()) { + c = (char)esp->getc(); +//if (state>=maxState){ + pc->putc(c); +// } + + buffer[bufferPnt]=c; + bufferPnt++; + if (bufferPnt>1024) { + ready=1; + } + if ((c==0x0a)||(c==0x0d)) { + ready=1; + } else if (c==0x0a) { + if (bufferPnt>1) { + if (buffer[bufferPnt -2]==0x0d) { + ready=1; + break; + } + } + } + if (!esp->readable()) { + wait_us(100); + } + } +} + +/** +* Checks if pattern is in test. Returns 1 for true, 0 for no. -1 if busy. -2 ERROR +* +*/ +int ESP8266_mdm::OKResponse(char *test, const char *pattern) +{ + + char *p= strstr(test,pattern); + if (p!=NULL) { + return 1; + } + p= strstr(test,"busy"); + if (p!=NULL) { + return -1; + } + p= strstr(test,"ERROR"); + if (p!=NULL) { + return -2; + } + return 0; +} +/** +* Loops through until state = maxState; +*/ +void ESP8266_mdm::doLoop(int initState) +{ + int resp; + state = initState; +// last->reset(); +// last->start(); + DBG("[%d] next %s : Waiting for %s",state,command[state],responseRequired[state]); + + while (1) { + // printf(" {t %d}", *last); + // Timeout code + /* if ((*last)>timeOut){ + WARN("Timeout on [%d] next %s : Waiting for %s",state,command[state],responseRequired[state]); + last->reset(); + if (state>0) { + esp->printf("%s\r\n",command[state-1]); + } else { + esp->printf("%s\r\n",command[state]); + } + + } + */ + if (ready) { + ready=0; + last->reset(); + bufferPnt=0; + printf("<%s>\r\n",buffer); + DBG("[%d] next %s : Waiting for %s",state,command[state],responseRequired[state]); + resp=OKResponse(buffer,responseRequired[state]); + switch (resp) { + case 0: { // not yet + break; + } + case 1: { //Yes + esp->printf("%s\r\n",command[state]); + state++; + break; + } + case -1: { // busy + if (state>0) { + esp->printf("%s\r\n",command[state-1]); + } else { + esp->printf("%s\r\n",command[state]); + } + break; + } + case -2: { //ERROR + esp->printf("AT\r\n"); + wait(1); + if (state>0) { + esp->printf("%s\r\n",command[state-1]); + } else { + esp->printf("%s\r\n",command[state]); + } + break; + } + } // switch + } // if + } // while + last->stop(); +} \ No newline at end of file
diff -r 000000000000 -r 011fdf85bfc8 ESP8266_mdm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266_mdm.h Tue May 23 10:26:19 2017 +0000 @@ -0,0 +1,131 @@ +/** + *@section DESCRIPTION + * ESP8266_mdm Library + * This sets up the ESP8266 as a server or client. + * It detect \r\n sequence from the ESP8266 to chunk the response into lines. This then drives a state machine. + *@section LICENSE + * Copyright (c) 2016, Malcolm McCulloch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * @file "ESP8266_mdm.h" + */ +#ifndef ESP8266_MDM_H +#define ESP8266_MDM_H +#include <mbed.h> +#define FUNCNAME "ESP8266_mdm" +#include "defs.h" + +#define BUFF_SIZE 256 + +/** \brief A library for the base of the solar nano grid controllers. + * + * This library contains the code to drive batteries, hub etc. + */ +class ESP8266_mdm +{ +public: + + // ********************* + // * Public variables: * + // ********************* + + // ********************* + // * Public hardware: * + // ********************* + + + + // ********************* + // * Public interrupts: * + // ********************* + + // ********************* + // * Public functions: * + // ********************* + + /** + * Constructor: + * @param esp A pointer to the serial connection to the esp. + * @param server =1 for server, 0 for client + */ + ESP8266_mdm(RawSerial *esp, int server, RawSerial *pc, PinName rstPn); + + /** + * Device has received a byte + */ + void dev_recv(); + +protected: + + + // ********************* + // * Protected variables: * + // ********************* + RawSerial *esp; /**< The serial port */ + int server; /**< Tells if server or not */ + RawSerial *pc; + PinName rstPn; + + char **command; + char **responseRequired; + int maxState; + + volatile int state; + volatile int ready; + char* buffer; + int bufferPnt; + + + static char* startCmnds[10]; + static char* startResp[10]; + static int startMaxState; + + int timeOut; + + // ********************* + // * Protected hardware: * + // ********************* + DigitalOut* reset; + Timer *last; + + // ********************* + // * Protected interrupts: * + // ********************* + + + // ********************* + // * Protected functions: * + // ********************* + + + + /** + * Loops through until state = maxState; + */ + void doLoop(int initState); + + /** + * Checks if pattern is in test. Returns 1 for true, 0 for no. -1 if busy. -2 ERROR + * + */ + int OKResponse(char *test, const char *pattern); + +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r 011fdf85bfc8 defs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/defs.h Tue May 23 10:26:19 2017 +0000 @@ -0,0 +1,57 @@ +/** + *@section DESCRIPTION + * mbed SolarNanogrid Library - definitions for messages + *@section LICENSE + * Copyright (c) 2016, Malcolm McCulloch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * @file "defs.h" + */ + +// +// Defines which messages to print +// +#define DEBUG +#define INFOMESSAGES +#define WARNMESSAGES +#define ERRMESSAGES + +#ifdef DEBUG +#define DBG(x, ...) printf("["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__); +#else +#define DBG(x, ...) +#endif + +#ifdef ERRMESSAGES +#define ERR(x, ...) printf("["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__); +#else +#define ERR(x, ...) +#endif + +#ifdef WARNMESSAGES +#define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__); +#else +#define WARN(x, ...) +#endif + +#ifdef INFOMESSAGES +#define INFO(x, ...) printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__); +#else +#define INFO(x, ...) +#endif