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@2:77388e8f0697, 2014-12-28 (annotated)
- Committer:
- quevedo
- Date:
- Sun Dec 28 21:58:49 2014 +0000
- Revision:
- 2:77388e8f0697
- Parent:
- 1:399414d48048
- Child:
- 3:8a8fb2e0958c
Added functions: Get connection status, Start server mode, Close server mode; now the ESP serial baudrate is passed as a parameter in the constructor
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 | 2:77388e8f0697 | 4 | ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) { |
quevedo | 2:77388e8f0697 | 5 | comm.baud(br); |
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 | 2:77388e8f0697 | 28 | // Converts integer number to null-terminated string |
quevedo | 2:77388e8f0697 | 29 | void ESP8266::itoa(int n, char * s) { |
quevedo | 2:77388e8f0697 | 30 | char k = 0; |
quevedo | 2:77388e8f0697 | 31 | char r[11]; |
quevedo | 2:77388e8f0697 | 32 | |
quevedo | 2:77388e8f0697 | 33 | if(n == 0) { |
quevedo | 2:77388e8f0697 | 34 | s[0] = '0'; |
quevedo | 2:77388e8f0697 | 35 | s[1] = 0; |
quevedo | 2:77388e8f0697 | 36 | } else { |
quevedo | 2:77388e8f0697 | 37 | while(n != 0) { |
quevedo | 2:77388e8f0697 | 38 | r[k]= (n % 10) + '0'; |
quevedo | 2:77388e8f0697 | 39 | n = n / 10; |
quevedo | 2:77388e8f0697 | 40 | k++; |
quevedo | 2:77388e8f0697 | 41 | } |
quevedo | 2:77388e8f0697 | 42 | while(k > 0) { |
quevedo | 2:77388e8f0697 | 43 | s[n] = r[k - 1] + '0'; |
quevedo | 2:77388e8f0697 | 44 | n++; |
quevedo | 2:77388e8f0697 | 45 | k--; |
quevedo | 2:77388e8f0697 | 46 | } |
quevedo | 2:77388e8f0697 | 47 | s[n] = 0; |
quevedo | 2:77388e8f0697 | 48 | } |
quevedo | 2:77388e8f0697 | 49 | } |
quevedo | 2:77388e8f0697 | 50 | |
quevedo | 0:e58f27687450 | 51 | // Sends command to ESP8266. Receives the command string |
quevedo | 0:e58f27687450 | 52 | void ESP8266::SendCMD(char * s) { |
quevedo | 0:e58f27687450 | 53 | AddEOL(s); |
quevedo | 0:e58f27687450 | 54 | comm.printf("%s", s); |
quevedo | 0:e58f27687450 | 55 | } |
quevedo | 0:e58f27687450 | 56 | |
quevedo | 0:e58f27687450 | 57 | // Resets the ESP8266 |
quevedo | 0:e58f27687450 | 58 | void ESP8266::Reset(void) { |
quevedo | 0:e58f27687450 | 59 | char rs[10]; |
quevedo | 0:e58f27687450 | 60 | strcpy(rs, "AT+RST"); |
quevedo | 0:e58f27687450 | 61 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 62 | } |
quevedo | 0:e58f27687450 | 63 | |
quevedo | 0:e58f27687450 | 64 | // Receive reply until no character is received after a given timeout in miliseconds |
quevedo | 0:e58f27687450 | 65 | void ESP8266::RcvReply(char * r, int to) { |
quevedo | 0:e58f27687450 | 66 | Timer t; |
quevedo | 0:e58f27687450 | 67 | bool ended = 0; |
quevedo | 0:e58f27687450 | 68 | char c; |
quevedo | 0:e58f27687450 | 69 | |
quevedo | 0:e58f27687450 | 70 | strcpy(r, ""); |
quevedo | 0:e58f27687450 | 71 | t.start(); |
quevedo | 0:e58f27687450 | 72 | while(!ended) { |
quevedo | 0:e58f27687450 | 73 | if(comm.readable()) { |
quevedo | 0:e58f27687450 | 74 | c = comm.getc(); |
quevedo | 0:e58f27687450 | 75 | AddChar(r, c); |
quevedo | 0:e58f27687450 | 76 | t.start(); |
quevedo | 0:e58f27687450 | 77 | } |
quevedo | 0:e58f27687450 | 78 | if(t.read_ms() > to) { |
quevedo | 0:e58f27687450 | 79 | ended = 1; |
quevedo | 0:e58f27687450 | 80 | } |
quevedo | 0:e58f27687450 | 81 | } |
quevedo | 0:e58f27687450 | 82 | AddChar(r, 0x00); |
quevedo | 0:e58f27687450 | 83 | } |
quevedo | 0:e58f27687450 | 84 | |
quevedo | 0:e58f27687450 | 85 | // Gets the AP list. Parameter: the string to receive the list |
quevedo | 0:e58f27687450 | 86 | void ESP8266::GetList(char * l) { |
quevedo | 0:e58f27687450 | 87 | char rs[15]; |
quevedo | 0:e58f27687450 | 88 | strcpy(rs, "AT+CWLAP"); |
quevedo | 0:e58f27687450 | 89 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 90 | RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
quevedo | 0:e58f27687450 | 91 | } |
quevedo | 0:e58f27687450 | 92 | |
quevedo | 0:e58f27687450 | 93 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
quevedo | 0:e58f27687450 | 94 | void ESP8266::Join(char * id, char * pwd) { |
quevedo | 0:e58f27687450 | 95 | char cmd[255]; |
quevedo | 0:e58f27687450 | 96 | strcpy(cmd, "AT+CWJAP="); |
quevedo | 0:e58f27687450 | 97 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 98 | strcat(cmd, id); |
quevedo | 0:e58f27687450 | 99 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 100 | AddChar(cmd, 0x2C); |
quevedo | 0:e58f27687450 | 101 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 102 | strcat(cmd, pwd); |
quevedo | 0:e58f27687450 | 103 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 104 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 105 | } |
quevedo | 0:e58f27687450 | 106 | |
quevedo | 0:e58f27687450 | 107 | // Gets ESP IP. Parameter: string to contain IP |
quevedo | 0:e58f27687450 | 108 | void ESP8266::GetIP(char * ip) { |
quevedo | 0:e58f27687450 | 109 | char cmd[15]; |
quevedo | 0:e58f27687450 | 110 | strcpy(cmd, "AT+CIFSR"); |
quevedo | 0:e58f27687450 | 111 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 112 | RcvReply(ip, 2000); |
quevedo | 1:399414d48048 | 113 | } |
quevedo | 1:399414d48048 | 114 | |
quevedo | 1:399414d48048 | 115 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
quevedo | 1:399414d48048 | 116 | void ESP8266::SetMode(char mode) { |
quevedo | 1:399414d48048 | 117 | char cmd[15]; |
quevedo | 1:399414d48048 | 118 | strcpy(cmd, "AT+CWMODE="); |
quevedo | 1:399414d48048 | 119 | mode = mode + 0x30; // Converts number into corresponding ASCII character |
quevedo | 1:399414d48048 | 120 | AddChar(cmd, mode); // Completes command string |
quevedo | 1:399414d48048 | 121 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 122 | } |
quevedo | 1:399414d48048 | 123 | |
quevedo | 1:399414d48048 | 124 | // Quits the AP |
quevedo | 1:399414d48048 | 125 | void ESP8266::Quit(void) { |
quevedo | 1:399414d48048 | 126 | char cmd[15]; |
quevedo | 1:399414d48048 | 127 | strcpy(cmd, "AT+CWQAP"); |
quevedo | 1:399414d48048 | 128 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 129 | } |
quevedo | 1:399414d48048 | 130 | |
quevedo | 1:399414d48048 | 131 | // Sets single connection |
quevedo | 1:399414d48048 | 132 | void ESP8266::SetSingle(void) { |
quevedo | 1:399414d48048 | 133 | char cmd[15]; |
quevedo | 1:399414d48048 | 134 | strcpy(cmd, "AT+CIPMUX=0"); |
quevedo | 1:399414d48048 | 135 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 136 | } |
quevedo | 1:399414d48048 | 137 | |
quevedo | 1:399414d48048 | 138 | // Sets multiple connection |
quevedo | 1:399414d48048 | 139 | void ESP8266::SetMultiple(void) { |
quevedo | 1:399414d48048 | 140 | char rs[15]; |
quevedo | 1:399414d48048 | 141 | strcpy(rs, "AT+CIPMUX=1"); |
quevedo | 1:399414d48048 | 142 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 143 | } |
quevedo | 2:77388e8f0697 | 144 | |
quevedo | 2:77388e8f0697 | 145 | // Gets connection status. Parameter: string to contain status |
quevedo | 2:77388e8f0697 | 146 | void ESP8266::GetConnStatus(char * st) { |
quevedo | 2:77388e8f0697 | 147 | char cmd[15]; |
quevedo | 2:77388e8f0697 | 148 | strcpy(cmd, "AT+CIPSTATUS"); |
quevedo | 2:77388e8f0697 | 149 | SendCMD(cmd); |
quevedo | 2:77388e8f0697 | 150 | RcvReply(st, 2000); |
quevedo | 2:77388e8f0697 | 151 | } |
quevedo | 2:77388e8f0697 | 152 | |
quevedo | 2:77388e8f0697 | 153 | // Starts server mode. Parameter: port to be used |
quevedo | 2:77388e8f0697 | 154 | void ESP8266::StartServerMode(int port) { |
quevedo | 2:77388e8f0697 | 155 | char rs[25]; |
quevedo | 2:77388e8f0697 | 156 | char t[4]; |
quevedo | 2:77388e8f0697 | 157 | strcpy(rs, "AT+CIPSERVER=1,"); |
quevedo | 2:77388e8f0697 | 158 | itoa(port, t); |
quevedo | 2:77388e8f0697 | 159 | strcat(rs, t); |
quevedo | 2:77388e8f0697 | 160 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 161 | } |
quevedo | 2:77388e8f0697 | 162 | |
quevedo | 2:77388e8f0697 | 163 | // Close server mode. |
quevedo | 2:77388e8f0697 | 164 | void ESP8266::CloseServerMode(void) { |
quevedo | 2:77388e8f0697 | 165 | char rs[20]; |
quevedo | 2:77388e8f0697 | 166 | strcpy(rs, "AT+CIPSERVER=0"); |
quevedo | 2:77388e8f0697 | 167 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 168 | } |