1
ESP8266.h@0:eb6507dc4669, 2018-06-27 (annotated)
- Committer:
- nikitoslav
- Date:
- Wed Jun 27 06:50:30 2018 +0000
- Revision:
- 0:eb6507dc4669
- Child:
- 1:af8bf1171171
Some changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikitoslav | 0:eb6507dc4669 | 1 | /* ESP8266Interface Example |
nikitoslav | 0:eb6507dc4669 | 2 | * Copyright (c) 2015 ARM Limited |
nikitoslav | 0:eb6507dc4669 | 3 | * |
nikitoslav | 0:eb6507dc4669 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nikitoslav | 0:eb6507dc4669 | 5 | * you may not use this file except in compliance with the License. |
nikitoslav | 0:eb6507dc4669 | 6 | * You may obtain a copy of the License at |
nikitoslav | 0:eb6507dc4669 | 7 | * |
nikitoslav | 0:eb6507dc4669 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nikitoslav | 0:eb6507dc4669 | 9 | * |
nikitoslav | 0:eb6507dc4669 | 10 | * Unless required by applicable law or agreed to in writing, software |
nikitoslav | 0:eb6507dc4669 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nikitoslav | 0:eb6507dc4669 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nikitoslav | 0:eb6507dc4669 | 13 | * See the License for the specific language governing permissions and |
nikitoslav | 0:eb6507dc4669 | 14 | * limitations under the License. |
nikitoslav | 0:eb6507dc4669 | 15 | */ |
nikitoslav | 0:eb6507dc4669 | 16 | |
nikitoslav | 0:eb6507dc4669 | 17 | #ifndef ESP8266_H |
nikitoslav | 0:eb6507dc4669 | 18 | #define ESP8266_H |
nikitoslav | 0:eb6507dc4669 | 19 | |
nikitoslav | 0:eb6507dc4669 | 20 | #include "ATParser.h" |
nikitoslav | 0:eb6507dc4669 | 21 | |
nikitoslav | 0:eb6507dc4669 | 22 | /** ESP8266Interface class. |
nikitoslav | 0:eb6507dc4669 | 23 | This is an interface to a ESP8266 radio. |
nikitoslav | 0:eb6507dc4669 | 24 | */ |
nikitoslav | 0:eb6507dc4669 | 25 | class ESP8266 |
nikitoslav | 0:eb6507dc4669 | 26 | { |
nikitoslav | 0:eb6507dc4669 | 27 | public: |
nikitoslav | 0:eb6507dc4669 | 28 | ESP8266(PinName tx, PinName rx, bool debug=false); |
nikitoslav | 0:eb6507dc4669 | 29 | |
nikitoslav | 0:eb6507dc4669 | 30 | /** |
nikitoslav | 0:eb6507dc4669 | 31 | * Startup the ESP8266 |
nikitoslav | 0:eb6507dc4669 | 32 | * |
nikitoslav | 0:eb6507dc4669 | 33 | * @param mode mode of WIFI 1-client, 2-host, 3-both |
nikitoslav | 0:eb6507dc4669 | 34 | * @return true only if ESP8266 was setup correctly |
nikitoslav | 0:eb6507dc4669 | 35 | */ |
nikitoslav | 0:eb6507dc4669 | 36 | bool startup(int mode); |
nikitoslav | 0:eb6507dc4669 | 37 | |
nikitoslav | 0:eb6507dc4669 | 38 | /** |
nikitoslav | 0:eb6507dc4669 | 39 | * Reset ESP8266 |
nikitoslav | 0:eb6507dc4669 | 40 | * |
nikitoslav | 0:eb6507dc4669 | 41 | * @return true only if ESP8266 resets successfully |
nikitoslav | 0:eb6507dc4669 | 42 | */ |
nikitoslav | 0:eb6507dc4669 | 43 | bool reset(void); |
nikitoslav | 0:eb6507dc4669 | 44 | |
nikitoslav | 0:eb6507dc4669 | 45 | /** |
nikitoslav | 0:eb6507dc4669 | 46 | * Connect ESP8266 to AP |
nikitoslav | 0:eb6507dc4669 | 47 | * |
nikitoslav | 0:eb6507dc4669 | 48 | * @param ap the name of the AP |
nikitoslav | 0:eb6507dc4669 | 49 | * @param passPhrase the password of AP |
nikitoslav | 0:eb6507dc4669 | 50 | * @return true only if ESP8266 is connected successfully |
nikitoslav | 0:eb6507dc4669 | 51 | */ |
nikitoslav | 0:eb6507dc4669 | 52 | bool connect(const char *ap, const char *passPhrase); |
nikitoslav | 0:eb6507dc4669 | 53 | |
nikitoslav | 0:eb6507dc4669 | 54 | /** |
nikitoslav | 0:eb6507dc4669 | 55 | * Disconnect ESP8266 from AP |
nikitoslav | 0:eb6507dc4669 | 56 | * |
nikitoslav | 0:eb6507dc4669 | 57 | * @return true only if ESP8266 is disconnected successfully |
nikitoslav | 0:eb6507dc4669 | 58 | */ |
nikitoslav | 0:eb6507dc4669 | 59 | bool disconnect(void); |
nikitoslav | 0:eb6507dc4669 | 60 | |
nikitoslav | 0:eb6507dc4669 | 61 | /** |
nikitoslav | 0:eb6507dc4669 | 62 | * Get the IP address of ESP8266 |
nikitoslav | 0:eb6507dc4669 | 63 | * |
nikitoslav | 0:eb6507dc4669 | 64 | * @return null-teriminated IP address or null if no IP address is assigned |
nikitoslav | 0:eb6507dc4669 | 65 | */ |
nikitoslav | 0:eb6507dc4669 | 66 | char *getIPAddress(void); |
nikitoslav | 0:eb6507dc4669 | 67 | |
nikitoslav | 0:eb6507dc4669 | 68 | /** |
nikitoslav | 0:eb6507dc4669 | 69 | * Get the MAC address of ESP8266 |
nikitoslav | 0:eb6507dc4669 | 70 | * |
nikitoslav | 0:eb6507dc4669 | 71 | * @return null-terminated MAC address or null if no MAC address is assigned |
nikitoslav | 0:eb6507dc4669 | 72 | */ |
nikitoslav | 0:eb6507dc4669 | 73 | const char *getMACAddress(void); |
nikitoslav | 0:eb6507dc4669 | 74 | |
nikitoslav | 0:eb6507dc4669 | 75 | /** |
nikitoslav | 0:eb6507dc4669 | 76 | * Check if ESP8266 is conenected |
nikitoslav | 0:eb6507dc4669 | 77 | * |
nikitoslav | 0:eb6507dc4669 | 78 | * @return true only if the chip has an IP address |
nikitoslav | 0:eb6507dc4669 | 79 | */ |
nikitoslav | 0:eb6507dc4669 | 80 | bool isConnected(void); |
nikitoslav | 0:eb6507dc4669 | 81 | |
nikitoslav | 0:eb6507dc4669 | 82 | /** |
nikitoslav | 0:eb6507dc4669 | 83 | * Open a socketed connection |
nikitoslav | 0:eb6507dc4669 | 84 | * |
nikitoslav | 0:eb6507dc4669 | 85 | * @param type the type of socket to open "UDP" or "TCP" |
nikitoslav | 0:eb6507dc4669 | 86 | * @param id id to give the new socket, valid 0-4 |
nikitoslav | 0:eb6507dc4669 | 87 | * @param port port to open connection with |
nikitoslav | 0:eb6507dc4669 | 88 | * @param addr the IP address of the destination |
nikitoslav | 0:eb6507dc4669 | 89 | * @return true only if socket opened successfully |
nikitoslav | 0:eb6507dc4669 | 90 | */ |
nikitoslav | 0:eb6507dc4669 | 91 | bool open(int linkId, int localPort); |
nikitoslav | 0:eb6507dc4669 | 92 | |
nikitoslav | 0:eb6507dc4669 | 93 | /** |
nikitoslav | 0:eb6507dc4669 | 94 | * Sends data to an open socket |
nikitoslav | 0:eb6507dc4669 | 95 | * |
nikitoslav | 0:eb6507dc4669 | 96 | * @param id id of socket to send to |
nikitoslav | 0:eb6507dc4669 | 97 | * @param data data to be sent |
nikitoslav | 0:eb6507dc4669 | 98 | * @param amount amount of data to be sent - max 1024 |
nikitoslav | 0:eb6507dc4669 | 99 | * @return true only if data sent successfully |
nikitoslav | 0:eb6507dc4669 | 100 | */ |
nikitoslav | 0:eb6507dc4669 | 101 | bool send(const void *data, uint32_t linkId, uint32_t amount, const char* addr, int port); |
nikitoslav | 0:eb6507dc4669 | 102 | |
nikitoslav | 0:eb6507dc4669 | 103 | /** |
nikitoslav | 0:eb6507dc4669 | 104 | * Receives data from an open socket |
nikitoslav | 0:eb6507dc4669 | 105 | * |
nikitoslav | 0:eb6507dc4669 | 106 | * @param id id to receive from |
nikitoslav | 0:eb6507dc4669 | 107 | * @param data placeholder for returned information |
nikitoslav | 0:eb6507dc4669 | 108 | * @param amount number of bytes to be received |
nikitoslav | 0:eb6507dc4669 | 109 | * @return the number of bytes received |
nikitoslav | 0:eb6507dc4669 | 110 | */ |
nikitoslav | 0:eb6507dc4669 | 111 | int32_t recv(void *data, uint32_t amount, char* IP, int* port); |
nikitoslav | 0:eb6507dc4669 | 112 | |
nikitoslav | 0:eb6507dc4669 | 113 | /** |
nikitoslav | 0:eb6507dc4669 | 114 | * Closes a socket |
nikitoslav | 0:eb6507dc4669 | 115 | * |
nikitoslav | 0:eb6507dc4669 | 116 | * @param id id of socket to close, valid only 0-4 |
nikitoslav | 0:eb6507dc4669 | 117 | * @return true only if socket is closed successfully |
nikitoslav | 0:eb6507dc4669 | 118 | */ |
nikitoslav | 0:eb6507dc4669 | 119 | bool close(int id); |
nikitoslav | 0:eb6507dc4669 | 120 | |
nikitoslav | 0:eb6507dc4669 | 121 | /** |
nikitoslav | 0:eb6507dc4669 | 122 | * Allows timeout to be changed between commands |
nikitoslav | 0:eb6507dc4669 | 123 | * |
nikitoslav | 0:eb6507dc4669 | 124 | * @param timeout_ms timeout of the connection |
nikitoslav | 0:eb6507dc4669 | 125 | */ |
nikitoslav | 0:eb6507dc4669 | 126 | void setTimeout(uint32_t timeout_ms); |
nikitoslav | 0:eb6507dc4669 | 127 | |
nikitoslav | 0:eb6507dc4669 | 128 | /** |
nikitoslav | 0:eb6507dc4669 | 129 | * Checks if data is available |
nikitoslav | 0:eb6507dc4669 | 130 | */ |
nikitoslav | 0:eb6507dc4669 | 131 | bool readable(); |
nikitoslav | 0:eb6507dc4669 | 132 | |
nikitoslav | 0:eb6507dc4669 | 133 | /** |
nikitoslav | 0:eb6507dc4669 | 134 | * Checks if data can be written |
nikitoslav | 0:eb6507dc4669 | 135 | */ |
nikitoslav | 0:eb6507dc4669 | 136 | bool writeable(); |
nikitoslav | 0:eb6507dc4669 | 137 | |
nikitoslav | 0:eb6507dc4669 | 138 | private: |
nikitoslav | 0:eb6507dc4669 | 139 | BufferedSerial _serial; |
nikitoslav | 0:eb6507dc4669 | 140 | ATParser _parser; |
nikitoslav | 0:eb6507dc4669 | 141 | |
nikitoslav | 0:eb6507dc4669 | 142 | char _ip_buffer[16]; |
nikitoslav | 0:eb6507dc4669 | 143 | char _mac_buffer[18]; |
nikitoslav | 0:eb6507dc4669 | 144 | }; |
nikitoslav | 0:eb6507dc4669 | 145 | |
nikitoslav | 0:eb6507dc4669 | 146 | #endif |