Library for using the Wifi module ESP8266, first version with basic commands, soon new commands will be added
Dependents: PIR_Sensor_wifi myESP8266forLOstFound Motin_Activated_Temperature_Humidity_BluetoothHC-06_ESP8266_IOT 1Project3_Robot1_Final ... more
Library updated in Dec 28, 2014. Now the ESP baudrate is passed as a parameter in the constructor. Also, some new functions were added. Here is an example code:
ESP8266 example code
#include "mbed.h" #include <string> #include "ESP8266.h" // Objects Serial pc(USBTX, USBRX); ESP8266 esp(PTE0, PTE1, 115200); // Global variables char snd[255], rcv[1000]; // Strings for sending and receiving commands / data / replies int main() { pc.baud(115200); pc.printf("START\r\n"); wait(3); pc.printf("Reset ESP\r\n"); esp.Reset(); esp.RcvReply(rcv, 400); pc.printf("%s", rcv); wait(2); pc.printf("Sending AT\r\n"); strcpy(snd, "AT"); esp.SendCMD(snd); esp.RcvReply(rcv, 400); pc.printf("%s", rcv); wait(2); pc.printf("Set mode to AP\r\n"); esp.SetMode(1); esp.RcvReply(rcv, 1000); pc.printf("%s", rcv); pc.printf("Receiving Wifi List\r\n"); esp.GetList(rcv); pc.printf("%s", rcv); pc.printf("Connecting to AP\r\n"); esp.Join("MyAP", "MyPasswd"); // Replace MyAP and MyPasswd with your SSID and password esp.RcvReply(rcv, 1000); pc.printf("%s", rcv); wait(8); pc.printf("Getting IP\r\n"); esp.GetIP(rcv); pc.printf("%s", rcv); pc.printf("Setting multiple connections\r\n"); esp.SetMultiple(); esp.RcvReply(rcv, 1000); pc.printf("%s", rcv); pc.printf("Getting Connection Status\r\n"); esp.GetConnStatus(rcv); pc.printf("%s", rcv); pc.printf("Start server mode on port 80\r\n"); esp.StartServerMode(80); esp.RcvReply(rcv, 1000); pc.printf("%s", rcv); wait(4); pc.printf("Close server mode\r\n"); esp.CloseServerMode(); esp.RcvReply(rcv, 1000); pc.printf("THE END"); while(1); }
ESP8266.h@0:e58f27687450, 2014-12-16 (annotated)
- Committer:
- quevedo
- Date:
- Tue Dec 16 01:08:47 2014 +0000
- Revision:
- 0:e58f27687450
- Child:
- 1:399414d48048
First version, only basic commands
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
quevedo | 0:e58f27687450 | 1 | #ifndef ESP8266_H |
quevedo | 0:e58f27687450 | 2 | #define ESP8266_H |
quevedo | 0:e58f27687450 | 3 | |
quevedo | 0:e58f27687450 | 4 | #include <string> |
quevedo | 0:e58f27687450 | 5 | #include "mbed.h" |
quevedo | 0:e58f27687450 | 6 | |
quevedo | 0:e58f27687450 | 7 | class ESP8266 |
quevedo | 0:e58f27687450 | 8 | { |
quevedo | 0:e58f27687450 | 9 | public: |
quevedo | 0:e58f27687450 | 10 | /** |
quevedo | 0:e58f27687450 | 11 | * ESP8266 constructor |
quevedo | 0:e58f27687450 | 12 | * |
quevedo | 0:e58f27687450 | 13 | * @param tx TX pin |
quevedo | 0:e58f27687450 | 14 | * @param rx RX pin |
quevedo | 0:e58f27687450 | 15 | */ |
quevedo | 0:e58f27687450 | 16 | ESP8266(PinName tx, PinName rx); |
quevedo | 0:e58f27687450 | 17 | |
quevedo | 0:e58f27687450 | 18 | /** |
quevedo | 0:e58f27687450 | 19 | * ESP8266 destructor |
quevedo | 0:e58f27687450 | 20 | */ |
quevedo | 0:e58f27687450 | 21 | ~ESP8266(); |
quevedo | 0:e58f27687450 | 22 | |
quevedo | 0:e58f27687450 | 23 | void SendCMD(char * s); |
quevedo | 0:e58f27687450 | 24 | void Reset(void); |
quevedo | 0:e58f27687450 | 25 | void RcvReply(char * r, int to); |
quevedo | 0:e58f27687450 | 26 | void GetList(char * l); |
quevedo | 0:e58f27687450 | 27 | void Join(char * id, char * pwd); |
quevedo | 0:e58f27687450 | 28 | void GetIP(char * ip); |
quevedo | 0:e58f27687450 | 29 | |
quevedo | 0:e58f27687450 | 30 | private: |
quevedo | 0:e58f27687450 | 31 | Serial comm; |
quevedo | 0:e58f27687450 | 32 | void AddEOL(char * s); |
quevedo | 0:e58f27687450 | 33 | void AddChar(char * s, char c); |
quevedo | 0:e58f27687450 | 34 | |
quevedo | 0:e58f27687450 | 35 | }; |
quevedo | 0:e58f27687450 | 36 | |
quevedo | 0:e58f27687450 | 37 | #endif |
quevedo | 0:e58f27687450 | 38 | /* |
quevedo | 0:e58f27687450 | 39 | COMMAND TABLE |
quevedo | 0:e58f27687450 | 40 | Basic: |
quevedo | 0:e58f27687450 | 41 | AT: Just to generate "OK" reply |
quevedo | 0:e58f27687450 | 42 | Wifi: |
quevedo | 0:e58f27687450 | 43 | AT+RST: restart the module |
quevedo | 0:e58f27687450 | 44 | AT+CWMODE: define wifi mode; AT+CWMODE=<mode> 1= Sta, 2= AP, 3=both; Inquiry: AT+CWMODE? or AT+CWMODE=? |
quevedo | 0:e58f27687450 | 45 | AT+CWJAP: join the AP wifi; AT+ CWJAP =<ssid>,< pwd > - ssid = ssid, pwd = wifi password, both between quotes; Inquiry: AT+ CWJAP? |
quevedo | 0:e58f27687450 | 46 | AT+CWLAP: list the AP wifi |
quevedo | 0:e58f27687450 | 47 | AT+CWQAP: quit the AP wifi; Inquiry: AT+CWQAP=? |
quevedo | 0:e58f27687450 | 48 | AT+CWSAP: set the parameters of AP; AT+CWSAP= <ssid>,<pwd>,<chl>,<ecn> - ssid, pwd, chl = channel, ecn = encryption; Inquiry: AT+CWJAP? |
quevedo | 0:e58f27687450 | 49 | TCP/IP: |
quevedo | 0:e58f27687450 | 50 | AT+CIPSTATUS: get the connection status |
quevedo | 0:e58f27687450 | 51 | AT+CIPSTART: set up TCP or UDP connection 1)single connection (+CIPMUX=0) AT+CIPSTART= <type>,<addr>,<port>; 2) multiple connection (+CIPMUX=1) AT+CIPSTART= <id><type>,<addr>, <port> - id = 0-4, type = TCP/UDP, addr = IP address, port= port; Inquiry: AT+CIPSTART=? |
quevedo | 0:e58f27687450 | 52 | AT+CIPSEND: send data; 1)single connection(+CIPMUX=0) AT+CIPSEND=<length>; 2) multiple connection (+CIPMUX=1) AT+CIPSEND= <id>,<length>; Inquiry: AT+CIPSEND=? |
quevedo | 0:e58f27687450 | 53 | AT+CIPCLOSE: close TCP or UDP connection; AT+CIPCLOSE=<id> or AT+CIPCLOSE; Inquiry: AT+CIPCLOSE=? |
quevedo | 0:e58f27687450 | 54 | AT+CIFSR: Get IP address; Inquiry: AT+ CIFSR=? |
quevedo | 0:e58f27687450 | 55 | AT+CIPMUX: set mutiple connection; AT+ CIPMUX=<mode> - 0 for single connection 1 for mutiple connection; Inquiry: AT+CIPMUX? |
quevedo | 0:e58f27687450 | 56 | AT+CIPSERVER: set as server; AT+ CIPSERVER= <mode>[,<port> ] - mode 0 to close server mode, mode 1 to open; port = port; Inquiry: AT+CIFSR=? |
quevedo | 0:e58f27687450 | 57 | +IPD: received data |
quevedo | 0:e58f27687450 | 58 | */ |