Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266 by
ESP8266.cpp@1:399414d48048, 2014-12-17 (annotated)
- Committer:
- quevedo
- Date:
- Wed Dec 17 13:54:34 2014 +0000
- Revision:
- 1:399414d48048
- Parent:
- 0:e58f27687450
- Child:
- 2:77388e8f0697
Added functions: SetMode (STA, AP, or both), Quit(quits the AP connection), and SetSingle / SetMultiple for single and multiple connections
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 | 1:399414d48048 | 90 | } |
quevedo | 1:399414d48048 | 91 | |
quevedo | 1:399414d48048 | 92 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
quevedo | 1:399414d48048 | 93 | void ESP8266::SetMode(char mode) { |
quevedo | 1:399414d48048 | 94 | char cmd[15]; |
quevedo | 1:399414d48048 | 95 | strcpy(cmd, "AT+CWMODE="); |
quevedo | 1:399414d48048 | 96 | mode = mode + 0x30; // Converts number into corresponding ASCII character |
quevedo | 1:399414d48048 | 97 | AddChar(cmd, mode); // Completes command string |
quevedo | 1:399414d48048 | 98 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 99 | } |
quevedo | 1:399414d48048 | 100 | |
quevedo | 1:399414d48048 | 101 | // Quits the AP |
quevedo | 1:399414d48048 | 102 | void ESP8266::Quit(void) { |
quevedo | 1:399414d48048 | 103 | char cmd[15]; |
quevedo | 1:399414d48048 | 104 | strcpy(cmd, "AT+CWQAP"); |
quevedo | 1:399414d48048 | 105 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 106 | } |
quevedo | 1:399414d48048 | 107 | |
quevedo | 1:399414d48048 | 108 | // Sets single connection |
quevedo | 1:399414d48048 | 109 | void ESP8266::SetSingle(void) { |
quevedo | 1:399414d48048 | 110 | char cmd[15]; |
quevedo | 1:399414d48048 | 111 | strcpy(cmd, "AT+CIPMUX=0"); |
quevedo | 1:399414d48048 | 112 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 113 | } |
quevedo | 1:399414d48048 | 114 | |
quevedo | 1:399414d48048 | 115 | // Sets multiple connection |
quevedo | 1:399414d48048 | 116 | void ESP8266::SetMultiple(void) { |
quevedo | 1:399414d48048 | 117 | char rs[15]; |
quevedo | 1:399414d48048 | 118 | strcpy(rs, "AT+CIPMUX=1"); |
quevedo | 1:399414d48048 | 119 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 120 | } |