ESP8266 library to get and send data.
Fork of ESP8266 by
ESP8266.cpp@5:5cc7894bb8cb, 2016-10-21 (annotated)
- Committer:
- Wosser1sProductions
- Date:
- Fri Oct 21 21:24:11 2016 +0000
- Revision:
- 5:5cc7894bb8cb
- Parent:
- 4:c05f7a1b4e47
Added deepsleep command (cannot wake up by pulling CH_PD down and then back up)
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) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 13 | char k = strlen(s); // Finds position of NULL character |
Wosser1sProductions | 4:c05f7a1b4e47 | 14 | s[k] = '\r'; // switch NULL for <CR> |
Wosser1sProductions | 4:c05f7a1b4e47 | 15 | s[k + 1] = '\n'; // Add <LF> |
Wosser1sProductions | 4:c05f7a1b4e47 | 16 | s[k + 2] = '\0'; // Add NULL at the end |
quevedo | 0:e58f27687450 | 17 | } |
quevedo | 0:e58f27687450 | 18 | |
quevedo | 0:e58f27687450 | 19 | // Add one ASCII character at the end of the string |
quevedo | 0:e58f27687450 | 20 | void ESP8266::AddChar(char * s, char c) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 21 | char k = strlen(s); |
Wosser1sProductions | 4:c05f7a1b4e47 | 22 | s[k] = c; |
Wosser1sProductions | 4:c05f7a1b4e47 | 23 | s[k + 1] = '\0'; |
quevedo | 2:77388e8f0697 | 24 | } |
quevedo | 2:77388e8f0697 | 25 | |
quevedo | 0:e58f27687450 | 26 | // Sends command to ESP8266. Receives the command string |
Wosser1sProductions | 4:c05f7a1b4e47 | 27 | void ESP8266::SendCMD(char* s) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 28 | comm.printf("%s%s", s, CMD_END); |
quevedo | 0:e58f27687450 | 29 | } |
quevedo | 0:e58f27687450 | 30 | |
quevedo | 0:e58f27687450 | 31 | // Resets the ESP8266 |
quevedo | 0:e58f27687450 | 32 | void ESP8266::Reset(void) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 33 | SendCMD("AT+RST"); |
quevedo | 0:e58f27687450 | 34 | } |
quevedo | 0:e58f27687450 | 35 | |
quevedo | 0:e58f27687450 | 36 | // Receive reply until no character is received after a given timeout in miliseconds |
Wosser1sProductions | 4:c05f7a1b4e47 | 37 | bool ESP8266::RcvReply(char* r, int to) { |
quevedo | 0:e58f27687450 | 38 | Timer t; |
quevedo | 0:e58f27687450 | 39 | strcpy(r, ""); |
quevedo | 0:e58f27687450 | 40 | t.start(); |
Wosser1sProductions | 4:c05f7a1b4e47 | 41 | |
Wosser1sProductions | 4:c05f7a1b4e47 | 42 | do { |
quevedo | 0:e58f27687450 | 43 | if(comm.readable()) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 44 | AddChar(r, comm.getc()); |
quevedo | 0:e58f27687450 | 45 | t.start(); |
quevedo | 0:e58f27687450 | 46 | } |
Wosser1sProductions | 4:c05f7a1b4e47 | 47 | } while(t.read_ms() < to); |
Wosser1sProductions | 4:c05f7a1b4e47 | 48 | |
Wosser1sProductions | 4:c05f7a1b4e47 | 49 | AddChar(r, '\0'); |
Wosser1sProductions | 4:c05f7a1b4e47 | 50 | return r[0] != '\0'; |
quevedo | 0:e58f27687450 | 51 | } |
quevedo | 0:e58f27687450 | 52 | |
quevedo | 0:e58f27687450 | 53 | // Gets the AP list. Parameter: the string to receive the list |
Wosser1sProductions | 4:c05f7a1b4e47 | 54 | bool ESP8266::GetList(char *l) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 55 | SendCMD("AT+CWLAP"); |
Wosser1sProductions | 4:c05f7a1b4e47 | 56 | return RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
quevedo | 0:e58f27687450 | 57 | } |
quevedo | 0:e58f27687450 | 58 | |
quevedo | 0:e58f27687450 | 59 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
Wosser1sProductions | 4:c05f7a1b4e47 | 60 | void ESP8266::Join(char *id, char *pwd) { |
quevedo | 0:e58f27687450 | 61 | char cmd[255]; |
Wosser1sProductions | 4:c05f7a1b4e47 | 62 | sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", id, pwd); |
quevedo | 0:e58f27687450 | 63 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 64 | } |
quevedo | 0:e58f27687450 | 65 | |
quevedo | 0:e58f27687450 | 66 | // Gets ESP IP. Parameter: string to contain IP |
Wosser1sProductions | 4:c05f7a1b4e47 | 67 | bool ESP8266::GetIP(char *ip) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 68 | SendCMD("AT+CIFSR"); |
Wosser1sProductions | 4:c05f7a1b4e47 | 69 | return RcvReply(ip, 2000); |
quevedo | 1:399414d48048 | 70 | } |
quevedo | 1:399414d48048 | 71 | |
quevedo | 1:399414d48048 | 72 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
Wosser1sProductions | 4:c05f7a1b4e47 | 73 | void ESP8266::SetMode(WiFiMode mode) { |
quevedo | 1:399414d48048 | 74 | char cmd[15]; |
quevedo | 1:399414d48048 | 75 | strcpy(cmd, "AT+CWMODE="); |
Wosser1sProductions | 4:c05f7a1b4e47 | 76 | AddChar(cmd, mode + '0'); // Completes command string |
quevedo | 1:399414d48048 | 77 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 78 | } |
quevedo | 1:399414d48048 | 79 | |
quevedo | 1:399414d48048 | 80 | // Quits the AP |
quevedo | 1:399414d48048 | 81 | void ESP8266::Quit(void) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 82 | SendCMD("AT+CWQAP"); |
quevedo | 1:399414d48048 | 83 | } |
quevedo | 1:399414d48048 | 84 | |
quevedo | 1:399414d48048 | 85 | // Sets single connection |
quevedo | 1:399414d48048 | 86 | void ESP8266::SetSingle(void) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 87 | SendCMD("AT+CIPMUX=0"); |
quevedo | 1:399414d48048 | 88 | } |
quevedo | 1:399414d48048 | 89 | |
quevedo | 1:399414d48048 | 90 | // Sets multiple connection |
quevedo | 1:399414d48048 | 91 | void ESP8266::SetMultiple(void) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 92 | SendCMD("AT+CIPMUX=1"); |
quevedo | 2:77388e8f0697 | 93 | } |
quevedo | 2:77388e8f0697 | 94 | |
quevedo | 2:77388e8f0697 | 95 | // Gets connection status. Parameter: string to contain status |
Wosser1sProductions | 4:c05f7a1b4e47 | 96 | bool ESP8266::GetConnStatus(char * st) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 97 | SendCMD("AT+CIPSTATUS"); |
Wosser1sProductions | 4:c05f7a1b4e47 | 98 | return RcvReply(st, 2000); |
quevedo | 2:77388e8f0697 | 99 | } |
quevedo | 2:77388e8f0697 | 100 | |
quevedo | 2:77388e8f0697 | 101 | // Starts server mode. Parameter: port to be used |
quevedo | 2:77388e8f0697 | 102 | void ESP8266::StartServerMode(int port) { |
quevedo | 2:77388e8f0697 | 103 | char rs[25]; |
Wosser1sProductions | 4:c05f7a1b4e47 | 104 | sprintf(rs, "AT+CIPSERVER=1,%d", port); |
quevedo | 2:77388e8f0697 | 105 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 106 | } |
quevedo | 2:77388e8f0697 | 107 | |
quevedo | 2:77388e8f0697 | 108 | // Close server mode. |
quevedo | 2:77388e8f0697 | 109 | void ESP8266::CloseServerMode(void) { |
Wosser1sProductions | 4:c05f7a1b4e47 | 110 | SendCMD("AT+CIPSERVER=0"); |
janhavi | 3:4f24e7e803a1 | 111 | } |
janhavi | 3:4f24e7e803a1 | 112 | |
janhavi | 3:4f24e7e803a1 | 113 | void ESP8266::setTransparent(void){ |
Wosser1sProductions | 4:c05f7a1b4e47 | 114 | SendCMD("AT+CIPMODE=0"); |
janhavi | 3:4f24e7e803a1 | 115 | } |
janhavi | 3:4f24e7e803a1 | 116 | |
janhavi | 3:4f24e7e803a1 | 117 | void ESP8266::startTCPConn(char *IP, int port){ |
janhavi | 3:4f24e7e803a1 | 118 | char rs[100]; |
Wosser1sProductions | 4:c05f7a1b4e47 | 119 | sprintf(rs, "AT+CIPSTART=0,\"TCP\",\"%s\",%d", IP, port); |
janhavi | 3:4f24e7e803a1 | 120 | SendCMD(rs); |
janhavi | 3:4f24e7e803a1 | 121 | } |
janhavi | 3:4f24e7e803a1 | 122 | |
Wosser1sProductions | 4:c05f7a1b4e47 | 123 | void ESP8266::sendURL(char *URL, char* IP, char *command){ |
Wosser1sProductions | 4:c05f7a1b4e47 | 124 | char snd[20], http_cmd[300]; |
Wosser1sProductions | 4:c05f7a1b4e47 | 125 | |
Wosser1sProductions | 4:c05f7a1b4e47 | 126 | sprintf(http_cmd, "GET %s HTTP/1.1%sHost: %s%sConnection: close%s%s", URL, CMD_END, IP, CMD_END, CMD_END, CMD_END); |
Wosser1sProductions | 4:c05f7a1b4e47 | 127 | sprintf(snd,"AT+CIPSEND=0,%d",strlen(http_cmd)); |
Wosser1sProductions | 4:c05f7a1b4e47 | 128 | strcpy(command, http_cmd); |
janhavi | 3:4f24e7e803a1 | 129 | |
janhavi | 3:4f24e7e803a1 | 130 | SendCMD(snd); |
Wosser1sProductions | 4:c05f7a1b4e47 | 131 | wait(1); |
Wosser1sProductions | 4:c05f7a1b4e47 | 132 | SendCMD(http_cmd); |
janhavi | 3:4f24e7e803a1 | 133 | } |
janhavi | 3:4f24e7e803a1 | 134 | |
Wosser1sProductions | 5:5cc7894bb8cb | 135 | void ESP8266::deepsleep(size_t ms) { |
Wosser1sProductions | 5:5cc7894bb8cb | 136 | char snd[20]; |
Wosser1sProductions | 5:5cc7894bb8cb | 137 | sprintf(snd, "AT+GSLP=%d", ms); |
Wosser1sProductions | 5:5cc7894bb8cb | 138 | SendCMD(snd); |
Wosser1sProductions | 5:5cc7894bb8cb | 139 | } |
janhavi | 3:4f24e7e803a1 | 140 |