Project in FIBO
Fork of ESP8266 by
ESP8266.cpp@4:8dfe0574a040, 2017-12-12 (annotated)
- Committer:
- Pairam
- Date:
- Tue Dec 12 11:39:55 2017 +0000
- Revision:
- 4:8dfe0574a040
- Parent:
- 3:4f24e7e803a1
Project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
quevedo | 0:e58f27687450 | 1 | #include "ESP8266.h" |
janhavi | 3:4f24e7e803a1 | 2 | #define HTTPCMD "GET " |
Pairam | 4:8dfe0574a040 | 3 | #define protocol " HTTP/1.1\n\n" |
janhavi | 3:4f24e7e803a1 | 4 | |
quevedo | 0:e58f27687450 | 5 | |
quevedo | 0:e58f27687450 | 6 | // Constructor |
Pairam | 4:8dfe0574a040 | 7 | ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) |
Pairam | 4:8dfe0574a040 | 8 | { |
quevedo | 2:77388e8f0697 | 9 | comm.baud(br); |
quevedo | 0:e58f27687450 | 10 | } |
quevedo | 0:e58f27687450 | 11 | |
quevedo | 0:e58f27687450 | 12 | // Destructor |
quevedo | 0:e58f27687450 | 13 | ESP8266::~ESP8266() { } |
quevedo | 0:e58f27687450 | 14 | |
quevedo | 0:e58f27687450 | 15 | // Add <CR> + <LF> at the end of the string |
Pairam | 4:8dfe0574a040 | 16 | void ESP8266::AddEOL(char * s) |
Pairam | 4:8dfe0574a040 | 17 | { |
quevedo | 0:e58f27687450 | 18 | char k; |
quevedo | 0:e58f27687450 | 19 | k = strlen(s); // Finds position of NULL character |
quevedo | 0:e58f27687450 | 20 | s[k] = 0x0D; // switch NULL for <CR> |
quevedo | 0:e58f27687450 | 21 | s[k + 1] = 0x0A; // Add <LF> |
quevedo | 0:e58f27687450 | 22 | s[k + 2] = 0; // Add NULL at the end |
quevedo | 0:e58f27687450 | 23 | } |
quevedo | 0:e58f27687450 | 24 | |
quevedo | 0:e58f27687450 | 25 | // Add one ASCII character at the end of the string |
Pairam | 4:8dfe0574a040 | 26 | void ESP8266::AddChar(char * s, char c) |
Pairam | 4:8dfe0574a040 | 27 | { |
quevedo | 0:e58f27687450 | 28 | char k; |
quevedo | 0:e58f27687450 | 29 | k = strlen(s); |
quevedo | 0:e58f27687450 | 30 | s[k] = c; |
quevedo | 0:e58f27687450 | 31 | s[k + 1] = 0; |
quevedo | 0:e58f27687450 | 32 | } |
quevedo | 0:e58f27687450 | 33 | |
quevedo | 2:77388e8f0697 | 34 | // Converts integer number to null-terminated string |
Pairam | 4:8dfe0574a040 | 35 | void ESP8266::itoa(int n, char * s) |
Pairam | 4:8dfe0574a040 | 36 | { |
quevedo | 2:77388e8f0697 | 37 | char k = 0; |
quevedo | 2:77388e8f0697 | 38 | char r[11]; |
Pairam | 4:8dfe0574a040 | 39 | |
quevedo | 2:77388e8f0697 | 40 | if(n == 0) { |
quevedo | 2:77388e8f0697 | 41 | s[0] = '0'; |
quevedo | 2:77388e8f0697 | 42 | s[1] = 0; |
quevedo | 2:77388e8f0697 | 43 | } else { |
quevedo | 2:77388e8f0697 | 44 | while(n != 0) { |
quevedo | 2:77388e8f0697 | 45 | r[k]= (n % 10) + '0'; |
quevedo | 2:77388e8f0697 | 46 | n = n / 10; |
quevedo | 2:77388e8f0697 | 47 | k++; |
quevedo | 2:77388e8f0697 | 48 | } |
quevedo | 2:77388e8f0697 | 49 | while(k > 0) { |
quevedo | 2:77388e8f0697 | 50 | s[n] = r[k - 1] + '0'; |
quevedo | 2:77388e8f0697 | 51 | n++; |
quevedo | 2:77388e8f0697 | 52 | k--; |
quevedo | 2:77388e8f0697 | 53 | } |
quevedo | 2:77388e8f0697 | 54 | s[n] = 0; |
quevedo | 2:77388e8f0697 | 55 | } |
quevedo | 2:77388e8f0697 | 56 | } |
quevedo | 2:77388e8f0697 | 57 | |
quevedo | 0:e58f27687450 | 58 | // Sends command to ESP8266. Receives the command string |
Pairam | 4:8dfe0574a040 | 59 | void ESP8266::SendCMD(char * s) |
Pairam | 4:8dfe0574a040 | 60 | { |
quevedo | 0:e58f27687450 | 61 | AddEOL(s); |
quevedo | 0:e58f27687450 | 62 | comm.printf("%s", s); |
quevedo | 0:e58f27687450 | 63 | } |
quevedo | 0:e58f27687450 | 64 | |
quevedo | 0:e58f27687450 | 65 | // Resets the ESP8266 |
Pairam | 4:8dfe0574a040 | 66 | void ESP8266::Reset(void) |
Pairam | 4:8dfe0574a040 | 67 | { |
quevedo | 0:e58f27687450 | 68 | char rs[10]; |
quevedo | 0:e58f27687450 | 69 | strcpy(rs, "AT+RST"); |
quevedo | 0:e58f27687450 | 70 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 71 | } |
quevedo | 0:e58f27687450 | 72 | |
quevedo | 0:e58f27687450 | 73 | // Receive reply until no character is received after a given timeout in miliseconds |
Pairam | 4:8dfe0574a040 | 74 | bool ESP8266::RcvReply(char * r, int to) |
Pairam | 4:8dfe0574a040 | 75 | { |
quevedo | 0:e58f27687450 | 76 | Timer t; |
quevedo | 0:e58f27687450 | 77 | bool ended = 0; |
quevedo | 0:e58f27687450 | 78 | char c; |
Pairam | 4:8dfe0574a040 | 79 | |
quevedo | 0:e58f27687450 | 80 | strcpy(r, ""); |
quevedo | 0:e58f27687450 | 81 | t.start(); |
quevedo | 0:e58f27687450 | 82 | while(!ended) { |
quevedo | 0:e58f27687450 | 83 | if(comm.readable()) { |
quevedo | 0:e58f27687450 | 84 | c = comm.getc(); |
quevedo | 0:e58f27687450 | 85 | AddChar(r, c); |
quevedo | 0:e58f27687450 | 86 | t.start(); |
quevedo | 0:e58f27687450 | 87 | } |
quevedo | 0:e58f27687450 | 88 | if(t.read_ms() > to) { |
Pairam | 4:8dfe0574a040 | 89 | ended = 1; |
quevedo | 0:e58f27687450 | 90 | } |
quevedo | 0:e58f27687450 | 91 | } |
quevedo | 0:e58f27687450 | 92 | AddChar(r, 0x00); |
janhavi | 3:4f24e7e803a1 | 93 | return ended; |
quevedo | 0:e58f27687450 | 94 | } |
quevedo | 0:e58f27687450 | 95 | |
quevedo | 0:e58f27687450 | 96 | // Gets the AP list. Parameter: the string to receive the list |
Pairam | 4:8dfe0574a040 | 97 | void ESP8266::GetList(char * l) |
Pairam | 4:8dfe0574a040 | 98 | { |
quevedo | 0:e58f27687450 | 99 | char rs[15]; |
quevedo | 0:e58f27687450 | 100 | strcpy(rs, "AT+CWLAP"); |
quevedo | 0:e58f27687450 | 101 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 102 | RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
quevedo | 0:e58f27687450 | 103 | } |
quevedo | 0:e58f27687450 | 104 | |
quevedo | 0:e58f27687450 | 105 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
Pairam | 4:8dfe0574a040 | 106 | void ESP8266::Join(char * id, char * pwd) |
Pairam | 4:8dfe0574a040 | 107 | { |
quevedo | 0:e58f27687450 | 108 | char cmd[255]; |
quevedo | 0:e58f27687450 | 109 | strcpy(cmd, "AT+CWJAP="); |
quevedo | 0:e58f27687450 | 110 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 111 | strcat(cmd, id); |
quevedo | 0:e58f27687450 | 112 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 113 | AddChar(cmd, 0x2C); |
quevedo | 0:e58f27687450 | 114 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 115 | strcat(cmd, pwd); |
quevedo | 0:e58f27687450 | 116 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 117 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 118 | } |
quevedo | 0:e58f27687450 | 119 | |
quevedo | 0:e58f27687450 | 120 | // Gets ESP IP. Parameter: string to contain IP |
Pairam | 4:8dfe0574a040 | 121 | void ESP8266::GetIP(char * ip) |
Pairam | 4:8dfe0574a040 | 122 | { |
quevedo | 0:e58f27687450 | 123 | char cmd[15]; |
quevedo | 0:e58f27687450 | 124 | strcpy(cmd, "AT+CIFSR"); |
quevedo | 0:e58f27687450 | 125 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 126 | RcvReply(ip, 2000); |
quevedo | 1:399414d48048 | 127 | } |
quevedo | 1:399414d48048 | 128 | |
Pairam | 4:8dfe0574a040 | 129 | void ESP8266::GetCon(char * ipp) |
Pairam | 4:8dfe0574a040 | 130 | { |
Pairam | 4:8dfe0574a040 | 131 | char cmd[15]; |
Pairam | 4:8dfe0574a040 | 132 | strcpy(cmd, "AT+CWJAP?"); |
Pairam | 4:8dfe0574a040 | 133 | SendCMD(cmd); |
Pairam | 4:8dfe0574a040 | 134 | RcvReply(ipp, 2000); |
Pairam | 4:8dfe0574a040 | 135 | } |
Pairam | 4:8dfe0574a040 | 136 | |
quevedo | 1:399414d48048 | 137 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
Pairam | 4:8dfe0574a040 | 138 | void ESP8266::SetMode(char mode) |
Pairam | 4:8dfe0574a040 | 139 | { |
quevedo | 1:399414d48048 | 140 | char cmd[15]; |
quevedo | 1:399414d48048 | 141 | strcpy(cmd, "AT+CWMODE="); |
quevedo | 1:399414d48048 | 142 | mode = mode + 0x30; // Converts number into corresponding ASCII character |
quevedo | 1:399414d48048 | 143 | AddChar(cmd, mode); // Completes command string |
quevedo | 1:399414d48048 | 144 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 145 | } |
Pairam | 4:8dfe0574a040 | 146 | void ESP8266::Check(char * i) |
Pairam | 4:8dfe0574a040 | 147 | { |
Pairam | 4:8dfe0574a040 | 148 | char cmd[15]; |
Pairam | 4:8dfe0574a040 | 149 | if(comm.readable()) { |
Pairam | 4:8dfe0574a040 | 150 | comm.gets(cmd,100);} |
Pairam | 4:8dfe0574a040 | 151 | strcpy(i,cmd); |
Pairam | 4:8dfe0574a040 | 152 | |
Pairam | 4:8dfe0574a040 | 153 | } |
quevedo | 1:399414d48048 | 154 | |
quevedo | 1:399414d48048 | 155 | // Quits the AP |
Pairam | 4:8dfe0574a040 | 156 | void ESP8266::Quit(void) |
Pairam | 4:8dfe0574a040 | 157 | { |
quevedo | 1:399414d48048 | 158 | char cmd[15]; |
quevedo | 1:399414d48048 | 159 | strcpy(cmd, "AT+CWQAP"); |
quevedo | 1:399414d48048 | 160 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 161 | } |
quevedo | 1:399414d48048 | 162 | |
quevedo | 1:399414d48048 | 163 | // Sets single connection |
Pairam | 4:8dfe0574a040 | 164 | void ESP8266::SetSingle(void) |
Pairam | 4:8dfe0574a040 | 165 | { |
quevedo | 1:399414d48048 | 166 | char cmd[15]; |
quevedo | 1:399414d48048 | 167 | strcpy(cmd, "AT+CIPMUX=0"); |
quevedo | 1:399414d48048 | 168 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 169 | } |
quevedo | 1:399414d48048 | 170 | |
quevedo | 1:399414d48048 | 171 | // Sets multiple connection |
Pairam | 4:8dfe0574a040 | 172 | void ESP8266::SetMultiple(void) |
Pairam | 4:8dfe0574a040 | 173 | { |
quevedo | 1:399414d48048 | 174 | char rs[15]; |
quevedo | 1:399414d48048 | 175 | strcpy(rs, "AT+CIPMUX=1"); |
quevedo | 1:399414d48048 | 176 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 177 | } |
Pairam | 4:8dfe0574a040 | 178 | void ESP8266::Close(void) |
Pairam | 4:8dfe0574a040 | 179 | { |
Pairam | 4:8dfe0574a040 | 180 | char rs[15]; |
Pairam | 4:8dfe0574a040 | 181 | strcpy(rs, "AT+CIPCLOSE"); |
Pairam | 4:8dfe0574a040 | 182 | SendCMD(rs); |
Pairam | 4:8dfe0574a040 | 183 | } |
Pairam | 4:8dfe0574a040 | 184 | void ESP8266::Disconnect(void) |
Pairam | 4:8dfe0574a040 | 185 | { |
Pairam | 4:8dfe0574a040 | 186 | char rs[15]; |
Pairam | 4:8dfe0574a040 | 187 | strcpy(rs, "AT+CWQAP"); |
Pairam | 4:8dfe0574a040 | 188 | SendCMD(rs); |
Pairam | 4:8dfe0574a040 | 189 | } |
quevedo | 2:77388e8f0697 | 190 | |
quevedo | 2:77388e8f0697 | 191 | // Gets connection status. Parameter: string to contain status |
Pairam | 4:8dfe0574a040 | 192 | void ESP8266::GetConnStatus(char * st) |
Pairam | 4:8dfe0574a040 | 193 | { |
quevedo | 2:77388e8f0697 | 194 | char cmd[15]; |
quevedo | 2:77388e8f0697 | 195 | strcpy(cmd, "AT+CIPSTATUS"); |
quevedo | 2:77388e8f0697 | 196 | SendCMD(cmd); |
quevedo | 2:77388e8f0697 | 197 | RcvReply(st, 2000); |
quevedo | 2:77388e8f0697 | 198 | } |
quevedo | 2:77388e8f0697 | 199 | |
quevedo | 2:77388e8f0697 | 200 | // Starts server mode. Parameter: port to be used |
Pairam | 4:8dfe0574a040 | 201 | void ESP8266::StartServerMode(int port) |
Pairam | 4:8dfe0574a040 | 202 | { |
quevedo | 2:77388e8f0697 | 203 | char rs[25]; |
quevedo | 2:77388e8f0697 | 204 | char t[4]; |
quevedo | 2:77388e8f0697 | 205 | strcpy(rs, "AT+CIPSERVER=1,"); |
quevedo | 2:77388e8f0697 | 206 | itoa(port, t); |
quevedo | 2:77388e8f0697 | 207 | strcat(rs, t); |
quevedo | 2:77388e8f0697 | 208 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 209 | } |
quevedo | 2:77388e8f0697 | 210 | |
quevedo | 2:77388e8f0697 | 211 | // Close server mode. |
Pairam | 4:8dfe0574a040 | 212 | void ESP8266::CloseServerMode(void) |
Pairam | 4:8dfe0574a040 | 213 | { |
quevedo | 2:77388e8f0697 | 214 | char rs[20]; |
quevedo | 2:77388e8f0697 | 215 | strcpy(rs, "AT+CIPSERVER=0"); |
quevedo | 2:77388e8f0697 | 216 | SendCMD(rs); |
janhavi | 3:4f24e7e803a1 | 217 | } |
janhavi | 3:4f24e7e803a1 | 218 | |
Pairam | 4:8dfe0574a040 | 219 | void ESP8266::setTransparent(void) |
Pairam | 4:8dfe0574a040 | 220 | { |
janhavi | 3:4f24e7e803a1 | 221 | char rs[20]; |
janhavi | 3:4f24e7e803a1 | 222 | strcpy(rs, "AT+CIPMODE=0"); |
janhavi | 3:4f24e7e803a1 | 223 | SendCMD(rs); |
janhavi | 3:4f24e7e803a1 | 224 | } |
janhavi | 3:4f24e7e803a1 | 225 | |
Pairam | 4:8dfe0574a040 | 226 | void ESP8266::startTCPConn(char *IP, int port, char * data ) |
Pairam | 4:8dfe0574a040 | 227 | { |
janhavi | 3:4f24e7e803a1 | 228 | char rs[100]; |
Pairam | 4:8dfe0574a040 | 229 | sprintf(rs, "AT+CIPSTART=4,\"TCP\",\"%s\",%d", IP, port); |
janhavi | 3:4f24e7e803a1 | 230 | SendCMD(rs); |
Pairam | 4:8dfe0574a040 | 231 | RcvReply(data, 5000); |
Pairam | 4:8dfe0574a040 | 232 | |
janhavi | 3:4f24e7e803a1 | 233 | } |
janhavi | 3:4f24e7e803a1 | 234 | |
janhavi | 3:4f24e7e803a1 | 235 | void ESP8266::sendURL(char *URL, char *command){ |
janhavi | 3:4f24e7e803a1 | 236 | char url[300], snd[300], http_cmd[300]; |
janhavi | 3:4f24e7e803a1 | 237 | |
janhavi | 3:4f24e7e803a1 | 238 | strcpy(http_cmd, HTTPCMD); |
janhavi | 3:4f24e7e803a1 | 239 | |
janhavi | 3:4f24e7e803a1 | 240 | strcat(http_cmd, URL); |
janhavi | 3:4f24e7e803a1 | 241 | |
janhavi | 3:4f24e7e803a1 | 242 | strcpy(url, http_cmd); |
Pairam | 4:8dfe0574a040 | 243 | sprintf(snd,"AT+CIPSEND=4,%d",strlen(url)); |
janhavi | 3:4f24e7e803a1 | 244 | strcpy(command, url); |
janhavi | 3:4f24e7e803a1 | 245 | SendCMD(snd); |
janhavi | 3:4f24e7e803a1 | 246 | SendCMD(url); |
janhavi | 3:4f24e7e803a1 | 247 | } |
Pairam | 4:8dfe0574a040 | 248 | |
Pairam | 4:8dfe0574a040 | 249 | |
Pairam | 4:8dfe0574a040 | 250 | void ESP8266::sendURL(char *URL, char *command, char * data) |
Pairam | 4:8dfe0574a040 | 251 | { |
Pairam | 4:8dfe0574a040 | 252 | char url[300], snd[300], http_cmd[300]; |
Pairam | 4:8dfe0574a040 | 253 | |
Pairam | 4:8dfe0574a040 | 254 | strcpy(http_cmd, HTTPCMD); |
Pairam | 4:8dfe0574a040 | 255 | strcat(http_cmd, URL); |
Pairam | 4:8dfe0574a040 | 256 | strcpy(url, http_cmd); |
janhavi | 3:4f24e7e803a1 | 257 | |
Pairam | 4:8dfe0574a040 | 258 | |
Pairam | 4:8dfe0574a040 | 259 | sprintf(snd,"AT+CIPSEND=4,%d",strlen(url)); |
Pairam | 4:8dfe0574a040 | 260 | strcpy(command, url); |
Pairam | 4:8dfe0574a040 | 261 | SendCMD(snd); |
Pairam | 4:8dfe0574a040 | 262 | SendCMD(url); |
Pairam | 4:8dfe0574a040 | 263 | RcvReply(data, 15000); |
Pairam | 4:8dfe0574a040 | 264 | } |
Pairam | 4:8dfe0574a040 | 265 | |
Pairam | 4:8dfe0574a040 | 266 |