Connect Wi-Fi
Dependencies: mbed
ESP8266.cpp@2:6738db3eb2ed, 2017-12-09 (annotated)
- Committer:
- chanagan2540
- Date:
- Sat Dec 09 22:20:18 2017 +0000
- Revision:
- 2:6738db3eb2ed
- Parent:
- 1:c0fbff97b740
- Child:
- 3:e1042768703b
esp8266
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chanagan2540 | 1:c0fbff97b740 | 1 | #include "ESP8266.h" |
chanagan2540 | 1:c0fbff97b740 | 2 | |
chanagan2540 | 1:c0fbff97b740 | 3 | // Constructor |
chanagan2540 | 1:c0fbff97b740 | 4 | ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) { |
chanagan2540 | 1:c0fbff97b740 | 5 | comm.baud(br); |
chanagan2540 | 1:c0fbff97b740 | 6 | } |
chanagan2540 | 1:c0fbff97b740 | 7 | |
chanagan2540 | 1:c0fbff97b740 | 8 | // Destructor |
chanagan2540 | 1:c0fbff97b740 | 9 | ESP8266::~ESP8266() { } |
chanagan2540 | 1:c0fbff97b740 | 10 | |
chanagan2540 | 1:c0fbff97b740 | 11 | void ESP8266::AddEOL(char * s) { |
chanagan2540 | 1:c0fbff97b740 | 12 | char k; |
chanagan2540 | 1:c0fbff97b740 | 13 | k = strlen(s); // Finds position of NULL character |
chanagan2540 | 1:c0fbff97b740 | 14 | s[k] = 0x0D; // switch NULL for <CR> |
chanagan2540 | 1:c0fbff97b740 | 15 | s[k + 1] = 0x0A; // Add <LF> |
chanagan2540 | 1:c0fbff97b740 | 16 | s[k + 2] = 0; // Add NULL at the end |
chanagan2540 | 1:c0fbff97b740 | 17 | } |
chanagan2540 | 1:c0fbff97b740 | 18 | |
chanagan2540 | 1:c0fbff97b740 | 19 | void ESP8266::AddChar(char * s, char c) { |
chanagan2540 | 1:c0fbff97b740 | 20 | char k; |
chanagan2540 | 1:c0fbff97b740 | 21 | k = strlen(s); |
chanagan2540 | 1:c0fbff97b740 | 22 | s[k] = c; |
chanagan2540 | 1:c0fbff97b740 | 23 | s[k + 1] = 0; |
chanagan2540 | 1:c0fbff97b740 | 24 | } |
chanagan2540 | 1:c0fbff97b740 | 25 | |
chanagan2540 | 2:6738db3eb2ed | 26 | void ESP8266::itoa(int n, char * s) { |
chanagan2540 | 2:6738db3eb2ed | 27 | char k = 0; |
chanagan2540 | 2:6738db3eb2ed | 28 | char r[11]; |
chanagan2540 | 2:6738db3eb2ed | 29 | |
chanagan2540 | 2:6738db3eb2ed | 30 | if(n == 0) { |
chanagan2540 | 2:6738db3eb2ed | 31 | s[0] = '0'; |
chanagan2540 | 2:6738db3eb2ed | 32 | s[1] = 0; |
chanagan2540 | 2:6738db3eb2ed | 33 | } else { |
chanagan2540 | 2:6738db3eb2ed | 34 | while(n != 0) { |
chanagan2540 | 2:6738db3eb2ed | 35 | r[k]= (n % 10) + '0'; |
chanagan2540 | 2:6738db3eb2ed | 36 | n = n / 10; |
chanagan2540 | 2:6738db3eb2ed | 37 | k++; |
chanagan2540 | 2:6738db3eb2ed | 38 | } |
chanagan2540 | 2:6738db3eb2ed | 39 | while(k > 0) { |
chanagan2540 | 2:6738db3eb2ed | 40 | s[n] = r[k - 1] + '0'; |
chanagan2540 | 2:6738db3eb2ed | 41 | n++; |
chanagan2540 | 2:6738db3eb2ed | 42 | k--; |
chanagan2540 | 2:6738db3eb2ed | 43 | } |
chanagan2540 | 2:6738db3eb2ed | 44 | s[n] = 0; |
chanagan2540 | 2:6738db3eb2ed | 45 | } |
chanagan2540 | 2:6738db3eb2ed | 46 | } |
chanagan2540 | 2:6738db3eb2ed | 47 | |
chanagan2540 | 1:c0fbff97b740 | 48 | void ESP8266::SendCMD(char * s) { |
chanagan2540 | 1:c0fbff97b740 | 49 | AddEOL(s); |
chanagan2540 | 1:c0fbff97b740 | 50 | comm.printf("%s", s); |
chanagan2540 | 1:c0fbff97b740 | 51 | } |
chanagan2540 | 1:c0fbff97b740 | 52 | |
chanagan2540 | 1:c0fbff97b740 | 53 | void ESP8266::Reset(void) { |
chanagan2540 | 1:c0fbff97b740 | 54 | char rs[10]; |
chanagan2540 | 1:c0fbff97b740 | 55 | strcpy(rs, "AT+RST"); |
chanagan2540 | 1:c0fbff97b740 | 56 | SendCMD(rs); |
chanagan2540 | 1:c0fbff97b740 | 57 | } |
chanagan2540 | 1:c0fbff97b740 | 58 | |
chanagan2540 | 1:c0fbff97b740 | 59 | void ESP8266::RcvReply(char * r, int to) { |
chanagan2540 | 1:c0fbff97b740 | 60 | Timer t; |
chanagan2540 | 1:c0fbff97b740 | 61 | bool ended = 0; |
chanagan2540 | 1:c0fbff97b740 | 62 | char c; |
chanagan2540 | 1:c0fbff97b740 | 63 | |
chanagan2540 | 1:c0fbff97b740 | 64 | strcpy(r, ""); |
chanagan2540 | 1:c0fbff97b740 | 65 | t.start(); |
chanagan2540 | 1:c0fbff97b740 | 66 | while(!ended) { |
chanagan2540 | 1:c0fbff97b740 | 67 | if(comm.readable()) { |
chanagan2540 | 1:c0fbff97b740 | 68 | c = comm.getc(); |
chanagan2540 | 1:c0fbff97b740 | 69 | AddChar(r, c); |
chanagan2540 | 1:c0fbff97b740 | 70 | t.start(); |
chanagan2540 | 1:c0fbff97b740 | 71 | } |
chanagan2540 | 1:c0fbff97b740 | 72 | if(t.read_ms() > to) { |
chanagan2540 | 1:c0fbff97b740 | 73 | ended = 1; |
chanagan2540 | 1:c0fbff97b740 | 74 | } |
chanagan2540 | 1:c0fbff97b740 | 75 | } |
chanagan2540 | 1:c0fbff97b740 | 76 | AddChar(r, 0x00); |
chanagan2540 | 1:c0fbff97b740 | 77 | } |
chanagan2540 | 1:c0fbff97b740 | 78 | |
chanagan2540 | 1:c0fbff97b740 | 79 | void ESP8266::SetMode(char mode) { |
chanagan2540 | 1:c0fbff97b740 | 80 | char cmd[15]; |
chanagan2540 | 1:c0fbff97b740 | 81 | strcpy(cmd, "AT+CWMODE="); |
chanagan2540 | 1:c0fbff97b740 | 82 | mode = mode + 0x30; // Converts number into corresponding ASCII character |
chanagan2540 | 1:c0fbff97b740 | 83 | AddChar(cmd, mode); // Completes command string |
chanagan2540 | 1:c0fbff97b740 | 84 | SendCMD(cmd); |
chanagan2540 | 1:c0fbff97b740 | 85 | } |
chanagan2540 | 1:c0fbff97b740 | 86 | |
chanagan2540 | 1:c0fbff97b740 | 87 | void ESP8266::GetList(char * l) { |
chanagan2540 | 1:c0fbff97b740 | 88 | char rs[15]; |
chanagan2540 | 1:c0fbff97b740 | 89 | strcpy(rs, "AT+CWLAP"); |
chanagan2540 | 1:c0fbff97b740 | 90 | SendCMD(rs); |
chanagan2540 | 1:c0fbff97b740 | 91 | RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
chanagan2540 | 1:c0fbff97b740 | 92 | } |
chanagan2540 | 1:c0fbff97b740 | 93 | |
chanagan2540 | 1:c0fbff97b740 | 94 | void ESP8266::Join(char * id, char * pwd) { |
chanagan2540 | 1:c0fbff97b740 | 95 | char cmd[255]; |
chanagan2540 | 1:c0fbff97b740 | 96 | strcpy(cmd, "AT+CWJAP="); |
chanagan2540 | 1:c0fbff97b740 | 97 | AddChar(cmd, 0x22); |
chanagan2540 | 1:c0fbff97b740 | 98 | strcat(cmd, id); |
chanagan2540 | 1:c0fbff97b740 | 99 | AddChar(cmd, 0x22); |
chanagan2540 | 1:c0fbff97b740 | 100 | AddChar(cmd, 0x2C); |
chanagan2540 | 1:c0fbff97b740 | 101 | AddChar(cmd, 0x22); |
chanagan2540 | 1:c0fbff97b740 | 102 | strcat(cmd, pwd); |
chanagan2540 | 1:c0fbff97b740 | 103 | AddChar(cmd, 0x22); |
chanagan2540 | 1:c0fbff97b740 | 104 | SendCMD(cmd); |
chanagan2540 | 1:c0fbff97b740 | 105 | } |
chanagan2540 | 1:c0fbff97b740 | 106 | |
chanagan2540 | 2:6738db3eb2ed | 107 | void ESP8266::Check(char * con) { |
chanagan2540 | 2:6738db3eb2ed | 108 | char cmd[15]; |
chanagan2540 | 2:6738db3eb2ed | 109 | strcpy(cmd, "AT+CWJAP?"); |
chanagan2540 | 2:6738db3eb2ed | 110 | SendCMD(cmd); |
chanagan2540 | 2:6738db3eb2ed | 111 | RcvReply(con, 2000); |
chanagan2540 | 2:6738db3eb2ed | 112 | } |
chanagan2540 | 2:6738db3eb2ed | 113 | |
chanagan2540 | 1:c0fbff97b740 | 114 | void ESP8266::GetIP(char * ip) { |
chanagan2540 | 1:c0fbff97b740 | 115 | char cmd[15]; |
chanagan2540 | 1:c0fbff97b740 | 116 | strcpy(cmd, "AT+CIFSR"); |
chanagan2540 | 1:c0fbff97b740 | 117 | SendCMD(cmd); |
chanagan2540 | 1:c0fbff97b740 | 118 | RcvReply(ip, 2000); |
chanagan2540 | 1:c0fbff97b740 | 119 | } |
chanagan2540 | 2:6738db3eb2ed | 120 | |
chanagan2540 | 2:6738db3eb2ed | 121 | void ESP8266::SetMultiple(void) { |
chanagan2540 | 2:6738db3eb2ed | 122 | char rs[15]; |
chanagan2540 | 2:6738db3eb2ed | 123 | strcpy(rs, "AT+CIPMUX=1"); |
chanagan2540 | 2:6738db3eb2ed | 124 | SendCMD(rs); |
chanagan2540 | 2:6738db3eb2ed | 125 | } |
chanagan2540 | 2:6738db3eb2ed | 126 | |
chanagan2540 | 2:6738db3eb2ed | 127 | void ESP8266::StartServerMode(int port) { |
chanagan2540 | 2:6738db3eb2ed | 128 | char rs[25]; |
chanagan2540 | 2:6738db3eb2ed | 129 | char t[4]; |
chanagan2540 | 2:6738db3eb2ed | 130 | strcpy(rs, "AT+CIPSERVER=1,"); |
chanagan2540 | 2:6738db3eb2ed | 131 | itoa(port, t); |
chanagan2540 | 2:6738db3eb2ed | 132 | strcat(rs, t); |
chanagan2540 | 2:6738db3eb2ed | 133 | SendCMD(rs); |
chanagan2540 | 2:6738db3eb2ed | 134 | } |