stm32l432 esp8266,MQ13,MQ7,SSD1306,SD1303,HC04
Dependents: Nucleo_SSD1306_DS1302_ESP8266_AM2320_BME280
Fork of ESP8266 by
ESP8266.cpp@3:a1da4dcd891a, 2017-05-11 (annotated)
- Committer:
- dadangjia
- Date:
- Thu May 11 16:55:21 2017 +0000
- Revision:
- 3:a1da4dcd891a
- Parent:
- 2:77388e8f0697
stm32l432 iot project
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 | strcpy(r, ""); |
quevedo | 0:e58f27687450 | 70 | t.start(); |
quevedo | 0:e58f27687450 | 71 | while(!ended) { |
quevedo | 0:e58f27687450 | 72 | if(comm.readable()) { |
quevedo | 0:e58f27687450 | 73 | c = comm.getc(); |
quevedo | 0:e58f27687450 | 74 | AddChar(r, c); |
quevedo | 0:e58f27687450 | 75 | t.start(); |
quevedo | 0:e58f27687450 | 76 | } |
quevedo | 0:e58f27687450 | 77 | if(t.read_ms() > to) { |
quevedo | 0:e58f27687450 | 78 | ended = 1; |
quevedo | 0:e58f27687450 | 79 | } |
quevedo | 0:e58f27687450 | 80 | } |
quevedo | 0:e58f27687450 | 81 | AddChar(r, 0x00); |
quevedo | 0:e58f27687450 | 82 | } |
quevedo | 0:e58f27687450 | 83 | |
quevedo | 0:e58f27687450 | 84 | // Gets the AP list. Parameter: the string to receive the list |
quevedo | 0:e58f27687450 | 85 | void ESP8266::GetList(char * l) { |
quevedo | 0:e58f27687450 | 86 | char rs[15]; |
quevedo | 0:e58f27687450 | 87 | strcpy(rs, "AT+CWLAP"); |
quevedo | 0:e58f27687450 | 88 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 89 | RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
quevedo | 0:e58f27687450 | 90 | } |
quevedo | 0:e58f27687450 | 91 | |
quevedo | 0:e58f27687450 | 92 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
quevedo | 0:e58f27687450 | 93 | void ESP8266::Join(char * id, char * pwd) { |
quevedo | 0:e58f27687450 | 94 | char cmd[255]; |
quevedo | 0:e58f27687450 | 95 | strcpy(cmd, "AT+CWJAP="); |
quevedo | 0:e58f27687450 | 96 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 97 | strcat(cmd, id); |
quevedo | 0:e58f27687450 | 98 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 99 | AddChar(cmd, 0x2C); |
quevedo | 0:e58f27687450 | 100 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 101 | strcat(cmd, pwd); |
quevedo | 0:e58f27687450 | 102 | AddChar(cmd, 0x22); |
quevedo | 0:e58f27687450 | 103 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 104 | } |
quevedo | 0:e58f27687450 | 105 | |
quevedo | 0:e58f27687450 | 106 | // Gets ESP IP. Parameter: string to contain IP |
quevedo | 0:e58f27687450 | 107 | void ESP8266::GetIP(char * ip) { |
quevedo | 0:e58f27687450 | 108 | char cmd[15]; |
quevedo | 0:e58f27687450 | 109 | strcpy(cmd, "AT+CIFSR"); |
quevedo | 0:e58f27687450 | 110 | SendCMD(cmd); |
quevedo | 0:e58f27687450 | 111 | RcvReply(ip, 2000); |
quevedo | 1:399414d48048 | 112 | } |
quevedo | 1:399414d48048 | 113 | |
quevedo | 1:399414d48048 | 114 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
quevedo | 1:399414d48048 | 115 | void ESP8266::SetMode(char mode) { |
quevedo | 1:399414d48048 | 116 | char cmd[15]; |
quevedo | 1:399414d48048 | 117 | strcpy(cmd, "AT+CWMODE="); |
quevedo | 1:399414d48048 | 118 | mode = mode + 0x30; // Converts number into corresponding ASCII character |
quevedo | 1:399414d48048 | 119 | AddChar(cmd, mode); // Completes command string |
quevedo | 1:399414d48048 | 120 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 121 | } |
quevedo | 1:399414d48048 | 122 | |
quevedo | 1:399414d48048 | 123 | // Quits the AP |
quevedo | 1:399414d48048 | 124 | void ESP8266::Quit(void) { |
quevedo | 1:399414d48048 | 125 | char cmd[15]; |
quevedo | 1:399414d48048 | 126 | strcpy(cmd, "AT+CWQAP"); |
quevedo | 1:399414d48048 | 127 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 128 | } |
quevedo | 1:399414d48048 | 129 | |
quevedo | 1:399414d48048 | 130 | // Sets single connection |
quevedo | 1:399414d48048 | 131 | void ESP8266::SetSingle(void) { |
quevedo | 1:399414d48048 | 132 | char cmd[15]; |
quevedo | 1:399414d48048 | 133 | strcpy(cmd, "AT+CIPMUX=0"); |
quevedo | 1:399414d48048 | 134 | SendCMD(cmd); |
quevedo | 1:399414d48048 | 135 | } |
quevedo | 1:399414d48048 | 136 | |
quevedo | 1:399414d48048 | 137 | // Sets multiple connection |
quevedo | 1:399414d48048 | 138 | void ESP8266::SetMultiple(void) { |
quevedo | 1:399414d48048 | 139 | char rs[15]; |
quevedo | 1:399414d48048 | 140 | strcpy(rs, "AT+CIPMUX=1"); |
quevedo | 1:399414d48048 | 141 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 142 | } |
quevedo | 2:77388e8f0697 | 143 | |
quevedo | 2:77388e8f0697 | 144 | // Gets connection status. Parameter: string to contain status |
quevedo | 2:77388e8f0697 | 145 | void ESP8266::GetConnStatus(char * st) { |
quevedo | 2:77388e8f0697 | 146 | char cmd[15]; |
quevedo | 2:77388e8f0697 | 147 | strcpy(cmd, "AT+CIPSTATUS"); |
quevedo | 2:77388e8f0697 | 148 | SendCMD(cmd); |
quevedo | 2:77388e8f0697 | 149 | RcvReply(st, 2000); |
quevedo | 2:77388e8f0697 | 150 | } |
quevedo | 2:77388e8f0697 | 151 | |
quevedo | 2:77388e8f0697 | 152 | // Starts server mode. Parameter: port to be used |
quevedo | 2:77388e8f0697 | 153 | void ESP8266::StartServerMode(int port) { |
quevedo | 2:77388e8f0697 | 154 | char rs[25]; |
quevedo | 2:77388e8f0697 | 155 | char t[4]; |
quevedo | 2:77388e8f0697 | 156 | strcpy(rs, "AT+CIPSERVER=1,"); |
quevedo | 2:77388e8f0697 | 157 | itoa(port, t); |
quevedo | 2:77388e8f0697 | 158 | strcat(rs, t); |
quevedo | 2:77388e8f0697 | 159 | SendCMD(rs); |
quevedo | 2:77388e8f0697 | 160 | } |
quevedo | 2:77388e8f0697 | 161 | |
quevedo | 2:77388e8f0697 | 162 | // Close server mode. |
quevedo | 2:77388e8f0697 | 163 | void ESP8266::CloseServerMode(void) { |
quevedo | 2:77388e8f0697 | 164 | char rs[20]; |
quevedo | 2:77388e8f0697 | 165 | strcpy(rs, "AT+CIPSERVER=0"); |
quevedo | 2:77388e8f0697 | 166 | SendCMD(rs); |
quevedo | 0:e58f27687450 | 167 | } |