Modified Library for using the Wifi module ESP8266, first version with basic commands, soon new commands will be added
Fork of ESP8266 by
ESP8266.cpp@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 | #include "ESP8266.h" |
quevedo | 0:e58f27687450 | 2 | |
quevedo | 0:e58f27687450 | 3 | // Constructor |
quevedo | 0:e58f27687450 | 4 | ESP8266::ESP8266(PinName tx, PinName rx) : comm(tx, rx) { |
quevedo | 0:e58f27687450 | 5 | comm.baud(115200); |
quevedo | 0:e58f27687450 | 6 | } |
quevedo | 0:e58f27687450 | 7 | |
quevedo | 0:e58f27687450 | 8 | // Destructor |
quevedo | 0:e58f27687450 | 9 | ESP8266::~ESP8266() { } |
quevedo | 0:e58f27687450 | 10 | |
quevedo | 0:e58f27687450 | 11 | // Add <CR> + <LF> at the end of the string |
quevedo | 0:e58f27687450 | 12 | void ESP8266::AddEOL(char * s) { |
quevedo | 0:e58f27687450 | 13 | char k; |
quevedo | 0:e58f27687450 | 14 | k = strlen(s); // Finds position of NULL character |
quevedo | 0:e58f27687450 | 15 | s[k] = 0x0D; // switch NULL for <CR> |
quevedo | 0:e58f27687450 | 16 | s[k + 1] = 0x0A; // Add <LF> |
quevedo | 0:e58f27687450 | 17 | s[k + 2] = 0; // Add NULL at the end |
quevedo | 0:e58f27687450 | 18 | } |
quevedo | 0:e58f27687450 | 19 | |
quevedo | 0:e58f27687450 | 20 | // Add one ASCII character at the end of the string |
quevedo | 0:e58f27687450 | 21 | void ESP8266::AddChar(char * s, char c) { |
quevedo | 0:e58f27687450 | 22 | char k; |
quevedo | 0:e58f27687450 | 23 | k = strlen(s); |
quevedo | 0:e58f27687450 | 24 | s[k] = c; |
quevedo | 0:e58f27687450 | 25 | s[k + 1] = 0; |
quevedo | 0:e58f27687450 | 26 | } |
quevedo | 0:e58f27687450 | 27 | |
quevedo | 0:e58f27687450 | 28 | // Sends command to ESP8266. Receives the command string |
quevedo | 0:e58f27687450 | 29 | void ESP8266::SendCMD(char * s) { |
quevedo | 0:e58f27687450 | 30 | AddEOL(s); |
quevedo | 0:e58f27687450 | 31 | comm.printf("%s", s); |
quevedo | 0:e58f27687450 | 32 | } |
quevedo | 0:e58f27687450 | 33 | |
quevedo | 0:e58f27687450 | 34 | // Resets the ESP8266 |
quevedo | 0:e58f27687450 | 35 | void ESP8266::Reset(void) { |
quevedo | 0:e58f27687450 | 36 | char rs[10]; |
quevedo | 0:e58f27687450 | 37 | strcpy(rs, "AT+RST"); |
quevedo | 0:e58f27687450 | 38 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 39 | } |
quevedo | 0:e58f27687450 | 40 | |
quevedo | 0:e58f27687450 | 41 | // Receive reply until no character is received after a given timeout in miliseconds |
quevedo | 0:e58f27687450 | 42 | void ESP8266::RcvReply(char * r, int to) { |
quevedo | 0:e58f27687450 | 43 | Timer t; |
quevedo | 0:e58f27687450 | 44 | bool ended = 0; |
quevedo | 0:e58f27687450 | 45 | char c; |
quevedo | 0:e58f27687450 | 46 | |
quevedo | 0:e58f27687450 | 47 | strcpy(r, ""); |
quevedo | 0:e58f27687450 | 48 | t.start(); |
quevedo | 0:e58f27687450 | 49 | while(!ended) { |
quevedo | 0:e58f27687450 | 50 | if(comm.readable()) { |
quevedo | 0:e58f27687450 | 51 | c = comm.getc(); |
quevedo | 0:e58f27687450 | 52 | AddChar(r, c); |
quevedo | 0:e58f27687450 | 53 | t.start(); |
quevedo | 0:e58f27687450 | 54 | } |
quevedo | 0:e58f27687450 | 55 | if(t.read_ms() > to) { |
quevedo | 0:e58f27687450 | 56 | ended = 1; |
quevedo | 0:e58f27687450 | 57 | } |
quevedo | 0:e58f27687450 | 58 | } |
quevedo | 0:e58f27687450 | 59 | AddChar(r, 0x00); |
quevedo | 0:e58f27687450 | 60 | } |
quevedo | 0:e58f27687450 | 61 | |
quevedo | 0:e58f27687450 | 62 | // Gets the AP list. Parameter: the string to receive the list |
quevedo | 0:e58f27687450 | 63 | void ESP8266::GetList(char * l) { |
quevedo | 0:e58f27687450 | 64 | char rs[15]; |
quevedo | 0:e58f27687450 | 65 | strcpy(rs, "AT+CWLAP"); |
quevedo | 0:e58f27687450 | 66 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 67 | RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
quevedo | 0:e58f27687450 | 68 | } |
quevedo | 0:e58f27687450 | 69 | |
quevedo | 0:e58f27687450 | 70 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
quevedo | 0:e58f27687450 | 71 | void ESP8266::Join(char * id, char * pwd) { |
quevedo | 0:e58f27687450 | 72 | char cmd[255]; |
quevedo | 0:e58f27687450 | 73 | strcpy(cmd, "AT+CWJAP="); |
quevedo | 0:e58f27687450 | 74 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 75 | strcat(cmd, id); |
quevedo | 0:e58f27687450 | 76 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 77 | AddChar(cmd, 0x2C); |
quevedo | 0:e58f27687450 | 78 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 79 | strcat(cmd, pwd); |
quevedo | 0:e58f27687450 | 80 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 81 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 82 | } |
quevedo | 0:e58f27687450 | 83 | |
quevedo | 0:e58f27687450 | 84 | // Gets ESP IP. Parameter: string to contain IP |
quevedo | 0:e58f27687450 | 85 | void ESP8266::GetIP(char * ip) { |
quevedo | 0:e58f27687450 | 86 | char cmd[15]; |
quevedo | 0:e58f27687450 | 87 | strcpy(cmd, "AT+CIFSR"); |
quevedo | 0:e58f27687450 | 88 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 89 | RcvReply(ip, 2000); |
quevedo | 0:e58f27687450 | 90 | } |