pengfei wu
/
ESP8266Program
For test
ESP8266.h@35:90be2bc2a492, 2015-02-05 (annotated)
- Committer:
- shennongmin
- Date:
- Thu Feb 05 06:48:45 2015 +0000
- Revision:
- 35:90be2bc2a492
- Parent:
- 34:65cd9c6eedb8
compile okay and test okay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shennongmin | 32:91b3f3162c6a | 1 | /** |
shennongmin | 32:91b3f3162c6a | 2 | * @file ESP8266.h |
shennongmin | 32:91b3f3162c6a | 3 | */ |
shennongmin | 10:9d4ec0359a5c | 4 | #ifndef __ESP8266_H__ |
shennongmin | 10:9d4ec0359a5c | 5 | #define __ESP8266_H__ |
shennongmin | 4:962bf18523f4 | 6 | |
shennongmin | 7:a11e381ce969 | 7 | #include "mbed.h" |
shennongmin | 10:9d4ec0359a5c | 8 | #include "time.h" |
shennongmin | 10:9d4ec0359a5c | 9 | #include "ArduinoSerial.h" |
shennongmin | 7:a11e381ce969 | 10 | |
shennongmin | 34:65cd9c6eedb8 | 11 | /** |
shennongmin | 34:65cd9c6eedb8 | 12 | * The way of SoftAP encrypstion. |
shennongmin | 34:65cd9c6eedb8 | 13 | * |
shennongmin | 34:65cd9c6eedb8 | 14 | */ |
shennongmin | 22:f5d148878815 | 15 | typedef enum { |
shennongmin | 34:65cd9c6eedb8 | 16 | ESP8266_ECN_OPEN = 0, /**< OPEN */ |
shennongmin | 34:65cd9c6eedb8 | 17 | ESP8266_ECN_WEP = 1, /**< WEP */ |
shennongmin | 34:65cd9c6eedb8 | 18 | ESP8266_ECN_WAP_PSK = 2, /**< WAP_PSK */ |
shennongmin | 34:65cd9c6eedb8 | 19 | ESP8266_ECN_WAP2_PSK = 3, /**< WAP2_PSK */ |
shennongmin | 34:65cd9c6eedb8 | 20 | ESP8266_ECN_WAP_WAP2_PSK = 4, /**< WAP_WAP2_PSK */ |
shennongmin | 22:f5d148878815 | 21 | } ESP8266Encrypstion; |
shennongmin | 4:962bf18523f4 | 22 | |
shennongmin | 34:65cd9c6eedb8 | 23 | /** |
shennongmin | 34:65cd9c6eedb8 | 24 | * The ip protocol for communication. |
shennongmin | 34:65cd9c6eedb8 | 25 | * |
shennongmin | 34:65cd9c6eedb8 | 26 | * Only TCP or UPD supported. |
shennongmin | 34:65cd9c6eedb8 | 27 | */ |
shennongmin | 22:f5d148878815 | 28 | typedef enum { |
shennongmin | 34:65cd9c6eedb8 | 29 | ESP8266_COMM_UDP = 0, /**< UPD */ |
shennongmin | 34:65cd9c6eedb8 | 30 | ESP8266_COMM_TCP = 1, /**< TCP */ |
shennongmin | 35:90be2bc2a492 | 31 | } ESP8266CommType; |
shennongmin | 4:962bf18523f4 | 32 | |
shennongmin | 34:65cd9c6eedb8 | 33 | /** |
shennongmin | 34:65cd9c6eedb8 | 34 | * The work mode. |
shennongmin | 34:65cd9c6eedb8 | 35 | * |
shennongmin | 34:65cd9c6eedb8 | 36 | * Station | SoftAP | Station + SoftAP. |
shennongmin | 34:65cd9c6eedb8 | 37 | */ |
shennongmin | 22:f5d148878815 | 38 | typedef enum { |
shennongmin | 34:65cd9c6eedb8 | 39 | ESP8266_MODE_STA = 1, /**< Station */ |
shennongmin | 34:65cd9c6eedb8 | 40 | ESP8266_MODE_SAP = 2, /**< SoftAP */ |
shennongmin | 34:65cd9c6eedb8 | 41 | ESP8266_MODE_STA_SAP = 3, /**< Station + SoftAP */ |
shennongmin | 22:f5d148878815 | 42 | } ESP8266WorkMode; |
shennongmin | 4:962bf18523f4 | 43 | |
shennongmin | 34:65cd9c6eedb8 | 44 | /** |
shennongmin | 35:90be2bc2a492 | 45 | * Mux single or multiple connection mode. |
shennongmin | 34:65cd9c6eedb8 | 46 | */ |
shennongmin | 28:c25aff31dcc5 | 47 | typedef enum { |
shennongmin | 35:90be2bc2a492 | 48 | ESP8266_MUX_SINGLE = 0, /**< Single mode */ |
shennongmin | 35:90be2bc2a492 | 49 | ESP8266_MUX_MULTIPLE = 1, /**< Multiple mode */ |
shennongmin | 35:90be2bc2a492 | 50 | } ESP8266Mux; |
shennongmin | 34:65cd9c6eedb8 | 51 | |
shennongmin | 32:91b3f3162c6a | 52 | /** |
shennongmin | 34:65cd9c6eedb8 | 53 | * Provide methods to manipulate ESP8266 for internet access via WiFi. |
shennongmin | 32:91b3f3162c6a | 54 | */ |
shennongmin | 9:95a0ce98e12b | 55 | class ESP8266 |
shennongmin | 4:962bf18523f4 | 56 | { |
shennongmin | 29:75185b05d6a4 | 57 | public: |
shennongmin | 29:75185b05d6a4 | 58 | |
shennongmin | 29:75185b05d6a4 | 59 | /** |
shennongmin | 29:75185b05d6a4 | 60 | * Constructor. |
shennongmin | 29:75185b05d6a4 | 61 | * |
shennongmin | 29:75185b05d6a4 | 62 | * Need an serial port for communication between MCU and ESP8266. |
shennongmin | 29:75185b05d6a4 | 63 | * |
shennongmin | 29:75185b05d6a4 | 64 | * @param tx - the TX of serial port. |
shennongmin | 29:75185b05d6a4 | 65 | * @param rx - the RX of serial port. |
shennongmin | 29:75185b05d6a4 | 66 | * @param baud_rate - the baud rate of communication(default 9600). |
shennongmin | 29:75185b05d6a4 | 67 | */ |
shennongmin | 29:75185b05d6a4 | 68 | ESP8266(PinName tx, PinName rx, int baud_rate = 9600); |
shennongmin | 4:962bf18523f4 | 69 | |
shennongmin | 31:ab3e7ca014c1 | 70 | /** |
shennongmin | 31:ab3e7ca014c1 | 71 | * Set as station mode and join in ssid with pwd. |
shennongmin | 31:ab3e7ca014c1 | 72 | * |
shennongmin | 31:ab3e7ca014c1 | 73 | * @param ssid - SSID of AP to join in. |
shennongmin | 31:ab3e7ca014c1 | 74 | * @param pwd - PASSWORD of AP to join in. |
shennongmin | 31:ab3e7ca014c1 | 75 | * @return true if success, false for failure. |
shennongmin | 31:ab3e7ca014c1 | 76 | */ |
shennongmin | 30:46a560b71e61 | 77 | bool setStationMode(String ssid, String pwd); |
shennongmin | 31:ab3e7ca014c1 | 78 | |
shennongmin | 31:ab3e7ca014c1 | 79 | /** |
shennongmin | 31:ab3e7ca014c1 | 80 | * Set as softap mode and start softap. |
shennongmin | 31:ab3e7ca014c1 | 81 | * |
shennongmin | 31:ab3e7ca014c1 | 82 | * @param sap_ssid - SSID of its self softap. |
shennongmin | 31:ab3e7ca014c1 | 83 | * @param sap_pwd - PASSWORD of its self softap. |
shennongmin | 35:90be2bc2a492 | 84 | * @param chl - the channel can be 1, 2, 3, ..., 13(default: 7). |
shennongmin | 35:90be2bc2a492 | 85 | * @param enc - the method of encrypstion(default: ESP8266_ECN_WAP_WAP2_PSK). |
shennongmin | 31:ab3e7ca014c1 | 86 | * @return true if success, false for failure. |
shennongmin | 31:ab3e7ca014c1 | 87 | */ |
shennongmin | 31:ab3e7ca014c1 | 88 | bool setSoftAPMode(String sap_ssid, String sap_pwd, |
shennongmin | 35:90be2bc2a492 | 89 | int chl = 7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK); |
shennongmin | 30:46a560b71e61 | 90 | |
shennongmin | 31:ab3e7ca014c1 | 91 | /** |
shennongmin | 31:ab3e7ca014c1 | 92 | * Set as softap + station mode and start softap and join in ssid with pwd. |
shennongmin | 31:ab3e7ca014c1 | 93 | * |
shennongmin | 31:ab3e7ca014c1 | 94 | * @param ssid - SSID of AP to join in. |
shennongmin | 31:ab3e7ca014c1 | 95 | * @param pwd - PASSWORD of AP to join in. |
shennongmin | 31:ab3e7ca014c1 | 96 | * @param sap_ssid - SSID of its self softap. |
shennongmin | 31:ab3e7ca014c1 | 97 | * @param sap_pwd - PASSWORD of its self softap. |
shennongmin | 35:90be2bc2a492 | 98 | * @param chl - the channel can be 1, 2, 3, ..., 13(default: 7). |
shennongmin | 35:90be2bc2a492 | 99 | * @param enc - the method of encrypstion(default: ESP8266_ECN_WAP_WAP2_PSK). |
shennongmin | 31:ab3e7ca014c1 | 100 | * @return true if success, false for failure. |
shennongmin | 31:ab3e7ca014c1 | 101 | */ |
shennongmin | 30:46a560b71e61 | 102 | bool setStationSoftAPMode(String ssid, String pwd, String sap_ssid, String sap_pwd , |
shennongmin | 35:90be2bc2a492 | 103 | int chl = 7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK); |
shennongmin | 7:a11e381ce969 | 104 | |
shennongmin | 34:65cd9c6eedb8 | 105 | /** |
shennongmin | 34:65cd9c6eedb8 | 106 | * Start a TCP connection to server or init UDP communication. |
shennongmin | 35:90be2bc2a492 | 107 | * |
shennongmin | 35:90be2bc2a492 | 108 | * @param type - TCP or UDP. |
shennongmin | 35:90be2bc2a492 | 109 | * @param addr - ip address or domain name of server to connect. |
shennongmin | 35:90be2bc2a492 | 110 | * @param port - the port number of server to connect. |
shennongmin | 35:90be2bc2a492 | 111 | * @param mux - single or mulitple connection mode(default: ESP8266_MUX_SINGLE). |
shennongmin | 35:90be2bc2a492 | 112 | * @param id - id number when mux is multiple(default: 0). |
shennongmin | 35:90be2bc2a492 | 113 | * @return true for success, false for failure. |
shennongmin | 34:65cd9c6eedb8 | 114 | */ |
shennongmin | 35:90be2bc2a492 | 115 | bool ipConfig(ESP8266CommType type, String addr, int port, ESP8266Mux mux = ESP8266_MUX_SINGLE, int id = 0); |
shennongmin | 4:962bf18523f4 | 116 | |
shennongmin | 35:90be2bc2a492 | 117 | /** |
shennongmin | 35:90be2bc2a492 | 118 | * Set the connection mode(sigle or multiple) |
shennongmin | 35:90be2bc2a492 | 119 | * |
shennongmin | 35:90be2bc2a492 | 120 | * @param mux - single or multiple. |
shennongmin | 35:90be2bc2a492 | 121 | * @return true for success, false for failure. |
shennongmin | 35:90be2bc2a492 | 122 | */ |
shennongmin | 35:90be2bc2a492 | 123 | bool confMux(ESP8266Mux mux); |
shennongmin | 35:90be2bc2a492 | 124 | |
shennongmin | 35:90be2bc2a492 | 125 | /** |
shennongmin | 35:90be2bc2a492 | 126 | * Start or stop the TCP server. |
shennongmin | 35:90be2bc2a492 | 127 | * |
shennongmin | 35:90be2bc2a492 | 128 | * @param mode - 1 to start, 0 to stop (restart needed). |
shennongmin | 35:90be2bc2a492 | 129 | * @param port - the port to listen. |
shennongmin | 35:90be2bc2a492 | 130 | * @return true for success, false for failure. |
shennongmin | 35:90be2bc2a492 | 131 | */ |
shennongmin | 35:90be2bc2a492 | 132 | bool confServer(int mode, int port); |
shennongmin | 34:65cd9c6eedb8 | 133 | |
shennongmin | 34:65cd9c6eedb8 | 134 | /** |
shennongmin | 34:65cd9c6eedb8 | 135 | * Send data in sigle connection mode(mux = 0). |
shennongmin | 34:65cd9c6eedb8 | 136 | * |
shennongmin | 35:90be2bc2a492 | 137 | * @param str - string to send. |
shennongmin | 35:90be2bc2a492 | 138 | * @return true for success, false for failure. |
shennongmin | 34:65cd9c6eedb8 | 139 | */ |
shennongmin | 23:4cec7b9e3715 | 140 | bool send(String str); |
shennongmin | 7:a11e381ce969 | 141 | |
shennongmin | 34:65cd9c6eedb8 | 142 | /** |
shennongmin | 34:65cd9c6eedb8 | 143 | * Send data in multiple connection mode(mux = 1). |
shennongmin | 34:65cd9c6eedb8 | 144 | * |
shennongmin | 35:90be2bc2a492 | 145 | * @param id - id number. |
shennongmin | 35:90be2bc2a492 | 146 | * @param str - string to send by id connection. |
shennongmin | 35:90be2bc2a492 | 147 | * @return true for success, false for failure. |
shennongmin | 34:65cd9c6eedb8 | 148 | */ |
shennongmin | 35:90be2bc2a492 | 149 | bool send(int id, String str); |
shennongmin | 34:65cd9c6eedb8 | 150 | |
shennongmin | 34:65cd9c6eedb8 | 151 | /** |
shennongmin | 34:65cd9c6eedb8 | 152 | * Start to receive data from network. |
shennongmin | 35:90be2bc2a492 | 153 | * |
shennongmin | 35:90be2bc2a492 | 154 | * @param buf - buffer for storing data from network. |
shennongmin | 35:90be2bc2a492 | 155 | * @return the size of data received. |
shennongmin | 34:65cd9c6eedb8 | 156 | */ |
shennongmin | 23:4cec7b9e3715 | 157 | int recvData(char *buf); |
shennongmin | 4:962bf18523f4 | 158 | |
shennongmin | 34:65cd9c6eedb8 | 159 | /** |
shennongmin | 34:65cd9c6eedb8 | 160 | * Get the current id of data received via member method:recvData in multiple connection mode. |
shennongmin | 35:90be2bc2a492 | 161 | * |
shennongmin | 35:90be2bc2a492 | 162 | * @return the id of connection which data received from. |
shennongmin | 34:65cd9c6eedb8 | 163 | */ |
shennongmin | 34:65cd9c6eedb8 | 164 | int getMuxID(void); |
shennongmin | 34:65cd9c6eedb8 | 165 | |
shennongmin | 34:65cd9c6eedb8 | 166 | /** |
shennongmin | 34:65cd9c6eedb8 | 167 | * Reset the module. |
shennongmin | 35:90be2bc2a492 | 168 | * |
shennongmin | 35:90be2bc2a492 | 169 | * @return true for success, false for failure. |
shennongmin | 34:65cd9c6eedb8 | 170 | */ |
shennongmin | 29:75185b05d6a4 | 171 | bool reset(void); |
shennongmin | 7:a11e381ce969 | 172 | |
shennongmin | 35:90be2bc2a492 | 173 | /** |
shennongmin | 35:90be2bc2a492 | 174 | * Get current work mode. |
shennongmin | 35:90be2bc2a492 | 175 | * |
shennongmin | 35:90be2bc2a492 | 176 | * @return the mode. |
shennongmin | 35:90be2bc2a492 | 177 | */ |
shennongmin | 35:90be2bc2a492 | 178 | String showMode(void); |
shennongmin | 34:65cd9c6eedb8 | 179 | |
shennongmin | 35:90be2bc2a492 | 180 | |
shennongmin | 35:90be2bc2a492 | 181 | /** |
shennongmin | 35:90be2bc2a492 | 182 | * Get the list of Access Points available. |
shennongmin | 35:90be2bc2a492 | 183 | * |
shennongmin | 35:90be2bc2a492 | 184 | * @return the list of SSID. |
shennongmin | 35:90be2bc2a492 | 185 | */ |
shennongmin | 35:90be2bc2a492 | 186 | String showAP(void); |
shennongmin | 35:90be2bc2a492 | 187 | |
shennongmin | 35:90be2bc2a492 | 188 | /** |
shennongmin | 35:90be2bc2a492 | 189 | * Get the SSID of AP joined already. |
shennongmin | 35:90be2bc2a492 | 190 | * |
shennongmin | 35:90be2bc2a492 | 191 | * @return the SSID joined. |
shennongmin | 35:90be2bc2a492 | 192 | */ |
shennongmin | 35:90be2bc2a492 | 193 | String showJAP(void); |
shennongmin | 35:90be2bc2a492 | 194 | |
shennongmin | 35:90be2bc2a492 | 195 | /** |
shennongmin | 35:90be2bc2a492 | 196 | * Quit the AP joined before. |
shennongmin | 35:90be2bc2a492 | 197 | * |
shennongmin | 35:90be2bc2a492 | 198 | * @return true for success, false for failure. |
shennongmin | 35:90be2bc2a492 | 199 | */ |
shennongmin | 35:90be2bc2a492 | 200 | bool quitAP(void); |
shennongmin | 35:90be2bc2a492 | 201 | |
shennongmin | 35:90be2bc2a492 | 202 | |
shennongmin | 35:90be2bc2a492 | 203 | /** |
shennongmin | 35:90be2bc2a492 | 204 | * Get SoftAP Parameters. |
shennongmin | 35:90be2bc2a492 | 205 | * |
shennongmin | 35:90be2bc2a492 | 206 | * @return the parameters of SoftAP. |
shennongmin | 35:90be2bc2a492 | 207 | */ |
shennongmin | 35:90be2bc2a492 | 208 | String showSAP(void); |
shennongmin | 35:90be2bc2a492 | 209 | |
shennongmin | 35:90be2bc2a492 | 210 | /** |
shennongmin | 35:90be2bc2a492 | 211 | * Get the status of connection. |
shennongmin | 35:90be2bc2a492 | 212 | * |
shennongmin | 35:90be2bc2a492 | 213 | * @return the status. |
shennongmin | 35:90be2bc2a492 | 214 | */ |
shennongmin | 35:90be2bc2a492 | 215 | String showStatus(void); |
shennongmin | 35:90be2bc2a492 | 216 | |
shennongmin | 35:90be2bc2a492 | 217 | /** |
shennongmin | 35:90be2bc2a492 | 218 | * Get the connection mode(sigle or multiple) |
shennongmin | 35:90be2bc2a492 | 219 | * |
shennongmin | 35:90be2bc2a492 | 220 | * @return the mux string. |
shennongmin | 35:90be2bc2a492 | 221 | */ |
shennongmin | 35:90be2bc2a492 | 222 | String showMux(void); |
shennongmin | 35:90be2bc2a492 | 223 | |
shennongmin | 35:90be2bc2a492 | 224 | /** |
shennongmin | 35:90be2bc2a492 | 225 | * Show the current ip address. |
shennongmin | 35:90be2bc2a492 | 226 | * |
shennongmin | 35:90be2bc2a492 | 227 | * @return the ip address. |
shennongmin | 35:90be2bc2a492 | 228 | */ |
shennongmin | 35:90be2bc2a492 | 229 | String showIP(void); |
shennongmin | 35:90be2bc2a492 | 230 | |
shennongmin | 35:90be2bc2a492 | 231 | /** |
shennongmin | 35:90be2bc2a492 | 232 | * Create new tcp or udp connection in sigle connection mode. |
shennongmin | 35:90be2bc2a492 | 233 | * |
shennongmin | 35:90be2bc2a492 | 234 | * @param type - UDP or TCP. |
shennongmin | 35:90be2bc2a492 | 235 | * @param addr - the ip or domain name of server. |
shennongmin | 35:90be2bc2a492 | 236 | * @param port - the port number. |
shennongmin | 35:90be2bc2a492 | 237 | */ |
shennongmin | 35:90be2bc2a492 | 238 | bool newMux(ESP8266CommType type, String addr, int port); |
shennongmin | 35:90be2bc2a492 | 239 | |
shennongmin | 35:90be2bc2a492 | 240 | /** |
shennongmin | 35:90be2bc2a492 | 241 | * Create new tcp or udp connection in multiple connection mode)(id:0-4). |
shennongmin | 35:90be2bc2a492 | 242 | * |
shennongmin | 35:90be2bc2a492 | 243 | * @param id - the id number. |
shennongmin | 35:90be2bc2a492 | 244 | * @param type - UDP or TCP. |
shennongmin | 35:90be2bc2a492 | 245 | * @param addr - the ip or domain name of server. |
shennongmin | 35:90be2bc2a492 | 246 | * @param port - the port number. |
shennongmin | 35:90be2bc2a492 | 247 | */ |
shennongmin | 35:90be2bc2a492 | 248 | bool newMux(int id, ESP8266CommType type, String addr, int port); |
shennongmin | 35:90be2bc2a492 | 249 | |
shennongmin | 35:90be2bc2a492 | 250 | /** |
shennongmin | 35:90be2bc2a492 | 251 | * Close tcp or udp in sigle connection mode. |
shennongmin | 35:90be2bc2a492 | 252 | */ |
shennongmin | 35:90be2bc2a492 | 253 | void closeMux(void); |
shennongmin | 35:90be2bc2a492 | 254 | |
shennongmin | 35:90be2bc2a492 | 255 | /** |
shennongmin | 35:90be2bc2a492 | 256 | * Close tcp or udp in multiple connection mode. |
shennongmin | 35:90be2bc2a492 | 257 | * |
shennongmin | 35:90be2bc2a492 | 258 | * @param id - the id number. |
shennongmin | 35:90be2bc2a492 | 259 | */ |
shennongmin | 35:90be2bc2a492 | 260 | void closeMux(int id); |
shennongmin | 35:90be2bc2a492 | 261 | |
shennongmin | 35:90be2bc2a492 | 262 | private: |
shennongmin | 35:90be2bc2a492 | 263 | |
shennongmin | 34:65cd9c6eedb8 | 264 | /** |
shennongmin | 34:65cd9c6eedb8 | 265 | * Set work mode. |
shennongmin | 34:65cd9c6eedb8 | 266 | * |
shennongmin | 34:65cd9c6eedb8 | 267 | */ |
shennongmin | 35:90be2bc2a492 | 268 | bool confMode(ESP8266WorkMode mode); |
shennongmin | 7:a11e381ce969 | 269 | |
shennongmin | 34:65cd9c6eedb8 | 270 | |
shennongmin | 34:65cd9c6eedb8 | 271 | /** |
shennongmin | 34:65cd9c6eedb8 | 272 | * Join in AP. |
shennongmin | 34:65cd9c6eedb8 | 273 | * |
shennongmin | 34:65cd9c6eedb8 | 274 | */ |
shennongmin | 7:a11e381ce969 | 275 | bool confJAP(String ssid , String pwd); |
shennongmin | 7:a11e381ce969 | 276 | |
shennongmin | 34:65cd9c6eedb8 | 277 | |
shennongmin | 34:65cd9c6eedb8 | 278 | /** |
shennongmin | 34:65cd9c6eedb8 | 279 | * Set SoftAP Parameters. |
shennongmin | 34:65cd9c6eedb8 | 280 | * |
shennongmin | 34:65cd9c6eedb8 | 281 | */ |
shennongmin | 35:90be2bc2a492 | 282 | bool confSAP(String ssid , String pwd , int chl , int ecn); |
shennongmin | 35:90be2bc2a492 | 283 | |
shennongmin | 35:90be2bc2a492 | 284 | |
shennongmin | 34:65cd9c6eedb8 | 285 | ArduinoSerial m_uart; /* The UART to communicate with ESP8266 */ |
shennongmin | 29:75185b05d6a4 | 286 | int m_mux_id; |
shennongmin | 4:962bf18523f4 | 287 | }; |
shennongmin | 9:95a0ce98e12b | 288 | |
shennongmin | 10:9d4ec0359a5c | 289 | #endif /* #ifndef __ESP8266_H__ */ |